Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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

3 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; }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

21 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

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

23 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?

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

25 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

26 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

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

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

29 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

30 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

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

32 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 14 10 3 64 5 2 9 15 8

33 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 14 10 3 64 5 2 9 15 8

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

35 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)

36 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

37 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)

38 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);

39 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); 14 10 3 64 5 2 9 15 8 Run on example graph

40 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);     14 10 3 64 5 2 9 15 8 Run on example graph

41 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    14 10 3 64 5 2 9 15 8 Pick a start vertex r r

42 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    14 10 3 64 5 2 9 15 8 Red vertices have been removed from Q u

43 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  14 10 3 64 5 2 9 15 8 Red arrows indicate parent pointers u

44 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  10 3 64 5 2 9 15 8 u

45 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  10 3 64 5 2 9 15 8 u

46 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  10 3 64 5 2 9 15 8 u

47 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  14 10 3 64 5 2 9 15 8 u

48 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  14 10 3 64 5 2 9 15 8 u

49 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  14 10 3 64 5 2 9 15 8 u

50 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  0815 3  14 10 3 64 5 2 9 15 8 u

51 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  0815 3  14 10 3 64 5 2 9 15 8 u

52 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); 1029 0815 3  14 10 3 64 5 2 9 15 8 u

53 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); 1029 0815 3 4 14 10 3 64 5 2 9 15 8 u

54 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); 529 0815 3 4 14 10 3 64 5 2 9 15 8 u

55 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); 529 0815 3 4 14 10 3 64 5 2 9 15 8 u

56 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); 529 0815 3 4 14 10 3 64 5 2 9 15 8 u

57 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); 529 0815 3 4 14 10 3 64 5 2 9 15 8 u

58 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); 529 0815 3 4 14 10 3 64 5 2 9 15 8 u

59 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?

60 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));

61 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?

62 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)

63 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?


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

Similar presentations


Ads by Google