Professional Documents
Culture Documents
Lecture 7
Lecture 7
Computer Science 3A - CSC3A10 1/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 3/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 4/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 5/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Entries:
An entry in a priority queue is simply a key-value pair
Priority queues store entries to allow for efficient insertion and removal based on
keys
Methods:
key(): returns the key for this entry
value(): returns the value associated with this entry
Computer Science 3A - CSC3A10 6/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 7/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 8/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Comparator ADT
Computer Science 3A - CSC3A10 9/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 10/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Implementation with an
Implementation with a sorted
unsorted list.
list.
Performance
Performance
insert takes O(1) time
insert takes O(n) time
since we can insert the
since we have to find the
item at the beginning or
place where to insert the
end of the sequence
item
removeMin and min take
removeMin and min take
O(n) time since we have
O(1) time, since the
to traverse the entire
smallest key is at the
sequence to find the
beginning
smallest key
Computer Science 3A - CSC3A10 11/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Selection Sort
Selection-sort is the variation of PQ-sort where the priority queue is implemented with
an unsorted list.
Running time of Selection-sort:
Phase 1: Inserting the elements into the priority queue with n insert operations
takes O(n) time
Phase 2: Bottleneck, Removing the elements in sorted order from the priority
queue with n removeMin operations takes time proportional to
n + (n − 1) + ... + 2 + 1 O(n + (n − 1) + ... + 2 + 1) = n(n + 1)/2
Selection-sort runs in O(n2 ) time as phase 2 runs in O(n2 )
Computer Science 3A - CSC3A10 12/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Selection-Sort II
Computer Science 3A - CSC3A10 13/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Insert-Sort
Insertion-sort is the variation of PQ-sort where the priority queue is implemented with
a sorted list.
Running time of Insertion-sort:
Phase 1: Insert into priority queue by scanning the list and inserting in the correct
position. Inserting the elements into the priority queue with n insert operations
takes time proportional to
1 + 2 + ... + n
O(1 + 2 + ... + (n − 1) + n) = n(n + 1)/2
Removing the elements in sorted order from the priority queue with a series of n
removeMin operations takes O(n) time
Insertion-sort runs in O(n2 ) time as phase 1 runs in O(n2 )
Computer Science 3A - CSC3A10 14/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Insert-Sort II
Computer Science 3A - CSC3A10 15/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
In-place Insertion-Sort
Computer Science 3A - CSC3A10 16/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
In-place Insertion-Sort II
Computer Science 3A - CSC3A10 17/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Heap ADT
Computer Science 3A - CSC3A10 18/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
A heap is a binary tree storing keys at its nodes and satisfying the following properties:
Heap-Order: for every internal node v other than the root
key (v ) ≥ key (parent(v ))
Complete Binary Tree:
let h be the height of the heap
for i = 0, ..., h − 1, there are 2i nodes of depth i
at depth h − 1, the internal nodes are to the left of the external nodes
Computer Science 3A - CSC3A10 19/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 20/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Height of a Heap
Computer Science 3A - CSC3A10 21/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Height of a Heap II
Computer Science 3A - CSC3A10 22/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 23/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 24/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
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:
1 Find the insertion node z (the new last node)
2 Store k at z
3 Restore the heap-order property (discussed next)
Computer Science 3A - CSC3A10 25/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 26/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Upheap
After the insertion of a new key k, the heap-order property may be violated
Algorithm upheap restores the heap-order 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 smaller than or equal to k
Since a heap has height O(logn), upheap runs in O(logn) time
Computer Science 3A - CSC3A10 27/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Upheap II
Computer Science 3A - CSC3A10 28/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
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
1 Replace the root key with the key of the last node w
2 Remove w
3 Restore the heap-order property (discussed next)
Computer Science 3A - CSC3A10 29/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 30/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
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 along a
downward path from the root
Downheap terminates when key k reaches a leaf or a node whose children have
keys greater than or equal to k
Since a heap has height O(logn), downheap runs in O(logn) time
Computer Science 3A - CSC3A10 31/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Downheap II
Computer Science 3A - CSC3A10 32/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 33/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 34/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Heap-Sort
Computer Science 3A - CSC3A10 35/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 36/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 37/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 38/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 39/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 40/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 41/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 42/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
1 A l g o r i t h m BottomUpHeap ( S )
2 I n p u t A L i s t L s t o r i n g pow ( 2 , i +1) −1 e n t r i e s
3 Output A heap T s t o r i n g t h e e n t r i e s i n L
4
5 If S . isEmpty ( ) then
6 r e t u r n an empty heap
7 e = L . remove ( L . f i r s t ( ) )
8 S p l i t L i n t o two l i s t s , L1 and L2 , e a c h s i z e ( n−1) /2
9 T1 = BottomUpHeap ( L1 )
10 T2 = BottomUpHeap ( L2 )
11 C r e a t e B i n a r y T r e e T w i t h r o o t r s t o r i n g e , l e f t s u b t r e e T1 and r i g h t
s u b t r e e T2
12 P e r f o r m a down−heap from t h e r o o t r o f T , i f n e c e s s a r y
13 r e t u r n T
Bottom-up Algorithm
Computer Science 3A - CSC3A10 43/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 44/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 45/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 46/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 47/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Downheap Analysis
We visualize the worst-case time of a downheap with a proxy path that goes first
right and then repeatedly goes left until the bottom of the heap (this path may
differ from the actual downheap path)
Since each node is traversed by at most two proxy paths, 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
Computer Science 3A - CSC3A10 48/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Downheap Analysis II
Computer Science 3A - CSC3A10 49/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 50/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 51/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 52/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 53/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 54/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 55/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 56/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Computer Science 3A - CSC3A10 57/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Performance
Using location-aware entries we can achieve the following running times (times better
than those achievable without location-aware entries are in bold):
Method Unsorted List Sorted List Heap
size, isEmpty O(1) O(1) O(1)
insert O(1) O(n) O(log n)
min O(n) O(1) O(1)
removeMin O(n) O(1) O(log n)
remove O(1) O(1) O(log n)
replaceKey O(1) O(n) O(log n)
replaceValue O(1) O(1) O(1)
Computer Science 3A - CSC3A10 58/59 Academy of Computer Science and Software Engineering
Outline Priority Queue ADT Heap ADT Adaptable Priority Queue ADT
Exercises
Reinforcement exercises:
R8.2
R8.3
R8.5 - R8.8
R8.11 - R8.13
R8.15 - R8.17
R8.23
Creativity exercises:
C8.3 - C8.5
C8.12 - C8.14
C8.16 - C8.17
Computer Science 3A - CSC3A10 59/59 Academy of Computer Science and Software Engineering