csci 3160 design and analysis of algorithms tutorial 3 chengyu lin
TRANSCRIPT
![Page 1: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/1.jpg)
CSCI 3160 Design and Analysis of
Algorithms
Tutorial 3
Chengyu Lin
![Page 2: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/2.jpg)
Outline• Union-find data structure
• Minimum spanning tree (MST)
• Kruskal’s algorithm
![Page 3: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/3.jpg)
Union-find• A data structure for disjoint sets• n = number of members, forming disjoint
groups• Two members are in the same group if and
only if they have a common leader• Operations:
o Union: merge two groupso Find: name the leader of the group
• We do not really care about who the leader is; we only want to tell one group from another
![Page 4: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/4.jpg)
Union-find• Idea: use a forest (= collection of trees)
o a group → a treeo leader → root of the tree
• Example:o Group 1: Alice, Bobo Group 2: Carol, Dave, Eve
• Store the height of each tree at the root
A
B C
D
E
1 1
![Page 5: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/5.jpg)
Union-find• Find: return the root of the group
• Union: make the leader of one group the boss of the othero Our heuristic: make the root of the shorter
tree point to the root of the other tree
o If both trees are of the same height h, then the resulting tree has height h+1
![Page 6: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/6.jpg)
Union-find in action• Initialize
o Everyone is his/her own boss
A B C D E0 0 0 0 0
![Page 7: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/7.jpg)
Union-find in action• Union(A, B)
o Find(A) returns A and Find(B) returns Bo Make Alice the boss of Bobo Increase the height of the tree
A B C D E1 0 0 0 0
![Page 8: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/8.jpg)
Union-find in action• Union(C, D)
o Find(C) returns C and Find(D) returns Do Make Carol the boss of Daveo Increase the height of the tree
A B C D E1 0 1 0 0
![Page 9: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/9.jpg)
Union-find in action• Union(B, D)
o Find(B) returns A and Find(D) returns Co Make Alice the boss of Carolo Increase the height of the tree
A B C D E2 0 1 0 0
![Page 10: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/10.jpg)
Your turn• Find(C)?
A B C D E2 0 1 0 0
![Page 11: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/11.jpg)
Your turn• Find(C)?
o Find(C) returns A
A B C D E2 0 1 0 0
![Page 12: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/12.jpg)
Your turn• Union(B, E)?
A B C D E2 0 1 0 0
![Page 13: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/13.jpg)
Your turn• Union(B, E)?
o Find(B) returns A and Find(E) returns Eo Make Alice the boss of Eveo No increase in the tree height (Why?)
A B C D E2 0 1 0 0
![Page 14: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/14.jpg)
Final result• Does this look more like a tree?
A
B C
D
E
2
0 1
0
0
![Page 15: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/15.jpg)
Analysis• Height of each tree = O(log n)
• Cost of Find(): O(log n)
• Cost of Union(): O(log n)o Dominated by the cost of Find()
![Page 16: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/16.jpg)
Minimum spanning tree
• G = (V, E): undirected, connected, (non-negatively) weighted
• Problem: find a subset of edges with minimum total weight such that all vertices in V are connected using these edgesA B
C D E
1
2
34 5 6
7
Total weight = 1 + 2 + 3 + 6 = 12
![Page 17: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/17.jpg)
Kruskal’s algorithm• In words:
o Sort the edges in ascending order of weightso Initialize: set T = Øo While T is not a spanning tree
• Consider the next edge in the sorted list• If adding it to T does not cause a cycle, add it
• The union-find structure helps us check for cycleso Adding an edge corresponds to putting the two
endpoints into the same groupo Connecting two vertices in the same group causes a
cycle
![Page 18: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/18.jpg)
Dry run• 1: Sort the edges
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
1
2
34 5 6
7
![Page 19: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/19.jpg)
Dry run• 2: Set T = Ø
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
1
2
34 5 6
7
A B
C D E
![Page 20: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/20.jpg)
Dry run• 3: Consider the first edge
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
1
2
34 5 6
7
A B
C D E
![Page 21: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/21.jpg)
Dry run• 4: Consider the second edge
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
1
2
34 5 6
7
A B
C D E
![Page 22: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/22.jpg)
Dry run• 5: Consider the third edge
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
1
2
34 5 6
7
A B
C D E
![Page 23: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/23.jpg)
Dry run• 6:
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
1
2
34 5 6
7
A B
C D E
![Page 24: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/24.jpg)
Dry run• 7:
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
1
2
34 5 6
7
A B
C D E
![Page 25: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/25.jpg)
Dry run• 8:
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
1
2
34 5 6
7
A B
C D E
![Page 26: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/26.jpg)
Dry run• 9: We have find an MST!
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
1
2
34 5 6
7
A B
C D E
![Page 27: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/27.jpg)
Analysis• Correctness
o In each step, we keep a set of edges T that is a subset of an MST
o Theorem: Let S be any tree in the forest (V, T). We can add the lightest edge from S to V-S to T, and the resulting set of edges is also a subset of an MST
• Space complexity = O(|V|+|E|)• Time complexity = O(|E|log|V|)
o Sorting alone takes time O(|E|log|V|) (Note that |E| = O(|V|2))
o Cycle checking takes O(log|V|) operations (Find())o Adding an edge also takes O(log|V|) operations
(Union())
![Page 28: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/28.jpg)
Dry run revisited• 2: Set T = Ø
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
A B C D E0 0 0 0 0
![Page 29: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/29.jpg)
Dry run revisited• 3: Consider the first edge
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
A B C D E1 0 0 0 0
![Page 30: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/30.jpg)
Dry run revisited• 4: Consider the second edge
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
A B C D E1 0 1 0 0
![Page 31: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/31.jpg)
Dry run revisited• 5: Consider the third edge
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
0A B C D E2 0 1 0
![Page 32: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/32.jpg)
Dry run revisited• 6:
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
0A B C D E2 0 1 0
![Page 33: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/33.jpg)
Dry run revisited• 7:
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
0A B C D E2 0 1 0
![Page 34: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/34.jpg)
Dry run revisited• 8:
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
A B C D E2 0 1 0 0
![Page 35: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/35.jpg)
Dry run revisited• 9: We have find an MST!
Edge Weight(A, B) 1(C, D) 2(B, D) 3(A, C) 4(A, D) 5(B, E) 6(D, E) 7
A B
C D E
A B C D E2 0 1 0 0
![Page 36: CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin](https://reader035.vdocument.in/reader035/viewer/2022062309/5697bfb81a28abf838c9f73f/html5/thumbnails/36.jpg)
End• Questions