0% found this document useful (0 votes)
3 views6 pages

BoGE Algo TD3 Solution

The seminar focuses on binary heaps, discussing properties such as height and depth, and demonstrating the minimum and maximum number of elements in a heap of height h. It covers the heapify algorithm, its time complexity, and how to build a max-heap from an unsorted array. Additionally, it provides proofs and pseudocode for various operations related to heaps.

Uploaded by

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

BoGE Algo TD3 Solution

The seminar focuses on binary heaps, discussing properties such as height and depth, and demonstrating the minimum and maximum number of elements in a heap of height h. It covers the heapify algorithm, its time complexity, and how to build a max-heap from an unsorted array. Additionally, it provides proofs and pseudocode for various operations related to heaps.

Uploaded by

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

Algorithms

Seminar 3 - Heaps

This seminar aims at playing around with and demonstrate some of the properties of the binary
heap. Throughout the rest of this seminar, we will rely on the following definitions:

1. The height of an element in the heap is the number of edges on the longest simple downward
path from the element to a leaf of the tree.

2. The depth of an element is the number of edges on the longest simple upward path from the
element to the root.

The depth and the height of a heap are equal.


1. What are the minimum and maximum number of elements in a heap of height h, as a function
of h? Demonstrate this property.
Hint: For the maximum, find a relationship between the level depth and the number of
elements on that level first.

Answer
The minimum number of elements is reached when there is a single element on the last
level. Conversely, the maximum is reached when all of the levels are full. Drawing a
few heaps should be enough to make the following obervations:

• nmin = 2h .

• nmax = 2h+1 − 1.

We use a proof by induction: first, let us prove that Hhmax : “nmax = 2h+1 − 1” is true
for any h.
H0max is obvious, hence we assume Hhmax true for a fixed h ∈ N.

First, we can notice that the number of elements on a full level is 2i , where i is the
depth of the level. This is easily proved by induction, so we will consider it true for
any level.

Then, we say that the maximum number of elements in a heap of height h + 1 happens
when the h + 1th level is full. The total number of elements in this heap is thus equal
to the number of elements of the heap of height h plus the number of elements on
the last level, which yields 2h+1 −1+2h+1 = 2·2h+1 −1 = 2h+2 −1. Hh+1
max
is therefore true.

To prove the result for nmin , we assume a heap of height h to which we append a single
element. The newly constructed heap has a height h + 1. The number of elements in
this new heap is the sum of the number of elements in the full heap of height h plus 1.

1
Using the previous demonstration, we have 2h+1 − 1 + 1 = 2h+1 elements in the heap,
which ends the proof. 

2. Show that the height of a heap that contains n elements is blog2 nc.

Answer
Using the answer to the previous question, we can write:

2h ≤ n ≤ 2h+1 − 1 < 2h+1


h ≤ log2 n < h + 1

which immediately yields h = blog2 nc 

3. When looking at an element in a heap, what can be said about the subtree whose root is this
element?

Answer
It is also a heap.

We now assume a max-heap containing n elements.

16

4 10

14 7 9 3

2 8 1

Figure 1: A max-heap whose property is violated by a misplaced value (in blue).

4. Figure 1 shows a max-heap with a misplaced value. Draw the successive trees which corre-
spond to each step of the heapify algorithm.

Answer
Following the heapify-down algorithm (swap the misplaced element with its child of
highest value), 4 and 14 must be swapped, then 4 and 8.

5. Based on your previous answer, write a pseudocode algorithm for the HEAPIFY-DOWN(A,
n, i) function which is used to recover the heap property in a max-heap. The function takes
as input an array A of size n and an index i which corresponds to the node that violates the
heap property.
Hint: Use a recursive approach.

2
Answer

Algorithm 1: HEAPIFY-DOWN(A, n, i)
1 if 2i ≤ n and A[2i] > A[i] then
2 largest = 2i
3 else
4 largest = i
5 if 2i + 1 ≤ n and A[2i + 1] > A[largest] then
6 largest = 2i + 1
7 if largest 6= i then
8 Exchange A[i] with A[largest]
9 HEAPIFY-DOWN(A, n, largest)

6. Using a simple argument, show that the heapify operation has a worst-case time complexity
of O(log n).

Answer
The worst-case scenario for the heapify operation happens when the misplaced element
is at the top of the heap. The number of swaps is then equal to the height of the heap,
which is blog nc.

7. Based on the previous questions, devise a way to build a max-heap from an unsorted array.
Write the corresponding BUILD-MAX-HEAP(A) algorithm in pseudo-code.

Answer
The global idea behind this algorithm is to consider the array as a complete binary
tree missing only the heap property. The HEAPIFY-DOWN algorithm will therefore
be called on the elements of the tree, starting from the leafs, to recover the property:
Algorithm 2: BUILD-MAX-HEAP(A, n)
1 for i = b c to 1 do
n
2
2 HEAPIFY-DOWN(A, n, i)

8. State a loop invariant for this algorithm, then proceed to show the algorithm is correct.

Answer
The loop invariant is the following one:

“At the start of each iteration of the for loop, each node i + 1, i + 2, ..., n
is the root of a max-heap.”

Initialization
Prior to the first iteration, i = b n2 c, therefore each i + 1, i + 2, ... is a leaf of the tree,
hence the root of a single-element tree, which is a max-heap.

Maintenance
The children of element i are max-heaps according to the loop invariant, which is the
only condition to call HEAPIFY-DOWN(A, n, i). Since HEAPIFY-DOWN(A, n, i)

3
keeps elements i + 1, i + 2, ... as roots of max-heaps, decrementing i in the update
reestablishes the loop invariant before the next iteration.

Termination
At the end of the loop, i = 0, and the loop invariant tells us that nodes 1, 2, ... are roots
to a max-heap. In particular, the first element of the array is the root of a max-heap.


9. What simple upper-bound can we derive for the worst-case time complexity?

Answer
A simple yet overestimated bound can be obtained by considering that we apply the
heapify operation to each element in the array. Since the operation takes O(log n) time,
and there are n elements in the array, we obtain a time complexity of O(n log n).

10. Show that the number of nodes nh at a given level of height h is at most d 2h+1
n
e

Answer
Again, we will use a proof by induction.

In the array representation of the heap, leaves are indexed by b n2 c + 1, ..., n, which
means there are d n2 e leaves in total. If all the leaves are on the bottommost level, we
then have that n0 = d n2 e, which validates the base case.

Assuming the property holds for a fixed h, we check what happens on level h + 1.
If there is an even number of elements on level h, then there are half as many elements
on level h + 1, thus
nh l nh m
nh+1 = =
2 2
If there is an odd number of elements on level h, then all of these elements are the
children of an element from h + 1 with the exception of a single one. This yields:
jn k ln m
h h
nh+1 = +1=
2 2
Finally,
ln m
h
nh+1 =
 2l 
1 n m

2 2h+1
l n m
= h+2
2
which ends the demonstration 

11. Based on the previous questions, show that this algorithm has a worst-case time complexity
of O(n).

4
Hint: Use the previous answer to bound the number of nodes at each level, and compute the
sum of operations performed on all of them.

Answer
The first question to answer is: what is the time complexity of the heapify operation
as a function of h? The answer is: O(h) (since h = blog2 nc).

Second question: on how many elements will the heapify operation be applied for a
given level?
The answer was given to the previous question: 2h+1 .
 n 

It follows that, for each level, the time complexity is 2h+1 O(h). Summing on all levels
 n 

give (replacing the O(·) notation with a constant c):


blog nc l blog nc
X n m X n
ch ≤ ch
h=0
2h+1 h=0
2h

blog nc
X h
= cn
h=0
2h

X h
≤ cn
h=0
2h

Method 1: Bounding h
3 h

∀h ≥ 0, 32 ≥ h, so 22h = , hence:
h 3 h
 h
4
≥ 2h

∞ ∞  h
X h X 3
h

h=0
2 h=0
4
1
= 3 =4
1− 4

Method 2: using uniform convergence (ask you math teacher)


Letting fh : x 7→ xh , with fh0 (x) = hxh−1 , we can see that h
2h
= 12 fh0 ( 12 )
We know that:

∞ ∞
!0
X X
∀x ∈ ]−1, 1[ , fh0 (x) = fh (x)
h=0 h=0
(uniform convergence on ]−1, 1[)
 0
1 1
= =
1−x (1 − x)2

Hence,
∞ ∞
X h 1X 0 1 1 1
= fh ( ) =  =2
h=0
2h 2 h=0 2 2 1− 1 2
2

5
Finally, we have
blog nc l
n m
with d = 2c or d = 4c
X
ch ≤ dn
h=0
2h+1
= O(n) 

You might also like