CS161: Design and Analysis of Algorithms
Summer 2004
Problem Set #3 Solutions
General Notes
•
Regrade Policy:
If you believe an error has been made in the grading of your problem
set, you may resubmit it for a regrade. If the error consists of more than an error in
addition of points, please include with your problem set a detailed explanation of
which problems you think you deserve more points on and why. We reserve the right
to regrade your entire problem set, so your final grade may either increase or decrease.
•
If we ask for an answer in terms of
n
and
d
, please give the answer in terms of
n
and
d
and do not assume that
d
is a constant. Many people did this on the first problem
as well as the skip list problem from the last problem set.
•
In part (b) of problem 3, many people only showed that the probability that
any
slot
had
k
elements was
≤
nQ
k
. You had to show the
maximum
number of elements was
k
.
1.
[14 points]
Analysis of
d
ary Heaps
Throughout this problem, when asked to give an implementation of a certain operation,
please provide pseudocode or a verbal description with the same level of clarity and
detail. When asked to analyze running times, your analysis does not need to be formal,
but should be justified.
Note that we are asking you to solve 62(e) before 62(d). Keep in mind that you may
always (not only in this problem) use the results of any previous parts in answering a
later part of a multipart problem.
(a)
[1 point]
Do problem 62(a) on page 143 of CLRS.
Answer:
A
d
ary heap can be represented in a 1dimensional array by keeping
the root of the heap in
A
[1], its
d
children in order in
A
[2] through
A
[
d
+ 1], their
children in order in
A
[
d
+2] through
A
[
d
2
+
d
+1], and so on. The two procedures
that map a node with index
i
to its parent and to its
j
th
child (for 1
≤
j
≤
d
) are
DPARENT(
i
)
1
return
ceilingleft
(
i

1)
/d
ceilingright
DCHILD(
i, j
)
2
return
d
(
i

1) +
j
+ 1
(b)
[1 point]
Do problem 62(b) on page 143 of CLRS.
Answer:
Since each node has
d
children, the total number of nodes in a tree
of height
h
is bounded above by 1 +
d
+
. . .
+
d
h
inclusively and below by
1+
d
+
. . .
+
d
h

1
exclusively. This yields
h
=
ceilingleft
log
d
(1+
n
(
d

1))
ceilingright
1 = Θ(log
d
n
).
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Problem Set #3 Solutions
2
(c)
[2 points]
Give an efficient implementation of HEAPIFY in a
d
ary maxheap.
Analyze its running time in terms of
d
and
n
.
Answer:
First, we will need to find if the root element given to DHEAPIFY is
larger than all its children. If not, we swap it with its largest child and recursively
call DHEAPIFY on that child.
DHEAPIFY(
A, i, d
)
1
largest
←
i
2
for
j
←
1
to
d
3
j
←
DCHILD(
i, j
)
4
if
j
≤
heapsize
[
A
] and
A
[
j
]
> A
[
largest
]
5
then
largest
←
j
6
if
largest
negationslash
=
i
7
then
exchange
A
[
i
]
↔
A
[
largest
]
8
DHEAPIFY(
A, largest, d
)
This algorithm does Θ(
d
) comparisons in each call to DHEAPIFY as well as
O
(log
d
n
) calls to HEAPIFY, one for each level of the tree. Therefore, the total
running time for this algorithm is
O
(
d
log
d
n
).
This is the end of the preview.
Sign up
to
access the rest of the document.
 Summer '09
 Algorithms, LG, Binary heap, lg lg, lg lg lg

Click to edit the document details