cs 225
play

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


  1. CS 225 Data Structures November 5 – Heap Analysis and Dis isjoint Sets Wad ade Fag agen-Ulm lmschneid ider

  2. buildHeap B 1. Sort the array – it’s a heap! U I L D H E 2. template <class T> 1 2 void Heap<T>::buildHeap() { 3 for (unsigned i = 2; i <= size_; i++) { A P W N O 4 heapifyUp(i); 5 } 6 } template <class T> 3. 1 2 void Heap<T>::buildHeap() { 3 for (unsigned i = parent(size); i > 0; i--) { 4 heapifyDown(i); 5 } 6 } B U I L D H E A P N O W

  3. B U I L D H E A P N O W B O(h) = 1 O(h) = 1 O(h) = 1 U I L D H E A P N O W

  4. B U I A D H E L P N O W O(h) = 2 O(h) = 2 B U I A D H E L P N O W

  5. B A E L D H I U P N O W O(h) = 3 B A E L D H I U P N O W

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

  7. Proving buildHeap Running Time Theorem: The running time of buildHeap on array of size n is: _________. Strategy: - - -

  8. Proving buildHeap Running Time S(h) : Sum of the heights of all nodes in a complete tree of height h . B S(0) = U I L D H E S(1) = A P W N O S(h) =

  9. Proving buildHeap Running Time Proof the recurrence: Base Case: General Case:

  10. Proving buildHeap Running Time From S(h) to RunningTime(n): S(h): Since h ≤ lg(n): RunningTime (n) ≤

  11. Mattox Monday

  12. Heap Sort 4 1. 5 6 2. 15 9 7 20 3. 16 25 14 12 11 4 5 6 15 9 7 20 16 25 14 12 11 Running Time? Why do we care about another sort?

  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: { ___, ___, ____, ___, ____, }

  14. Disjoint Sets 2 5 9 7 0 1 4 8 3 6

  15. Disjoint Sets 2 5 9 7 0 1 4 8 3 6 Operation: find(4)

  16. Disjoint Sets 2 5 9 7 0 1 4 8 3 6 Operation: find(4) == find(8)

  17. Disjoint Sets 2 5 9 7 0 1 4 8 3 6 Operation: if ( find(2) != find(7) ) { union( find(2), find(7) ); }

  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)

  19. Disjoint Sets ADT • Maintain a collection S = {s 0 , s 1 , … s k } • 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);

  20. Im Implementation #1 0 1 4 2 7 3 5 6 0 1 2 3 4 5 6 7 0 0 2 3 0 3 3 2 Find(k): Union(k1, k2):

  21. Implementation #2 Im • 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 : 0 1 2 3 0 1 2 3 -1 -1 -1 -1

  22. UpTrees 0 1 2 3 0 1 2 3 0 1 2 3 -1 -1 -1 -1 0 1 2 3 0 1 2 3

  23. Disjoint Sets 2 5 9 7 0 1 4 8 3 6 4 3 7 5 6 0 8 9 2 1 0 1 2 3 4 5 6 7 8 9 4 8 5 6 -1 -1 -1 -1 4 5

  24. Disjoint Sets Find int DisjointSets::find() { 1 2 if ( s[i] < 0 ) { return i; } 3 else { return _find( s[i] ); } 4 } Running time? What is the ideal UpTree?

  25. Disjoint Sets Union void DisjointSets::union(int r1, int r2) { 1 0 4 2 3 4 } 8 1

  26. Disjoint Sets – Union 4 7 8 6 9 10 3 0 1 2 5 11 0 1 2 3 4 5 6 7 8 9 10 11 6 6 6 8 -1 10 7 -1 7 7 4 5

  27. Disjoint Sets – Smart Union 4 7 8 6 9 10 3 0 1 2 5 11 Idea : Keep the height of Union by height 0 1 2 3 4 5 6 7 8 9 10 11 the tree as small as 6 6 6 8 10 7 7 7 4 5 possible.

  28. Disjoint Sets – Smart Union 4 7 8 6 9 10 3 0 1 2 5 11 Idea : Keep the height of Union by height 0 1 2 3 4 5 6 7 8 9 10 11 the tree as small as 6 6 6 8 10 7 7 7 4 5 possible. Idea : Minimize the 0 1 2 3 4 5 6 7 8 9 10 11 Union by size number of nodes that 6 6 6 8 10 7 7 7 4 5 increase in height Both guarantee the height of the tree is: _____________.

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

  30. Path Compression 10 9 11 1 7 8 2 4 3 5 6

  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*(2 65536 ) ?

  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.

Recommend


More recommend