0% found this document useful (0 votes)
59 views5 pages

Homework 4

The document summarizes the student's homework on heap data structures. It includes problems on building a max heap from an array, relationships between parent and child nodes in a d-ary heap, calculating the height of a d-ary heap, sorting elements using a d-ary heap, and analyzing the optimal number of children for each node to minimize comparisons. Diagrams of heaps are provided to illustrate steps of sifting max elements and building the heap. Formulas are derived and an experiment is presented to determine that 2, 3, or 4 children per node require the fewest comparisons for sorting.

Uploaded by

Ebmesem
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)
59 views5 pages

Homework 4

The document summarizes the student's homework on heap data structures. It includes problems on building a max heap from an array, relationships between parent and child nodes in a d-ary heap, calculating the height of a d-ary heap, sorting elements using a d-ary heap, and analyzing the optimal number of children for each node to minimize comparisons. Diagrams of heaps are provided to illustrate steps of sifting max elements and building the heap. Formulas are derived and an experiment is presented to determine that 2, 3, or 4 children per node require the fewest comparisons for sorting.

Uploaded by

Ebmesem
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/ 5

Homework 4

Brian Mesembe

March 5, 2018

Problem 1
(a) (a) The diagram below shows the heap

Figure 1: Heap as tree


(b) The array is:

Figure 2: Heap as array


(c) The heap creation used 12 comparisons.

(b) (a) Array from (a)

Figure 3: First sift. Heap as array: A[n=9] = 90;


The first sift happened after 12 comparisons
(b) The second largest element is sifted to the (n-1) position. It took 4 comparisons.

Figure 4: Second sift. Heap as array: A[n-1=8] = 80;

1
(c) The third largest element is sifted to the (n-2) position. It took 4 comparisons to sift down
this element.

Figure 5: Third sift. Heap as array: A[n-2=7] = 70;


(d) The fourth largest element is sifted to the (n-3) position. It took 4 comparisons to sift
down this element.

Figure 6: Fourth sift. Heap as array: A[n-3=6] = 60;


(e) The fifth largest element is sifted to the (n-4) position. It took 4 comparisons to sift down
this element.

Figure 7: Fifth sift. Heap as array: A[n-4=5] = 50;


(f) The sixth largest element is sifted to the (n-5) position. It took 2 comparisons to sift down
this element.

Figure 8: Sixth sift. Heap as array: A[n-5=4] = 40;


(g) The seventh largest element is sifted to the (n-6) position. It took 2 comparisons to sift
down this element.

Figure 9: Seventh sift. Heap as array: A[n-6=3] = 30;


(h) The total number of comparisons excluding heap creation is: 20

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.

(c) The parent of the child stored at i is (i-1)/d


Heaping elements is about putting them into groups of size d and organizing them in a hierarchy
of height logd n.
Suppose we experiment with the counting numbers and with a binary heap structure. The
picture below is a mininum heap of 7 elements.

Figure 10: A minimum heap of size 7

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

The parent column shows a pattern as the numbers increase from 1 to 3.


The leftmost children for all the parents are even and they increase, ever so uniformly.
The rightmost column are the odd numbers in increasing order. Interestingly, these values
represent the indices of a 1-based array. And, arithmetically, the parent column represents the
result of applying integer divisions to the children column. That is, 22 = 1, 32 = 1. Hence, in
dividing a sorted sequence of 1...n into groups of size 3, the parents are identical to the 2i .

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

(d) The height of a d-ary heap of n elements is: logd n.


Let the root be located at level i=0. Each level i has at most di elements. Suppose h is the
height of the tree.
n
Let dh
= 1, where h is the height and 1 is a constant.

⇒ 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.

(f) Statistics about the sorting using a d-ary heap

(a) n elements to sort


(b) Height of tree is logd n
(c) There are d comparisons per level
(d) Each element travels at most logd n times.
(e) Hence, there will be d · nlogd (n)
(f) The higher order term is: n · logd (n)

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.

You might also like