0% found this document useful (0 votes)
15 views32 pages

Lecture 5

Big data
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views32 pages

Lecture 5

Big data
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Quality Content for Outcome based Learning

Design & Analysis of Algorithms


Lecture 5

Ver. No.: 1.1 Copyright © 2021, ABES Engineering College


Quality Content for Outcome based Learning

Heap Sort

Ver. No.: 1.1 Copyright © 2021, ABES Engineering College


Heap data structure

Copyright © 2021, ABES Engineering College


Example

Copyright © 2021, ABES Engineering College


Example

 Given an array of size N. The task is to sort the array elements by


using Heap Sort.
 Input:
 N=10
 Arr []:{16, 4, 10, 14, 7, 9, 3, 2, 8, 1}
 Output: 1 2 3 4 7 8 9 10 14 16

Copyright © 2021, ABES Engineering College


Example

 Given an array of size N. The task is to sort the array elements by


using Heap Sort.
 Input:
 N = 10
 arr[] = {10,9,8,7,6,5,4,3,2,1}
 Output:1 2 3 4 5 6 7 8 9 10

Copyright © 2021, ABES Engineering College


Heap property

 For max-heaps (largest element at root), max-heap property: for all nodes
i,
excluding the root, A[PARENT(i )] ≥ A[i ].
 For min-heaps (smallest element at root), min-heap property: for all nodes
i,
excluding the root, A[PARENT(i )] ≤ A[i ].

Copyright © 2021, ABES Engineering College


Max Heapify Algorithm

Copyright © 2021, ABES Engineering College


Max Heapify Algorithm

Copyright © 2021, ABES Engineering College


Working of Max Heapify Algorithm

Copyright © 2021, ABES Engineering College


Building a heap:

Copyright © 2021, ABES Engineering College


Example
Building a max-heap from the following unsorted array results in the first heap example.

Copyright © 2021, ABES Engineering College


Copyright © 2021, ABES Engineering College
Analysis of Heap Sort:

• Simple bound: O(n) calls to MAX-HEAPIFY, each of which takes O(lg n)


time ⇒ O(n lg n).

•Tighter analysis observation:


An n element heap has height and at most nodes of any height h.

Copyright © 2021, ABES Engineering College


Tighter analysis Proof

BUILD-MAX-HEAP(A,n) 9

do MAX-HEAPIFY (A,i,n) 6 5
i

0 8 2 1
1 2 3 4 5 6 7 8
9 6 5 0 8 2 1 3
3

Copyright © 2021, ABES Engineering College


Tighter analysis Proof

 For easy understanding, Let us take a complete binary Tree,

 The height of a node is the number of edges from the node to the deepest
leaf.
 The depth of a node is the no of edges from the root of the node.

Copyright © 2021, ABES Engineering College


Tighter analysis Proof
 All the leaves are of height 0, therefore there are 8 nodes at height 0.
 4 numbers of nodes at height 1.
 2 numbers of nodes at height 2.
 and one node at height 3.

Copyright © 2021, ABES Engineering College


Tighter analysis Proof
 Hence the question is how many nodes are there at height ‘h’ in a complete
binary tree?
 The answer is :
 If there are n nodes in tree, then at most nodes are available at height h.

Copyright © 2021, ABES Engineering College


Tighter analysis Proof
 Now if we apply MAX-HEAPIFY() on any node of any level, then the
time taken by MAX-HEAPIFY() is the height of the node.
 Hence in case of root the time taken is
 Hence

H ea pify()
e by Max- es of
k don f no d
Wor nu m ber o
on the height h.

Copyright © 2021, ABES Engineering College


Tighter analysis Proof

Hence the running time of BUILD-MAX-HEAP(A,n) is in tight bound .

Copyright © 2021, ABES Engineering College


The Heapsort algorithm:
Given an input array, the Heapsort algorithm acts as follows:

• Builds a max-heap from the array.

• Starting with the root (the maximum element), the algorithm places the maximum
element into the correct place in the array by swapping it with the element in the last
position in the array.

• “Discard” this last node (knowing that it is in its correct place) by decreasing the heap
size, and calling MAX-HEAPIFY on the new (possibly incorrectly-placed) root.

• Repeat this “discarding” process until only one node (the smallest element) remains,
and therefore is in the correct place in the array.

Copyright © 2021, ABES Engineering College


The Heapsort Algorithm:

Copyright © 2021, ABES Engineering College


Example

Copyright © 2021, ABES Engineering College


Analysis:

BUILD-MAX-HEAP: O(n)
• For loop: n − 1 times
• exchange elements: O(1)
• MAX-HEAPIFY: O(lg n)

Total time: O(n lg n).

Copyright © 2021, ABES Engineering College


Heap implementation of priority queue
 Heaps efficiently implement priority queues. These notes will deal with max
priority queues implemented with max-heaps. Min-priority queues are
implemented with min-heaps similarly.
 A heap gives a good compromise between fast insertion but slow extraction and
vice versa. Both operations take O(lg n) time.

Copyright © 2021, ABES Engineering College


Priority queue
Maintains a dynamic set S of elements.
• Each set element has a key-an associated value.
• Max-priority queue supports dynamic-set operations:
• INSERT(S, x): inserts element x into set S.
• MAXIMUM(S): returns element of S with largest key.
• EXTRACT-MAX(S): removes and returns element of S with largest key.
• INCREASE-KEY(S, x, k): increases value of element x’s key to k. Assume k
≥ x’s current key value.
• Example max-priority queue application: schedule jobs on shared computer.

Copyright © 2021, ABES Engineering College


Priority queue

Min-priority queue supports similar operations:


• INSERT(S, x): inserts element x into set S.
• MINIMUM(S): returns element of S with smallest key.
• EXTRACT-MIN(S): removes &returns element of S with smallest key.
• DECREASE-KEY(S, x, k): decreases value of element x’s key to k.
Assume k ≤ x’s current key value.

Example min-priority queue application: Event - driven simulator.

Copyright © 2021, ABES Engineering College


Priority queue Operations:
Finding the maximum element
Getting the maximum element is easy: it’s the root.
HEAP-MAXIMUM(A)
return A[1]

Time: (1).

Copyright © 2021, ABES Engineering College


Priority queue Operations:
Extracting max element: Given the array A:
• Make sure heap is not empty.
• Make a copy of the maximum element (the root).
• Make the last node in the tree the new root.
• Re-heapify the heap, with one fewer node.
• Return the copy of the maximum element.
HEAP-EXTRACT-MAX(A, n)
if n < 1
then error .heap underflow.
max ← A[1]
A[1] ← A[n]
MAX-HEAPIFY(A, 1, n − 1) remakes heap
return max

Copyright © 2021, ABES Engineering College


Priority queue Operations:

HEAP-INCREASE-KEY(A,i,key)
1. If key<A[i]
2. error” new key is smaller than the current key”.
3. A[i]= key
4. While i>1 and A[parent(i)]<A[i]
5. swap(A[parent(i)], A[i])
6. i=parent(i)
The running time of HEAP-INCREASE-KEY(A,i,key) is

Copyright © 2021, ABES Engineering College


Priority queue Operations:
 MAX-HEAP-INSERT(A,key)
1. A.heap-size= A.heap-size+1
2. A[heap-size]=-
3. HEAP-INCREASE-KEY(A,heap-size,key)
The running time of MAX-HEAP-INSERT(A,key) is .

Copyright © 2021, ABES Engineering College


Copyright © 2021, ABES Engineering College

You might also like