0% found this document useful (0 votes)
5 views

DAA - Module 3 Heap sort

The document discusses the transform-and-conquer approach, focusing on heaps and heap sort. It defines heaps as partially ordered data structures suitable for priority queues, detailing their properties, construction methods, and deletion processes. The heap sort algorithm is explained as a two-stage process involving heap construction and maximum deletions, with an efficiency analysis indicating a time complexity of Θ(n log n).

Uploaded by

cadono3270
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DAA - Module 3 Heap sort

The document discusses the transform-and-conquer approach, focusing on heaps and heap sort. It defines heaps as partially ordered data structures suitable for priority queues, detailing their properties, construction methods, and deletion processes. The heap sort algorithm is explained as a two-stage process involving heap construction and maximum deletions, with an efficiency analysis indicating a time complexity of Θ(n log n).

Uploaded by

cadono3270
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Module – 3 ADA – Heaps and Heap sort

Transform and Conquer Approach


We call this general technique transform-and-conquer because these methods work as two-
stage procedures. First, in the transformation stage, the problem’s instance is modified to be,
for one reason or another, more amenable to solution. Then, in the second or conquering stage,
it is solved.
There are three major variations of this idea that differ by what we transform a given instance
to (Figure 6.1):
 Transformation to a simpler or more convenient instance of the same problem—we call
it instance simplification.
 Transformation to a different representation of the same instance—we call it
representation change.
 Transformation to an instance of a different problem for which an algorithm is already
available—we call it problem reduction.

Heaps
Heap is a partially ordered data structure that is especially suitable for implementing priority
queues. Priority queue is a multiset of items with an orderable characteristic called an item’s
priority, with the following operations:

 finding an item with the highest (i.e., largest) priority


 deleting an item with the highest priority
 adding a new item to the multiset
Notion of the Heap
Definition: A heap can be defined as a binary tree with keys assigned to its nodes, one key per
node, provided the following two conditions are met:
1. The shape property—the binary tree is essentially complete (or simply complete),
i.e., all its levels are full except possibly the last level, where only some rightmost leaves
may be missing.
2. The parental dominance or heap property—the key in each node is greater than or
equal to the keys in its children.
Illustration: The illustration of the definition of heap is shown bellow: only the left most tree
is heap. The second one is not a heap, because the tree’s shape property is violated. The left
child of last subtree cannot be empty. And the third one is not a heap, because the parental
dominance fails for the node with key 5.

Properties of Heap
1. There exists exactly one essentially complete binary tree with n nodes. Its height is
equal to ⌊𝑙𝑜𝑔2𝑛⌋
2. The root of a heap always contains its largest element.
3. A node of a heap considered with all its descendants is also a heap.
4. A heap can be implemented as an array by recording its elements in the top down, left-
to-right fashion. It is convenient to store the heap’s elements in positions 1 through n
of such an array, leaving H[0] either unused or putting there a sentinel whose value is
greater than every element in the heap. In such a representation,
a. the parental node keys will be in the first ⌊n/2⌋. positions of the array, while the
leaf keys will occupy the last ⌊n/2⌋ positions;
b. the children of a key in the array’s parental position i (1≤ i ≤⌊𝑛/2⌋) will be in
positions 2i and 2i + 1, and, correspondingly, the parent of a key in position i (2
≤ i≤ n) will be in position ⌊𝑛/2⌋.

Heap and its array representation


Thus, we could also define a heap as an array H[1..n] in which every element in position i in
the first half of the array is greater than or equal to the elements in positions 2i and 2i + 1, i.e.,
H[i]≥max {H [2i], H [2i + 1]} for i= 1. . .⌊𝑛/2⌋
Constructions of Heap - There are two principal alternatives for constructing Heap.
1) Bottom-up heap construction 2) Top-down heap construction

Bottom-up heap construction:


The bottom-up heap construction algorithm is illustrated bellow. It initializes the essentially
complete binary tree with n nodes by placing keys in the order given and then “heapifies” the
tree as follows.
 Starting with the last parental node, the algorithm checks whether the parental
dominance holds for the key in this node. If it does not, the algorithm exchanges the
node’s key K with the larger key of its children and checks whether the parental
dominance holds for K in its new position. This process continues until the parental
dominance for K is satisfied. (Eventually, it has to because it holds automatically for
any key in a leaf.)
 After completing the “heapification” of the subtree rooted at the current parental node,
the algorithm proceeds to do the same for the node’s immediate predecessor.
 The algorithm stops after this is done for the root of the tree.

Illustration
Bottom-up construction of a heap for the list 2, 9, 7, 6, 5, 8. The double headed arrows show
key comparisons verifying the parental dominance.
Analysis of efficiency - bottom up heap construction algorithm:
Assume, for simplicity, that n = 2k− 1 so that a heap’s tree is full, i.e., the largest possible
number of nodes occurs on each level. Let h be the height of the tree.
According to the first property of heaps in the list at the beginning of the section, h=⌊𝑙𝑜𝑔2𝑛⌋
or just ⌊𝑙𝑜𝑔2(𝑛 + 1)⌋= k − 1 for the specific values of n we are considering.
Each key on level I of the tree will travel to the leaf level h in the worst case of the heap
construction algorithm. Since moving to the next level down requires two comparisons—one
to find the larger child and the other to determine whether the exchange is required—the total
number of key comparisons involving a key on level I will be 2(h − i).
Therefore, the total number of key comparisons in the worst case will be

where the validity of the last equality can be proved either by using the closed-form formula

for the sum or by mathematical induction on h.


Thus, with this bottom-up algorithm, a heap of size n can be constructed with fewer than 2n
comparisons.
Top-down heap construction algorithm:
It constructs a heap by successive insertions of a new key into a previously constructed heap.
1. First, attach a new node with key K in it after the last leaf of the existing heap.
2. Then shift K up to its appropriate place in the new heap as follows.
a. Compare K with its parent’s key: if the latter is greater than or equal to K, stop (the
structure is a heap); otherwise, swap these two keys and compare K with its new
parent.
b. This swapping continues until K is not greater than its last parent or it reaches root.
Obviously, this insertion operation cannot require more key comparisons than the heap’s
height. Since the height of a heap with n nodes is about log2n, the time efficiency of insertion
is in O(log n).
Illustration of inserting a new key: Inserting a new key (10) into the
heap is constructed bellow. The new key is shifted up via a swap with its
parents until it is not larger than its parents (or is in the root).

Delete an item from a heap: Deleting the root’s key from a heap can be done with the
following algorithm:
Maximum Key Deletion from a heap
1. Exchange the root’s key with the last key K of the heap.
2. Decrease the heap’s size by 1.
3. “Heapify” the smaller tree by sifting K down the tree exactly in the same way we did it
in the bottom-up heap construction algorithm. That is, verify the parental dominance
for K: if it holds, we are done; if not, swap K with the larger of its children and repeat
this operation until the parental dominance condition holds for K in its new position.

Illustration

The efficiency of deletion is determined by the number of key comparisons needed to


“heapify” the tree after the swap has been made and the size of the tree is decreased by 1.Since
this cannot require more key comparisons than twice the heap’s height, the time efficiency of
deletion is in O (log n) as well.

Heap Sort
Heapsort - an interesting sorting algorithm is discovered byJ. W. J. Williams. This is a two-
stage algorithm that works as follows.
Stage 1 (heap construction): Construct a heap for a given array.
Stage 2 (maximum deletions): Apply the root-deletion operation n−1 times to the
remaining heap.
As a result, the array elements are eliminated in decreasing order. But since under the array
implementation of heaps an element being deleted is placed last, the resulting array will be
exactly the original array sorted in increasing order.
Heap sort is traced on a specific input is shown below:

Analysis of efficiency: Since we already know that the heap construction stage of the algorithm
is in O(n), we have to investigate just the time efficiency of the second stage. For the number
of key comparisons, C(n), needed for eliminating the root keys from the heaps of diminishing
sizes from n to 2, we get the following inequality:

This means that C(n) ∈ O(n log n) for the second stage of heapsort. For both stages, we get
O(n) + O(n log n) = O(n log n). A more detailed analysis shows that the time efficiency of
heapsort is, in fact, in Θ(n log n) in both the worst and average cases. Thus, heapsort’s time
efficiency falls in the same class as that of mergesort.
Heapsort is in-place, i.e., it does not require any extra storage. Timing experiments on random
files show that heapsort runs more slowly than quicksort but can be competitive with mergesort.

*****

You might also like