360 likes | 526 Views
CSCI 3160 Design and Analysis of Algorithms Tutorial 3. Yang LIU. Outline. Union-find data structure Minimum spanning tree (MST ) Kruskal’s algorithm. Union-find. A data structure for disjoint sets n = number of members, forming disjoint groups
E N D
CSCI 3160 Design and Analysis of AlgorithmsTutorial 3 Yang LIU
Outline Union-find data structure Minimum spanning tree (MST) Kruskal’s algorithm
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: • Union: merge two groups • 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
Union-find • Idea: use a forest (= collection of trees) • a group → a tree • leader → root of the tree • Example: • Group 1: Alice, Bob • Group 2: Carol, Dave, Eve • Store the height of each tree at the root A 1 D 1 B C E
Union-find • Find: return the root of the group • Union: make the leader of one group the boss of the other • Our heuristic: make the root of the shorter tree point to the root of the other tree • If both trees are of the same height h, then the resulting tree has height h+1
Union-find in action • Initialize • Everyone is his/her own boss A C E B D 0 0 0 0 0
Union-find in action • Union(A, B) • Find(A) returns A and Find(B) returns B • Make Alice the boss of Bob • Increase the height of the tree A C E B D 1 0 0 0 0
Union-find in action • Union(C, D) • Find(C) returns C and Find(D) returns D • Make Carol the boss of Dave • Increase the height of the tree A C E B D 1 1 0 0 0
Union-find in action • Union(B, D) • Find(B) returns A and Find(D) returns C • Make Alice the boss of Carol • Increase the height of the tree A C E B D 2 1 0 0 0
Your turn • Find(C)? A C E B D 2 1 0 0 0
Your turn • Find(C)? • Find(C) returns A A C E B D 2 1 0 0 0
Your turn • Union(B, E)? A C E B D 2 1 0 0 0
Your turn • Union(B, E)? • Find(B) returns A and Find(E) returns E • Make Alice the boss of Eve • No increase in the tree height (Why?) A C E B D 2 1 0 0 0
Final result • Does this look more like a tree? 2 A C 1 E 0 B 0 D 0
Analysis • Height of each tree = O(log n) • Cost of Find(): O(log n) • Cost of Union(): O(log n) • Dominated by the cost of Find()
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 edges 1 B A 4 5 3 6 C D E 2 7 Total weight = 1 + 2 + 3 + 6 = 12
Kruskal’s algorithm • In words: • Sort the edges in ascending order of weights • Initialize: set T = Ø • 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 cycles • Adding an edge corresponds to putting the two endpoints into the same group • Connecting two vertices in the same group causes a cycle
Dry run • 1: Sort the edges 1 B A 4 5 3 6 C D E 2 7
Dry run • 2: Set T = Ø B A C D E 1 B A 4 5 3 6 C D E 2 7
Dry run • 3: Consider the first edge B A C D E 1 B A 4 5 3 6 C D E 2 7
Dry run • 4: Consider the second edge B A C D E 1 B A 4 5 3 6 C D E 2 7
Dry run • 5: Consider the third edge B A C D E 1 B A 4 5 3 6 C D E 2 7
Dry run • 6: B A C D E 1 B A 4 5 3 6 C D E 2 7
Dry run • 7: B A C D E 1 B A 4 5 3 6 C D E 2 7
Dry run • 8: B A C D E 1 B A 4 5 3 6 C D E 2 7
Dry run • 9: We have find an MST! B A C D E 1 B A 4 5 3 6 C D E 2 7
Analysis • Correctness • In each step, we keep a set of edges T that is a subset of an MST • 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|) • Sorting alone takes time O(|E|log|V|) (Note that |E| = O(|V|2)) • Cycle checking takes O(log|V|) operations (Find()) • Adding an edge also takes O(log|V|) operations (Union())
Dry run revisited • 2: Set T = Ø B A C D E A C E B D 0 0 0 0 0
Dry run revisited • 3: Consider the first edge B A C D E A C E B D 1 0 0 0 0
Dry run revisited • 4: Consider the second edge B A C D E A C E B D 1 1 0 0 0
Dry run revisited • 5: Consider the third edge B A C D E A C E B D 2 1 0 0 0
Dry run revisited • 6: B A C D E A C E B D 2 1 0 0 0
Dry run revisited • 7: B A C D E A C E B D 2 1 0 0 0
Dry run revisited • 8: B A C D E A C E B D 2 1 0 0 0
Dry run revisited • 9: We have find an MST! B A C D E A C E B D 2 1 0 0 0
End • Questions