Lecture I Introduction Problem Dynamic Connectivity Solutions correctness
Lecture I Introduction • Problem: Dynamic Connectivity • Solutions: correctness, complexity, improvements • Applications ACKNOWLEDGEMENTS: Some slides in this lecture source from COS 226 of Princeton University by Kevin Wayne and Bob Sedgewick
The problem union(4, 3) union(3, 8) Problem: Dynamic. Connectivity Input: n sites, m operations (union or connected query) Output: the answer of connected queries union(6, 5) union(9, 4) union(2, 1) connected(0, 7) ✗ connected(8, 9) ✔ 0 1 2 3 4 5 6 7 8 9 union(5, 0) union(7, 2) union(6, 1) connected(0, 7) ✔ union(1, 0) 2/25/2021 Xiaojuan Cai 2
More difficult example Q. Connected(p, q) p q A. Yes. 2/25/2021 Xiaojuan Cai 3
Where are we? • Problem: Dynamic Connectivity • Solutions: correctness, complexity, improvements • Applications 2/25/2021 Xiaojuan Cai 4
Model the problem union(2, 5) 0 1 2 3 4 5 6 7 {0}{145}{2367} {0}{1234567} 2 connected components 3 connected components Connected(2, 5): Are 2 and 5 in the same connected components? 2/25/2021 Xiaojuan Cai 6
S 1: Quick find id[] 0 1 2 3 4 5 6 7 8 9 0 1 1 8 8 0 0 1 0, 5 and 6 are connected 1, 2, and 7 are connected 8 8 3, 4, 8, and 9 are connected 0 1 2 3 4 5 6 7 8 9 Find(p, q). Check if p and q have the same id. Union(p, q). To merge components containing p and q, change all entries whose id equals id[p] to id[q]. 2/25/2021 Xiaojuan Cai 7
Quiz Assume there are N sites, then in the worst case, – quick-find need ( ) array access for union. – quick-find need ( ) array access for find. A. N 2/25/2021 B. 1 C. N 2 Xiaojuan Cai D. log. N 8
S 2: Quick union id[] 0 1 2 3 4 5 6 7 8 9 0 1 9 4 9 6 6 7 8 9 0 1 9 2 6 4 p 5 3 7 8 q 3's root is 9; 5's root is 6 3 and 5 are not connected Find(p, q). Check if p and q have the same root. Union(p, q). To merge components containing p and q, set the id of p's root to the id of q's root. 2/25/2021 Xiaojuan Cai 9
Quiz Assume there are N sites, then in the worst case, – quick-union need ( ) array access for union. – quick-union need ( ) array access for find. A. N 2/25/2021 B. 1 C. N 2 Xiaojuan Cai D. log. N 10
Complexity algorithm initialize union find quick-find N N 1 quick-union N N N Can we do better? 2/25/2021 Xiaojuan Cai 11
S 3: Weighted union Find(p, q). Check if p and q have the same root. Union(p, q). if size[root(p)] <= size[root(q)] then id[root(p)] = id[root(q)] else id[root(q)] = id[root(p)] 2/25/2021 Xiaojuan Cai 12
S 3: Weighted union 2/25/2021 Xiaojuan Cai 13
Quiz Assume there are N sites, then in the worst case, – weighted-QU need ( ) array access for union. – weighted-QU need ( ) array access for find. A. N 2/25/2021 B. 1 C. 2 N Xiaojuan Cai D. log. N 14
Complexity Proposition. Depth of any node x is at most log N. 2/25/2021 Xiaojuan Cai 15
Complexity Proposition. Depth of any node x is at most log N. algorithm 2/25/2021 initialize union connect ed quick-find N N 1 quick-union N N N weighted QU N log N Xiaojuan Cai 16
S 4: Path compression Quick union with path compression. Just after computing the root of p, set the id of each node on root the path to point to that root. 0 1 3 4 2 5 7 6 p 8 2/25/2021 10 x 11 9 Xiaojuan Cai 12 17
S 4: Path compression Quick union with path compression. Just after computing the root of p, set the id of each node on root the path to point to that root. 0 p 1 9 11 3 12 x 6 4 2 5 7 8 2/25/2021 10 Xiaojuan Cai 18
S 4: Path compression Quick union with path compression. Just after computing the root of p, set the id of each node on root the path to point to that root. 0 p 6 9 11 12 8 1 x 4 5 7 10 2/25/2021 3 2 Xiaojuan Cai 19
S 4: Path compression Quick union with path compression. Just after computing the root of p, set the id of each node on root the path to point to that root. 0 p 6 9 11 12 8 3 7 x 1 4 2 5 10 2/25/2021 Xiaojuan Cai 20
Complexity Proposition. Starting from an empty data structure, any sequence of M union-find operations on N objects makes at most proportional to N + M log* N array accesses. John Hopcroft 2/25/2021 Jeffrey Ullman N log* N 1 0 2 1 4 2 16 3 65536 4 265536 5 Proofs are refer to [textbook] 4. 3 and [DPV]5. 1. 4. Xiaojuan Cai Bob Tarjan 21
Complexity Proposition. Starting from an empty data structure, any sequence of M union-find operations on N objects makes at most proportional to N + M log* N array accesses. Can we do better? Amazing fact. No linear-time algorithm exists. 2/25/2021 Xiaojuan Cai 22
Where are we? • Problem: Dynamic Connectivity • Solutions: correctness, complexity, improvements • Applications 2/25/2021 Xiaojuan Cai 23
Application • Percolation. [Coursera. Algo. Part. I. PA 1] • Dynamic connectivity. • Hindley-Milner polymorphic type inference. • Kruskal's minimum spanning tree algorithm. • Compiling equivalence statements in Fortran. • . . . 2/25/2021 Xiaojuan Cai 24
Conclusion • Model a problem • Solve it, then ask three questions: Is it correct? How much time does it take? Can we do better? • Applications 2/25/2021 Xiaojuan Cai 25
- Slides: 24