1 Special Events TODAY: March 6, 12:15, Kendade 307, Lunch with Sowmya Subramanian ’96, Senior Director of Engineering, Google TODAY: March 6, 7:00 p.m., Gamble Auditorium, Sowmya Subramanian, Empowering Women Through Technology 2 Minimum Spanning Tree Minimum spanning tree. Given a connected graph G = (V , E) with real-valued edge weights c e , a Minimum Spanning Tree (MST) is a subset of the edges T ⊆ E such that T is a spanning tree whose sum of edge weights is minimized. 1 a c 2 3 1 1 1 e f 4 b d 1 3 Multiple Greedy Algorithms Solve MST! Prim’ s algorithm: Greedily grow the tree adding minimum cost edge between S and V-S. Kruskal's algorithm. Sort all edges from low to high cost and add an edge as long as it does not create a cycle. Reverse-Delete algorithm. Start with T = G. Sort edges from high to low cost. Delete an edge as long as it does not disconnect the graph. Slides12 - UnionFind.key - March 6, 2019
4 Kruskal’ s Algorithm 1 a c 2 3 1 1 1 e f 4 b d 1 5 Kruskal’ s Algorithm 1 a c 2 3 1 1 1 e f 4 b d 1 6 Kruskal’ s Algorithm 1 a c 2 3 1 1 1 e f 4 b d 1 Slides12 - UnionFind.key - March 6, 2019
7 Kruskal’ s Algorithm 1 a c 2 3 1 1 1 e f 4 b d 1 8 Kruskal’ s Algorithm 1 a c 2 3 1 1 1 e f 4 b d 1 9 Kruskal’ s Algorithm 1 a c 2 3 1 1 1 e f 4 b d 1 Do not add the (c,d) edge since c and d are already connected. Slides12 - UnionFind.key - March 6, 2019
10 Kruskal’ s Algorithm 1 a c 2 3 1 1 1 e f 4 b d 1 Kruskal’ s Algorithm 11 Kruskal(G, c) { Sort edges by weights so that c 1 ≤ c 2 ≤ ... ≤ c m . T ← {} foreach (u ∈ V) make a set containing singleton u for each edge e i = (u, v) if (u and v are in different sets) { T ← T ∪ {e i } merge the sets containing u and v } return T } 12 Union-Find Data structure that allows us to quickly find which set an element is in and to union 2 sets Assumptions: Sets are disjoint We are not interested in splitting sets Operations: MakeUnionFind (G) - create the initial sets each containing 1 node Find(v) - determine which set a node is in Union(S 1 , S 2 ) - union 2 sets Slides12 - UnionFind.key - March 6, 2019
13 Kruskal’ s Algorithm Kruskal(G, c) { Sort edges by weights so that c 1 ≤ c 2 ≤ ... ≤ c m . T ← {} foreach (u ∈ V) make a set containing singleton u for each edge e i = (u, v) if (u and v are in different sets) { T ← T ∪ {e i } merge the sets containing u and v } return T } Kruskal’ s Algorithm 14 Kruskal(G, c) { Sort edges by weights so that c 1 ≤ c 2 ≤ ... ≤ c m . T ← {} foreach (u ∈ V) make a set containing singleton u for each edge e i = (u, v) if (u and v are in different sets) { T ← T ∪ {e i } merge the sets containing u and v } return T } 15 Kruskal’ s Algorithm Kruskal(G, c) { Sort edges by weights so that c 1 ≤ c 2 ≤ ... ≤ c m . T ← {} MakeUnionFind (G) for each edge e i = (u, v) if (u and v are in different sets) { T ← T ∪ {e i } merge the sets containing u and v } return T } Slides12 - UnionFind.key - March 6, 2019
16 Kruskal’ s Algorithm Kruskal(G, c) { Sort edges by weights so that c 1 ≤ c 2 ≤ ... ≤ c m . T ← {} MakeUnionFind (G) for each edge e i = (u, v) if (u and v are in different sets) { T ← T ∪ {e i } merge the sets containing u and v } return T } Kruskal’ s Algorithm 17 Kruskal(G, c) { Sort edges by weights so that c 1 ≤ c 2 ≤ ... ≤ c m . T ← {} MakeUnionFind (G) for each edge e i = (u, v) s1 = Find (u) s2 = Find (v) if (s1 != s2) { T ← T ∪ {e i } merge the sets containing u and v } return T } 18 Kruskal’ s Algorithm Kruskal(G, c) { Sort edges by weights so that c 1 ≤ c 2 ≤ ... ≤ c m . T ← {} MakeUnionFind (G) for each edge e i = (u, v) s1 = Find (u) s2 = Find (v) if (s1 != s2) { T ← T ∪ {e i } merge the sets containing u and v } return T } Slides12 - UnionFind.key - March 6, 2019
19 Kruskal’ s Algorithm Kruskal(G, c) { Sort edges by weights so that c 1 ≤ c 2 ≤ ... ≤ c m . T ← {} MakeUnionFind (G) for each edge e i = (u, v) s1 = Find (u) s2 = Find (v) if (s1 != s2) { T ← T ∪ {e i } s = Union (s1, s2) } return T } 20-1 Union-Find Data Structure / / Array maps a node to the set it is in. Set[] components; Cost of MakeUnionFind(G)? Cost of Find(v)? Cost of Union(S 1 , S 2 )? 20-2 Union-Find Data Structure / / Array maps a node to the set it is in. Set[] components; Cost of MakeUnionFind(G)? O(n) Cost of Find(v)? O(1) Cost of Union(S 1 , S 2 )? O(n) Slides12 - UnionFind.key - March 6, 2019
21 Union Operation Union (S 1 , S 2 ) { for i = 1 to n { if (components[i] == S 1 || components[i] == S 2 ) { components[i] = S new ; } } } How can we avoid walking the entire array? How can we avoid renaming all the elements in S 1 and S 2 ? Union Operation 22 Union (S 1 , S 2 ) { if (S 1 .size() > S 2 .size() { shortS = S 2 ; longS = S 1 } else { shortS = S 1 ; longS = S 2; } for each node n in shortS { components[n] = longS; longS.append(n); } } Union Operation 23-1 Union (S 1 , S 2 ) { if (S 1 .size() > S 2 .size() { shortS = S 2 ; longS = S 1 } else { shortS = S 1 ; longS = S 2; } for each node n in shortS { components[n] = longS; longS.append(n); } } Slides12 - UnionFind.key - March 6, 2019
Union Operation 23-2 Union (S 1 , S 2 ) { if (S 1 .size() > S 2 .size() { shortS = S 2 ; Single call of this longS = S 1 } operation is O(n) else { shortS = S 1 ; longS = S 2; } for each node n in shortS { components[n] = longS; longS.append(n); } } Cost of Kruskal’ s Algorithm? 24 Kruskal(G, c) { Sort edge weights so that c 1 ≤ c 2 ≤ ... ≤ c m . T ← {} MakeUnionFind (G); for each edge e i = (u, v) S 1 = Find(u) S 2 = Find(v) if (S 1 != S 2 ) { T ← T ∪ {e i } Union (S 1 , S 2 ) } return T } 25-1 Observations on Union Operation Initially, each element is in a separate set. After k Union operations, what is the maximum number of elements that have been involved in a union? Slides12 - UnionFind.key - March 6, 2019
25-2 Observations on Union Operation Initially, each element is in a separate set. After k Union operations, what is the maximum number of elements that have been involved in a union? 2k 25-3 Observations on Union Operation Initially, each element is in a separate set. After k Union operations, what is the maximum number of elements that have 2k been involved in a union? After k Union operations, how large can the largest set be? 25-4 Observations on Union Operation Initially, each element is in a separate set. After k Union operations, what is the maximum number of elements that have 2k been involved in a union? After k Union operations, how large can the 2k largest set be? Slides12 - UnionFind.key - March 6, 2019
25-5 Observations on Union Operation Initially, each element is in a separate set. After k Union operations, what is the maximum number of elements that have 2k been involved in a union? After k Union operations, how large can the 2k largest set be? After k Union operations, what is the maximum number of times that a single element v can be renamed? 25-6 Observations on Union Operation Initially, each element is in a separate set. After k Union operations, what is the maximum number of elements that have 2k been involved in a union? After k Union operations, how large can the largest set be? 2k After k Union operations, what is the maximum number of times that a single element v can be log 2k renamed? Union Operation 26-1 Cost of k Union Union (S 1 , S 2 ) { operations? if (S 1 .size() > S 2 .size() { shortS = S 2 ; longS = S 1 } else { shortS = S 1 ; longS = S 2; } for each node n in shortS { components[n] = longS; longS.append(n); } } Slides12 - UnionFind.key - March 6, 2019
Union Operation 26-2 Cost of k Union Union (S 1 , S 2 ) { operations? if (S 1 .size() > S 2 .size() { shortS = S 2 ; O(k log 2k), or simply longS = S 1 O(k log k) } else { shortS = S 1 ; longS = S 2; } for each node n in shortS { components[n] = longS; longS.append(n); } } Union Operation 26-3 Cost of k Union Union (S 1 , S 2 ) { operations? if (S 1 .size() > S 2 .size() { shortS = S 2 ; O(k log 2k), or simply longS = S 1 O(k log k) } else { Maximum number of shortS = S 1 ; longS = S 2; Union operations? } for each node n in shortS { components[n] = longS; longS.append(n); } } Union Operation 26-4 Cost of k Union Union (S 1 , S 2 ) { operations? if (S 1 .size() > S 2 .size() { shortS = S 2 ; O(k log 2k), or simply longS = S 1 O(k log k) } else { Maximum number of shortS = S 1 ; longS = S 2; Union operations? } for each node n in shortS { n, so Union is components[n] = longS; O(n log n) for the longS.append(n); entire graph } } Slides12 - UnionFind.key - March 6, 2019
Recommend
More recommend