heaps
play

Heaps Carola Wenk 9/8/17 1 CMPS 2200 Introduction to Algorithms - PowerPoint PPT Presentation

CMPS 2200 Fall 2017 Heaps Carola Wenk 9/8/17 1 CMPS 2200 Introduction to Algorithms Priority Queue A priority queue is a data structure which supports operations Insert Find_max Extract_max Several possible implementations:


  1. CMPS 2200 – Fall 2017 Heaps Carola Wenk 9/8/17 1 CMPS 2200 Introduction to Algorithms

  2. Priority Queue A priority queue is a data structure which supports operations • Insert • Find_max • Extract_max Several possible implementations: Insert Find_max Extract_max Unsorted array: O (1) O ( n ) O ( n ) Sorted array: O ( n ) O (1) O ( n ) or O(1) Balanced BST: O (log n ) O (log n ) O (log n ) Heaps: O (log n ) O (1) O (log n ) Fibonacci Heaps: O (1) O (1) O (log n ) amortized amortized amortized 9/8/17 2 CMPS 2200 Introduction to Algorithms

  3. Heaps 1) • A max-heap is an almost complete binary tree (flushed left on the last level). Each node stores a key. The tree fulfills the max-heap property : 2) x For every node x holds: • y  x , for all y in any subtree of x  x  x 20 20 7 10 10 8 1 5 6 2 4 9/8/17 3 CMPS 2200 Introduction to Algorithms

  4. Heap Storage • Because a max-heap is an almost complete binary tree it can be stored in an array level by level: 0 20 20 1 2 7 10 10 0 1 2 3 4 5 6 7 8 4 6 3 5 20 7 10 5 6 8 1 2 4 5 6 8 1 7 8 2 4 • Implement child/parent “pointers”: ��� ��� � � • Find_max: O(1) time 9/8/17 4 CMPS 2200 Introduction to Algorithms

  5. Heap Height • Lemma: A complete binary tree of height has ��� nodes. Proof: Induction on . � � 0 � � 1 � � 2 � � 3 � � 1 � � 3 � � 7 � � 15 • Lemma: An almost complete binary tree with nodes has height . Proof idea: � ��� . 9/8/17 CMPS 2200 Introduction to Algorithms

  6. : O( h )=O(log n ) Insert, Heapify_up 0 20 20 n=9 Insert(A,n,key){ 1 2 n=10 n++; 7 10 10 A[n-1]=key; 4 6 3 5 8 1 5 6 Heapify_up(A,n-1); } 7 8 9 =i 9 2 4 Insert 9 0 1 2 3 4 5 6 7 8 9 9 20 7 10 5 6 8 1 2 4 A: Heapify_up(A,i){ while(i>0 && A[parent(i)]<A[i]){ swap(A[parent(i)],A[i]); 0 i=parent(i); 0 20 20 20 20 } 1 i= 1 2 2 } 9 10 10 7 10 10 6 4 3 5 4 6 3 =i 5 8 1 5 7 8 1 5 9 7 8 9 7 8 9 6 2 4 9/8/17 6 6 2 4 CMPS 2200 Introduction to Algorithms

  7. Extract_max, Heapify_down 0 6 20 20 n=10 Extract_max(A,n,key){ 1 2 max=A[0]; 9 10 10 n=9 4 6 5 3 A[0]=A[n-1]; 8 1 5 7 n--; 7 8 9 Heapify_down(A,n,0); 6 Extract_max 2 4 return max; } 0 1 2 3 4 5 6 7 8 9 6 20 9 10 5 7 8 6 1 2 4 A: Heapify_down(A,n,i){ while(left(i)<n){//left child exists maxchild=left(i); if(right(i)<n && A[right(i)]>A[left(i)] 0 maxchild =right(i); 10 10 if(A[maxchild]<=A[i]) break; // done 1 2 6 swap(A[i], A[maxchild]); 9 10 10 4 6 3 5 i=maxchild; 8 1 5 7 } 8 } 2 4 9/8/17 7 CMPS 2200 Introduction to Algorithms

  8. : O(log n ) Extract_max, Heapify_down 0 6 20 20 n=10 Extract_max(A,n,key){ 1 2 max=A[0]; 9 10 10 n=9 4 6 5 3 A[0]=A[n-1]; 8 1 5 7 n--; 7 8 9 Heapify_down(A,n,0); 6 Extract_max 2 4 return max; } 0 1 2 3 4 5 6 7 8 9 6 20 9 10 5 7 8 6 1 2 4 A: Heapify_down(A,n,i){ while(left(i)<n){//left child exists maxchild=left(i); if(right(i)<n && A[right(i)]>A[left(i)] 0 maxchild =right(i); 10 10 if(A[maxchild]<=A[i]) break; // done 1 2 swap(A[i], A[maxchild]); 9 8 4 6 3 5 i=maxchild; 6 3 1 5 7 } 8 } 2 4 9/8/17 8 CMPS 2200 Introduction to Algorithms

  9. : O( n log n ) Heapsort • Insert all numbers in a max-heap • Repeatedly extract max Heapsort(A,n){ O( n log n ) Build_heap(A); //Insert all elements for(i=n-1; i>=1; i--){ swap(A[0],A[i]); // moves max to A[n] O( n log n ) n--; Heapify_down(A,n,0); } } 9/8/17 9 CMPS 2200 Introduction to Algorithms

Recommend


More recommend