CS 225 Data Structures No Novem ember er 4 Di Disjoint Sets G - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures No Novem ember er 4 Di Disjoint Sets G - - PowerPoint PPT Presentation

CS 225 Data Structures No Novem ember er 4 Di Disjoint Sets G G Carl Evans Heap Heap Sort 4 1. 5 6 2. 15 9 7 20 3. 16 25 11 14 12 4 5 6 15 9 7 20 16 25 14 12 11 Running Time? Why do we care about another


slide-1
SLIDE 1

CS 225

Data Structures

No Novem ember er 4 – Di Disjoint Sets

G G Carl Evans

slide-2
SLIDE 2

Heap Heap Sort

Running Time? Why do we care about another sort?

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

1. 2. 3.

slide-3
SLIDE 3

A( A(no nothe her) ) throwback k to CS 173…

Let R be an equivalence relation on us where (s, t) ∈ R if s and t have the same favorite among: { ___, ___, ____, ___, ____, }

slide-4
SLIDE 4

Di Disjoint S Sets

2 5 9 7 0 1 4 8 3 6

slide-5
SLIDE 5

Di Disjoint S Sets

2 5 9 7 0 1 4 8 3 6

Operation: find(4)

slide-6
SLIDE 6

Di Disjoint S Sets

2 5 9 7 0 1 4 8 3 6

Operation: find(4) == find(8)

slide-7
SLIDE 7

Di Disjoint S Sets

2 5 9 7 0 1 4 8 3 6

Operation: if ( find(2) != find(7) ) { union( find(2), find(7) ); }

slide-8
SLIDE 8

Di Disjoint S Sets

2 5 9 7 0 1 4 8 3 6

Key Ideas:

  • Each element exists in exactly one set.
  • Every set is an equitant representation.
  • Mathematically: 4 ∈ [0]R à 8 ∈ [0]R
  • Programmatically: find(4) == find(8)
slide-9
SLIDE 9

Di Disjoint S Sets A ADT

  • Maintain a collection S = {s0, s1, … sk}
  • Each set has a representative member.
  • API: void makeSet(const T & t);

void union(const T & k1, const T & k2); T & find(const T & k);

slide-10
SLIDE 10

Im Implem plemen entatio tion n #1

0 1 4 2 7 3 5 6

1 2 3 4 5 6 7

Find(k): Union(k1, k2):

slide-11
SLIDE 11

Im Implem plemen entatio tion n #2

  • We will continue to use an array where the index is the

key

  • The value of the array is:
  • -1, if we have found the representative element
  • The index of the parent, if we haven’t found the rep. element
  • We will call theses UpTrees:

1 2 3

  • 1
  • 1
  • 1
  • 1

1 2 3

slide-12
SLIDE 12

Up UpTrees ees

1 2 3

1 2 3

1 2 3 1 2 3 1 2 3

slide-13
SLIDE 13

Di Disjoint S Sets

2 5 9 7 0 1 4 8 3 6

1 2 3 4 5 6 7 8 5 6

  • 1
  • 1
  • 1
  • 1

4 8 9 4 5

1 2 3 4 5 6 7 8 9

slide-14
SLIDE 14

Di Disjoint S Sets F Find

Running time? What is the ideal UpTree?

int DisjointSets::find() { if ( s[i] < 0 ) { return i; } else { return _find( s[i] ); } } 1 2 3 4

slide-15
SLIDE 15

Di Disjoint S Sets U Union

void DisjointSets::union(int r1, int r2) { } 1 2 3 4

1 4 8

slide-16
SLIDE 16

Di Disjoint S Sets – Un Unio ion

1 2 3 4 5 6 7 8 9 10 11

1 2 3 4 5 6 7 6 6 8

  • 1

10 7

  • 1

6 8 9 7 7 10 11 4 5

slide-17
SLIDE 17

Di Disjoint S Sets – Sma Smart rt U Union

  • n

1 2 3 4 5 6 7 8 9 10 11

1 2 3 4 5 6 7 6 6 8 10 7 6 8 9 7 7 10 11 4 5

Union by height

Idea: Keep the height of the tree as small as possible.

slide-18
SLIDE 18

Di Disjoint S Sets – Sma Smart rt U Union

  • n

1 2 3 4 5 6 7 8 9 10 11

1 2 3 4 5 6 7 6 6 8 10 7 6 8 9 7 7 10 11 4 5 1 2 3 4 5 6 7 6 6 8 10 7 6 8 9 7 7 10 11 4 5

Union by height Union by size

Idea: Keep the height of the tree as small as possible. Idea: Minimize the number of nodes that increase in height

Both guarantee the height of the tree is: _____________.

slide-19
SLIDE 19

Di Disjoint S Sets F Find

int DisjointSets::find(int i) { if ( s[i] < 0 ) { return i; } else { return _find( s[i] ); } } 1 2 3 4 void DisjointSets::unionBySize(int root1, int root2) { int newSize = arr_[root1] + arr_[root2]; // If arr_[root1] is less than (more negative), it is the larger set; // we union the smaller set, root2, with root1. if ( arr_[root1] < arr_[root2] ) { arr_[root2] = root1; arr_[root1] = newSize; } // Otherwise, do the opposite: else { arr_[root1] = root2; arr_[root2] = newSize; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

slide-20
SLIDE 20

Pa Path Compression

1 2 3 6 7 8 9 4 5 10 11

slide-21
SLIDE 21

Di Disjoint S Sets A Anal alysis

The iterated log function: The number of times you can take a log of a number. log*(n) = 0 , n ≤ 1 1 + log*(log(n)) , n > 1 What is lg*(265536)?

slide-22
SLIDE 22

Di Disjoint S Sets A Anal alysis

In an Disjoint Sets implemented with smart unions and path compression on find: Any sequence of m union and find operations result in the worse case running time of O( ____________ ), where n is the number of items in the Disjoint Sets.