CS 225 Data Structures No Novem ember er 20 20 – MS MSTs: Kruskal + Prim’s Algorithm G G Carl Evans
Kr Kruskal’s Algorithm (A, D) B 15 1 KruskalMST(G): 5 (E, H) 2 DisjointSets forest C 5 16 A (F, G) 3 foreach (Vertex v : G): 2 10 11 4 forest.makeSet(v) (A, B) 16 13 E D 5 2 17 (B, D) 6 PriorityQueue Q // min edge weight H 8 12 7 foreach (Edge e : G): F (G, E) 12 9 8 Q.insert(e) 4 G (G, H) 9 10 Graph T = (V, {}) (E, C) 11 (C, H) 12 while |T.edges()| < n-1: 13 Vertex (u, v) = Q.removeMin() (E, F) 14 if forest.find(u) != forest.find(v): (F, C) 15 T.addEdge(u, v) 16 forest.union( forest.find(u), (D, E) B C 17 forest.find(v) ) A D (B, C) 18 19 return T (C, D) E F H G (A, F) (D, F)
Kr Kruskal’s Algorithm Priority Queue: 1 KruskalMST(G): 2 DisjointSets forest Heap Sorted Array 3 foreach (Vertex v : G): Building 4 forest.makeSet(v) :6-8 5 6 PriorityQueue Q // min edge weight Each removeMin 7 foreach (Edge e : G): :13 8 Q.insert(e) 9 10 Graph T = (V, {}) 11 12 while |T.edges()| < n-1: 13 Vertex (u, v) = Q.removeMin() 14 if forest.find(u) != forest.find(v): 15 T.addEdge(u, v) 16 forest.union( forest.find(u), 17 forest.find(v) ) 18 19 return T
Kruskal’s Algorithm Kr Priority Queue: 1 KruskalMST(G): 2 DisjointSets forest Total Running Time 3 foreach (Vertex v : G): Heap 4 forest.makeSet(v) 5 6 PriorityQueue Q // min edge weight Sorted Array 7 foreach (Edge e : G): 8 Q.insert(e) 9 10 Graph T = (V, {}) 11 12 while |T.edges()| < n-1: 13 Vertex (u, v) = Q.removeMin() 14 if forest.find(u) != forest.find(v): 15 T.addEdge(u, v) 16 forest.union( forest.find(u), 17 forest.find(v) ) 18 19 return T
Kr Kruskal’s Algorithm Which Priority Queue Implementation is better for running Kruskal’s Algorithm? • Heap: • Sorted Array:
Pa Partition Property Consider an arbitrary partition of the vertices on G into two subsets U and V . V U A 4 8 2 B D 7 1 2 C 5 3 9 F E
Pa Partition Property Consider an arbitrary partition of the vertices on G into two subsets U and V . V U A Let e be an edge of 4 8 2 B D minimum weight across 7 e 1 2 the partition. C 5 3 9 F E Then e is part of some minimum spanning tree.
Pa Partition Property The partition property suggests an algorithm: B 15 2 C 5 A 13 17 11 5 8 10 D E 16 H 9 12 9 16 4 F G
Prim’s Algorithm Pr 1 PrimMST(G, s): 2 Input: G, Graph; 3 s, vertex in G, starting vertex 4 Output: T, a minimum spanning tree (MST) of G 5 6 foreach (Vertex v : G): B 7 d[v] = +inf 15 2 8 p[v] = NULL C 9 d[s] = 0 5 A 10 13 11 PriorityQueue Q // min distance, defined by d[v] 11 17 12 Q.buildHeap(G.vertices()) 8 13 Graph T // "labeled set" D E 16 14 9 15 repeat n times: 12 16 Vertex m = Q.removeMin() 17 T.add(m) 18 foreach (Vertex v : neighbors of m not in T): F 19 if cost(v, m) < d[v]: 20 d[v] = cost(v, m) 21 p[v] = m 22 23 return T
6 PrimMST(G, s): Pr Prim’s Algorithm 7 foreach (Vertex v : G): 8 d[v] = +inf 9 p[v] = NULL 10 d[s] = 0 11 12 PriorityQueue Q // min distance, defined by d[v] 13 Q.buildHeap(G.vertices()) 14 Graph T // "labeled set" 15 16 repeat n times: 17 Vertex m = Q.removeMin() 18 T.add(m) 19 foreach (Vertex v : neighbors of m not in T): 20 if cost(v, m) < d[v]: 21 d[v] = cost(v, m) 22 p[v] = m Adj. Matrix Adj. List Heap Unsorted Array
6 PrimMST(G, s): Pr Prim’s Algorithm 7 foreach (Vertex v : G): 8 d[v] = +inf 9 p[v] = NULL Sparse Graph: 10 d[s] = 0 11 12 PriorityQueue Q // min distance, defined by d[v] 13 Q.buildHeap(G.vertices()) 14 Graph T // "labeled set" 15 16 repeat n times: Dense Graph: 17 Vertex m = Q.removeMin() 18 T.add(m) 19 foreach (Vertex v : neighbors of m not in T): 20 if cost(v, m) < d[v]: 21 d[v] = cost(v, m) 22 p[v] = m Adj. Matrix Adj. List O(n 2 + m lg(n)) O(n lg(n) + m lg(n)) Heap Unsorted O(n 2 ) O(n 2 ) Array
MS MST Algori orithm m Ru Runtime me: • Prim’s Algorithm: • Kruskal’s Algorithm: O(n lg(n) + m lg(n)) O(n + m lg(n)) • What must be true about the connectivity of a graph when running an MST algorithm? • How does n and m relate?
MS MST Algori orithm m Ru Runtime me: • Kruskal’s Algorithm: O(n + m lg(n)) • Prim’s Algorithm: O(n lg(n) + m lg(n))
Recommend
More recommend