David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.

Slides:



Advertisements
Similar presentations
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Advertisements

Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
Applications Data Structures and Algorithms (60-254)
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Tirgul 11 DFS Properties of DFS Topological sort.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
David Luebke 1 5/20/2015 CS 332: Algorithms Graph Algorithms.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
Topological Sorting and Least-cost Path Algorithms.
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
1  Introduction to Graph Data Structure  Applications  Graph Searching  Minimum Spanning Trees  Shortest Path problems.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
CSC 413/513: Intro to Algorithms Graph Algorithms.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Topological Sort Minimum Spanning Tree
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
Many slides here are based on D. Luebke slides
CS6045: Advanced Algorithms
Algorithms and Data Structures Lecture XII
Minimum Spanning Trees
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
CS 3013: DS & Algorithms Shortest Paths.
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Presentation transcript:

David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees

David Luebke 2 9/15/2015 Review: Breadth-First Search BFS(G, s) { initialize vertices; Q = {s};// Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v  u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } v->p represents parent in tree v->d represents depth in tree

David Luebke 3 9/15/2015 Review: DFS Code DFS(G) { for each vertex u  G->V { u->color = WHITE; } time = 0; for each vertex u  G->V { if (u->color == WHITE) DFS_Visit(u); } DFS_Visit(u) { u->color = YELLOW; time = time+1; u->d = time; for each v  u->Adj[] { if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; time = time+1; u->f = time; }

David Luebke 4 9/15/2015 Review: DFS Example source vertex

David Luebke 5 9/15/2015 Review: DFS Example 1 | | | | | | | | source vertex d f

David Luebke 6 9/15/2015 Review: DFS Example 1 | | | | | | 2 | | source vertex d f

David Luebke 7 9/15/2015 Review: DFS Example 1 | | | | |3 | 2 | | source vertex d f

David Luebke 8 9/15/2015 Review: DFS Example 1 | | | | |3 | 4 2 | | source vertex d f

David Luebke 9 9/15/2015 Review: DFS Example 1 | | | |5 |3 | 4 2 | | source vertex d f

David Luebke 10 9/15/2015 Review: DFS Example 1 | | | |5 | 63 | 4 2 | | source vertex d f

David Luebke 11 9/15/2015 Review: DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

David Luebke 12 9/15/2015 Review: DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

David Luebke 13 9/15/2015 Review: DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 | source vertex d f

David Luebke 14 9/15/2015 Review: DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 15 9/15/2015 Review: DFS Example 1 |8 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 16 9/15/2015 Review: DFS Example 1 |128 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 17 9/15/2015 Review: DFS Example 1 |128 |1113| |5 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 18 9/15/2015 Review: DFS Example 1 |128 |1113| 14|5 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 19 9/15/2015 Review: DFS Example 1 |128 |1113| 14|155 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 20 9/15/2015 Review: DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f

David Luebke 21 9/15/2015 Review: Kinds Of Edges ● Thm: If G is undirected, a DFS produces only tree and back edges ● Thm: An undirected graph is acyclic iff a DFS yields no back edges ● Thus, can run DFS to find cycles

David Luebke 22 9/15/ |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edgesForward edgesCross edges Review: Kinds of Edges

David Luebke 23 9/15/2015 DFS And Cycles ● Running time: O(V+E) ● We can actually determine if cycles exist in O(V) time: ■ In an undirected acyclic forest, |E|  |V| - 1 ■ So count the edges: if ever see |V| distinct edges, must have seen a back edge along the way ■ Why not just test if |E| <|V| and answer the question in constant time?

David Luebke 24 9/15/2015 Directed Acyclic Graphs ● A directed acyclic graph or DAG is a directed graph with no directed cycles:

David Luebke 25 9/15/2015 DFS and DAGs ● Argue that a directed graph G is acyclic iff a DFS of G yields no back edges: ■ Forward: if G is acyclic, will be no back edges ○ Trivial: a back edge implies a cycle ■ Backward: if no back edges, G is acyclic ○ Argue contrapositive: G has a cycle   a back edge  Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle  When v discovered, whole cycle is white  Must visit everything reachable from v before returning from DFS-Visit()  So path from u  v is yellow  yellow, thus (u, v) is a back edge

David Luebke 26 9/15/2015 Topological Sort ● Topological sort of a DAG: ■ Linear ordering of all vertices in graph G such that vertex u comes before vertex v if edge (u, v)  G ● Real-world example: getting dressed

David Luebke 27 9/15/2015 Getting Dressed UnderwearSocks ShoesPants Belt Shirt Watch Tie Jacket

David Luebke 28 9/15/2015 Getting Dressed UnderwearSocks ShoesPants Belt Shirt Watch Tie Jacket SocksUnderwearPantsShoesWatchShirtBeltTieJacket

David Luebke 29 9/15/2015 Topological Sort Algorithm Topological-Sort() { Run DFS When a vertex is finished, output it Vertices are output in reverse topological order } ● Time: O(V+E) ● Correctness: Want to prove that (u,v)  G  u  f > v  f

David Luebke 30 9/15/2015 Correctness of Topological Sort ● Claim: (u,v)  G  u  f > v  f ■ When (u,v) is explored, u is yellow ○ v = yellow  (u,v) is back edge. Contradiction (Why?) ○ v = white  v becomes descendent of u  v  f < u  f (since must finish v before backtracking and finishing u) ○ v = black  v already finished  v  f < u  f

David Luebke 31 9/15/2015 Minimum Spanning Tree ● Problem: given a connected, undirected, weighted graph:

David Luebke 32 9/15/2015 Minimum Spanning Tree ● Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight

David Luebke 33 9/15/2015 Minimum Spanning Tree ● Which edges form the minimum spanning tree (MST) of the below graph? HBC GED F A

David Luebke 34 9/15/2015 Minimum Spanning Tree ● Answer: HBC GED F A

David Luebke 35 9/15/2015 Minimum Spanning Tree ● MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees ■ Let T be an MST of G with an edge (u,v) in the middle ■ Removing (u,v) partitions T into two trees T 1 and T 2 ■ Claim: T 1 is an MST of G 1 = (V 1,E 1 ), and T 2 is an MST of G 2 = (V 2,E 2 ) (Do V 1 and V 2 share vertices? Why?) ■ Proof: w(T) = w(u,v) + w(T 1 ) + w(T 2 ) (There can’t be a better tree than T 1 or T 2, or T would be suboptimal)

David Luebke 36 9/15/2015 Minimum Spanning Tree ● Thm: ■ Let T be MST of G, and let A  T be subtree of T ■ Let (u,v) be min-weight edge connecting A to V-A ■ Then (u,v)  T

David Luebke 37 9/15/2015 Minimum Spanning Tree ● Thm: ■ Let T be MST of G, and let A  T be subtree of T ■ Let (u,v) be min-weight edge connecting A to V-A ■ Then (u,v)  T ● Proof: in book (see Thm 24.1)

David Luebke 38 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

David Luebke 39 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Run on example graph

David Luebke 40 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);     Run on example graph

David Luebke 41 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  0    Pick a start vertex r r

David Luebke 42 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  0    Red vertices have been removed from Q u

David Luebke 43 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  0  3  Red arrows indicate parent pointers u

David Luebke 44 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14  0  3  u

David Luebke 45 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14  0  3  u

David Luebke 46 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14  08  3  u

David Luebke 47 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10  08  3  u

David Luebke 48 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10  08  3  u

David Luebke 49 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 102  08  3  u

David Luebke 50 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 102   u

David Luebke 51 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 102   u

David Luebke 52 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  u

David Luebke 53 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

David Luebke 54 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

David Luebke 55 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

David Luebke 56 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

David Luebke 57 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

David Luebke 58 9/15/2015 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

David Luebke 59 9/15/2015 Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What is the hidden cost in this code?

David Luebke 60 9/15/2015 Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));

David Luebke 61 9/15/2015 Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v)); How often is ExtractMin() called? How often is DecreaseKey() called?

David Luebke 62 9/15/2015 Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time? A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)

David Luebke 63 9/15/2015 Single-Source Shortest Path ● Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v ■ “Shortest-path” = minimum weight ■ Weight of path is sum of edges ■ E.g., a road map: what is the shortest path from Chapel Hill to Charlottesville?