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

β–Ά
disjoint sets
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Disjoint sets

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

slide-2
SLIDE 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

March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

0 1 4 2 7 3 5 6

index representative 1 2 3 4 5 6 7 2 3 3 3 2 Cost of Find()? Cost of Union()?

slide-3
SLIDE 3

A better structure for disjoint sets

  • 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

March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

"Uptrees"

–1 –1 –1 –1 index parent 1 2 3 1 2 3 1 –1 –1 2 index parent 1 2 3 1 2 3 1 –1 1 2 index parent 1 2 3 1 2 3

slide-4
SLIDE 4

Tree-based disjoint sets

– 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

int DisjointSets::Find(int x) { if (parent[x] < 0) return x; else return Find(parent[x]); } Running time?

slide-5
SLIDE 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

March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

1 2 3 Union(0, 1); Union(1, 2); Union(0, 3); 1 2 3 Can still end up with bad trees!

slide-6
SLIDE 6

"Smart" union

– 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 6 9 2 8 3 4 1 11 5 10 6 6 6 8 10 7 index parent 1 2 3 4 5 6 7 7 7 4 5 8 9 10 11 6 6 6 8 10 7 index parent 1 2 3 4 5 6 7 7 7 4 5 8 9 10 11 Union by height Keeps overall tree height as small as possible Union by size Increases distance from root for as few nodes as possible 4 7

slide-7
SLIDE 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

  • f the path

March 20, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7

4 11 5 10 7 Find(5); 4 11 5 10 7 Find(10);

slide-8
SLIDE 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