t oday
play

T ODAY Minimum Spanning Trees Greedy algorithm Edge-weighted - PowerPoint PPT Presentation

BBM 202 - ALGORITHMS D EPT . OF C OMPUTER E NGINEERING M INIMUM S PANNING T REES Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University. T ODAY Minimum Spanning


  1. 
 
 
 
 
 Removing two simplifying assumptions Q. What if edge weights are not all distinct? A. Greedy MST algorithm still correct if equal weights are present! 
 (our correctness proof fails, but that can be fixed) 1 2 1.00 1 2 1.00 1 3 0.50 1 3 0.50 2 4 1.00 2 4 1.00 3 4 0.50 3 4 0.50 Q. What if graph is not connected? A. Compute minimum spanning forest = MST of each component. 4 5 0.61 4 6 0.62 5 6 0.88 1 5 0.11 2 3 0.35 0 3 0.6 1 6 0.10 0 2 0.22 can independently compute MSTs of components 28

  2. M INIMUM S PANNING T REES ‣ Greedy algorithm ‣ Edge-weighted graph API ‣ Kruskal's algorithm ‣ Prim's algorithm ‣ Context

  3. Weighted edge API Edge abstraction needed for weighted edges. public class Edge implements Comparable<Edge> Edge(int v, int w, double weight) create a weighted edge v-w int either() either endpoint int other(int v) the endpoint that's not v int compareTo(Edge that) compare this edge to that edge double weight() the weight string representation String toString() weight w v Idiom for processing an edge e : int v = e.either(), w = e.other(v); 30

  4. Weighted edge: Java implementation public class Edge implements Comparable<Edge> { private final int v, w; private final double weight; public Edge(int v, int w, double weight) 
 constructor { this.v = v; this.w = w; this.weight = weight; } public int either() 
 either endpoint { return v; } public int other(int vertex) 
 other endpoint { if (vertex == v) return w; else return v; } public int compareTo(Edge that) 
 compare edges by weight { if (this.weight < that.weight) return -1; else if (this.weight > that.weight) return +1; else return 0; } } 31

  5. Edge-weighted graph API public class EdgeWeightedGraph EdgeWeightedGraph(int V) create an empty graph with V vertices create a graph from input stream EdgeWeightedGraph(In in) void add weighted edge e to this graph addEdge(Edge e) Iterable<Edge> edges incident to v adj(int v) Iterable<Edge> all edges in this graph edges() int V() number of vertices int E() number of edges string representation String toString() Conventions. Allow self-loops and parallel edges. 32

  6. Edge-weighted graph: adjacency-lists representation Maintain vertex-indexed array of Edge lists. 6 0 .58 0 2 .26 0 4 .38 0 7 .16 Bag tinyEWG.txt objects V 8 E 1 3 .29 1 2 .36 1 7 .19 1 5 .32 16 adj[] 4 5 0.35 0 4 7 0.37 6 2 .40 2 7 .34 1 2 .36 0 2 .26 2 3 .17 5 7 0.28 1 0 7 0.16 2 1 5 0.32 3 6 .52 1 3 .29 2 3 .17 3 0 4 0.38 2 3 0.17 4 6 4 .93 0 4 .38 4 7 .37 4 5 .35 1 7 0.19 5 0 2 0.26 references to the 6 1 2 0.36 1 5 .32 5 7 .28 4 5 .35 same Edge object 7 1 3 0.29 2 7 0.34 6 4 .93 6 0 .58 3 6 .52 6 2 .40 6 2 0.40 3 6 0.52 6 0 0.58 2 7 .34 1 7 .19 0 7 .16 5 7 .28 5 7 .28 6 4 0.93 33

  7. Edge-weighted graph: adjacency-lists implementation public class EdgeWeightedGraph { private final int V; same as Graph , but adjacency private final Bag<Edge>[] adj; lists of Edge s instead of integers public EdgeWeightedGraph(int V) { constructor this.V = V; adj = (Bag<Edge>[]) new Bag[V]; for (int v = 0; v < V; v++) adj[v] = new Bag<Edge>(); } public void addEdge(Edge e) { int v = e.either(), w = e.other(v); add edge to both adj[v].add(e); adjacency lists adj[w].add(e); } public Iterable<Edge> adj(int v) { return adj[v]; } } 34

  8. Minimum spanning tree API Q. How to represent the MST? public class MST MST(EdgeWeightedGraph G) constructor Iterable<Edge> edges in MST edges() weight of MST double weight() tinyEWG.txt V 8 E 16 % java MST tinyEWG.txt 4 5 0.35 MST edge 0-7 0.16 4 7 0.37 ( black ) 1-7 0.19 5 7 0.28 0 7 0.16 0-2 0.26 1 5 0.32 2-3 0.17 0 4 0.38 5-7 0.28 2 3 0.17 1 7 0.19 4-5 0.35 0 2 0.26 6-2 0.40 1 2 0.36 1.81 1 3 0.29 2 7 0.34 non-MST edge 6 2 0.40 ( gray ) 3 6 0.52 6 0 0.58 6 4 0.93 35

  9. Minimum spanning tree API Q. How to represent the MST? public class MST MST(EdgeWeightedGraph G) constructor Iterable<Edge> edges in MST edges() weight of MST double weight() public static void main(String[] args) % java MST tinyEWG.txt { 0-7 0.16 In in = new In(args[0]); 1-7 0.19 EdgeWeightedGraph G = new EdgeWeightedGraph(in); 0-2 0.26 MST mst = new MST(G); 2-3 0.17 for (Edge e : mst.edges()) 5-7 0.28 StdOut.println(e); 4-5 0.35 StdOut.printf("%.2f\n", mst.weight()); 6-2 0.40 } 1.81 36

  10. M INIMUM S PANNING T REES ‣ Greedy algorithm ‣ Edge-weighted graph API ‣ Kruskal's algorithm ‣ Prim's algorithm ‣ Context

  11. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. graph edges sorted by weight 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 1-2 0.36 6 4 4-7 0.37 0-4 0.38 an edge-weighted graph 6-2 0.40 3-6 0.52 6-0 0.58 6-4 0.93 38

  12. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. in MST 0-7 0.16 1 3 5 7 2 0 6 4 does not create a cycle 39

  13. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. does not 0-7 0.16 create a cycle in MST 2-3 0.17 1 3 5 7 2 0 6 4 40

  14. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. does not create a cycle 0-7 0.16 2-3 0.17 1 in MST 1-7 0.19 3 5 7 2 0 6 4 41

  15. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 in MST 0-2 0.26 5 7 2 0 6 4 does not create a cycle 42

  16. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 in MST 5-7 0.28 7 2 0 6 4 does not create a cycle 43

  17. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. creates a cycle 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 not in MST 1-3 0.29 2 0 6 4 44

  18. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. creates a cycle 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 not in MST 1-5 0.32 0 6 4 45

  19. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. creates a cycle 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 not in MST 2-7 0.34 6 4 46

  20. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 in MST 4-5 0.35 6 4 does not create a cycle 47

  21. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. creates a cycle 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 not in 1-2 0.36 MST 6 4 48

  22. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 1-2 0.36 6 not in 4 MST 4-7 0.37 creates a cycle 49

  23. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 1-2 0.36 6 4 4-7 0.37 not in MST 0-4 0.38 creates a cycle 50

  24. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 1-2 0.36 6 4 4-7 0.37 0-4 0.38 does not create a cycle in MST 6-2 0.40 51

  25. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 creates a cycle 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 1-2 0.36 6 4 4-7 0.37 0-4 0.38 6-2 0.40 not in MST 3-6 0.52 52

  26. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 1-2 0.36 6 4 4-7 0.37 0-4 0.38 creates a cycle 6-2 0.40 3-6 0.52 not in MST 6-0 0.58 53

  27. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 1-2 0.36 6 4 4-7 0.37 0-4 0.38 creates a cycle 6-2 0.40 3-6 0.52 6-0 0.58 not in MST 6-4 0.93 54

  28. Kruskal's algorithm • Consider edges in ascending order of weight. • Add next edge to tree T unless doing so would create a cycle. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 1-2 0.36 6 4 4-7 0.37 0-4 0.38 a minimum spanning tree 6-2 0.40 3-6 0.52 6-0 0.58 6-4 0.93 55

  29. Kruskal's algorithm: visualization 56

  30. 
 Kruskal's algorithm: correctness proof Proposition. [Kruskal 1956] Kruskal's algorithm computes the MST. Pf. Kruskal's algorithm is a special case of the greedy MST algorithm. • Suppose Kruskal's algorithm colors the edge e = v – w black. • Cut = set of vertices connected to v in tree T . • No crossing edge is black. • No crossing edge has lower weight. Why? add edge to tree 57

  31. Kruskal's algorithm: implementation challenge Challenge. Would adding edge v – w to tree T create a cycle? If not, add it. How difficult? • E + V • V run DFS from v, check if w is reachable 
 (T has at most V – 1 edges) • log V use the union-find data structure ! 
 • log* V (log* function: number of times needed to take the lg of a number until reaching 1) • 1 adding edge to tree add edge to tree would create a cycle 58

  32. 
 Kruskal's algorithm: implementation challenge Challenge. Would adding edge v – w to tree T create a cycle? If not, add it. Efficient solution. Use the union-find data structure. • Maintain a set for each connected component in T . • If v and w are in same set, then adding v – w would create a cycle. • To add v – w to T , merge sets containing v and w . w v w v Case 1: adding v – w creates a cycle Case 2: add v – w to T and merge sets containing v and w 59

  33. Kruskal's algorithm: Java implementation public class KruskalMST { private Queue<Edge> mst = new Queue<Edge>(); public KruskalMST(EdgeWeightedGraph G) { MinPQ<Edge> pq = new MinPQ<Edge>(); build priority queue for (Edge e : G.edges()) pq.insert(e); UF uf = new UF(G.V()); while (!pq.isEmpty() && mst.size() < G.V()-1) { Edge e = pq.delMin(); greedily add edges to MST int v = e.either(), w = e.other(v); if (!uf.connected(v, w)) edge v–w does not create cycle { merge sets uf.union(v, w); mst.enqueue(e); add edge to MST } } } public Iterable<Edge> edges() { return mst; } } 60

  34. 
 
 
 
 
 
 
 
 
 
 
 Kruskal's algorithm: running time Proposition. Kruskal's algorithm computes MST in time proportional to 
 E log E (in the worst case). Pf. operation frequency time per op build pq 1 E delete-min E log E log* function: 
 union V log* V † number of times needed to take 
 log* V † connected E the lg of a number until reaching 1 † amortized bound using weighted quick union with path compression recall: log* V ≤ 5 in this universe Remark. If edges are already sorted, order of growth is E log* V . 61

  35. M INIMUM S PANNING T REES ‣ Greedy algorithm ‣ Edge-weighted graph API ‣ Kruskal's algorithm ‣ Prim's algorithm ‣ Context

  36. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 1-2 0.36 6 4 4-7 0.37 0-4 0.38 an edge-weighted graph 6-2 0.40 3-6 0.52 6-0 0.58 6-4 0.93 63

  37. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 1 3 5 7 2 0 6 4 64

  38. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges with exactly min weight edge with one endpoint in T exactly one endpoint in T (sorted by weight) 1 3 5 in MST 0-7 0.16 0-2 0.26 7 2 0-4 0.38 6-0 0.58 0 6 4 65

  39. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 1 3 5 7 2 0 6 4 MST edges 0-7 66

  40. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges with exactly min weight edge with one endpoint in T exactly one endpoint in T (sorted by weight) 1 3 5 1-7 0.19 in MST 0-2 0.26 7 2 5-7 0.28 2-7 0.34 0 4-7 0.37 0-4 0.38 6 4 6-0 0.58 MST edges 0-7 67

  41. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 1 3 5 7 2 0 6 4 MST edges 0-7 1-7 68

  42. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges with exactly min weight edge with one endpoint in T exactly one endpoint in T (sorted by weight) 1 3 5 0-2 0.26 in MST 5-7 0.28 7 2 1-3 0.29 1-5 0.32 0 2-7 0.34 1-2 0.36 6 4 4-7 0.37 0-4 0.38 6-0 0.58 MST edges 0-7 1-7 69

  43. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 1 3 5 7 2 0 6 4 MST edges 0-7 1-7 0-2 70

  44. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges with exactly min weight edge with one endpoint in T exactly one endpoint in T (sorted by weight) 1 3 5 2-3 0.17 in MST 5-7 0.28 7 2 1-3 0.29 1-5 0.32 0 4-7 0.37 0-4 0.38 6 4 6-2 0.40 6-0 0.58 MST edges 0-7 1-7 0-2 71

  45. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 1 3 5 7 2 0 6 4 MST edges 0-7 1-7 0-2 2-3 72

  46. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges with exactly min weight edge with one endpoint in T exactly one endpoint in T (sorted by weight) 1 3 5 5-7 0.28 in MST 1-5 0.32 7 2 4-7 0.37 0-4 0.38 0 6-2 0.40 3-6 0.52 6 4 6-0 0.58 MST edges 0-7 1-7 0-2 2-3 73

  47. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 1 3 5 7 2 0 6 4 MST edges 0-7 1-7 0-2 2-3 5-7 74

  48. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges with exactly min weight edge with one endpoint in T exactly one endpoint in T (sorted by weight) 1 3 5 4-5 0.35 in MST 4-7 0.37 7 2 0-4 0.38 6-2 0.40 0 3-6 0.52 6-0 0.58 6 4 MST edges 0-7 1-7 0-2 2-3 5-7 75

  49. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 1 3 5 7 2 0 6 4 MST edges 0-7 1-7 0-2 2-3 5-7 4-5 76

  50. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges with exactly min weight edge with one endpoint in T exactly one endpoint in T (sorted by weight) 1 3 5 6-2 0.40 in MST 3-6 0.52 7 2 6-0 0.58 6-4 0.93 0 6 4 MST edges 0-7 1-7 0-2 2-3 5-7 4-5 77

  51. Prim's algorithm • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 1 3 5 7 2 0 6 4 MST edges 0-7 1-7 0-2 2-3 5-7 4-5 6-2 78

  52. Prim’s algorithm: visualization 79

  53. 
 Prim's algorithm: proof of correctness Proposition. [Jarník 1930, Dijkstra 1957, Prim 1959] 
 Prim's algorithm computes the MST. Pf. Prim's algorithm is a special case of the greedy MST algorithm. • Suppose edge e = min weight edge connecting a vertex on the tree 
 to a vertex not on the tree. • Cut = set of vertices connected on tree. • No crossing edge is black. • No crossing edge has lower weight. edge e = 7-5 added to tree 80

  54. Prim's algorithm: implementation challenge Challenge. Find the min weight edge with exactly one endpoint in T . How difficult? • E try all edges • V • log E use a priority queue ! • log* E • l 1-7 is min weight edge with exactly one endpoint in T priority queue of crossing edges 1-7 0.19 0-2 0.26 5-7 0.28 2-7 0.34 4-7 0.37 0-4 0.38 6-0 0.58 81

  55. 
 Prim's algorithm: lazy implementation Challenge. Find the min weight edge with exactly one endpoint in T . Lazy solution. Maintain a PQ of edges with (at least) one endpoint in T . • Key = edge; priority = weight of edge. • Delete-min to determine next edge e = v – w to add to T . • Disregard if both endpoints v and w are in T . • Otherwise, let v be vertex not in T : - add to PQ any edge incident to v (assuming other endpoint not in T ) - add v to T 1-7 is min weight edge with exactly one endpoint in T priority queue of crossing edges 1-7 0.19 0-2 0.26 5-7 0.28 2-7 0.34 4-7 0.37 0-4 0.38 6-0 0.58 82

  56. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 0-7 0.16 2-3 0.17 1 1-7 0.19 3 0-2 0.26 5 5-7 0.28 7 1-3 0.29 2 1-5 0.32 0 2-7 0.34 4-5 0.35 1-2 0.36 6 4 4-7 0.37 0-4 0.38 an edge-weighted graph 6-2 0.40 3-6 0.52 6-0 0.58 6-4 0.93 83

  57. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. 1 3 5 7 2 0 6 4 84

  58. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. add to PQ all edges incident to 0 edges on PQ 1 (sorted by weight) 3 5 * 0-7 0.16 * 0-2 0.26 7 2 * 0-4 0.38 * 6-0 0.58 0 6 4 85

  59. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. delete 0-7 and add to MST edges on PQ 1 (sorted by weight) 3 5 0-7 0.16 0-2 0.26 7 2 0-4 0.38 6-0 0.58 0 6 4 86

  60. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges on PQ 1 (sorted by weight) 3 5 0-2 0.26 0-4 0.38 7 2 6-0 0.58 0 6 4 MST edges 0-7 87

  61. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. add to PQ all edges incident to 7 edges on PQ 1 (sorted by weight) 3 5 * 1-7 0.19 0-2 0.26 7 2 * 5-7 0.28 * 2-7 0.34 0 * 4-7 0.37 0-4 0.38 6 4 6-0 0.58 MST edges 0-7 88

  62. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. delete 1-7 and add to MST edges on PQ 1 (sorted by weight) 3 5 1-7 0.19 0-2 0.26 7 2 5-7 0.28 2-7 0.34 0 4-7 0.37 0-4 0.38 6 4 6-0 0.58 MST edges 0-7 89

  63. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges on PQ 1 (sorted by weight) 3 5 0-2 0.26 5-7 0.28 7 2 2-7 0.34 4-7 0.37 0 0-4 0.38 6-0 0.58 6 4 MST edges 0-7 1-7 90

  64. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. add to PQ all edges incident to 1 edges on PQ 1 (sorted by weight) 3 5 0-2 0.26 5-7 0.28 7 2 * 1-3 0.29 * 1-5 0.32 0 2-7 0.34 * 1-2 0.36 6 4 4-7 0.37 0-4 0.38 6-0 0.58 MST edges 0-7 1-7 91

  65. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. delete edge 0-2 and add to MST edges on PQ 1 (sorted by weight) 3 5 0-2 0.26 5-7 0.28 7 2 1-3 0.29 1-5 0.32 0 2-7 0.34 1-2 0.36 6 4 4-7 0.37 0-4 0.38 6-0 0.58 MST edges 0-7 1-7 92

  66. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edge becomes obsolete (lazy implementation leaves on PQ) edges on PQ 1 (sorted by weight) 3 5 5-7 0.28 1-3 0.29 7 2 1-5 0.32 2-7 0.34 0 1-2 0.36 4-7 0.37 6 4 0-4 0.38 6-0 0.58 MST edges 0-7 1-7 0-2 93

  67. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. no need to add edge 1-2 or 2-7 because it's already obsolete add to PQ all edges incident to 2 edges on PQ 1 (sorted by weight) 3 5 * 2-3 0.17 5-7 0.28 7 2 1-3 0.29 1-5 0.32 0 2-7 0.34 1-2 0.36 6 4 4-7 0.37 0-4 0.38 * 6-2 0.40 6-0 0.58 MST edges 0-7 1-7 0-2 94

  68. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. delete 2-3 and add to MST edges on PQ 1 (sorted by weight) 3 5 * 2-3 0.17 5-7 0.28 7 2 1-3 0.29 1-5 0.32 0 2-7 0.34 1-2 0.36 6 4 4-7 0.37 0-4 0.38 * 6-2 0.40 6-0 0.58 MST edges 0-7 1-7 0-2 95

  69. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges on PQ 1 (sorted by weight) 3 5 5-7 0.28 1-3 0.29 7 2 1-5 0.32 2-7 0.34 0 1-2 0.36 4-7 0.37 6 4 0-4 0.38 6-2 0.40 6-0 0.58 MST edges 0-7 1-7 0-2 2-3 96

  70. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. add to PQ all edges incident to 3 edges on PQ 1 (sorted by weight) 3 5 5-7 0.28 1-3 0.29 7 2 1-5 0.32 2-7 0.34 0 1-2 0.36 4-7 0.37 6 4 0-4 0.38 6-2 0.40 * 3-6 0.52 6-0 0.58 MST edges 0-7 1-7 0-2 2-3 97

  71. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. delete 5-7 and add to MST edges on PQ 1 (sorted by weight) 3 5 5-7 0.28 1-3 0.29 7 2 1-5 0.32 2-7 0.34 0 1-2 0.36 4-7 0.37 6 4 0-4 0.38 6-2 0.40 3-6 0.52 6-0 0.58 MST edges 0-7 1-7 0-2 2-3 98

  72. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. edges on PQ 1 (sorted by weight) 3 5 1-3 0.29 1-5 0.32 7 2 2-7 0.34 1-2 0.36 0 4-7 0.37 0-4 0.38 6 4 6-2 0.40 3-6 0.52 6-0 0.58 MST edges 0-7 1-7 0-2 2-3 5-7 99

  73. Prim's algorithm - Lazy implementation • Start with vertex 0 and greedily grow tree T . • Add to T the min weight edge with exactly one endpoint in T . • Repeat until V-1 edges. add to PQ all edges incident to 5 edges on PQ 1 (sorted by weight) 3 5 1-3 0.29 1-5 0.32 7 2 2-7 0.34 * 4-5 0.35 0 1-2 0.36 4-7 0.37 6 4 0-4 0.38 6-2 0.40 3-6 0.52 6-0 0.58 MST edges 0-7 1-7 0-2 2-3 5-7 100

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend