1、Heapsort,Algorithm : Design & Analysis 7,In the last class,Mergesort Worst Case Analysis of Mergesort Lower Bound for Average Behavior Worst Case Average Behavior Lower Bound of Key Comparison Based Sorting Algorithm,Heapsort,Heap Structure and Patial Order Tree Property The Strategy of Heapsort Kee
2、p the Partial Order Tree Property after the maximal element is removed Constructing the Heap Complexity of Heapsort Accelerated Heapsort,Priority Queue ADT,“FIFO” in some special sense. The “first” means some kind of “priority”, such as value(largest or smallest) Priority queue is closely associated
3、 with the algorithm design strategy known as “greedy method”. Priority queue cant be implemented in such a way that all operations are in (1),Heap: a Data Structure,A binary tree T is a heap structure if: T is complete at least through depth h-1 All leaves are at depth h or h-1 All path to a leaf of
4、 depth h are to the left of all path to a leaf of depth h-1 Partial order tree property A tree T is a (maximizing) partial order tree if and only if the key at any node is greater than or equal to the keys at each of its children (if it has any).,A heap is: A heap structure, satisfying Partial order
5、 tree property,Heap: Examples,The maximal key is always with the root,9,7,5,1,4,3,6,24,20,6,50,5,12,3,18,21,30,Heapsort: the Strategy,heapSort(E,n)Construct H from E, the set of n elements to be sorted;for (i=n;i1;i-)curMax = getMax(H);deleteMax(H);Ei = curMax deleteMax(H)Copy the rightmost element
6、on the lowest level of H into K;Delete the rightmost element on the lowest level of H;fixHeap(H,K),FixHeap: Keeping the Partial Order Tree Property,Input: A nonempty binary tree H with a “vacant” root and its two subtrees in partial order. An element K to be inserted. Output: H with K inserted and s
7、atisfying the partial order tree property. Procedure: fixHeap(H,K)if (H is a leaf) insert K in root(H);elseSet largerSubHeap;if (K.keyroot(largerSubHeap).key) insert K in root(H)else insert root(largerSubHeap) in root(H);fixHeap(largerSubHeap, K);return,Recursion,One comparison: largerSubHeap is lef
8、t- or right-Subtree(H), the one with larger key at its root. Special case: rightSubtree is empty,“Vacant” moving down,fixHeap: an Example,24,20,6,50,K=6,K=6,5,12,3,18,21,30,24,20,5,12,3,18,21,30,24,20,5,12,3,18,21,30,vacant,24,20,5,12,3,18,21,30,Worst Case Analysis for fixHeap,2 comparisons at most
9、in one activation of the procedure The tree height decreases by one in the recursive call So, 2h comparisons are needed in the worst case, where h is the height of the tree Procesure: fixHeap(H,K)if (H is a leaf) insert K in root(H);elseSet largerSubHeap;if (K.keyroot(largerSubHeap).key) insert K in
10、 root(H)else insert root(largerSubHeap) in root(H);fixHeap(largerSubHeap, K);return,Recursion,One comparison: largerSubHeap is left- or right-Subtree(H), the one with larger key at its root. Special case: rightSubtree is empty,“Vacant” moving down,Heap Construction,Note: if left subtree and right su
11、btree both satisfy the partial order tree property, then fixHeap(H,root(H) gets the thing done. We begin from a Heap Structure H: void constructHeap(H)if (H is not a leaf)constructHeap(left subtree of H);constructHeap(right subtree of H);Element K=root(H);fixHeap(H,K)return,left,right,root,Post-orde
12、r Traversal,Specification Input: A heap structure H, not necessarily having the partial order tree property. Output: H with the same nodes rearranged to satisfy the partial order tree property. void constructHeap(H)if (H is not a leaf)constructHeap(left subtree of H);constructHeap(right subtree of H
13、);Element K=root(H);fixHeap(H,K)return,Correctness of constructHeap,H is a leaf: base case, satisfied trivially.,Preconditions hold respectively?,Postcondition of constructHeap satisfied?,The Heap Is Constructed in Linear Time!,The recursion equation:W(n)=W(n-r-1)+W(r)+2lg(n) A special case: H is a
14、complete binary tree: The size N=2d-1, and N/2nN, so W(n)W(N), Note: W(N)=2W(N-1)/2)+2lg(N) The Master Theorem applys, with b=c=2, and the critical exponent E=1, f(N)=2lg(N) Note: When 01, this limit is equal to zero So, 2lg(N)(N E-), case 1 satisfied, we have W(N)(N), so, W(n)(n),Number of nodes in
15、 right subheap,Cost of fixHeap,LHpitals Rule,For your reference: Master Theorem case 1: If f(n)O(nE-) for some positive , then T(n)(nE),Implementing Heap Using Array,9,5,1,4,7,3,6,12,5,24,20,21,6,30,18,3,50,50,24,30,20,21,18,3,6,5,12,Looking for the Children Quickly,Starting from 1, not zero, then t
16、he j th level has 2j-1 elements. and there are 2j-1-1 elements in the proceeding i-1 levels altogether.So, If Ei is the kth element at level j, then i=(2j-1-1)+k, and the index of its left child (if existing) is i+(2j-1-k)+2(k-1)+1=2i,12,5,24,20,21,6,30,18,3,50,50,24,30,20,21,18,3,6,5,12,For Ei: Lef
17、t subheap: E2i right subheap: E2i+1,In-space Implementation of Heapsort,E1: The largest key to be moved to En,EnK: removed to be inserted,Heap implemented as a array (initial),EheapsizeK: removed to be inserted,E1: The largest key in current heap, to be moved to Eheapsize,Sorted portion,Current heap
18、: processed by fixHeap,Heapsort: the Algorithm,Input: E, an unsorted array with n(0) elements, indexed from 1 Sorted E, in nondecreasing order Procedure:void heapSort(Element E, int n)int heapsizeconstructHeap(E,n,root)for (heapsize=n; heapsize2; heapsize-;)Element curMax=E1;Element K=Eheapsize;fixH
19、eap(E,heapsize-1,1,K);Eheapsize=curMax;return;,New Version,Worst Case Analysis of Heapsort,We have: It has been known that: Recall that:So, W(n) 2nlgn + (n), that is W(n)(nlogn),Coefficient doubles that of mergeSort approximately,heapSort: the Right Choice,For heapSort, W(n) (nlgn) Of course, A(n) (
20、nlgn) More good news: heapSort is an in-space algorithm Itll be more competitive if only the coefficient of the leading term can be decreased to 1,Number of Comparisons in fixHeap,Procesure: fixHeap(H,K)if (H is a leaf) insert K in root(H);elseSet largerSubHeap;if (K.keyroot(largerSubHeap).key) inse
21、rt K in root(H)else insert root(largerSubHeap) in root(H);fixHeap(largerSubHeap, K);return,2 comparisons are done in filtering down for one level.,A One-Comparison-per-Level Fixing,Bubble-Up Heap Algorithm: void bubbleUpHeap(Element E, int root, Element K, int vacant)if (vacant=root) Evacant=K;elsei
22、nt parent=vacant/2;if (K.keyEparent.key)Evacant=KelseEvacant=Eparent;bubbleUpHeap(E,root,K,parent);,Bubbling up from vacant through to the root, recursively,In fact, the “risk” is no more than “no improvement”,Element(55) to be inserted bubling up: element vs. parent,“Vacant” filtering down: left vs
23、. right,Risky FixHeap,90,65,80,70,25,60,45,50,40,15,35,75,80,65,70,60,25,50,45,40,15,35,75,90,If the element is smaller, filtering down half-half-way,“Vacant” filtering down only half-way,Improvement by Divide-and-Conquer,90,65,80,70,25,60,45,50,40,15,35,75,80,65,70,60,25,50,45,40,15,35,75,90,The bu
24、bbling up will not beyond last vacStop,Depth Bounded Filtering Down,int promote(Element E, int hStop, int vacant, int h)int vacStop;if (hhStop) vacStop=vacant;else if (E2*vacant.keyE2*vacant+1.key)Evacant=E2*vacant+1;vacStop=promote(E, hStop, 2*vacan+1, h-1);elseEvacant=E2*vacant;vacStop=promote(E,
25、hStop, 2*vacant, h-1);return vacStop,Depth Bound,fixHeap Using Divide-and-Conquer,void fixHeapFast(Element E, Element K, int vacant, int h) /h=lg(n+1)/2 in uppermost call if (h1) Process heap of height 0 or 1;elseint hStop=h/2;int vacStop=promote(E, hStop, vacant, h);int vacParent=vacStop/2;if (Evac
26、Parent.keyK.key)EvacStop=EvacParent;bubbleUpHeap(E, vacant, K, vacParent);elsefixHeapFast(E, K, vacStop, hStop),Number of Comparisons in the Worst Case for Accelerated fixHeap,Moving the vacant one level up or down need one comparison exactly in promote or bubbleUpHeap. In a cycle, t calls of promot
27、e and 1 call of bubbleUpHeap are executed at most. So, the number of comparisons in promote and bubbleUpHeap calls are: At most, lg(h) checks for reverse direction are executed. So, the number of comparisons in a cycle is at most h+lg(h) So, for accelerated heapSort: W(n)=nlgn+(nloglogn),Recursion E
28、quation of Accelerated heapSort,The recurrence equation about h, which is about lg(n+1)Assuming T(h)h, then:,For sorting a sequence of size n, n cycles of fixHeap are executed, so: n.(h+ lg(h+1),Solving the Recurrence Equation by Recursive Tree,T(n),T(n/2),T(n/4),h/2+1,h/4+1,h/8+1,T(1),1+1,lg(h+1) l
29、evels,So, the total cost is: h+ lg(h+1),Inductive Proof,The recurrence equation for fixHeapFast: Proving the following solution by induction: T(h) = h+lg(h+1) According to the recurrence equation: T(h+1)=(h+1)/2+1+T(h+1)/2) Applying the inductive assumption to the last term: T(h+1)=(h+1)/2+1+(h+1)/2+ lg(h+1)/2+1) (It can be proved that for any positive integer: lg(h)/2+1)+1= lg(h+1) ),The sum is h+1,Home Assignment,pp.213- 4.37 4.38 4-39 4-44,