hw03 Solution PDF
hw03 Solution PDF
hw03 Solution PDF
CSCI-GA.1170-001/Summer 2016
Solution to Homework 3
Problem 1. (1 point) Illustrate the operation of heapsort on the array:
A = (19, 2, 11, 14, 7, 17, 4, 3, 5, 15)
By showing the values in array A after initial heapification and after each call to max-heapify.
Solution:
A
A
A
A
A
A
A
A
A
A
=
=
=
=
=
=
=
=
=
=
[19 , 15 , 17 , 14 , 7, 11 , 4, 3, 5, 2]
[17 , 15 , 11 , 14 , 7, 2, 4, 3, 5, 19]
[15 , 14 , 11 , 5, 7, 2, 4, 3, 17 , 19]
[14 , 7, 11 , 5, 3, 2, 4, 15 , 17 , 19]
[11 , 7, 4, 5, 3, 2, 14 , 15 , 17 , 19]
[7 , 5, 4, 2, 3, 11 , 14 , 15 , 17 , 19]
[5 , 3, 4, 2, 7, 11 , 14 , 15 , 17 , 19]
[4 , 3, 2, 5, 7, 11 , 14 , 15 , 17 , 19]
[3 , 2, 4, 5, 7, 11 , 14 , 15 , 17 , 19]
[2 , 3, 4, 5, 7, 11 , 14 , 15 , 17 , 19]
#
#
#
#
#
#
#
#
#
#
After
After
After
After
After
After
After
After
After
After
initial heapification
extracting 19
extracting 17
extracting 15
extracting 14
extracting 11
extracting 7
extracting 5
extracting 4
extracting 3
C
C
C
C
=
=
=
=
0
[0 ,
[1 ,
[1 ,
[0 ,
1
0,
1,
2,
1,
2
0,
0,
2,
2,
3
0,
2,
4,
2,
4
0,
1,
5,
4,
5
0,
4,
9,
5,
6
0]
1]
10]
9]
#
#
#
#
After
After
After
After
initialization
counting elements
computing running sum
producing sorted B
1 2 3 4 5 6 7 8 9 10
B = [0 , 1, 3, 3, 4, 5, 5, 5, 5, 6] # Sorted B
Problem 3. (1 point) Illustrate the operation of radix sort on the array:
A = (392, 517, 364, 931, 726, 912, 299, 250, 600, 185)
By showing the values in array A after each intermediate sort.
Solution:
A = [392 ,
517 ,
364 ,
[250 ,
600 ,
931 ,
[600 ,
912 ,
517 ,
[185 ,
250 ,
299 ,
1
931 ,
726 ,
912 ,
299 ,
250 ,
600 ,
185]
->
392 ,
912 ,
364 ,
185 ,
726 ,
517 ,
299]
->
726 ,
931 ,
250 ,
364 ,
185 ,
392 ,
299]
->
364 ,
392 ,
517 ,
600 ,
726 ,
912 ,
931]
B = {0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
[0.02]
[0.18]
[0.23 , 0.25]
[]
[0.49]
[0.56 , 0.57]
[0.69]
[0.74]
[0.88]
[]}
Problem 5. (3 points) Consider a d-ary heap a generalization of a binary heap, in which all
internal nodes (with at most one exception) have d children. Design a method to store a d-ary
heap in an array and derive the expressions for:
(a) Parent of i-th node in a d-ary heap.
(b) j-th child of i-th node in a d-ary heap.
(c) Maximum number of nodes of height h in any n-element d-ary heap.
(d) Maximum height of any n-element d-ary heap.
Solution:
(a) Parent of i-th node in a d-ary heap.
(b) j-th child of i-th node in a d-ary heap.
Assuming 1-based indexing, we can extend the standard level-wise method of storing
binary heaps to d-ary heaps by taking:
i1
,
par ent(i) =
d
child(i, j) = d(i 1) + j + 1.
2
n1
.
n0 = n
d
Removing all leaf nodes from the heap of size n would result in a heap of size n0 :
n1
0
n = n n0 =
d
0
with n0 leaves:
0
n 1
0
0
n0 = n
d
n1
1
n1
d
=
d
d
n1
n1d
=
.
d
d2
Removing all leaf nodes from the heap of size n0 would result in a heap of size n00 :
n00 = n0 n00
n1
n1d
n1
+
=
d
d
d2
n1d
=
d2
with n000 leaves:
n00 1
=n
d
n1d
1
n1d
d2
=
d2
d
n1d
n 1 d d2
,
=
d2
d3
suggesting a general expression for a number of leaves in a heap obtained from a heap
of size n by removing all leaf nodes k times:
n 1 d ... d k1
n 1 d ... d k
(k)
n0 =
dk
d k+1
Pk
Pk1 i
n i=0 d
n i=0 d i
=
dk
d k+1
k
k+1
1
n dd1
n d d11
=
for d > 1.
dk
d k+1
n000
00
We now observe that the leaves in heap H 0 , obtained from heap H by removing all leaf
nodes, had height 1 in the original heap H. The leaves in heap H 00 , obtained from heap
H 0 by removing all leaf nodes, had height 1 in H 0 and height 2 in H. In general, nh the
number of nodes of height h in an n-element d-ary heap H is the number of leaves in
H (h) , obtained from H by removing all leaf nodes h times:
h+1
h
1
n d d11
n dd1
for d > 1.
nh =
dh
d h+1
(d) Maximum height of any n-element d-ary heap.
We notice that a maximum number of nodes in a d-ary heap of height h is observed when
the last level is full, and a minimum number of nodes when the last level has only one
node:
d h+1 1
for d 6= 1,
d 1
dh 1
nmin (h) = d 0 + d 1 + ... + d h1 + 1 =
+ 1 for d 6= 1.
d 1
This allows us to provide a tight bound on the size of a d-ary heap of height h (we assume
d 6= 1 for the rest of this problem):
d h+1 1
dh 1
+1 n
.
d 1
d 1
With integer n we can get rewrite and then reduce the inequality as follows:
dh 1
d h+1 1
<n
,
d 1
d 1
d h 1 < n(d 1) d h+1 1,
d h < n(d 1) + 1 d h+1 ,
h < logd (n(d 1) + 1) h + 1.
For integer n and real x:
n 1 < x n iff
n = dxe,
and so:
h = logd (n(d 1) + 1) 1.
Problem 6. (4 points) Consider a min-priority queue representing a set of integers and supporting the following operations: insert(k) to insert element with value k, get-min() to return
the minimum element, and extract-min() to remove and return the minimum element.
Give pseudocode and worst-case running time for each operation, assuming the priority queue
is implemented with the following data structures:
4
def insert (k ):
array . append (k )
We note that the true worst case is O(n) and is observed when the array needs to
be reallocated (though amortized running time is still constant).
get-min(): With no order to rely upon, we have to resort to a O(n) linear scan.
def insert (k ):
index = find - insert - position ( array , k) # O(lg n)
5
def insert (k ):
list . insert (k , list . head ())
We note that the linked list data does not have to be contiguous and we are unaffected by the arrays "true worst case" when the storage needs to be reallocated.
get-min(): As with the unordered array, we have to resort to a O(n) linear scan.
def insert (k ):
ref = find - insert - position ( list , k) # O(n)
list . insert (k , ref ) # O(1)
6
def insert (k ):
++ heap . size
i = heap . size
heap [i] = k
while i > 0 and heap [ parent (i )] > heap [i ]: # O(lg n)
exchange ( heap [ parent (i )] , heap [i ])
i = parent (i)
get-min(): The minimum element in a min-heap is at the root and can be accessed
in O(1):