Download presentation
Presentation is loading. Please wait.
1
Priority Queues Queues: first-in first-out in printer schedule Disadvantage: short job, important job need to wait Priority queue is a data structure that allows at least two operations enqueue insert dequeue deleteMin
2
Simple Implementation 1. Linked list insert O(1) deleteMin O(N) 2. Keep list Sorted. insert O(N) deleteMin O(1) 3. BST O(logN) average running time for both operation delete the minimum make right subtree heavy
3
Binary Heap Heap is a binary tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right. number of notes: 2^h to 2^ (h+1) – 1 height h is log N
4
Array implementation A BC DE FG HIJ ABCDEFGHIJ 12345876910 For node i Left child 2i Right child: 2i+1 Parent: i/2
5
Heap-Order Property The smallest element should be at the root Any node should be smaller than all of its descendants By this property, the minimum element can always be found at the root. This property allows operations to be performed quickly
6
Basic Heap operations Ensure that heap-order property is maintained Insert(X): create a hole in next available location, if X can placed in the hole, then we are done, otherwise, slide the element in hole’s parent node into the hole, thus, bubbling up the hole towards the root until X can be placed in hole.
7
Insert 14 2116 2431 1968 2632 13 65 31 21 14 General Strategy is known as percolate up
8
Basic Operation(continued) deleteMin:When minimum is removed, a hole is created at the root. The last element X in the heap must move somewhere in the heap. If X can be placed in the hole, then we are done. This is unlikely, so we slide the smaller of the hole’s children into the hole, Repeat until X can be placed in the hole.
9
DeleteMin 13 This strategy is known as percolate down 16 24 1968 2632 13 65 31 21 14 31 14 21 31
10
Other Heap operations DecreaseKey: lowers the value of the item at position p by a positive amount b. Since this might violate heap order, it must be fixed by a percolate up. This operation could be useful to system administrators
11
Continue incresaeKey(p,b) : increases the value of the item at position p by a positive amount b. This is done with percolate down. Many schedulers automatically drop the priority of a process that is consuming excessive CPU time.
12
Continue Remove(p): removes the node at position p from the heap. This is done by first performing decreaseKey(p, BIG integer) and then performing deleteMin. When a process is terminated by a user(instead of finishing normally), it must be removed from the priority queue
13
Continue BuildHeap: takes as input N items and places them into an empty heap. This can be done within N successive inserts. Each insert take O(1) average and O(logN) worst-case. Total time would be O(N) average and O(NlogN) worst-case
14
Continue BuildHeap Algorithm: Place the N items into the tree in any order, maintaining the structure property. for each node from the node at size/2 to the root, do percolate down. That creates a Heap-order tree. Running time: O(N) ( proof omitted)
15
Application of Priority Queues Selection problem 1. Sort the array O(N^2) 2. Sort first k elements, then process remaining one by one. O(N k) worst case O(N^2) 3. Read N elements and buildHeap. Then deleteMin k times O(N) for construct the heap O(logN) for deleteMin, there are k deleteMin total O(N+klogN) if k = [N/2], Θ(NlogN)
16
4. Read first k element to build Heap. O(k) the time to process each remaining: O(1) + O(logk) Total time is O(k + (N-k) logk) = O(Nlogk) If k = [N/2], O(NlogN)
17
Heap Sort Read N items and build Heap maintaining Heap- Order property deleteMin N times and record the element in another array. Copying the array back Running time O(NlogN) Disadvantage: Need memory for another array
18
A clever way to avoid using a second array 16 19 1868 2632 13 65 31 21 14 31 14 21 31 1419162621186865313213 14161921186865263231
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.