Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a...
Transcript of Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a...
![Page 1: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/1.jpg)
1
Heaps and HeapSort
2
6 5
7 9
![Page 2: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/2.jpg)
2
Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority Queue ADT n insertItem(k, e)
inserts an item with key k and element e
n removeMin() removes the item with smallest key and returns its element
Additional methods n minKey()
returns, but does not remove, the smallest key of an item
n minElement() returns, but does not remove, the element of an item with smallest key
n size(), isEmpty() Applications: n Standby flyers n Auctions n Stock market
![Page 3: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/3.jpg)
3
Sorting with Priority Queues We can use a priority queue to sort a set of comparable elements n Insert the elements one
by one with a series of insertItem(e, e) operations
n Remove the elements in sorted order with a series of removeMin() operations
The running time of this sorting method depends on the priority queue implementation
PQ-Sort(S) P = priority queue while S.isEmpty == false
e = S. get(0) S.remove(0) P.insertItem(e, e)
while P.isEmpty() == false e = P.removeMin() S.add(e)
![Page 4: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/4.jpg)
4
Sequence-Based Priority Queue Implementation with an unsorted list
Performance: n insertItem takes O(1) time
since we can insert the item at the beginning or end of the sequence
n removeMin, minKey and minElement take O(n) time since we have to traverse the entire sequence to find the smallest key
Implementation with a sorted list
Performance: n insertItem takes O(n) time
since we have to find where to insert the item
n removeMin, minKey and minElement take O(1) time since the smallest key is at the beginning of the sequence
4 5 2 3 1 1 2 3 4 5
![Page 5: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/5.jpg)
5
PQ-Sort with Unsorted List PQ
Running time: n Inserting the elements into the priority queue with n
insertItem operations takes O(n) time n Removing the elements in sorted order from the priority
queue with n removeMin operations takes time: 1 + 2 + …+ n
O(n2) time (Phase 2 is like SelectionSort)
![Page 6: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/6.jpg)
6
PQ-Sort with Sorted List PQ
Running time: n Inserting the elements into the priority queue with n
insertItem operations takes time: 1 + 2 + …+ n
n Removing the elements in sorted order from the priority queue with a series of n removeMin operations takes O(n) time
O(n2) time (Phase 1 is like Insertion Sort)
![Page 7: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/7.jpg)
7
Heaps A heap is a binary tree storing keys at its nodes and satisfying the following properties: n Heap Property: for every node
v other than the root, key(v) ≥ key(parent(v))
n Complete Binary Tree: w all levels are full except
possibly the last one w all the nodes at the last level
are as far left as possible
n Important: A heap storing n keys has height O(log n)
2
6 5
7 9
last node
![Page 8: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/8.jpg)
8
Heaps and Priority Queues We can use a heap to implement a priority queue We store a <key, value> pair at each node We keep track of the position of the last node For simplicity, we show only the keys in subsequent pictures
(2, Sue)
(6, Mark) (5, Pat)
(9, Jeff) (7, Anna)
![Page 9: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/9.jpg)
9
Insertion into Heap Method insertItem of the priority queue ADT corresponds to the insertion of a key k to the heap The insertion algorithm consists of three steps: n Find the insertion point z
(the new last node) n Store k at z n Restore the heap property
(discussed next)
2
6 5
7 9
insertion node
z
2
6 5
7 9 1 z
![Page 10: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/10.jpg)
10
Upheap After the insertion of a new key k, the heap property may be violated Algorithm upheap restores the heap property by swapping k along an upward path from the insertion node Upheap terminates when the key k reaches the root or a node whose parent has a key less than or equal to k Since a heap has height O(log n), upheap runs in O(log n) time
1
2 5
7 9 6 z
2
1 5
7 9 6 z
2
6 5
7 9 1 z
![Page 11: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/11.jpg)
11
Removal from a Heap Method removeMin of the priority queue ADT corresponds to the removal of the root key from the heap The removal algorithm consists of three steps: n Remove the root key n Move the key of the last
node w to the root n Restore the heap property
(discussed next)
2
6 5
7 9
last node
w
7
6 5
9
![Page 12: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/12.jpg)
12
Downheap After replacing the root key with the key k of the last node, the heap-order property may be violated Algorithm downheap restores the heap-order property by swapping key k with one of its children along a downward path from the root. Which one? Downheap terminates when key k reaches a node whose children have keys greater than or equal to k Since a heap has height O(log n), downheap runs in O(log n) time
7
6 5
9
5
6 7
9
![Page 13: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/13.jpg)
13
Heap Implementation Using ArrayLists or Arrays
We can represent a heap with n keys by means of an ArrayList or array of length n + 1 For the node at index i n the left child is at index 2i n the right child is at index 2i + 1 n the parent is at index floor(i/2)
Links between nodes are not explicitly stored The cell at index 0 is not used Operation insertItem corresponds to inserting at index n + 1 and upheaping Operation removeMin corresponds to moving the item at index n to index 1 and downheaping
2
6 5
7 9
2 5 6 9 7 1 2 3 4 5 0
![Page 14: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/14.jpg)
14
Given a list S of n items (keys), remove the items in S and make a heap-based priority queue from the n keys
Remove the n keys from the heap one at a time and add them to S
Heap-Sort
![Page 15: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/15.jpg)
15
Insert n keys one at a time
How long does it take to insert key i?
So:
Top-Down Heap Construction
lgi ≈ n lgni=0
n
∑
lgi
![Page 16: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/16.jpg)
16
We can construct a heap storing n given keys using a bottom-up construction with log n phases In phase i, pairs of heaps with 2i -1 keys are merged into heaps with 2i+1-1 keys
Bottom-Up Heap Construction
2i -1 2i -1
2i+1-1
![Page 17: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/17.jpg)
17
Merging Two Heaps We are given two heaps and a key k We create a new heap with the root node storing k and with the two heaps as subtrees We perform downheap to restore the heap-order property
7
3
5 8
2
6 4
3
5 8
2
6 4
2
3
5 8
4
6 7
![Page 18: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/18.jpg)
18
Example
15 16 12 4 9 6 20 23
25
15 16
5
12 4
11
9 6
27
20 23
![Page 19: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/19.jpg)
19
Example (contd.)
25
15 16
5
12 4
11
9 6
27
20 23
15
25 16
4
12 5
6
9 11
20
27 23
![Page 20: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/20.jpg)
20
Example (contd.)
7
15
25 16
4
12 5
8
6
9 11
20
27 23
4
15
25 16
5
12 7
6
8
9 11
20
27 23
![Page 21: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/21.jpg)
21
Example (end)
4
15
25 16
5
12 7
10
6
8
9 11
20
27 23
5
15
25 16
7
12 10
4
6
8
9 11
20
27 23
![Page 22: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/22.jpg)
22
Visual Analysis We visualize the worst-case time using proxy paths that first go right and then repeatedly go left until the bottom of the heap is reached (this path may differ from the actual downheap path) Since no edge is traversed more than once, the total number of nodes of the proxy paths is O(n) Thus, bottom-up heap construction runs in O(n) time Bottom-up heap construction is faster than n successive insertions and speeds up the first phase of heap-sort
![Page 23: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/23.jpg)
Non-Visual Analysis During phase i there are (n+1)/2i+1 pairs of heaps being combined Each pair has a downheap path length of i Length of all downheap paths during phase i is (n+1)(i/2i+1)< n(i/2i) Sum these over the lg n phases:
And use this fact:
So:
And bottom-up heap construction is O(n) time
€
n i2 i
=i=1
lg n
∑ n i2 i
=i=1
lg n
∑ n i 12( )i
i=1
lg n
∑
€
ix i =x
1− x( )2i=0
∞
∑ , x <1
€
n i 12( )i <
i=1
lg n
∑ n i 12( )i
i=0
∞
∑ = 2n
![Page 24: Heaps and HeapSort · Heaps and HeapSort 2 5 6 9 7 . 2 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority](https://reader036.fdocuments.us/reader036/viewer/2022071021/5fd52f420ffdb846f9068b13/html5/thumbnails/24.jpg)
24
Given a list S of n items (keys), remove the items in S and make a heap-based priority queue from the n keys: O(n) time
Remove the n keys from the heap one at a time and add them to S: O(?) time
So: O(n lg n) time
Total: O(n lg n) time
Heap-Sort
lgi ≈ n lgni=0
n
∑