dag and minimum spanning trees

of 95/95
T l i lS t Topological Sort Minimum Spanning Trees M. Shoaib Farooq 1

Post on 10-Jun-2015

105 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

  • 1. T l i l S tTopological Sort Minimum Spanning Treesp g M. Shoaib Farooq 1

2. Directed Acyclic Graphsy p A directed acyclic graph or DAG is a directedA directed acyclic graph or DAG is a directed graph with no directed cycles: M. Shoaib Farooq 2 3. DFS and DAGs Argue that a directed graph G is acyclic if aArgue that a directed graph G is acyclic if a DFS of G yields no back edges: Forward: if G is acyclic will be 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 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 d f h lpredecessor of v on the cycle When v discovered, whole cycle is white Must visit everything reachable from v before returning from M. Shoaib Farooq 3 DFS-Visit() So path from uv is yellowyellow, thus (u, v) is a back edge 4. Topological Sortp g Topological sort of a DAG: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) Gg ( , ) TS can be viewed as an ordering of its vertices along a horizontal line so that all directed edges gog g g from left to right. Example Given a set of jobs, courses, etc., with i it t i t t t th j b i d th tprerequisite constraints, output the jobs in an order that does not violate any of the prerequisites. l ld l i d d M. Shoaib Farooq 4 Real-world example: getting dressed 5. Existence of Topological Sortp g Lemma G can be topologically sorted iff it has no cycle, that i iff it i d (di t d li h)is, iff it is a dag (directed acyclic graph). Proof If G has a cycle, then it cannot be topologically sorted.y p g y a b If G has no cycle, then it can be topologically sorted. Constructive proof: An algorithm that sorts any dag. M. Shoaib Farooq 5 6. Topological Sort of Digraphp g g p Ordering < over V(G) such that u < v whenever (u, v) E(G). a b Some topological sorts: c d 1. a, c, e, b, d, g, f 2. a, b, c, d, g, f, ec e f g g f 3. b, d, g, a, c, f, e e f g M. Shoaib Farooq 6 7. Getting Dressedg UndeShorts 11/16 Socks 17/18UndeShorts 11/16 Shoes 13/14Pants 12/15 Watch 9/10 Belt 6/7 Shirt 1/8 Tie 2/5 Jacket 3/4Jacket 3/4 M. Shoaib Farooq 7 8. Getting Dressed M. Shoaib Farooq 8 9. Topological Sort AlgorithmTopological Sort Algorithm M. Shoaib Farooq 9 10. Topological Sort Algorithmp g g Initialize a global queue L within DFS(G) Add a line to DFS-visit DFS(G) for each vertex u V[G] color[u] = WHITE; [u]= NIl time = 0; for each vertex u V[G] if (color[u] == WHITE)if (color[u] == WHITE) DFS_Visit(u); L insert(v, L) // insert v in the front of L M. Shoaib Farooq 10 11. Topological Sort Algorithmp g g Depth-first search takes O(V+E) time and itDepth first search takes O(V E) time and it takes O(1) time to insert each of the |V| vertices onto the front of the link list.ve t ces o to t e o t o t e st. Ti O(V+E) Time: O(V+E) M. Shoaib Farooq 11 12. Minimum Spanning Tree (MST)p g ( ) Problem Select edges in a connected and undirected graph to form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. d a 2 7 1 f d b 2 5 45 2 1 f b g 7 1 34 M. Shoaib Farooq 12 c e 4 Total weight of tree edges: 14 13. Applications of MSTpp Telephone wiring (use as little wire as possible)p g ( p ) Electronic circuit board design Cancer imaging (MSTs describe arrangements of nuclei in skin cells) Biomedical image analysis (detect actin fibers in cell images) Astronomy (find clusters of quasars and Seyfert galaxies) Finding road networks in satellite and aerial imagery M. Shoaib Farooq 13 Finding road networks in satellite and aerial imagery 14. Minimum Spanning Tree M. Shoaib Farooq 14 15. Minimum Spanning Treep g Problem: given a connected, undirected,Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weightedges t at e t e tota we g t 6 4 5 9 14 5 2 9 10 2 15 M. Shoaib Farooq 15 3 8 16. Minimum Spanning Treep g The phrase minimum spanning tree is a shortenedp p g form of the phrase minimum-weight spanning tree. All spanning trees have exactly |v| -1 edges. MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees M. Shoaib Farooq 16 17. Free Tree Mi i S i T (T )Minimum Spanning Tree (Terms) A free tree is a tree with no vertex designated as theg root vertex. A free tree with n vertices has exactly n- 1 edges. There exists a unique path between any two vertices of a free tree. Adding any edge to a free tree creates a unique cycle. Breaking any edge on this cycle restores the free tree. M. Shoaib Farooq 17 18. Viable Set Mi i S i T (T )Minimum Spanning Tree (Terms) A subset A subset of E is viable if 1. A is a subset of edges of some MST 2. A cannot contains a cycle. ca ot co ta s a cyc e M. Shoaib Farooq 18 19. Safe Edge Mi i S i T (T )Minimum Spanning Tree (Terms) A subset A subset of E is viable if A is a subset of edges of some MST. An edge (u, v) subset of { E- A} is safe ifedge (u, v) subset o { } s safe A [ {(u, v)} is viable. In other words the choice (u v) is a safe choice to In other words, the choice (u, v) is a safe choice to add so that A can still be extended to form a MST. OROR A edge is safe if it will not create a cycle in MST M. Shoaib Farooq 19 20. CUT Mi i S i T (T )Minimum Spanning Tree (Terms) A cut (S, V- S) is just a partition of vertices into two( , ) j p disjoint subsets. An edge (u, v) crosses the cut if one endpoint is in S and the other is in V-S. Given a subset of edges A, a cut respects A if no edge in A crosses the cut. M. Shoaib Farooq 20 21. Growing a MSTg GENERIC-MST(G, w) 1. A 1. A 2. while A doesnot form a spanning tree 3. do find an edge(u, v) that is safe for A 4. A A U {(u, v)} 5. return A 22. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 S V - S 23. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 S V - S 24. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 25. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 26. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 27. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 28. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 29. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 30. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 31. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 32. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 33. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 34. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 35. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 36. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 37. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 38. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 39. Growing MSTg b c d 8 7 94 a 9 ei 144 4 11 2 h g f 10 ei 4 6 8 7 h g f 1 2 Total Weight = 37 40. Minimum Spanning Treep g Two Algorithms are used to solve minimum spanning tree problem Kruskals algorithm Two Algorithms are used to solve minimum spanning tree problem Let A be a subset of edges included in some MST A is a forest. The safe edge added to A has least weight among all g The safe edge added to A has least weight among all edges connecting its two components. Prims algorithmPrims algorithm A is a tree. The safe edge is a light edge connecting a vertex in A M. Shoaib Farooq 40 to one not in A. 41. Kruskals Algorithm (Operations)g Make-set(u): Create a set containing a singleMake set(u): Create a set containing a single item u. Find-set(u): Find the set that contains u Find-set(u): Find the set that contains u Union(u,v): merge the set containing u and set containing v into a common set. M. Shoaib Farooq 41 42. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 42 Return T 43. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 43 Return T 44. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 44 Return T 45. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1? 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 45 Return T 46. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 46 Return T 47. Kruskals Algorithmg Kruskal() 2? 19 Run the algorithm: T = ; 2? 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 47 Return T 48. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 48 Return T 49. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5? 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5? 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 49 Return T 50. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 50 Return T 51. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8? for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 51 Return T 52. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 52 Return T 53. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9? 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 53 Return T 54. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 54 Return T 55. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 13?21 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 55 Return T 56. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 56 Return T 57. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14? 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 57 Return T 58. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 58 Return T 59. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17? 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 59 Return T 60. Kruskals Algorithmg Kruskal() 2 19? Run the algorithm: T = ; 2 19? 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 60 Return T 61. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321? sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 61 Return T 62. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25? 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 62 Return T 63. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 63 Return T 64. Kruskals Algorithmg Kruskal() 2 19 Run the algorithm: T = ; 2 19 9 5 17 25 14 8 for each v V MakeSet(v); sort E by increasing edge weight w 1 5 1321 sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v)if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(u, v); M. Shoaib Farooq 64 Return T 65. Prims Algorithm an Exampleg p d a 7 f d b 2 5 45 2 choosen f b g1 1 34 c e 7 4 M. Shoaib Farooq 65 edge candidates 66. Prims Algorithm an Exampleg p d a 7 f d b 2 5 45 2 f b g1 1 34 c e 7 4 M. Shoaib Farooq 66 67. Prims Algorithm an Exampleg p d a 7 f d b 2 5 45 2 f b g1 1 34 c e 7 4 M. Shoaib Farooq 67 68. Prims Algorithm an Exampleg p d a 7 f d b 2 5 45 2 f b g1 1 34 c e 7 4 M. Shoaib Farooq 68 69. Prims Algorithm an Exampleg p d a 7 f d b 2 5 45 2 f b g1 1 34 c e 7 4 M. Shoaib Farooq 69 70. Prims Algorithm an Example Total weight of the MST: 14 g p d a 7 f d b 2 5 45 2 f b g1 1 34 c e 7 4 M. Shoaib Farooq 70 71. Prims Algorithmg MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ;y key[r] = 0; p[r] = NULL; while (Q not empty)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); M. Shoaib Farooq 71 72. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] Run on example graph if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 72 73. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] Run on example graph if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 73 74. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 10 3 15 8 r while (Q not empty) u = ExtractMin(Q); for each v Adj[u] Pick a start vertex r if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 74 75. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 10 3 15 8 u while (Q not empty) u = ExtractMin(Q); for each v Adj[u] Red vertices have been removed from Q if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 75 76. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 3 10 3 15 8 u while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 Red arrows indicate parent pointers if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 76 77. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 14 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 3 10 3 15 8 u while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 77 78. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 14 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3u if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 78 79. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 14 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3u if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 79 80. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 10 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3u if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 80 81. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 10 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 u if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 81 82. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 10 2 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 u if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 82 83. Prims Algorithmg MST-Prim(G, w, r) 6 4 9Q = V[G]; for each u Q key[u] = ; 10 2 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 15 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 u if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 83 84. Prims Algorithmg MST-Prim(G, w, r) 6 4 9 u Q = V[G]; for each u Q key[u] = ; 10 2 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 15 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 84 85. Prims Algorithmg MST-Prim(G, w, r) 6 4 9 u Q = V[G]; for each u Q key[u] = ; 10 2 9 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 15 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 85 86. Prims Algorithmg MST-Prim(G, w, r) 46 4 9 u Q = V[G]; for each u Q key[u] = ; 10 2 9 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 15 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 86 87. Prims Algorithmg MST-Prim(G, w, r) 46 4 9 u Q = V[G]; for each u Q key[u] = ; 5 2 9 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 15 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 87 88. Prims Algorithmg MST-Prim(G, w, r) 46 4 9 u Q = V[G]; for each u Q key[u] = ; 5 2 9 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 15 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 88 89. Prims Algorithmg MST-Prim(G, w, r) 46 4 9 u Q = V[G]; for each u Q key[u] = ; 5 2 9 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 15 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 89 90. Prims Algorithmg MST-Prim(G, w, r) 46 4 9 u Q = V[G]; for each u Q key[u] = ; 5 2 9 14 10 5 2 9 y key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 15 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 90 91. Prims Algorithmg MST-Prim(G, w, r) 46 4 9Q = V[G]; for each u Q key[u] = ; 5 2 9 14 10 5 2 9 uy key[r] = 0; p[r] = NULL; while (Q not empty) 0 8 15 3 10 3 15 8 while (Q not empty) u = ExtractMin(Q); for each v Adj[u] 3 if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); M. Shoaib Farooq 91 92. Review: Prims Algorithmg MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ;y key[r] = 0; p[r] = NULL; while (Q not empty) What is the hidden cost in this code? 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); M. Shoaib Farooq 92 93. Review: Prims Algorithmg MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ;y key[r] = 0; p[r] = NULL; while (Q not empty)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)); M. Shoaib Farooq 93 94. Review: Prims Algorithmg MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; How often is ExtractMin() called?y key[r] = 0; p[r] = NULL; while (Q not empty) How often is ExtractMin() called? How often is DecreaseKey() called? 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)); M. Shoaib Farooq 94 95. Review: Prims Algorithmg MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; What will be the running time? A: Depends on queuey key[r] = 0; p[r] = NULL; while (Q not empty) A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E) 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); M. Shoaib Farooq 95