Data Structures and Algorithms - L10
Data Structures and Algorithms - L10
BITS Pilani
Hyderabad Campus
Queue
“Queue” is the fancy name for waiting in line
Bus
Stop
Queue:
– First in, first out
– First to be removed: First to enter
Priority Queue:
– First in, highest (/lowest) priority element out
– First to be removed: element with highest (/lowest)
priority
• Process scheduling
– Give CPU resources to most urgent task
• Communications
– Send most urgent message first
• Event-driven simulation
– Pick next event (by time) to be simulated
Priority Queue implementations
Algorithm
enqueue P1 a1 a4
– put in to proper queue
P2 b3 b1 b27 b3 b3
dequeue
– get from P1 first P3 c6 c4 c9
– then get from P2
..
– then get from P3 .
– …
P123 7 12 41
– then get from P123
P1 a1 a4 Algorithm
enqueue
P2 b3 b1 b27 b3 b3 – put in to proper queue
dequeue
P3 c6 c4 c9
– get from P1 first
..
. – then get from P2
– then get from P3
P123 7 12 41 – …
– then get from P123
Insert()
– Search for appropriate place to insert
– O(n)
Remove()
– Remove first in list
– O(1)
Priority Queue implementation(3)
Unsorted linked-list, with head pointer
Insert()
– Insert at the end of linked list
– O(1)
Remove()
– Search for the element (e) with min or max priority
– Remove element (e)
– O(n)
Priority Queue implementation(3)
Almost full:
– Balanced (all leaves at max height h or at h-1)
– All leaves to the left
24
16 15
10 3 11 12
4 2 1
Heap or not?
Heap
Heap Examples
24
16 15
10 3 17 12
4 2 1
Heap or not?
Not Heap
(does not maintain heap property)
(17 > 15)
Heap Examples
24
16 15
10 3 11 12
4 2
1
Heap or not?
Not Heap
(balanced, but leaf with priority 1 is not in place)
Representing heap in an array
In the array:
24 16 13 14 3 11 12 4 2 1
Index: 0 1 2 3 4 5 6 7 8 9
Heap property
• Heap property: parent priority >= child
– For all nodes
• Any sub-tree has the heap property
– Thus, root contains max-priority item
• Every path from root to leaf is descending
– This does not mean a sorted array
– In the array:
24 16 13 14 3 11 12 4 2 1
Maintaining heap property (heapness)
Remove-top():
Get root
Somehow fix heap to maintain heapness
Insert()
Put item somewhere in heap
Somehow fix the heap to maintain heapness
Remove-top(array-heap h)
In the array:
24 16 13 14 3 11 12 4 2 1
Index: 0 1 2 3 4 5 6 7 8 9
Remove-top() example:
0
Out: 24 1
1 2
16 4 13
3 5 6
14 3 11 12
4 2
7 8 9
In the array:
1 16 13 14 3 11 12 4 2
Index: 0 1 2 3 4 5 6 7 8 9
Remove-top() example:
0
16
1 2
1 4 13
3 5 6
14 3 11 12
4 2
7 8 9
In the array:
16 1 13 14 3 11 12 4 2
Index: 0 1 2 3 4 5 6 7 8 9
Remove-top() example:
0
16
1 2
14 4 13
3 5 6
1 3 11 12
4 2
7 8 9
In the array:
16 14 13 1 3 11 12 4 2
Index: 0 1 2 3 4 5 6 7 8 9
Remove-top() example:
0
16
1 2
14 4 13
3 5 6
4 3 11 12
1 2
7 8 9
In the array:
16 14 13 4 3 11 12 1 2
Index: 0 1 2 3 4 5 6 7 8 9
Heapify_down(heap-array h, index i)
1. l LEFT(i) // 2*i+1
2. r RIGHT(i) // 2*i+2
3. if l < h.length // left child exists
4. if h[l] > h[r]
5. largest l
6. else largest r
7. if h[largest] > h[i] // child > parent
8. swap(h[largest],h[i])
9. Heapify_down(h,largest) // recursive
Remove-top() Complexity
Remove-top() - O(log n)
Insert(heap-array h, item t)
Insertion works in a similar manner
We put the new element at the end of array
Exchange with ancestors to maintain heapness
If necessary.
Repeatedly.
h[h.length] t
h.length h.length + 1
Heapify_up(h, h.length) // see next
Insert() example:
0
In: 15 24
1 2
16 4 13
3 5 6
14 3 11 12
4 2 1
7 8 9
In the array:
24 16 13 14 3 11 12 4 2 1
Index: 0 1 2 3 4 5 6 7 8 9
Insert() example:
0
24
1 2
16 4 13
3 5 6
14 3 11 12
4 2 1 15
7 8 9 10
In the array:
24 16 13 14 3 11 12 4 2 1 15
Index: 0 1 2 3 4 5 6 7 8 9 10
Insert() example:
0
24
1 2
16 4 13
3 5 6
14 15 11 12
4 2 1 3
7 8 9 10
In the array:
24 16 13 14 15 11 12 4 2 1 3
Index: 0 1 2 3 4 5 6 7 8 9 10
Heapify_up(heap-array h, index i)
Insert() - O(log n)
Priority queue as heap as binary tree in
array
Complexity is O(log n)
Both insert() and remove-top()
Heapsort()
Heapsort(array a)
BuildMaxHeap(A)
BuildMaxHeap(A)
1. heap-size[A]
1. heap-size[A] length[A]
length[A]
2. for ii
2. for length[A]/2
length[A]/2 downto
downto 11
3.
3. do
do MaxHeapify(A,
MaxHeapify(A, i)i)
BITS Pilani, Hyderabad Campus
BuildMaxHeap – Example
Input Array:
24 21 23 22 36 29 30 34 28 27
Initial Heap:
(not max-heap) 24
21 23
22 36 29 30
34 28 27
BITS Pilani, Hyderabad Campus
Data Structure Binary Heap
• Min-Heap
– For every node excluding the root,
value is at least that of its parent: A[parent[i]] A[i]
• Smallest element is stored at the root.
• In any subtree, no values are smaller than the value stored at subtree
root
Max-heap as a binary
tree.
26
24 20
18 17 19 13
• Fix the offending node by exchanging the value at the node with the
larger of the values at its children.
– May lead to the subtree at the child not being a heap.
• Recursively fix the children until all of them satisfy the max-heap
property.
MaxHeapify(A, 2)
26
24
14 20
18
14
24 17 19 13
12 14
14
18 11
BuildMaxHeap(A)
BuildMaxHeap(A)
1. heap-size[A]
1. heap-size[A] length[A]
length[A]
2. for ii
2. for length[A]/2
length[A]/2 downto
downto 11
3.
3. do
doMaxHeapify(A,
MaxHeapify(A, i)i)
24 21 23 22 36 29 30 34 28 27
Initial Heap:
(not max-heap) 24
21 23
22 36 29 30
34 28 27
BITS Pilani, Hyderabad Campus
BuildMaxHeap – Example
MaxHeapify(10/2 = 5)
MaxHeapify(4)
MaxHeapify(3) 24
36
MaxHeapify(2)
21
34
24
36 30
23
MaxHeapify(1)
28
34
24
22 36
27
21 29 30
23
34
22 28
24 27
21
• Loop Invariant: At the start of each iteration of the for loop, each node
i+1, i+2, …, n is the root of a max-heap.
• Initialization:
– Before first iteration i = n/2
– Nodes n/2+1, n/2+2, …, n are leaves and hence roots of max-
heaps.
• Maintenance:
– By LI, subtrees at children of node i are max heaps.
– Hence, MaxHeapify(i) renders node i a max heap root (while
preserving the max heap root property of higher-numbered nodes).
– Decrementing i reestablishes the loop invariant for the next iteration.