SLIDE 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
1
Minimum Spanning Tree
Minimum spanning tree. Given a connected graph G = (V , E) with real-valued edge weights ce, 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.
a e b d c f 1 1 1 1 1 2 3 4
2
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.
3 Slides12 - UnionFind.key - March 6, 2019
SLIDE 2 Kruskal’ s Algorithm
a e b d c f 1 1 1 1 1 2 3 4
4
a e b d c f 1 1 1 1 1 2 3 4
Kruskal’ s Algorithm
5
a e b d c f 1 1 1 1 1 2 3 4
Kruskal’ s Algorithm
6 Slides12 - UnionFind.key - March 6, 2019
SLIDE 3 a e b d c f 1 1 1 1 1 2 3 4
Kruskal’ s Algorithm
7
a e b d c f 1 1 1 1 1 2 3 4
Kruskal’ s Algorithm
8
a e b d c f 1 1 1 1 1 2 3 4
Kruskal’ s Algorithm
Do not add the (c,d) edge since c and d are already connected.
9 Slides12 - UnionFind.key - March 6, 2019
SLIDE 4 a e b d c f 1 1 1 1 1 2 3 4
Kruskal’ s Algorithm
10
Kruskal’ s Algorithm
Kruskal(G, c) { Sort edges by weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} foreach (u ∈ V) make a set containing singleton u for each edge ei = (u, v) if (u and v are in different sets) { T ← T ∪ {ei} merge the sets containing u and v } return T }
11
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(S1, S2) - union 2 sets
12 Slides12 - UnionFind.key - March 6, 2019
SLIDE 5
Kruskal’ s Algorithm
Kruskal(G, c) { Sort edges by weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} foreach (u ∈ V) make a set containing singleton u for each edge ei = (u, v) if (u and v are in different sets) { T ← T ∪ {ei} merge the sets containing u and v } return T }
13
Kruskal’ s Algorithm
Kruskal(G, c) { Sort edges by weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} foreach (u ∈ V) make a set containing singleton u for each edge ei = (u, v) if (u and v are in different sets) { T ← T ∪ {ei} merge the sets containing u and v } return T }
14
Kruskal’ s Algorithm
Kruskal(G, c) { Sort edges by weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G) for each edge ei = (u, v) if (u and v are in different sets) { T ← T ∪ {ei} merge the sets containing u and v } return T }
15 Slides12 - UnionFind.key - March 6, 2019
SLIDE 6
Kruskal’ s Algorithm
Kruskal(G, c) { Sort edges by weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G) for each edge ei = (u, v) if (u and v are in different sets) { T ← T ∪ {ei} merge the sets containing u and v } return T }
16
Kruskal’ s Algorithm
Kruskal(G, c) { Sort edges by weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G) for each edge ei = (u, v) s1 = Find (u) s2 = Find (v) if (s1 != s2) { T ← T ∪ {ei} merge the sets containing u and v } return T }
17
Kruskal’ s Algorithm
Kruskal(G, c) { Sort edges by weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G) for each edge ei = (u, v) s1 = Find (u) s2 = Find (v) if (s1 != s2) { T ← T ∪ {ei} merge the sets containing u and v } return T }
18 Slides12 - UnionFind.key - March 6, 2019
SLIDE 7
Kruskal’ s Algorithm
Kruskal(G, c) { Sort edges by weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G) for each edge ei = (u, v) s1 = Find (u) s2 = Find (v) if (s1 != s2) { T ← T ∪ {ei} s = Union (s1, s2) } return T }
19
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(S1, S2)?
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(S1, S2)? O(n) O(1) O(n)
20-2 Slides12 - UnionFind.key - March 6, 2019
SLIDE 8 Union Operation
Union (S1, S2) { for i = 1 to n { if (components[i] == S1 || components[i] == S2) { components[i] = Snew; } } }
How can we avoid walking the entire array? How can we avoid renaming all the elements in S1 and S2?
21
Union Operation
Union (S1, S2) { if (S1.size() > S2.size() { shortS = S2; longS = S1 } else { shortS = S1; longS = S2; } for each node n in shortS { components[n] = longS; longS.append(n); } }
22
Union Operation
Union (S1, S2) { if (S1.size() > S2.size() { shortS = S2; longS = S1 } else { shortS = S1; longS = S2; } for each node n in shortS { components[n] = longS; longS.append(n); } }
23-1 Slides12 - UnionFind.key - March 6, 2019
SLIDE 9 Union Operation
Single call of this
Union (S1, S2) { if (S1.size() > S2.size() { shortS = S2; longS = S1 } else { shortS = S1; longS = S2; } for each node n in shortS { components[n] = longS; longS.append(n); } }
23-2
Cost of Kruskal’ s Algorithm?
Kruskal(G, c) { Sort edge weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G); for each edge ei = (u, v) S1 = Find(u) S2 = Find(v) if (S1 != S2) { T ← T ∪ {ei} Union (S1, S2) } return T }
24
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?
25-1 Slides12 - UnionFind.key - March 6, 2019
SLIDE 10
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-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? After k Union operations, how large can the largest set be?
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 been involved in a union? After k Union operations, how large can the largest set be?
2k 2k
25-4 Slides12 - UnionFind.key - March 6, 2019
SLIDE 11 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? After k Union operations, how large can the largest set be? After k Union operations, what is the maximum number of times that a single element v can be renamed?
2k 2k
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 been involved in a union? After k Union operations, how large can the largest set be? After k Union operations, what is the maximum number of times that a single element v can be renamed?
2k 2k log 2k
25-6
Union Operation
Cost of k Union
Union (S1, S2) { if (S1.size() > S2.size() { shortS = S2; longS = S1 } else { shortS = S1; longS = S2; } for each node n in shortS { components[n] = longS; longS.append(n); } }
26-1 Slides12 - UnionFind.key - March 6, 2019
SLIDE 12 Union Operation
Cost of k Union
O(k log 2k), or simply O(k log k)
Union (S1, S2) { if (S1.size() > S2.size() { shortS = S2; longS = S1 } else { shortS = S1; longS = S2; } for each node n in shortS { components[n] = longS; longS.append(n); } }
26-2
Union Operation
Cost of k Union
O(k log 2k), or simply O(k log k) Maximum number of Union operations?
Union (S1, S2) { if (S1.size() > S2.size() { shortS = S2; longS = S1 } else { shortS = S1; longS = S2; } for each node n in shortS { components[n] = longS; longS.append(n); } }
26-3
Union Operation
Cost of k Union
O(k log 2k), or simply O(k log k) Maximum number of Union operations? n, so Union is
O(n log n) for the entire graph
Union (S1, S2) { if (S1.size() > S2.size() { shortS = S2; longS = S1 } else { shortS = S1; longS = S2; } for each node n in shortS { components[n] = longS; longS.append(n); } }
26-4 Slides12 - UnionFind.key - March 6, 2019
SLIDE 13
Cost of Kruskal’ s Algorithm?
Kruskal(G, c) { Sort edges weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G); for each edge ei = (u, v) S1 = Find(u) S2 = Find(v) if (S1 != S2) { T ← T ∪ {ei} Union (S1, S2) } return T }
27-1
Cost of Kruskal’ s Algorithm?
Kruskal(G, c) { Sort edges weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G); for each edge ei = (u, v) S1 = Find(u) S2 = Find(v) if (S1 != S2) { T ← T ∪ {ei} Union (S1, S2) } return T }
Sort? MakeUnionFind? Total Find cost? Total Union cost? Overall total?
27-2
Cost of Kruskal’ s Algorithm?
Kruskal(G, c) { Sort edges weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G); for each edge ei = (u, v) S1 = Find(u) S2 = Find(v) if (S1 != S2) { T ← T ∪ {ei} Union (S1, S2) } return T }
Sort? MakeUnionFind? Total Find cost? Total Union cost? Overall total? O(m log n)
27-3 Slides12 - UnionFind.key - March 6, 2019
SLIDE 14
Cost of Kruskal’ s Algorithm?
Kruskal(G, c) { Sort edges weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G); for each edge ei = (u, v) S1 = Find(u) S2 = Find(v) if (S1 != S2) { T ← T ∪ {ei} Union (S1, S2) } return T }
Sort? MakeUnionFind? Total Find cost? Total Union cost? Overall total? O(m log n) O(n)
27-4
Cost of Kruskal’ s Algorithm?
Kruskal(G, c) { Sort edges weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G); for each edge ei = (u, v) S1 = Find(u) S2 = Find(v) if (S1 != S2) { T ← T ∪ {ei} Union (S1, S2) } return T }
Sort? MakeUnionFind? Total Find cost? Total Union cost? Overall total? O(m log n) O(n) O(m)
27-5
Cost of Kruskal’ s Algorithm?
Kruskal(G, c) { Sort edges weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G); for each edge ei = (u, v) S1 = Find(u) S2 = Find(v) if (S1 != S2) { T ← T ∪ {ei} Union (S1, S2) } return T }
Sort? MakeUnionFind? Total Find cost? Total Union cost? Overall total? O(m log n) O(n) O(m) O(n log n)
27-6 Slides12 - UnionFind.key - March 6, 2019
SLIDE 15
Cost of Kruskal’ s Algorithm?
Kruskal(G, c) { Sort edges weights so that c1 ≤ c2 ≤ ... ≤ cm. T ← {} MakeUnionFind (G); for each edge ei = (u, v) S1 = Find(u) S2 = Find(v) if (S1 != S2) { T ← T ∪ {ei} Union (S1, S2) } return T }
Sort? MakeUnionFind? Total Find cost? Total Union cost? Overall total? O(m log n) O(n) O(m) O(n log n) O(m log n)
27-7
Hallmarks of Greedy Algorithms
Algorithm structure: Sort Make optimally local decisions in order based on the sort Result is globally optimal Performance: generally not better than
O(n log n) due to the sort Proof techniques: Staying ahead (induction) Exchange (contradiction)
28 Slides12 - UnionFind.key - March 6, 2019