Disjoint sets March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
A data structure for disjoint sets β’ Maintains a collection π = π‘ 0 , π‘ 1 , β¦ , π‘ π of disjoint sets β’ Each set has a representative member β’ Required operations: β void MakeSet(int k); β void Union(int x, int y); β int Find(int x); β’ Let's start with an array-based structure 3 5 6 0 1 4 2 7 Cost of Find()? Cost of Union()? representative 0 0 2 3 0 3 3 2 index 0 1 2 3 4 5 6 7 March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2
A better structure for disjoint sets "Uptrees" β’ A tree where a node points to its parent β still array-based, but representative is the root of the tree β’ if array value is β1 , then the index is a root node β’ otherwise, the array value is the index's parent β’ π¦ and π§ are in the same tree β π¦ and π§ are in the same set 1 1 2 2 0 0 0 2 3 3 1 3 β 1 β 1 β 1 β 1 β 1 β 1 β 1 parent parent parent 1 2 1 1 2 index index index 0 1 2 3 0 1 2 3 0 1 2 3 March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3
Tree-based disjoint sets int DisjointSets::Find(int x) { Running time? if (parent[x] < 0) return x; else return Find(parent[x]); } β It depends on the height of the trees in the disjoint sets β’ average: π log π , worst: π π , best: π 1 March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4
Tree-based disjoint sets β’ Union: given arbitrary indices π¦ and π§ , join their trees β naΓ―vely: set root of π¦ to π§ , or vice-versa β slightly better: set root of π¦ to root of π§ , or vice-versa 3 Union(0, 1); 2 Union(1, 2); 0 2 1 3 1 Union(0, 3); 0 Can still end up with bad trees! March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5
"Smart" union 7 4 10 8 9 6 5 0 1 2 3 11 Keeps overall tree 6 6 6 8 10 7 4 7 7 4 5 Union by height parent height as small as possible index 0 1 2 3 4 5 6 7 8 9 10 11 Increases distance 6 6 6 8 7 10 7 7 7 4 5 Union by size parent from root for as index 0 1 2 3 4 5 6 7 8 9 10 11 few nodes as possible β both schemes guarantee that the height of the tree is π log π β’ but we will ignore the proof for now March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6
Path compression β’ During a Find operation, we follow a path up the tree through a sequence of nodes β i.e. we look up a number entries in an array, where each lookup is π 1 β’ Why don't we add an additional π 1 operation for each entry we process? β Set the parent of each node along the path, to the root found at the end of the path 7 7 4 4 5 Find(5); 10 10 11 5 Find(10); 11 March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7
Readings for this lesson β’ Wikipedia β https://en.wikipedia.org/wiki/Disjoint-set_data_structure β’ Next class: β Carrano & Henry: Chapter 20.1 β 20.2 (Graph terminology and ADT) March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8
Recommend
More recommend