323 Lecture Notes 5
323 Lecture Notes 5
323 Lecture Notes 5
Transform-and-Conquer: Heap
Sort
CMPE 323 Algorithms
3
Representation
4
Heap Sort
Height of a node: Number of edges on the longest simple
downward path from the node to a leaf
Height of the heap = Height of the root
Height of an element heap =
Basic procedure to be used in heapsort algorithm:
1. BUILD-MAX-HEAP: to produce max-heap from an
Transform
unordered input array
2. MAX-HEAPIFY: to maintain the max-heap property
Conquer
3. HEAPSORT: sorts array in place
5
MAX-HEAPIFY
6
MAX-HEAPIFY
7
Analysis of MAX-HEAPIFY
Worst case analysis of MAX-HEAPIFY
Running time for the algorithm = Step + Step
1. Time to fix up the relationships among the elements , , =
8
Analysis of MAX-HEAPIFY
Worst case example for Step 2: Unbalanced tree in favour of
left sub-tree 1
(𝑛−1)
𝑛 2
9
Analysis of MAX-HEAPIFY
10
BUILD-MAX-HEAP
Building a heap:
Convert an array into a max-heap
Use MAX-HEAPIFY in bottom-up manner
No need to MAX-HEAPIFY the leaf nodes, as required they are
already replaced with non-leaf nodes
11
BUILD-MAX-HEAP
12
BUILD-MAX-HEAP
13
Correctness of BUILD-MAX-HEAP
Correctness of BUILD-MAX-HEAP: (Loop Invariant)
At the start of each iteration of the for loop of Lines 2-3, it is
guaranteed that each node is the root of a max-
heap
Show that the invariant is true: Before loop iteration, during loop
execution and when loop terminates
14
Correctness of BUILD-MAX-HEAP
1. Initialization: . Each node
is a leaf then the root of a trivial max-heap
2. Maintenance: MAX-HEAPIFY() makes node a max-heap
root and it does not damage the max-heap root property of
other nodes . Thus, the loop invariant does
not change
3. Termination: When , each node is the root
of a max-heap
15
Analysis of BUILD-MAX-HEAP
Worst case analysis of BUILD-MAX-HEAP
An upper bound for BUILD-MAX-HEAP: call for MAX-HEAPIFY
whose running time was . So,
Is it possible to get a tighter upper bound ? YES
Assume we have an element heap then its height is
At any height the number of nodes is at most
16
Analysis of BUILD-MAX-HEAP
Then, the total cost for BUILD-MAX-HEAP:
make a sloppiness so,
because
Result: A max-heap for an unordered array can be build in
linear time .
17
Heap Sort
HEAP-SORT:
1. Call BUILD-MAX-HEAP()
2. is the largest value then put it into the correct position i.e. to
the end and put the current last element to the first position
3. Update to
4. Call MAX-HEAPIFY() since the heap is corrupted because of
the new value at the root
5. Go back to step 2 and repeat until all nodes are processed
18
Heap Sort
19
Heap Sort
20
Priority Queues
Running time for HEAPSORT is since,
BUILD-MAX-HEAP takes time
We have calls to MAX-HEAPIFY() each takes times
Priority Queues:
Priority queue maintains a set of elements each holding an
associated key value as well
Use heap to implement priority queue
For max-priority queue use max-heap, for min-priority queue
use min-heap
21
Priority Queues
Priority queue supports the following operations:
1. INSERT(): Inserts element to the set , i.e.
2. MAXIMUM(): Returns the element of with the largest key
3. EXTRACT-MAX(): Removes and returns the element of with
the largest key
4. INCREASE-KEY(): Increases the value of element ’s key to the
new value , which is assumed to be at least as large as ’s
current key value
22
Priority Queues
Max-priority queue model e.g. Job scheduling in shared computer
Element x : Job
’s key value : Job’s priority
INSERT(): To insert new job to the queue
EXTRACT-MAX(): Job selection for execution based on priority
23
Priority Queues
Min-priority queue model e.g. Event driven simulator
Element x : Event to be simulated
’s key value : Time of occurrence of event
INSERT(): To add to the queue as a new event
EXTRACT-MIN(): The next event to be simulated
DECREASE-KEY() and MINIMUM() operations are also valid
24
Priority Queues
25
Priority Queues
26
Priority Queues
27
Priority Queues
28
Priority Queues
29
Analysis of Priority Queue Operations
Analysis of Priority Queue operations:
HEAP-MAXIMUM() :
HEAP-EXTRACT-MAX() :
HEAP-INCREASE-KEY() : since path length can be at most
MAX-HEAP-INSERT-KEY() :
In general, a heap can support any Priority Queue operation on a set of
size in time
30