Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CS 124: Data Structures & Algorithms Spring 2014

Lecture 22: March 19


Lecturer: Robert Snapp Scribe: Grace Ahmed

Note: LaTeX template courtesy of UC Berkeley EECS dept.


Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications.
They may be distributed outside this class only with the permission of the Instructor.

22.1 Max-Heapify

Consider the following “heap”:

1
0
2 3
8 17

4 5 5 8 6 4 7 13
8 9 10 11
2 4 1 7

which can be stored in an 11-element, unit-based indexed array as,

i 1 2 3 4 5 6 7 8 9 10 11

A[i] 0 8 17 5 8 4 13 2 4 1 7

Although we can call it a “heap”, it should be evident that this is neither a max heap, nor a min heap.
Although the left and right subtrees of the root node are each max heaps (e.g. A[2] ≥ max(A[4], A[5]), etc.),
the value of the root node is less than the maximum value of its children. Fortunately there is a simple
procedure that will restore the max heap property in these situations: Max-Heapify to the rescue!

22-1
22-2 Lecture 22: March 19

Max-Heapify(A, i)
1 l = LEFT(i) // 2i
2 r = RIGHT(i) // 2i + 1
3 if l ≤ A.heap-size and A[l] > A[i]
4 largest = l
5 else
6 largest = i
7 if r ≤ A. heap-size and A[r] > A[largest]
8 largest = r
9 if i 6= largest
10 swap(A[i], A[largets])
11 Max-Heapify(A, largest)

We begin by calling Max-Heapify(A, 1). During the first invocation l = 2 and r = 3. Lines 3–8 result in
largest = 3. Since the predicate in line 9 is true, the values of A[1] and A[3] are swapped, resulting in the
heap,

1
17
2 3
8 0

4 5 5 8 6 4 7 13
8 9 10 11
2 4 1 7

Continuing with line 11, we recursively call Max-Heapify(A, 3), since largest = 3. In the first recursive
call (with now i = 3), l = 6 and r = 7. Lines 3–8 result it largest = 7. Since i 6= largest, lines 10 and 11
are called again. After swapping A[3] with A[7], the heap looks like,

1
17
2 3
8 13

4 5 5 8 6 4 7 0
8 9 10 11
2 4 1 7

Line 11 results in the recursive call, Max-Heapify(A,7). Now with i = 7 one obtains l = 14 and r = 15.
Since both values exceed the value of A.heap-size = 11, largest is set to i. Consequently the function returns,
and A now satisfies the max-heap property.
Lecture 22: March 19 22-3

22.1.1 Derivation of the Recurrence Relation for Max-Heapify

We will now estimate the asymptotic running time of Max-Heapify using some of the methods that we
studied recently in Chapter 4 of CLRS. Let n denote the number of nodes in a heap, and let h denote the
height.

2 3

4 5 6 7

8 9 10 11

In the above diagram, n = 11 and h = 3, as before. However, it will be helpful to think about the heap in
more general terms. Note that the root node is now colored red. Likewise, the sub-heap rooted at the root
node’s left child (node 2) is colored blue, and the sub-heap rooted at the root node’s right child (node 3) is
colored green. So more generally, if the height of the heap is h, then the height of the left sub-heap is h − 1,
while that of the right sub-heap is either h − 1 or h − 2. The latter is the case that is shown, as it turns out
that this will yield the worst-case situation. In the following, let nl denote the size of the left sub-heap, and
nr , that of the right.
If you examine the function Max-Heapify (on the previous page), you will note that within each call of the
function, lines 1–10 are each executed once, at most, and each incurs at most constant time (Θ(1), we like
to say). Consequently, if we let T (n) denote the cost of running Max-Heapify on a heap of size n, then

T (n) ≤ T (n0 ) + Θ(1)

where n0 represents the size of the largest possible sub-heap that is rooted at either the left or right child.
Since we want to consider the worst case, we will assume that n0 = nl , corresponding to the size of the left
sub-heap of the root.
For the worst case analysis, we assume that the left sub-heap is fully populated. Consequently its size will
be,
h−1
X
nl ≤ 20 + 21 + 22 + · · · + 2h−1 = 2k = 2h − 1 < 2h .
k=0

We will also assume that the right sub-heap has the minimum possible size. (This minimizes the performance
gain one obtains from the divide and conquer heuristic.) Consequently,
h−2
X
nr ≥ 20 + 21 + 22 + · · · + 2h−2 = 2k = 2h−1 − 1.
k=0

The total number of nodes in the original heap satisfies,

n = nl + nr + 1.

Claim: nl ≤ 23 n.
22-4 Lecture 22: March 19

Proof:
nl nl
=
n nl + nr + 1

1
=
nr + 1
1+
nl
1

2h−1 − 1 + 1
1+
2h
1
=
2h−1
1+
2h
1
=
1
1+
2
2
= .
3

Thus we obtain the recurrence relation,


 
2
T (n) ≤ T n + Θ(1).
3

22.1.2 Solution by Master Theorem

By the master theorem, with a = 1, b = 23 , one obtains logb a = log3/2 1 = 0 Since f (n) = Θ(1) = Θ(n0 ),
Case 2 applies. Thus, T (n) = O(lg n).
Note that because of the inequality in the recurrence equation, it is appropriate to use big-O instead of
big-Θ.

You might also like