Heaps & Adaptable Priority
Queues
By – Priyanka Rana
Session 2 review
In list-based implementation of priority queue:
If we use unsorted lists:
 insertion takes O(1) time
 deletion takes O(n) time.
If we use sorted lists:
 insertion takes O(n) time
 deletion takes O(1) time.
Session 2 Review
Selection-sort:
Phase 1 -> fast running time.
Phase 2 -> slow running time.
Insertion-sort:
Phase 1 -> slow running time.
Phase 2 -> fast running time.
Balancing the running time
Heap
Heap Data structure
Is a binary tree T that stores a collection of entries at its nodes
and satisfies two additional properties:
Order Property
Structure Property
Heap properties
Order Property:
For every internal node v, other than the root, key(v) ≥
key(parent(v)).
Structure Property:
A heap T with height h is a complete binary tree, i.e.:
for depth/level i < h, there are 2i nodes.
at depth h−1, the internal nodes are to the left of the external
nodes.
Heap data structure
The last node of a heap is the rightmost node of the
maximum depth.
2
65
89 Last node
Height of heap
A heap storing n keys has height h = [lg n]
For each depth i < h , there are 2i nodes and
at least one node at depth h. Thus,
n ≥ 20 + 21+ 22 + ... + 2h-1 + 1
≥ 1 + 2 + 4 + ... + 2h-1 + 1
≥ 2h
Which gives, h ≤ lgn.
Array list representation of heap
For the node at rank i
the left child is at rank 2i.
the right child is at rank 2i+1.
The cell at rank 0 is not used
A heap with n keys is represented by means of a array size n+1.
2
65
79
0 1 2 3 4 5
2 5 6 79
Insertion Algorithm
The insertion algorithm consists of three steps:
Find the insertion node z (i.e. the new last node).
Store k at z. (Here k=1)
Restore the heap-order property (discussed next).
2
65
79 2
5
79
z
Insertion Node
6
1
Up-heap bubbling
1
5
79
2
6
2
5
79
1
6
Removal from a heap
The removal algorithm consists of three steps:
 Replace the root key with the key of the last node
w.
 Remove w.
 Restore the heap-order property (discussed next).
2
65
79 7
5
9
w
Last Node
6
New Last Node
Down-heap bubbling
5
7
9
6
7
5
w9
6
Heap-sort
A heap-based priority queue, sorts a sequence of n
elements in O(nlgn) time.
performs n series of removeMin() operation.
Since each call of removeMin() takes O(lgn) time, sorting a
heap with n elements takes O(nlgn) time.
Merging two heaps
2
6
3
8 5
72
47
4
Exercise
At which nodes of a heap can an entry with the
largest key be stored?
Adaptable Priority Queues
Priority queues which are adaptable.
When priority queue methods are not sufficient.
For example, in a standby airline passenger application.
Adaptable Priority Queue ADT
remove(e):
Remove from P and return e.
replaceKey(e, k):
Replace with k and return the key of entry e of P.
replaceValue(e, x):
 Replace with x and return the value of entry e of P.
Exercise
Operation Output
Insert(5,A) e1
Insert(3,B) e2
Insert(7,C) e3
min() e2
Key(e2) 3
remove(e1) e1
replaceKey(e2,9) 3
replaceValue(e3,D
)
C
Remove(e2) e2
isEmpty() “false”
(3,B)
,
(5,A),(7,C),(9,B),(7,D)
Location aware entry
mechanism for finding the position of entry e of Priority Queue P.
This position is called the location of the entry
implementation of an entry that keeps track of its position is called
location-aware entry.
List implementation of an adaptable
PQ
A location-aware list entry is an object storing
key
value
position (or rank) of the entry in the list.
Heap Implementation of an Adaptable PQ
A location-aware list entry is an object storing
key
value
position of the entry in the heap.
Performance of Adaptable PQ Implementations
Method Unsorted List Sorted List Heap
Size, isEmpty() O(1) O(1) O(1)
insert O(1) O(n) O(n)
min O(n) O(1) O(1)
removeMin O(n) O(1) O(lg n)
remove O(1) O(1) O(lg n)
replaceKey O(1) O(n) O(lg n)
replaceValue O(1) O(1) O(1)
THANK YOU

Heaps & Adaptable priority Queues

  • 1.
    Heaps & AdaptablePriority Queues By – Priyanka Rana
  • 2.
    Session 2 review Inlist-based implementation of priority queue: If we use unsorted lists:  insertion takes O(1) time  deletion takes O(n) time. If we use sorted lists:  insertion takes O(n) time  deletion takes O(1) time.
  • 3.
    Session 2 Review Selection-sort: Phase1 -> fast running time. Phase 2 -> slow running time. Insertion-sort: Phase 1 -> slow running time. Phase 2 -> fast running time.
  • 4.
  • 5.
    Heap Data structure Isa binary tree T that stores a collection of entries at its nodes and satisfies two additional properties: Order Property Structure Property
  • 6.
    Heap properties Order Property: Forevery internal node v, other than the root, key(v) ≥ key(parent(v)). Structure Property: A heap T with height h is a complete binary tree, i.e.: for depth/level i < h, there are 2i nodes. at depth h−1, the internal nodes are to the left of the external nodes.
  • 7.
    Heap data structure Thelast node of a heap is the rightmost node of the maximum depth. 2 65 89 Last node
  • 8.
    Height of heap Aheap storing n keys has height h = [lg n] For each depth i < h , there are 2i nodes and at least one node at depth h. Thus, n ≥ 20 + 21+ 22 + ... + 2h-1 + 1 ≥ 1 + 2 + 4 + ... + 2h-1 + 1 ≥ 2h Which gives, h ≤ lgn.
  • 9.
    Array list representationof heap For the node at rank i the left child is at rank 2i. the right child is at rank 2i+1. The cell at rank 0 is not used A heap with n keys is represented by means of a array size n+1. 2 65 79 0 1 2 3 4 5 2 5 6 79
  • 10.
    Insertion Algorithm The insertionalgorithm consists of three steps: Find the insertion node z (i.e. the new last node). Store k at z. (Here k=1) Restore the heap-order property (discussed next). 2 65 79 2 5 79 z Insertion Node 6 1
  • 11.
  • 12.
    Removal from aheap The removal algorithm consists of three steps:  Replace the root key with the key of the last node w.  Remove w.  Restore the heap-order property (discussed next). 2 65 79 7 5 9 w Last Node 6 New Last Node
  • 13.
  • 14.
    Heap-sort A heap-based priorityqueue, sorts a sequence of n elements in O(nlgn) time. performs n series of removeMin() operation. Since each call of removeMin() takes O(lgn) time, sorting a heap with n elements takes O(nlgn) time.
  • 15.
  • 16.
    Exercise At which nodesof a heap can an entry with the largest key be stored?
  • 17.
    Adaptable Priority Queues Priorityqueues which are adaptable. When priority queue methods are not sufficient. For example, in a standby airline passenger application.
  • 18.
    Adaptable Priority QueueADT remove(e): Remove from P and return e. replaceKey(e, k): Replace with k and return the key of entry e of P. replaceValue(e, x):  Replace with x and return the value of entry e of P.
  • 19.
    Exercise Operation Output Insert(5,A) e1 Insert(3,B)e2 Insert(7,C) e3 min() e2 Key(e2) 3 remove(e1) e1 replaceKey(e2,9) 3 replaceValue(e3,D ) C Remove(e2) e2 isEmpty() “false” (3,B) , (5,A),(7,C),(9,B),(7,D)
  • 20.
    Location aware entry mechanismfor finding the position of entry e of Priority Queue P. This position is called the location of the entry implementation of an entry that keeps track of its position is called location-aware entry.
  • 21.
    List implementation ofan adaptable PQ A location-aware list entry is an object storing key value position (or rank) of the entry in the list.
  • 22.
    Heap Implementation ofan Adaptable PQ A location-aware list entry is an object storing key value position of the entry in the heap.
  • 23.
    Performance of AdaptablePQ Implementations Method Unsorted List Sorted List Heap Size, isEmpty() O(1) O(1) O(1) insert O(1) O(n) O(n) min O(n) O(1) O(1) removeMin O(n) O(1) O(lg n) remove O(1) O(1) O(lg n) replaceKey O(1) O(n) O(lg n) replaceValue O(1) O(1) O(1)
  • 24.

Editor's Notes

  • #2 &amp;lt;number&amp;gt;
  • #3 &amp;lt;number&amp;gt;
  • #4 &amp;lt;number&amp;gt;
  • #5 &amp;lt;number&amp;gt;
  • #6 &amp;lt;number&amp;gt;
  • #7 Order property is about every internal node whose key value must be greater than the key value of its parent. Depth: number of ancestors a node has Height: the height of a tree is given as the maximum depth of any node // structure property considers a heap of height &amp;quot;h&amp;quot; a complete binary tree when: its depth is less than its height and there are 2 raise to power of i nodes. another condition is that at height h-1 , internal nodes are to the left of the external nodes that // also means in a way that last node of a heap is the rightmost node of the maximum depth. &amp;lt;number&amp;gt;
  • #8 &amp;lt;number&amp;gt;
  • #9 this is about height of the heap, which is said to be as log n where n is the number of entries, now here we consider //the structural property of heap which says that at depth i ,// heap has 2 raise to power of i nodes. and //hence using mathematical operations// and in the end taking// log on both sides it is easily proved that height is log of number of entries. &amp;lt;number&amp;gt;
  • #10 We store a (key and value) for each entry item at each internal node, and keep track of the position of the last node. &amp;lt;number&amp;gt;
  • #11 Insertion into heap has three steps. // // // After the insertion of a new node, the heap-order property may be violated. we need to restore it by some swappings of the values stored at z with the entry stored in its ancestor nodes.// &amp;lt;number&amp;gt;
  • #12 The upward movement of the newly inserted entry //by means of swaps is called up-heap bubbling. Since the heap has height lgn, up-heap runs in O(lgn) time. &amp;lt;number&amp;gt;
  • #13 removal from a heap has three steps as well, // // // After replacing the root key with key k of the last node, again the heap-order property gets violated. Then we need to restore the heap-order by swapping the entries stored at root with the entries stored in its descendent nodes.// here The downward movement of the root entry by means of swaps is call down-heap bubbling. as the heap has height lgn, down-heap runs in O(lgn) time. &amp;lt;number&amp;gt;
  • #15 Using a heap-based priority queue, we can sort a sequence of n elements in O(nlgn) time, we have already covered sorting in detail, heap sort is also similar. //The algorithm simply performs n series of removeMin() operation. //Since each call of removeMin() takes O(lgn) time, sorting a heap with n elements takes O(nlgn) time. Traaversing takes O(logn) time --. Sorting takes O(nlog(n)) time &amp;lt;number&amp;gt;
  • #16 lets see how two are merged together, lets say 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 down-heap bubbling to restore the heap-order property. &amp;lt;number&amp;gt;
  • #17 &amp;lt;number&amp;gt;
  • #18 APQ are the priority queues which are adaptable, //There are some situations where the basic methods of the priority queue ADT (given in previous section) are not sufficient. //For example, in a standby airline passenger application: A passenger may request to be removed from the waiting list.A passenger presents a document (e.g. gold frequent flyer card) that implies to update his/her order in the waiting list.a passenger notices her name is misspelled on the ticket and asks it to be corrected. So, we need the priority queues be adaptable. &amp;lt;number&amp;gt;
  • #19 &amp;lt;number&amp;gt;
  • #21 &amp;lt;number&amp;gt;
  • #23 &amp;lt;number&amp;gt;
  • #24 &amp;lt;number&amp;gt;