Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.

Similar presentations


Presentation on theme: "David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree."— Presentation transcript:

1 David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree

2 David Luebke 2 10/1/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

3 David Luebke 3 10/1/2015 Review: Depth-First Search l Depth-first search is another strategy for exploring a graph n Explore “deeper” in the graph whenever possible n Edges are explored out of the most recently discovered vertex v that still has unexplored edges n When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

4 David Luebke 4 10/1/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 = GREY; 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; }

5 David Luebke 5 10/1/2015 DFS Example 1 | | | | | | | | source vertex d f

6 David Luebke 6 10/1/2015 DFS Example 1 | | | | | | 2 | | source vertex d f

7 David Luebke 7 10/1/2015 DFS Example 1 | | | | |3 | 2 | | source vertex d f

8 David Luebke 8 10/1/2015 DFS Example 1 | | | | |3 | 4 2 | | source vertex d f

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

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

11 David Luebke 11 10/1/2015 DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

12 David Luebke 12 10/1/2015 DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

13 David Luebke 13 10/1/2015 DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 | source vertex d f What is the structure of the grey vertices? What do they represent?

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

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

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

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

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

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

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

21 David Luebke 21 10/1/2015 Review: Depth-First Sort Analysis l Running time: O(V+E) l Show by amortized analysis n “Charge” the exploration of edge to the edge: u Each loop in DFS_Visit can be attributed to an edge in the graph u Runs once/edge if directed graph, twice if undirected u Thus loop will run in O(E) time, algorithm O(V+E) ] Considered linear for graph, b/c adj list requires O(V+E) storage n Important to be comfortable with this kind of reasoning and analysis

22 David Luebke 22 10/1/2015 Review: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex u The tree edges form a spanning forest u Can tree edges form cycles? Why or why not?

23 David Luebke 23 10/1/2015 Review: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex n Back edge: from descendent to ancestor u Encounter a grey vertex (grey to grey)

24 David Luebke 24 10/1/2015 Review: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex n Back edge: from descendent to ancestor n Forward edge: from ancestor to descendent u Not a tree edge, though u From grey node to black node

25 David Luebke 25 10/1/2015 Review: Kinds of edges l DFS introduces an important distinction among edges in the original graph: n Tree edge: encounter new (white) vertex n Back edge: from descendent to ancestor n Forward edge: from ancestor to descendent n Cross edge: between a tree or subtrees u From a grey node to a black node

26 David Luebke 26 10/1/2015 Review: DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edgesForward edgesCross edges

27 David Luebke 27 10/1/2015 Review: Kinds Of Edges l Thm: If G is undirected, a DFS produces only tree and back edges l Thm: An undirected graph is acyclic iff a DFS yields no back edges n If acyclic, no back edges n If no back edges, acyclic u No back edges implies only tree edges (Why?) u Only tree edges implies we have a tree or a forest u Which by definition is acyclic l Thus, can run DFS to find cycles

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

29 David Luebke 29 10/1/2015 Directed Acyclic Graphs l A directed acyclic graph or DAG is a directed graph with no directed cycles:

30 David Luebke 30 10/1/2015 DFS and DAGs l Argue that a directed graph G is acyclic iff a DFS of G yields no back edges: n Forward: if G is acyclic, will be no back edges u Trivial: a back edge implies a cycle n Backward: if no back edges, G is acyclic u 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 grey  grey, thus (u, v) is a back edge

31 David Luebke 31 10/1/2015 Topological Sort l Topological sort of a DAG: n Linear ordering of all vertices in graph G such that vertex u comes before vertex v if edge (u, v)  G l Real-world example: getting dressed

32 David Luebke 32 10/1/2015 Getting Dressed UnderwearSocks ShoesPants Belt Shirt Watch Tie Jacket

33 David Luebke 33 10/1/2015 Getting Dressed UnderwearSocks ShoesPants Belt Shirt Watch Tie Jacket SocksUnderwearPantsShoesWatchShirtBeltTieJacket

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

35 David Luebke 35 10/1/2015 Correctness of Topological Sort l Claim: (u,v)  G  u  f > v  f n When (u,v) is explored, u is grey u v = grey  (u,v) is back edge. Contradiction (Why?) u v = white  v becomes descendent of u  v  f < u  f (since must finish v before backtracking and finishing u) u v = black  v already finished  v  f < u  f

36 David Luebke 36 10/1/2015 Minimum Spanning Tree l Problem: given a connected, undirected, weighted graph: 14 10 3 64 5 2 9 15 8

37 David Luebke 37 10/1/2015 Minimum Spanning Tree l Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight 14 10 3 64 5 2 9 15 8

38 David Luebke 38 10/1/2015 Minimum Spanning Tree l Which edges form the minimum spanning tree (MST) of the below graph? HBC GED F A 14 10 3 64 5 2 9 15 8

39 David Luebke 39 10/1/2015 Minimum Spanning Tree l Answer: HBC GED F A 14 10 3 64 5 2 9 15 8

40 David Luebke 40 10/1/2015 Minimum Spanning Tree l MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees n Let T be an MST of G with an edge (u,v) in the middle n Removing (u,v) partitions T into two trees T 1 and T 2 n 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?) n 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)

41 David Luebke 41 10/1/2015 Minimum Spanning Tree l Thm: n Let T be MST of G, and let A  T be subtree of T n Let (u,v) be min-weight edge connecting A to V-A n Then (u,v)  T

42 David Luebke 42 10/1/2015 Minimum Spanning Tree l Thm: n Let T be MST of G, and let A  T be subtree of T n Let (u,v) be min-weight edge connecting A to V-A n Then (u,v)  T l Proof: in book (see Thm 24.1)

43 David Luebke 43 10/1/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);

44 David Luebke 44 10/1/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 10 3 64 5 2 9 15 8 Run on example graph

45 David Luebke 45 10/1/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); What will be the running time?

46 David Luebke 46 10/1/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); What will be the running time? A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)

47 David Luebke 47 10/1/2015 The End


Download ppt "David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree."

Similar presentations


Ads by Google