Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture I Introduction

Similar presentations


Presentation on theme: "Lecture I Introduction"— Presentation transcript:

1 Lecture I Introduction
Problem: Dynamic Connectivity Solutions: correctness, complexity, improvements Applications ACKNOWLEDGEMENTS: Some slides in this lecture source from COS226 of Princeton University by Kevin Wayne and Bob Sedgewick

2 The problem Problem: DynamicConnectivity
Input: n sites, m operations (union or connected query) Output: the answer of connected queries union(4, 3) union(3, 8) union(6, 5) union(9, 4) union(2, 1) 1 2 3 4 connected(0, 7) connected(8, 9) union(5, 0) 5 6 7 8 9 union(7, 2) union(6, 1) connected(0, 7) union(1, 0) 9/4/2018 Xiaojuan Cai

3 More difficult example
Q. Connected(p, q) p q A. Yes. 9/4/2018 Xiaojuan Cai

4 Where are we? Problem: Dynamic Connectivity
Solutions: correctness, complexity, improvements Applications 9/4/2018 Xiaojuan Cai

5 Model the problem { 0 } { 1 4 5 } { 2 3 6 7 } { 0 } { 1 2 3 4 5 6 7 }
union(2, 5) 1 2 3 1 2 3 4 5 6 7 4 5 6 7 { 0 } { } { } { 0 } { } 3 connected components 2 connected components Connected(2, 5): Are 2 and 5 in the same connected components? 9/4/2018 Xiaojuan Cai

6 1, 2, and 7 are connected 3, 4, 8, and 9 are connected
S1: Quick find 0, 5 and 6 are connected 1, 2, and 7 are connected 3, 4, 8, and 9 are connected 1 8 2 3 4 5 6 7 9 id[] Demo: 0-9 union (4,3) (3,8) (6,5) (9,4) (2,1) connected (0,7) (8,9) union (5,0) (7,2) (6,1) connected (0,7) union (1,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]. 9/4/2018 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 B C. N2 D. logN 9/4/2018 Xiaojuan Cai

8 3's root is 9; 5's root is 6 3 and 5 are not connected
S2: Quick union 1 9 4 2 3 6 5 7 8 id[] 3 5 4 7 1 9 6 8 2 p 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. 9/4/2018 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 B C. N2 D. logN 9/4/2018 Xiaojuan Cai

10 Complexity Can we do better? algorithm initialize union find
quick-find N 1 quick-union Can we do better? 9/4/2018 Xiaojuan Cai

11 S3: 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)] 9/4/2018 Xiaojuan Cai

12 S3: Weighted union 9/4/2018 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 B C. N2 D. logN 9/4/2018 Xiaojuan Cai

14 Complexity Proposition. Depth of any node x is at most log N. 9/4/2018
Xiaojuan Cai

15 Complexity algorithm initialize union connected quick-find N 1
Proposition. Depth of any node x is at most log N. algorithm initialize union connected quick-find N 1 quick-union weighted QU log N 9/4/2018 Xiaojuan Cai

16 S4: Path compression Quick union with path compression. Just after computing the root of p, set the id of each node on the path to point to that root. 2 5 4 1 root 7 3 10 8 6 p 12 11 9 x 9/4/2018 Xiaojuan Cai

17 S4: Path compression Quick union with path compression. Just after computing the root of p, set the id of each node on the path to point to that root. 2 5 4 1 root p 12 11 9 7 3 10 8 6 x 9/4/2018 Xiaojuan Cai

18 S4: Path compression Quick union with path compression. Just after computing the root of p, set the id of each node on the path to point to that root. 2 5 4 1 root p 12 11 9 10 8 6 x 7 3 9/4/2018 Xiaojuan Cai

19 S4: Path compression Quick union with path compression. Just after computing the root of p, set the id of each node on the path to point to that root. 2 5 4 1 root p 12 11 9 10 8 6 7 3 x 9/4/2018 Xiaojuan Cai

20 Proofs are refer to [textbook] 4.3 and [DPV]5.1.4.
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. N log* N 1 2 4 16 3 65536 265536 5 Union by rank alone: n + mlogn for n sites, m operations Path compression alone: n + f(1 + log_{2+f/n} n ) for n sites, f find-perations Both: one complexity by Hopcroft and Ulman is n + mlog*n, another complexity by Tarjan is n + m\alpha(m,n) Bob Tarjan John Hopcroft Jeffrey Ullman Proofs are refer to [textbook] 4.3 and [DPV]5.1.4. 9/4/2018 Xiaojuan Cai

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. 9/4/2018 Xiaojuan Cai

22 Where are we? Problem: Dynamic Connectivity
Solutions: correctness, complexity, improvements Applications 9/4/2018 Xiaojuan Cai

23 Application Dynamic connectivity.
Percolation. [Coursera. AlgoPartI. PA1] Dynamic connectivity. Hindley-Milner polymorphic type inference. Kruskal's minimum spanning tree algorithm. Compiling equivalence statements in Fortran. ...... 9/4/2018 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 9/4/2018 Xiaojuan Cai


Download ppt "Lecture I Introduction"

Similar presentations


Ads by Google