Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2420: Lecture 22 Vladimir Kulyukin Computer Science Department Utah State University.

Similar presentations


Presentation on theme: "CS2420: Lecture 22 Vladimir Kulyukin Computer Science Department Utah State University."— Presentation transcript:

1 CS2420: Lecture 22 Vladimir Kulyukin Computer Science Department Utah State University

2 Outline Binary Heaps as Priority Queues –Chapter 6 The Selection Problem (K-th Order Statistic) –Sections 6.4.1 and 7.7.6.

3 Inserting Keys into a Binary Heap To insert a key, we add a new leaf to the complete binary tree. We then traverse the tree from the new leaf all the way to the root looking for a proper node for the newly inserted key. If the heap is represented as an array, we either have to –1) know the size of the heap in advance and return an error once the size of the heap reaches a maximum; or –2) be ready to resize the heap at run time.

4 Inserting Keys into a Binary Heap 16 14 142 93 10 78 1 23 4 567 89 15 We would like to insert 15

5 Inserting Keys into a Binary Heap 16 14 142 93 10 78 1 23 4 567 89 1) We expand the heap by one node at position 11. 15 2) We push the newly inserted key up the heap for as long as we can. 11

6 Inserting Keys into a Binary Heap 16 14 142 93 10 7 8 1 23 4 567 89 We swap positions 11 and 5, because key 15 > key 7. 15 11

7 Inserting Keys into a Binary Heap 16 14 142 93 10 7 8 1 23 4 567 89 We swap positions 2 and 5, because key 15 > key 14. We can stop now, because positions 1 and 2 cannot be swapped: key 15 < key 16. 15 11

8 HeapInsert HeapInsert(A, key) { mHeapSize = mHeapSize + 1; i = mHeapSize; while ( i > 1 && A[Parent(i)] < key ) { A[i] = A[Parent(i)]; i = Parent(i); } A[i] = key; }

9 HeapInsert: Asymptotic Analysis The performance of HeapInsert depends on the height of the binary heap with N nodes. Since binary heaps are complete binary trees, the height is logN. Thus, HeapInsert = O(logN).

10 Deleting the Maximum Element from a Binary Heap The maximum element always resides on the top. We take the top of the heap and save it in a variable. We put the element in the last position on the top. We decrement the size of the heap by 1. We restore the heap property at the top.

11 Deleting the Maximum Element from a Binary Heap DeleteMax(A) { if ( mHeapSize < 1 ) return Error; max = A[1]; A[1] = A[mHeapSize]; mHeapSize = mHeapSize – 1; RestoreHeap(A, 1); return max; }

12 Finding the K-th Order Statistic Given a list of N elements and an integer k, find the k-th largest (smallest) element.

13 Finding the K-th Order Statistic Finding the largest K-th order statistic is symmetrical with finding the smallest K-th order statistic. For example, if there is an array of 10 elements, finding the 2 nd largest is the same as finding the 9 th smallest.

14 Solution 1 Read the elements into an array and use an asymptotically optimal comparison sort. This solution gives us O(NlogN) so long as we can sort in memory. If the number of elements that we have to sort exceeds the amount of available RAM, solution 1 is not acceptable.

15 Solution 2 Read the elements into an array. Build a heap from that array. Perform DeleteMax k times. The running time of this algorithm is O(N + klogN), which, if k = N, is also O(NlogN). In the worst case, solution 2 is similar to solution 1, because we have to read all elements into memory at once.

16 Solution 3 1.Read k elements into an array. 2.Build a minimum heap out of the array. 3.When the new element X is read, compare it to the minimum element M of the heap. 1.If X <= M, ignore it. 2.If M < X, remove M from the heap and insert X.

17 Solution 3 Step 1 is O(k). Step 2 is O(k). Step 3 runs (N - k) times. In the worst case, we need to remove the top of the heap, which is O(logk), and insert a new element into the heap, which is also O(logk). Total running time is O(k + k + (N-k)logk) = O(Nlogk). Note that, if k = 1 or k = N, then the total running time is O(N), because we are looking for the smallest or the largest element.

18 Solution 4: Quickselect A What is the K-th smallest element in A? V Choose V and partition L R L has |L| elements, i.e. |L| = L.size() R has |R| elements, i.e. |R| = R.size()

19 Quickselect: Three Cases K <= |L|  –K-th smallest element is in L. K = |L| + 1  –K-th smallest element is V. K > |L| + 1  –1) K-th smallest element is in R; –2) it is the (K-|L|-1)-st smallest element in R.


Download ppt "CS2420: Lecture 22 Vladimir Kulyukin Computer Science Department Utah State University."

Similar presentations


Ads by Google