Download presentation
Presentation is loading. Please wait.
Published byBennett Byrd Modified over 9 years ago
1
COMP 261 Lecture 12 Disjoint Sets
2
Menu Kruskal's minimum spanning tree algorithm Disjoint-set data structure and Union-Find algorithm Administrivia –Marking. PLEASE DON’T MISS YOUR MARKING SESSION!
3
Graph Algorithms Minimum Spanning Tree: Kruskal's Algorithm A B C E D I H G J F 5 1 3 7 4 9 6 2 3 2323 7 1818 4 1717 1010 8 2525 1010 1414 6
4
Refining Kruskal's algorithm Given: a graph with N nodes and E weighted edges forest ← a set of N sets of nodes, each containing one node of the graph edges ← a priority queue of all the edges: 〈n 1, n 2, length 〉 spanningTree ← an empty set of edges Repeat until forest contains only one tree or edges is empty: 〈 n 1, n 2, length 〉 ← dequeue(edges) If n 1 and n 2 are in different sets in forest then merge the two sets in forest Add edge to the spanningTree return spanningTree Implementing forest : –set of sets with two operations: findSet(n 1 ) =?= findSet(n 2 ) = "find" the set that n is in merge(s 1, s 2 )= replace s 1, s 2 by their "union" What's the cost? priority: short edges first
5
Implementing sets of sets 1: forest = set of sets of nodes: –findSet(n 1 ) iterate through all sets, calling s.contains(n 1 ) –merge (s 1, s 2 ) add each element of s 1 to s 2 and remove s 1 –cost? 2: forest = mark each node with ID of its set –findSet(n 1 ): look up n 1.setID –merge(s 1, s 2 ) iterate through all nodes, changing IDs of nodes in s 1 –cost?
6
Union–Find structure 3: forest: set of inverted trees of nodes: Each set represented by a linked tree with links pointing towards the root (= "shared linked list structure") The nodes in these trees are the nodes of the graph! A G X Q N W R S C E T D R Y H J U Z K
7
Union–Find structure MakeSet(x): x.parent ← x add x to set Find(x) if x.parent = x return x else root ← Find(x.parent) return root Union(x, y): xroot ← Find(x) yroot ← Find(y) If xroot = yroot exit else xroot.parent ← yroot remove xroot from set. What's the cost? Recurses up the tree
8
Union–Find structure find(Q), union(E, Z), union(H, K), union(Y, G) Problem: the trees can get unreasonably long Solutions: shorten the trees; add shorter trees to longer A G X Q N W R S C E T D R Y H J U Z K
9
Union–Find: Better MakeSet(x): x.parent ← x add x to set x.rank ← 0 Find(x) if x.parent = x return x else x.parent ← Find(x.parent) return x.parent Union(x,y) xroot ← Find(x) yroot ← Find(y) if xroot = yroot then exit if xroot.rank < yroot.rank xroot.parent ← yroot remove xroot from set else yroot.parent ← xroot remove yroot from set if xroot.rank = yroot.rank xroot.rank ++ keeping track of size lets us add the smaller tree to the larger tree Amortised cost < 5! Modify tree to keep paths short
10
Union–Find structure find(Q), union(E, Z), union(H, K), union(Y, G) A4A4 G0G0 X3X3 Q1Q1 N0N0 W0W0 R1R1 S0S0 C3C3 E2E2 T0T0 D2D2 R0R0 Y1Y1 H0H0 J2J2 U1U1 Z0Z0 K0K0
11
Applications Union-Find is very efficient for a collection of sets if –Have an explicit collection of possible members –All sets are disjoint. –Only asking for same set membership and merging two sets. Inefficient for –enumerating the elements of a set –removing an element from a set Doesn't work –if the sets could share elements.
12
Exercise: A B C E D I H G J F 1 2 3 4 5 6 1919 7 1818 1717 1616 1515 1414 1313 1212 1 1010 9 8 2020 ABCEDIHGJF
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.