Lec 6
Lec 6
Algorithms-I
Lecture 6
2
Binary Heap
● The binary heap is the classic method used to implement
priority queues.
● We use the term heap to refer to the binary heap.
● Heap is different from the term heap used in dynamic
memory allocation.
● Heap has two properties:
– Structure property
– Ordering property
3
Structure Property
4
Properties of a complete binary tree
5
Figure 21.1
A complete binary tree and its array representation
6
Heap-Order Property
● In a heap, for every node X with parent P, the key in P is
smaller than or equal to the key in X.
● Thus the minimum element is always at the root.
– Thus we get the extra operation findMin in constant time.
● A max heap supports access of the maximum element
instead of the minimum, by changing the heap property
slightly.
7
Figure 21.3
Two complete trees: (a) a heap; (b) not a heap
8
Heap
2 16 1 3
14 10
5 6 7 1 2 3 4 5 6
4 7 8 9 10
8 7 9 3 1614 10 8 7 9 3 2 4 1
8
9
2 410 1
9
Heap as a Tree
root of tree: first element in the array, corresponding to i =
1 parent(i) =i/2: returns index of node's parent
left(i)=2i: returns index of node's left child
right(i)=2i+1: returns index of node's right child
2 16 1 3
14 10
5 6 7 1 2 3 4 5 6
4 7 8 9 10
8 7 9 3 1614 10 8 7 9 3 2 4 1
8
9
2 410 1
n)
Heap Operations
11
Max_heapify
• Assumethat the trees rooted at left(i) and right(i) are
max-heaps
• If
element A[i] violates the max-heap property, correct
violation by “trickling” element A[i] down the tree, making
the subtree rooted at index i a max-heap
12
Max_heapify (Example)
14
Max_heapify (Example)
Time=? O(log
n) 15
Max_Heapify Pseudocode
l = left(i)
r = right(i)
if (l <= heap-size(A) and A[l] >
A[i]) then largest = l
else largest = i
if (r <= heap-size(A) and A[r] >
A[largest]) then largest = r
if largest = i
then exchange A[i] and
16
A[largest] Max_Heapify(A,
Build_Max_Heap(A)
Build_Max_Heap(A):
for i=n/2 downto 1
do Max_Heapify(A, i)
Build_Max_Heap(A):
for i=n/2 downto 1
do Max_Heapify(A, i)
Build_Max_Heap(A):
for i=n/2 downto 1
do Max_Heapify(A, i)
20
Build-Max-Heap Demo
21
Build-Max-Heap
22
Heap-Sort
Sorting Strategy:
23
Heap-Sort
Sorting Strategy:
24
Heap-Sort
Sorting Strategy:
25
Heap-Sort
Sorting Strategy:
26
Heap-Sort
Sorting Strategy:
Max_heapify(A,1)
28
Heap-Sort Demo
29
Heap-Sort Demo
30
Heap-Sort Demo
31
Heap-Sort
● Running time:
● after n iterations the Heap is empty
● every iteration involves a swap and a max_heapify operation;
hence it takes O(log n) time
32
Analysis of Heapsort
● It is an O(N log N) algorithm.
– First phase: Build heap O(N)
– Second phase: N deleteMax operations: O(NlogN).
● Detailed analysis shows that, the average case for heapsort is
poorer than quick sort.
– Quicksort’s worst case however is far worse.
● An average case analysis of heapsort is very complicated, but
empirical studies show that there is little difference between
the average and worst cases.
– Heapsort usually takes about twice as long as quicksort.
– Heapsort therefore should be regarded as something of an insurance
policy:
– On average, it is more costly, but it avoids the possibility of O(N2).
33