790 likes | 806 Views
Learn about Prim's Algorithm for finding Minimum Spanning Trees in Graphs step-by-step with example graph traversal and explanations.
E N D
Presentation on Algorithms Single-Source Shortest Path by WWW.ASSIGNMENTPOINT.COM www.assignmentpoint.com
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); www.assignmentpoint.com
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); 6 4 9 5 14 2 10 15 3 8 Run on example graph www.assignmentpoint.com
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); 6 4 9 5 14 2 10 15 3 8 Run on example graph www.assignmentpoint.com
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); 6 4 9 5 14 2 10 15 r 0 3 8 Pick a start vertex r www.assignmentpoint.com
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); 6 4 9 5 14 2 10 15 u 0 3 8 Red vertices have been removed from Q www.assignmentpoint.com
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); 6 4 9 5 14 2 10 15 u 0 3 8 3 Red arrows indicate parent pointers www.assignmentpoint.com
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); 6 4 9 5 14 14 2 10 15 u 0 3 8 3 www.assignmentpoint.com
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); 6 4 9 5 14 14 2 10 15 0 3 8 3 u www.assignmentpoint.com
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); 6 4 9 5 14 14 2 10 15 0 8 3 8 3 u www.assignmentpoint.com
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); 6 4 9 5 10 14 2 10 15 0 8 3 8 3 u www.assignmentpoint.com
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); 6 4 9 5 10 14 2 10 15 0 8 3 8 3 u www.assignmentpoint.com
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); 6 4 9 5 10 2 14 2 10 15 0 8 3 8 3 u www.assignmentpoint.com
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); 6 4 9 5 10 2 14 2 10 15 0 8 15 3 8 3 u www.assignmentpoint.com
Prim’s Algorithm u 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); 6 4 9 5 10 2 14 2 10 15 0 8 15 3 8 3 www.assignmentpoint.com
Review: Prim’s Algorithm u 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); 6 4 9 5 10 2 9 14 2 10 15 0 8 15 3 8 3 www.assignmentpoint.com
Review: Prim’s Algorithm u 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); 4 6 4 9 5 10 2 9 14 2 10 15 0 8 15 3 8 3 www.assignmentpoint.com
Review: Prim’s Algorithm u 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); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3 www.assignmentpoint.com
Review: Prim’s Algorithm u 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); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3 www.assignmentpoint.com
Review: Prim’s Algorithm u 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); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3 www.assignmentpoint.com
Review: Prim’s Algorithm u 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); 4 6 4 9 5 5 2 9 14 2 10 15 0 8 15 3 8 3 www.assignmentpoint.com
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); 4 6 4 9 5 5 2 9 u 14 2 10 15 0 8 15 3 8 3 www.assignmentpoint.com
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? www.assignmentpoint.com
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)); www.assignmentpoint.com
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? www.assignmentpoint.com
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) www.assignmentpoint.com
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 Rupnagar to Farmgate? www.assignmentpoint.com
Shortest Path Properties • Again, we have optimal substructure: the shortest path consists of shortest subpaths: • Proof: suppose some subpath is not a shortest path • There must then exist a shorter subpath • Could substitute the shorter subpath for a shorter path • But then overall path is not shortest path. Contradiction www.assignmentpoint.com
Shortest Path Properties • Define (u,v) to be the weight of the shortest path from u to v • Shortest paths satisfy the triangle inequality: (u,v) (u,x) + (x,v) • “Proof”: x u v This path is no longer than any other path www.assignmentpoint.com
Shortest Path Properties • In graphs with negative weight cycles, some shortest paths will not exist (Why?): < 0 www.assignmentpoint.com
2 2 9 6 5 5 Relax Relax 2 2 7 6 5 5 Relaxation • A key technique in shortest path algorithms is relaxation • Idea: for all v, maintain upper bound v.d on (s,v) Relax(u,v,w) { if (v.d > u.d+w) then v.d=u.d+w; } www.assignmentpoint.com
Initialize d, which will converge to shortest-path value Relaxation: Make |V|-1 passes, relaxing each edge Test for solutionUnder what condition do we get a solution? Bellman-Ford Algorithm BellmanFord() for each v V v.d = ; s.d = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (v.d > u.d + w(u,v)) return “no solution”; Relax(u,v,w): if (v.d > u.d+w) then v.d=u.d+w www.assignmentpoint.com
Bellman-Ford Algorithm BellmanFord() for each v V v.d = ; s.d = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (v.d > u.d + w(u,v)) return “no solution”; Relax(u,v,w): if (v.d > u.d+w) then v.d=u.d+w What will be the running time? www.assignmentpoint.com
Bellman-Ford Algorithm BellmanFord() for each v V v.d = ; s.d = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (v.d > u.d + w(u,v)) return “no solution”; Relax(u,v,w): if (v.d > u.d+w) then v.d=u.d+w What will be the running time? A: O(VE) www.assignmentpoint.com
Bellman-Ford Algorithm BellmanFord() for each v V v.d = ; s.d = 0; for i=1 to |V|-1 for each edge (u,v) E Relax(u,v, w(u,v)); for each edge (u,v) E if (v.d > u.d + w(u,v)) return “no solution”; Relax(u,v,w): if (v.d > u.d+w) then v.d=u.d+w B -1 s 2 A E 2 3 1 -3 4 C D 5 Ex: work on board www.assignmentpoint.com
Bellman-Ford • Note that order in which edges are processed affects how quickly it converges • Correctness: show v.d = (s,v) after |V|-1 passes • Consider any vertex v that is reachable from s, and let p = {v0, v1, ….., vk}, where v0 = s and vk = v, be any shortest path from s to v. • Because shortest paths are simple, p has at most |V| – 1 edges, and so k <= V – 1. • Each of the |V| – 1 iteration of the for loop relaxes all |E| edges. • Among the edges relaxed in the ith iteration, for i = 1, 2, …, k is (vi– 1, vi). • By the path relaxation property, therefore, v.d = vk.d = (s,vk) = (s,v) www.assignmentpoint.com
Bellman-Ford • Path relaxation property: if p = {v0, v1, ..,vk}, is a shortest path from s = v0 to vk, and we relax the edges of p in the order (v0, v1), (v1, v2), …, (vk-1,vk), then vk.d = (s,vk). • This property holds regardless of any other relaxation steps that occur, even if they are intermixed with relaxations of the edges of p. www.assignmentpoint.com
DAG Shortest Paths • Problem: finding shortest paths in DAG • Bellman-Ford takes O(VE) time. • How can we do better? • Idea: use topological sort • If were lucky and processes vertices on each shortest path from left to right, would be done in one pass • Every path in a dag is subsequence of topologically sorted vertex order, so processing verts in that order, we will do each path in forward order (will never relax edges out of vert before doing all edges into vert). • Thus: just one pass. What will be the running time? www.assignmentpoint.com
Dijkstra’s Algorithm • If no negative edge weights, we can beat BF • Similar to breadth-first search • Grow a tree gradually, advancing from vertices taken from a queue • Also similar to Prim’s algorithm for MST • Use a priority queue keyed on v.d www.assignmentpoint.com
B 2 10 A 4 3 D 5 1 C Ex: run the algorithm RelaxationStep Note: thisis really a call to Q->DecreaseKey() Dijkstra’s Algorithm Dijkstra(G) for each v V v.d = ; s.d = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q); S = S U {u}; for each v u->Adj[] if (v.d > u.d+w(u,v)) v.d = u.d+w(u,v); www.assignmentpoint.com
Dijkstra’s Algorithm Dijkstra(G) for each v V v.d = ; s.d = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q); S = S U {u}; for each v u->Adj[] if (v.d > u.d+w(u,v)) v.d = u.d+w(u,v); How many times is ExtractMin() called? How many times is DecraseKey() called? What will be the total running time? www.assignmentpoint.com
Dijkstra’s Algorithm Dijkstra(G) for each v V v.d = ; s.d = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q); S = S U {u}; for each v u->Adj[] if (v.d > u.d+w(u,v)) v.d = u.d+w(u,v); How many times is ExtractMin() called? How many times is DecraseKey() called? A: O(E lg V) using binary heap for Q Can acheive O(V lg V + E) with Fibonacci heaps www.assignmentpoint.com
Dijkstra’s Algorithm Dijkstra(G) for each v V v.d = ; s.d = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q); S = S U{u}; for each v u->Adj[] if (v.d > u.d+w(u,v)) v.d = u.d+w(u,v); Correctness: we must show that when u is removed from Q, it has already converged www.assignmentpoint.com
Correctness Of Dijkstra's Algorithm • Note that v.d (s,v) v • Let u be first vertex picked s.t. shorter path than u.d u.d > (s,u) • Let y be first vertex V-S on actual shortest path from su d[y] = (s,y) • Because d[x] is set correctly for y's predecessor x S on the shortest path, and • When we put x into S, we relaxed (x,y), giving d[y] the correct value p2 u s y x p2 www.assignmentpoint.com
Correctness Of Dijkstra's Algorithm • Note that v.d (s,v) v • Let u be first vertex picked s.t. shorter path than u.d u.d > (s,u) • Let y be first vertex V-S on actual shortest path from su d[y] = (s,y) • u.d > (s,u) = (s,y) + (y,u) (Why?) = d[y] + (y,u) d[y] But if u.d > d[y], wouldn't have chosen u. Contradiction. p2 u s y x p2 www.assignmentpoint.com
Disjoint-Set Union Problem • Want a data structure to support disjoint sets • Collection of disjoint sets S = {Si}, Si∩ Sj = • Need to support following operations: • MakeSet(x): S = SU {{x}} • Union(Si, Sj): S = S - {Si, Sj} U {SiU Sj} • FindSet(X): return Si S such that x Si • Before discussing implementation details, we look at example application: MSTs www.assignmentpoint.com
Kruskal’s Algorithm Kruskal() { T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } www.assignmentpoint.com
Kruskal’s Algorithm Run the algorithm: Kruskal() { T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 14 17 8 25 5 21 13 1 www.assignmentpoint.com
Kruskal’s Algorithm Run the algorithm: Kruskal() { T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 14 17 8 25 5 21 13 1 www.assignmentpoint.com
Kruskal’s Algorithm Run the algorithm: Kruskal() { T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 14 17 8 25 5 21 13 1 www.assignmentpoint.com