Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 43

nalysis of

lgorithms

National University of Colombia


College of Engineering
Department of Computer Systems Engineering
OUTLINE

 Heaps
 Heap Sort
 Priority Queues
Heap Sort
INTRODUCTION
In this chapter we introduce another
sorting algorithm which combines
the better attributes of insertion sort
and merge sort algorithms.
Heap sort also introduces another
algorithm desingn thechnique: the
use of a data structure called heap,
to manage information during the
execution of the algorithm.
HEAPS

The (binary) heap data structure


is an array object that can be
viewed as a nearly complete
binary tree.
Each node of the tree
corresponds to an element of
the array that stores the value
in the node.
BINARY TREE1
16
2 3
14 10
4 5 6 7
8 7 9 3
8 9 10
2 4 1

1 2 3 4 5 6 7 8 9 10
16 14 10 8 7 9 3 2 4 1
HEAPS
An array A that represents a heap is
an object with two attributes:

• length[A]: the number of elements


in he array.
• Heap-size[A]: the number of
elements in the heap
stored within array A
HEAPS
The root of the tree is A[1], and given the
index i of a node, the indices of its parent
Parent(i), left child Left(i), and right child
Right(i) can be computed
 i / 2 simply:

 
• Parent(i): return i / 2
• Left(i): return 2i.
• Right(i): return 2i +1.
HEAPS
There are two kinds of binary heaps:
Max-heaps and min-heaps.
In both kinds, the values in the nodes
satisfy a heap property, the specifics
of which depend on the kind of heap.
HEAPS
In a max-heap, the max- heap property
is that for every node i other than the Root,

A[Parent(i)]  A[i]
That is, the value of a node is at most the
value of its parent. Thus the largest element
in a max-heap is stored at the root, and the
Subtree rooted at a node contains values no
larger than the contained at the node itself.
HEAPS
A min-heap is organized in the opposite way;
The min-heap property is that for every node
i other than the root,

A[Parent(i)]  A[i]
The smallest element in a min-heap is at the
root.
For the heapsort algorithm, we use
max-heaps.
HEAP PROPERTIES
Viewing a heap as a tree, we define
the height of a node in a heap to be
the number of edges on the longest
simple downward path from the node
to a leaf, and we define the height of
the heap to be the height of its root.
Since a heap of n elements is based on
a complete binary tree , its height is
 (lg n).
HEIGHT

1 Height
16 3

2 3
14 10 2
4 5 6 7
8 7 9 3 1
8 9 10
2 4 1 0
BASIC PROCEDURES
Here we present five basic procedures and
shows how they are used in a sorting
Algorithm and a priority-queue data structure
• Max-Heapify.
• Build-Max-Heap.
• HEAPSORT.
• Max-Heap-Insert, Heap-Extract-Max,
Heap-increase-key, and Heap-Maximum.
MAINTAINING THE
MAINTAINING THE
HEAP PROPERTY
HEAP PROPERTY
MAX-HEAPIFY
Max-Heapify is an important subroutine for
Manipulating max-heaps. Its inputs are an
array A and an index i into the array.
When Max-Heapify is called, it is assumed
that the binary trees rooted at LEFT(i) and
RIGHT(i) are max-heaps, but that A[i] may be
smaller than its children, thus violating
the max-heap property. The function of
Max-Heapify is to let the value at A[i] “float
down” in the max-heap so that the subtree
rooted at index i becomes a max-heap.
MAX-HEAPIFY
Max-Heapify(A,i)
1 l LEFT(A,i)
2 r RIGHT(i)
3 If l  heap-size[A] and A[l]  A[i]
4 then largest l
5 else largest i
6 If r  heap-size[A] and A[r] > A[largest]
7 then largest r
8 If largest  i
9 then exchange A[i]  A[largest]
10 Max-Heapify(A,largest)
MAX-HEAPIFY(A,2)
11
16
16
22 33
14
4
14 10
10
44 55 66 77
14
48
4 77 99 33
88 99 10
10
22 84
8 11
MAX-HEAPIFY
RUNNING TIME
The running time of Max-Heapify on a
Subtree of size n rooted at given node
i is the  (1) time to fix up the
relationships among the elements A[i],
A[LEFT(i)], and A[RIGHT(i)], plus the
time to run Max-Heapify on a subtree
rooted at one of the children of node i.
MAX-HEAPIFY
RUNNING TIME
The children´s subtrees each have size at
most 2n/3 –-the worst case occurs when the
last row of the tree is exactly half full--- and
the running time of Max-Heapify can be
described by the recurrence:

T(n)  T(2n /3) + (1). n/3


2n/3
The solution of this recurrence, by case 2 of
the master theorem is T(n) = O(lg n).
BUILDING A HEAP
BUILD-MAX-HEAP
We can use the procedure Max-Heapify
in a bottom-up manner to convert an
array A[1..n], where n = length[A], into
a max-heap.
The elements in the subarray A[(  n / 2 +1)..n]
Are all leaves of the tree, and so each is a
1-element heap to begin with. The procedure
Build-Max-Heap goes through the remaining
nodes of the tree and runs Max-Heapify on
each one.
BUILD-MAX-HEAP
Build-Max-Heap(A)
1 Heap-size[A] length[A]
2 for i  length[ A] / 2 downto 1
3 do Max-Heapify(A,i)
BUILD-MAX-HEAP
11
16
44
22 33
16
14
11 10
33
44 55 66 77
14
14
28
2 16
167
1 99 10
10
3
88 99 10
10
14
14
22 84
8 71
7
BUILD-MAX-HEAP LOOP
INVARIANT
To show why Build-Max Heap works
correctly, we use the following loop
Invariant:

“At the start of each iteration of the for


loop
of lines 2-3, each node i+1, i+2....n is
the root
of a max heap.”
BUILD-MAX-HEAP LOOP
INVARIANT
Initialization:
 
Prior to the first iteration of the loop, i = n / 2
   
Each node n / 2 +1, n / 2 + 2....n is a leaf
and is thus the root of a trivial Max-heap.
Maintenance:
By the loop invariant the children of node i
are both roots of max-heaps. This is precisely the
condition required for the call Max-Heapify(A,i) to
make node i a max-Heap root. Decrementing i in the
for loop update reestablishes the loop invariant for the
next iteration.
BUILD-MAX-HEAP LOOP
INVARIANT
Termination:
At termination , i = 0. By the loop
invariant , each node 1, 2, ...,n is the
root of a max-heap. In particular, node
1 is.
BUILD-MAX-HEAP
RUNNING TIME
Our analysis relies on the properties that an
n-element heap has height  
lg n and at
most  
n / 2 h 1 nodes of any height h.
So we can express the total cost of Build-
Max-Heap as:

  lg n   n    n  lg n  h 
h  0  2 h 1  O(h)  =   h  0 h 
    2 2 
BUILD-MAX-HEAP
RUNNING TIME
Where,
h 1/ 2
 h  0 2 h  1  1 / 2  2  2

Thus, the running time of Build-max-Heap


can be bounded as

 n  lg n  h  n 
 h 0 h    2   (n)
2 2  2 
THE HEAPSORT
ALGORITHM
THE HEAPSORT
ALGORITHM
The heapsort algorithm starts by using
Build-MaxHeap to build a max-heap on
the imput array A[1..n], where
n = length[A]. Since the maximum
element of the array is stored at the
root A[1], it can be put into its correct
final position by exchanging it with
A[n].
THE HEAPSORT
ALGORITHM
If we now “discard” node n from the
heap ( by decrementing heap-size[A] ),
we observe that A[1..(n-1)] can easily
be made into max-heap. The children
of the root remain max-heaps, but the
new root element may violate the max-
heap property. All that is needed to restore
the max-heap property, however, is one call
to Max-Heapify(A,1), which leaves a max-
heap in A[1..(n-1)].
THE HEAPSORT
ALGORITHM
The heapsort algorithm then repeats
this process for the max-heap of size
n-1 down to a heap of size 2.

HEAPSORT(A)
1 Build-Max-Heap(A)
2 For i length[A] downto 2
3 do exchange A[1]  A[i]
4 heap-size[A] heap-size[A] – 1
5 Max-Heapify(A,1)
HEAPSORT RUNNING
TIME
The heapsort procedure takes time
 (n lgn), since the call to Build-Max-
Heap takes time (n) and each of the
n –1 calls to Max-Heapify takes time
 (lg n).
Heapsort Applet

Taken from

http://www2.hawaii.edu/~copley/665/
PRIORITY QUEUES
PRIORITY QUEUES
Now we present one of the most
popular applications of a heap : its use
as an efficient priority queue. As with
heaps, there are two kinds of priority
queues: max-priority queues and min-
priority queues. We will focus here on
how to implement max-priority queues,
which are in turn based on max-heaps.
PRIORITY QUEUES
A priority queue is a data structure for
maintaining a set S of elements, each
with an associated value called a key.
A max-priority queue supports the
following operations:
• Insert(S,x) inserts the element x into S.
• Maximum(S) returns the element of S with
the largest key.
• Extract-Max(S) removes and returns the element of

S with the largest key.


• Increase-Key(S,x,k) increases the value of element
x´s key to the new value k,
which is assumed to be at least
as large as x´s current key value.
HEAP MAXIMUM
The procedure Heap-Maximum
implements the Maximum operation
in  (1) time.

Heap-Maximum(A)
Return A[1];
HEAP-EXTRACT-MAX
The procedure Heap-Extract-Max implements
the extract-Max operation.

Heap-Extract-Max(A)
1 If heap-size[A] < 1
2 then error “heap underflow”
3 Max A[1]
4 A[1] A[heap-size[A]]
5 Heap-size[A] heap-size[A] -1
6 Max-Heapify(A,1)
7 return max

The running time of Heap-Extract-Max is  (lg n),


since it performs a constant amount of work on top of
the O(lg n) time for Max-Heapify.
HEAP INCREASE KEY
Heap-increase-key(A,i,key)
1 If key < A[i]
2 then error “new key is smaller than current key”
3 A[i] key
4 While i > 1 and A[PARENT(i)] < A[i]
5 do exchange A[i]  A[PARENT(i)]
6 i PARENT(i)

The running time of Heap-Increase-Key on an


n-element heap is (lg n), since the path traced
from the node updated in line 3 to the root has
length  (lg n).
MAX-HEAP-INSERT
Max-Heap-Insert(A,key)
1 heap-size[A] heap-size[A] + 1
2 A[heap-size[A]] -
3 Heap-Increase-Key(A,heáp-size[A],key)

The running time of Max-Heap-Insert on an


n-element heap is  (lg n).
THE END

You might also like