Union-find Data Structure Last time Today Next Trees within - - PowerPoint PPT Presentation
Union-find Data Structure Last time Today Next Trees within - - PowerPoint PPT Presentation
Union-find Data Structure Last time Today Next Trees within Graphs Trees within Graphs Final Spanning Trees More efficient END OF 15-122! Minimum Spanning Trees Reminders Final exam Review session Practice
Last time
Trees within Graphs
- Spanning Trees
- Minimum Spanning
Trees Trees within Graphs
- More efficient
Today Next
Final END OF 15-122!
Reminders
- Final exam
- Review session
- Practice exams
- Practice written homework
Spanning Tree
3 4 1 2 5 8 6 7 A spanning tree is a subgraph of original graph that is a tree containing all vertices but only a minimal subset of the original edges
Finding a minimum spanning tree
Edge-centric algorithm: Kruskal’s algorithm Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T Greedy algorithm
- Choice at each step will be part of the solution
- No need for backtracking
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2
A-B (11) A-C (6) A-H (8) A-I (5) B-C (7) B-E (5) C-D (2) C-E (2) C-H(11) C-I (2) D-E (2) D-I (3) F-H (3) F-I (9) F-J (6) G-H (1) H-J (7)
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 2 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
3 1 2 2 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 1 2 2 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 1 2 2 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 1 2 2 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 1 2 2 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 1 2 2 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 2 2 1 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 2 2 1 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 2 2 1 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 1 2 2 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 2 2 1 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 2 2 1 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 2 2 1 2
Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston
6 7 9 11 8 5 6 11 3 7 5
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
2 3 2 2 1 2
Spanning Tree
3 4 1 2 5 8 6 7 Complexity
- General graph DFS/BFS
𝑃(𝑓) with adjacency lists
- Running DFS/BFS on spanning tree
𝑃(𝑓) but 𝑓 = 𝑤 − 1 𝑃(𝑤)
Finding a minimum spanning tree
Edge-centric algorithm: Kruskal’s algorithm Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T O(e log e) O(1) e O(v) O(1) O(1) = O(ev + e log e) O(ev) Where can we make it faster?
Canonical Representatives
Trying to speed up connectivity check 3 4 1 2 5 8 6 7 9
Relation of being connected is
- reflexive
- symmetric
- transitive
Canonical Representatives
Trying to speed up connectivity check 3 4 1 2 5 8 6 7 9
Every connected component is an equivalence class
Union-find
- 1. Check connectivity, finding canonical representative
- 2. Create union
3 4 1 2 5 8 6 7 9
Basic Union-find
4 5 3 2 1 4 5 3 2 1
1 2 3 4 5
Edge order (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1) Union-find data structure
1 2 3 4 4 1 2 3 3 4 1 1 3 3 4 1 1 1 3 4
Init
1 1 1 1 3 4
After (4,5) After (3,5) After (1,2) After (2,3) After (0,2) How fast is the connectivity check?
Union-find (UF) Tree
4 5 3 2 1
Finding a minimum spanning tree
Edge-centric algorithm: Kruskal’s algorithm Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T O(e log e) O(1) e * O(v) O(1) O(1) = O(ev + e log e) O(ev) With our basic union-find, still O(v) because UF tree may not be balanced
Basic Union-find
4 5 3 2 1 4 5 3 2 1
1 2 3 4 5
Edge order (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1) Union-find data structure
1 2 3 4 4 1 2 3 3 4 1 1 3 3 4 1 1 1 3 4
Init
1 1 1 1 3 4
After (4,5) After (3,5) After (1,2) After (2,3) After (0,2)
Height Tracking
How should we combine trees to control height?
Merge shorter tree into taller tree by appointing the root of the taller tree as the canonical representative of the combined tree height becomes the height of the bigger tree If trees have the same height, it does not matter which tree is merged into which height increases by 1
1 2 3 4 5
Basic Union-find
4 5 3 2 1 4 5 3 2 1
- 1
- 1
- 1
- 1
- 1
- 1
Edge order (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1)
Union-find data structure
- 1
- 1
- 1
- 1
- 2
4
- 1
- 1
- 1
- 3
3 4
- 1
- 2
1
- 3
3 4
- 1
- 4
1 1 3 4
Init
1
- 4
1 1 3 4
After (4,5) After (3,5) After (1,2) After (2,3) After (0,2) Negative numbers: height (used for canonical rep. only)
1 2 3 4 5
Height-tracking Union-find
4 5 3 2 1 4 5 3 2 1
- 1
- 1
- 1
- 1
- 1
- 1
Edge order (4,5) (3,5) (1,2) (3,4) (2,3) (0,2) (0,1)
Union-find data structure
- 1
- 1
- 1
- 1
- 2
4
- 1
- 1
- 1
4
- 2
4
- 1
- 2
1 4
- 2
4
- 1
- 3
1 4 1 4
Init
1
- 3
1 1 1 4
After (4,5) After (3,5) After (1,2) After (2,3) After (0,2) Negative numbers: height (used for canonical rep. only)
Finding a minimum spanning tree
Edge-centric algorithm: Kruskal’s algorithm Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T O(e log e) O(1) e * O(log v) O(1) O(1)
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9
- 1
- 1
- 1
- 1
- 1
- 1
- 2
6
- 1
- 1
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9
- 1
- 1
4
- 1
- 2
- 1
- 2
6
- 1
- 1
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9
- 1
- 1
4
- 1
- 2
- 1
- 2
6 4
- 1
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9
- 1
- 1
4 4
- 2
- 1
- 2
6 4
- 1
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9
- 1
- 1
4 4
- 2
- 1
- 2
6 4
- 1
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9
- 1
- 1
4 4
- 2
6
- 2
6 4
- 1
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9
- 1
4 4 4
- 2
6
- 2
6 4
- 1
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4
- 2
6
- 2
6 4
- 1
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4
- 2
6
- 2
6 4 6
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4
- 2
6
- 2
6 4 6
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4
- 2
6
- 2
6 4 6
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4
- 2
6
- 2
6 4 6
Union-find with Height Tracking
J F C E B I D A H G 5 3 8 4 2 1 6 9 7
G-H (1) C-E (2) C-I (2) D-E (2) C-D (2) D-I (3) F-H (3) B-E (5) A-I (5) F-J (6) A-C (6) B-C (7) H-J (7) A-H (8) F-I (9) C-H (11) A-B (11)
A B C D E F G H I J 1 2 3 4 5 6 7 8 9 4 4 4 4 6 6
- 3
6 4 6
Finding a minimum spanning tree
Edge-centric algorithm: Kruskal’s algorithm with height tracking Sort edges T = graph_new For each edge (i,j) of G: Are i and j already connected in T? Yes: Discard edge No: Add edge to T O(e log e) O(1) e O(log v) O(1) O(1) = O(e log v + e log e) O(e log e)
Height invariant for UF graphs
A tree with height h has at least 2h-1 nodes A tree with v vertices has height at most log v + 1 Equivalently,
Height invariant for UF graphs
A tree with height h has at least 2h-1 nodes
Base case: h = 1 Inductive case: h > 1 (height of tree after merging T1 with height h1 and T2 with height h2) case: h1 > h2 case: h2 > h1 case: h1 = h2
Height invariant for UF graphs
A tree with height h has at least 2h-1 nodes
Inductive case: h > 1 (height of tree after merging T1 with height h1 and T2 with height h2) case: h1 > h2 We merge the shorter tree into the taller one, so h = h1. By inductive hypothesis, T1 has at least 2h-1 nodes. T is the combined tree. So, it has at least 2h-1 nodes.
Height invariant for UF graphs
A tree with height h has at least 2h-1 nodes
Inductive case: h > 1 (height of tree after merging T1 with height h1 and T2 with height h2) case: h1 < h2 …
Height invariant for UF graphs
A tree with height h has at least 2h-1 nodes
Inductive case: h > 1 (height of tree after merging T1 with height h1 and T2 with height h2) case: h1 = h2 h = h1+ 1 = h2+ 1. By inductive hypothesis, T1 has at least 2 h1 -1 nodes and T1 has at least 2 h2 -1 nodes . T is the combined tree. So it has at least 2h-2 + 2h-2 = 2h-1 nodes.
Complexity of Union-find
Finding a canonical representative costs O(log v) Can we do better?
- As we follow a path to the root,
point all the intermediate nodes to the root
- This is called path compression and has almost constant amortized complexity.
2 3 4 1
After looking for the canonical representative of 3
2 3 4 1