Download presentation
Presentation is loading. Please wait.
1
Data Structures and Algorithms
Disjoint Sets Data Structures and Algorithms CSE 373 SP 18 - Kasey Champion
2
Warm Up Finding a MST using Kruskal’s algorithm 8 7 b c d 9 4 2 4 14 e
11 i 6 a 10 7 8 h g f 1 2 CSE 373 SP 18 - Kasey Champion
3
Warm Up Finding a MST using Kruskal’s algorithm 8 7 b c d 9 4 2 4 14 e
11 i 6 a 10 7 8 h g f 1 2 CSE 373 SP 18 - Kasey Champion
4
New ADT Set ADT state behavior Disjoint-Set ADT state behavior B A D C
create(x) - creates a new set with a single member, x Count of Elements state behavior Set of elements Elements must be unique! No required order add(x) - adds x into set if it is unique, otherwise add is ignored remove(x) – removes x from set size() – returns current number of elements in set Disjoint-Set ADT makeSet(x) – creates a new set within the disjoint set where the only member is x. Picks representative for set Count of Sets state behavior Set of Sets Disjoint: Elements must be unique across sets No required order Each set has representative findSet(x) – looks up the set containing element x, returns representative of that set union(x, y) – looks up set containing x and set containing y, combines two sets into one. Picks new representative for resulting set B A C G H D F D A C B CSE 373 SP 18 - Kasey Champion
5
Example new() makeSet(a) makeSet(b) makeSet(c) makeSet(d) makeSet(e)
findSet(a) findSet(d) union(a, c) b Rep: 1 a Rep: 0 c Rep: 2 e Rep: 4 d Rep: 3 CSE 373 WI 18 – Michael Lee
6
Example new() makeSet(a) makeSet(b) makeSet(c) makeSet(d) makeSet(e)
findSet(a) findSet(d) union(a, c) union(b, d) b Rep: 1 a Rep: 0 c e Rep: 4 d Rep: 3 CSE 373 WI 18 – Michael Lee
7
Example new() makeSet(a) makeSet(b) makeSet(c) makeSet(d) makeSet(e)
findSet(a) findSet(d) union(a, c) union(b, d) b Rep: 1 a Rep: 0 d c e Rep: 4 findSet(a) == findSet(c) findSet(a) == findSet(d) CSE 373 WI 18 – Michael Lee
8
TreeDisjointSet<E>
Implementation TreeSet<E> TreeSet(x) state behavior SetNode overallRoot add(x) remove(x, y) getRep()-returns data of overallRoot Disjoint-Set ADT makeSet(x) – creates a new set within the disjoint set where the only member is x. Picks representative for set Count of Sets state behavior Set of Sets Disjoint: Elements must be unique across sets No required order Each set has representative findSet(x) – looks up the set containing element x, returns representative of that set union(x, y) – looks up set containing x and set containing y, combines two sets into one. Picks new representative for resulting set TreeDisjointSet<E> makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory SetNode<E> SetNode(x) state behavior E data addChild(x) removeChild(x, y) Collection<SetNode> children CSE 373 SP 18 - Kasey Champion
9
TreeDisjointSet<E>
makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement makeSet(x) forest makeSet(0) makeSet(1) makeSet(2) makeSet(3) makeSet(4) makeSet(5) 1 2 3 4 5 1 2 3 4 5 Worst case runtime? O(1) CSE 373 SP 18 - Kasey Champion
10
TreeDisjointSet<E>
makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement union(x, y) forest union(3, 5) 1 2 3 4 5 1 2 3 4 5 -> CSE 373 SP 18 - Kasey Champion Runtime Call findSet on both x and y Figure out where to add y into x Worst case runtime? O(n)
11
TreeDisjointSet<E>
makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement union(x, y) forest union(3, 5) union(2, 1) 1 2 3 4 5 1 2 3 4 5 -> CSE 373 SP 18 - Kasey Champion Runtime Call findSet on both x and y Figure out where to add y into x Worst case runtime? O(n)
12
TreeDisjointSet<E>
makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement union(x, y) forest union(3, 5) union(2, 1) union(2, 5) 2 3 4 1 5 1 2 3 4 5 -> CSE 373 SP 18 - Kasey Champion Runtime Call findSet on both x and y Figure out where to add y into x Worst case runtime? O(n)
13
TreeDisjointSet<E>
makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement union(x, y) forest union(3, 5) union(2, 1) union(2, 5) 2 4 1 3 5 1 2 3 4 5 CSE 373 SP 18 - Kasey Champion Runtime Call findSet on both x and y Figure out where to add y into x Worst case runtime? O(n)
14
TreeDisjointSet<E>
makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement findSet(x) forest findSet(0) findSet(3) findSet(5) 2 4 1 3 5 1 2 3 4 5 Worst case runtime? O(n) Worst case runtime of union? CSE 373 SP 18 - Kasey Champion
15
Improving union Problem: Trees can be unbalanced
Solution: Union-by-rank! let rank(x) be a number representing the upper bound of the height of x so rank(x) >= height(x) Keep track of rank of all trees When unioning make the tree with larger rank the root If it’s a tie, pick one randomly and increase rank by one rank = 0 rank = 0 rank = 1 rank = 0 rank = 2 2 4 4 1 3 5 CSE 373 SP 18 - Kasey Champion
16
Practice Given the following disjoint-set what would be the result of the following calls on union if we add the “union-by-rank” optimization. Draw the forest at each stage with corresponding ranks for each tree. rank = 2 rank = 0 rank = 2 rank = 1 6 2 8 7 4 3 9 10 11 13 1 5 12 union(2, 13) union(4, 12) union(2, 8) CSE 373 SP 18 - Kasey Champion
17
Practice Given the following disjoint-set what would be the result of the following calls on union if we add the “union-by-rank” optimization. Draw the forest at each stage with corresponding ranks for each tree. rank = 3 8 6 9 10 11 7 4 3 12 13 2 1 5 union(2, 13) union(12, 4) union(2, 8) Does this improve the worst case runtimes? findSet is more likely to be O(log(n)) than O(n) CSE 373 SP 18 - Kasey Champion
18
Improving findSet() Problem: Every time we call findSet() you must traverse all the levels of the tree to find representative Solution: Path Compression Collapse tree into fewer levels by updating parent pointer of each node you visit Whenever you call findSet() update each node you touch’s parent pointer to point directly to overallRoot rank = 3 rank = 2 findSet(5) findSet(4) 8 8 6 9 10 11 7 5 4 6 9 10 11 7 Does this improve the worst case runtimes? findSet is more likely to be O(1) than O(log(n)) 4 3 12 13 2 3 12 13 2 5 CSE 373 SP 18 - Kasey Champion
19
Example Using the union-by-rank and path-compression optimized implementations of disjoint-sets draw the resulting forest caused by these calls: makeSet(a) makeSet(b) makeSet(c) makeSet(d) makeSet(e) makeSet(f) makeSet(h) union(c, e) union(d, e) union(a, c) union(g, h) union(b, f) union(g, f) union(b, c) rank = 2 e a b c d f h g CSE 373 SP 18 - Kasey Champion
20
Array Representation Like heaps, pretend the tree exists, but use an Array for actual implementation CSE 373 SP 18 - Kasey Champion
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.