disjoint sets
play

Disjoint sets March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey - PowerPoint PPT Presentation

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


  1. Disjoint sets March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

  2. 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

  3. 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

  4. 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

  5. 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

  6. "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

  7. 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

  8. 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