Homework 4
Homework 4
Brian Mesembe
March 5, 2018
Problem 1
(a) (a) The diagram below shows the heap
1
(c) The third largest element is sifted to the (n-2) position. It took 4 comparisons to sift down
this element.
Problem 2
(a) In an array with 0-based indexing, the biggest element of a max heap will be located at position
0. Let d stand for the number of children. Each parent at position i will have it’s d children at
locations {(d*i)+1, (d*i)+2, . . . (d*i)+d}.
2
Let d=2. The leftmost child will be at (2*0)+1 = 1, and the rightmost child is (2*0)+2 = 2
and that’s very accurate.
Suppose that a 2-ary tree is to be extended to (2+1)-ary tree. For the 2-nary heap, the rightmost
element is at position (2*i)+2 and the leftmost element (2*i)+1. Now the extension makes the
rightmost element to reside in position (2*i)+3. We can keep adding new rightmost elements
in this position until we arrive at (2*i)*d.
(b) The leftmost child of the node stored at index i is: (i*d) + 1 As shown in (a) above, the
rightmost child at position (i*d)+d. The element that precedes the rightmost element is
(i*d)+(d-1), and then *(i*d)+(d-2). Since there are *d children, the rightmost (d-1)
elements all succeed the element at position (i*d)+(d-(d-1)) which is (i*d)+1. Hence the
leftmost element is at position (i*d)+1.
The tree above shows the parents and their children. We can collect them as such:
parent children
1 2, 3
2 4, 5
3 6, 7
3
In general, the parent will be di , where i is the index and d is the number of children per group.
Suppose now that we use 0-based array indexing. The table above changes to:
parent children
0 1, 2
1 3, 4
2 5, 6
This is identical to shifting the array leftward by 1. Hence, each parent is decremented by 1.
And the even and odd pattern is maintained, although the leftmost children of each group are
now the odd numbers and the rightmost are now the even numbers.
Due to the left shift, we can simply subract 1 from each parent, but maintain the arithmetic
operations for obtaining parents and children.
i−1
Hence, the root element for any index i is: d
⇒ n = dh
⇒ logd n = hlogd d
⇒ logd n = h
Since h stands the height of the d-ary heap, it means its height is logd (n)
(e) I’d find the maximum element from the d children and then compare with the parent. It requires
d − 1 comparisons to get the largest child and then 1 comparison to promote the largest child,
if it is bigger than parent, or maintain the current parent. Hence, it requires d comparisons. So
I will swap the largest element which is at the first index of the array with the element at the
last index, n.
Next, I’ll restore the heap property for the subarray, A[1..n-1]. The n− elements will form a
heap of height logd (n − 1) and it will require d comparisons for each subheap as the maximum
element finds its way to the top.
Hence, it will take d · logd (n) comparisons.
4
(g) From above, it takes d · n · logd n to sort n elements.
Suppose d is made as large as n.
lim (d · n · logd n) = n · n = n2
d→n
It follows that if d is limited to n, the maximum number of comparisons we can have is n2 . And
this means the heap in question will have 1 level or will be a simple linear list.
From observation, or intuitively, tree branching starts from 2 branches and I’ll limit it to n.
But n2 is a hypothetical worst case when d = n. Hence, the optimal values are between 2 and
n − 1. I’ll now attempt to make the optimal values tighter.
There are n − 3 values between 2 and n, n excluded. And limd→(n−1) (d · n − 1 · logd (n − 1))
reveals an increase of the results towards (n − 1)2 .
This implies that the values of d that are between 2 and n − 1 and that yield the smallest results
will be the optimal values.
The table below shows a sample experiment for n = 10
d d · n · logd n floor(Result)
10 10 · 10 · log1 010 100
9 9 · 10 · log9 10 94
8 8 · 10 · log8 10 88
7 7 · 10 · log7 10 82
6 6 · 10 · log6 10 77
5 5 · 10 · log5 10 71
4 4 · 10 · log4 10 66
3 3 · 10 · log3 10 62
2 2 · 10 · log2 10 66
From the table above, the values of d with the lowest number of comparisons are 2, 3, 4. And
d · n · log2 n is increasing. So increasing n to n + 1 pushes the maximum comparisons as d → n to
(n + 1)2 and the results for d = 2 to d = n will stay thesame.
Hence, my optimal values for d are: 2, 3, 4.