CS 225 Data Structures November 5 Heap Analysis and Dis isjoint - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures November 5 Heap Analysis and Dis isjoint - - PowerPoint PPT Presentation

CS 225 Data Structures November 5 Heap Analysis and Dis isjoint Sets Wad ade Fag agen-Ulm lmschneid ider buildHeap B 1. Sort the array its a heap! U I L D H E 2. template <class T> 1 2 void


slide-1
SLIDE 1

CS 225

Data Structures

November 5 – Heap Analysis and Dis isjoint Sets

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2

buildHeap

U L D P B I H E W A O N

B U I L D H E A P N O W

  • 1. Sort the array – it’s a heap!

2. 3.

template <class T> void Heap<T>::buildHeap() { for (unsigned i = 2; i <= size_; i++) { heapifyUp(i); } } 1 2 3 4 5 6 template <class T> void Heap<T>::buildHeap() { for (unsigned i = parent(size); i > 0; i--) { heapifyDown(i); } } 1 2 3 4 5 6

slide-3
SLIDE 3

U L D P B I H E W A O N

B U I L D H E A P N O W

O(h) = 1 O(h) = 1 O(h) = 1

slide-4
SLIDE 4

U A D P B I H E W L O N

B U I D H E P N O W

O(h) = 2 O(h) = 2

A L

slide-5
SLIDE 5

A L D P B E H I W U O N

B D H P N O W

O(h) = 3

A E L I U

slide-6
SLIDE 6

B L D P A E H I W U O N

E L D H I U P N O W A B E L D H I U P N O W A B

slide-7
SLIDE 7

Proving buildHeap Running Time

Theorem: The running time of buildHeap on array of size n is: _________. Strategy:

slide-8
SLIDE 8

Proving buildHeap Running Time

S(h): Sum of the heights of all nodes in a complete tree of height h. S(0) = S(1) = S(h) =

U L D P B I H E W A O N

slide-9
SLIDE 9

Proving buildHeap Running Time

Proof the recurrence: Base Case: General Case:

slide-10
SLIDE 10

Proving buildHeap Running Time

From S(h) to RunningTime(n): S(h): Since h ≤ lg(n): RunningTime(n) ≤

slide-11
SLIDE 11

Mattox Monday

slide-12
SLIDE 12

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

A( A(nother) throwback 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-14
SLIDE 14

Disjoint Sets

2 5 9 7 0 1 4 8 3 6

slide-15
SLIDE 15

Disjoint Sets

2 5 9 7 0 1 4 8 3 6

Operation: find(4)

slide-16
SLIDE 16

Disjoint Sets

2 5 9 7 0 1 4 8 3 6

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

slide-17
SLIDE 17

Disjoint Sets

2 5 9 7 0 1 4 8 3 6

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

slide-18
SLIDE 18

Disjoint 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-19
SLIDE 19

Disjoint Sets 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-20
SLIDE 20

Im Implementation #1

0 1 4 2 7 3 5 6

1 2 3 4 5 6 7 2 3 3 3 2

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

slide-21
SLIDE 21

Im Implementation #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-22
SLIDE 22

UpTrees

1 2 3

  • 1
  • 1
  • 1
  • 1

1 2 3

1 2 3 1 2 3 1 2 3

slide-23
SLIDE 23

Disjoint 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-24
SLIDE 24

Disjoint Sets 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-25
SLIDE 25

Disjoint Sets Union

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

1 4 8

slide-26
SLIDE 26

Disjoint Sets – Union

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

Disjoint Sets – Smart Union

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

Disjoint Sets – Smart Union

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

Disjoint Sets 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-30
SLIDE 30

Path Compression

1 2 3 6 7 8 9 4 5 10 11

slide-31
SLIDE 31

Disjoint Sets Analysis

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

Disjoint Sets Analysis

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.