Professional Documents
Culture Documents
MIT students: This problem set is due in lecture on Monday, September 24.
SMA students: This problem set is due after the video-conferencing session on Wednesday, Septem-
ber 26.
MIT students: Each problem should be done on a separate sheet (or sheets) of three-hole punched
paper.
SMA students: Each problem should be done on a separate sheet (or sheets) of two-hole punched
paper.
You will often be called upon to “give an algorithm” to solve a certain problem. Your write-up
should take the form of a short essay. A topic paragraph should summarize the problem you are
solving and what your results are. The body of your essay should provide the following:
2. At least one worked example or diagram to show more precisely how your algorithm works.
Remember, your goal is to communicate. Graders will be instructed to take off points for convo-
luted and obtuse descriptions.
2 Handout 12: Problem Set 2 Solutions
Solution:
R ANDOMIZE -I N -P LACE
-
For our base case, we have initialized to 2. Therefore we must show that for each possible 1-
$54 -36 7*98;:<=8?>@3:#
permutation, the subarray A[1] contains this 1-permutation with probability
Clearly this is the case, as each element has a chance of 7:<
of being in the first position.
.
Solution:
By definition, a element heap has a height of . Therefore A BDCFE G where H>I is A and our base
case is correct.
J4K L
B F
C E G
Now we use induction, and assume that all trees with
nodes or fewer has a height of .
Next we consider a tree with nodes. Looking at the node, we know its height is one greater
B +:M.#G BDCNEB 22:(:(.#.#GGG G5> G
than its parent (and since we’re not in the base case, all nodes have a parent). The parent of the th
node in the tree is also the
+
(
: 3
.
G
G >
th node in the tree. Therefore its height is
th node in the tree has a height of 6OBPCFEB B 6QCNE B 2
(
: #
.
G 5
G >
. Then the
.
BDCFE 6R CNEG B
. BLCFE
Therefore by induction we have shown that the height of an node tree is . BDCNE
Exercise 2-3. Do Exercise 6.4-3 on page 136 of CLRS.
Solution:
S $ FC E 2*
The running time of H EAPSORT on an array of length that is already sorted in increasing order
is because even though it is already sorted, it will be transformed back into a heap and
sorted.
$ +*
The running time of H EAPSORT on an array of length that is sorted in decreasing order will
"J(S T FC E
be . This occurs because even though the heap will be built in linear time, every time the
element is removed and the H EAPIFY is called it will cover the full height of the tree.
Solution:
Handout 12: Problem Set 2 Solutions 3
S $L U9*
V $+*W>
The running time will be because every time partition is called, all of the elements will
Solution:
l L$ NC E +*
assumption that all input permutations are equally likely, not only is the running time of ordinary
quicksort , but it performs essentially the same comparisons and exchanges between input
elements as randomized quicksort.
Consider the implementation of PARTITION given in lecture on a subarray monnip3 :
$ q'rm+')pM*
TX / m?
PARTITION
- sm um p
1
2
3 for t to
6 t wvOT
- - 6
4 do if
5 then
6
7 exchange mx / - -
exchange t
8 return -
Let y be a set of distinct elements which are provided in random order (all orders equally likely)
as the input array / monnip# to PARTITION , where >zp{4/m 6 is the size of the array. Let T denote
the initial value of mx .
4 Handout 12: Problem Set 2 Solutions
Solution:
monnip3 =8
Given that
permutations of the >pW4Rm 6
is random (all orders are equally likely), there are
elements. Each element has a
possible
probability 7:<
elements is 83:#%>$L4
3*8
. Consequently the $4
3*98
of being chosen as the pivot, therefore the number of permutations of the remaining
permutations are equally
likely.
Solution:
PARTITION takes different branches based only on the comparisons made in the
$]*
function. This is clear by observing line 4 of the function, as it is the only place where
the sequence of instructions may differ. As the arrays have identical function
values, they must take the same branches, calculate the same indicies and perform the
same exchanges. Consequently PARTITION will perform the same partition on both
arrays, which follows directly as the exchanges performed are identical.
> r19' U 'nnn'71 to be an $L='x* input pattern if (> A , 7= ¡}4o(' 6 M~ for
- >\.¢')Z?'nnn') £ } -| 7 2>@4qM~ £ >\4K .
Define a sequence
, and
Define a sequence >I]19' U 'nnn'71 to be an $L='?* output pattern if
4o if - O¤'
3 > A if - >z¤'
6 if - O¤n
We say that a permutation ]<9' U 'nnn'M of y satisfies a pattern >¥¦1' U 'nnn'3( if $]L*[>g7
for all - >@('.'nnn9') .
Handout 12: Problem Set 2 Solutions 5
(c) How many $L='x* input patterns are there? How many $'x* output patterns are there?
Solution:
<¨©
4\
There are § ª ¨©¦« $'x*
input patterns because we can choose
>4qpositions out of
'
¬4
possible positions to have
®4\
. There is one (
) output pattern because
¯4O
the pattern must be exactly negative ones followed by a 0, followed by
ones.
y $='x*
(d) How many permutations of satisfy a particular
y
permutations of satisfy a particular
input pattern? How many
output pattern? $L='x*
Solution:
$L°4
x*8P$]®4g3*98 permutations are possible of y
to satisfy a particular input pattern.
This is the total number of ways to rearrange the elements which have a value of 4o
are also $°4Ox*98N$r®4z3*98
amongst themselves, and rearrange those with a value of 1 amongst themselves. There
permutations possible to satisfy a particular output pattern
for the same reason.
Solution:
All members of y³£ ²
satisfy and so they all have the same result when the function
is applied to its elements. Therefore by part (b) when all these inputs are given to
PARTITION they are subject to the same permutation. Using the hint, we then know
all $L¤4µx*8P$]o4Q3*98
that after all of the distinct inputs are run through PARTITION that they will produce
distinct outputs. From part we know that and are the $¢* y³£ ² y³£ ²x´
same size, and also we have proven that PARTITION is onto, and therefore PARTITION
must be a bijection!
/ m 6 | nn)p3
y 4I}7T+~ T¶> mx
(f) Suppose that before the call to PARTITION , the input subarray is a random
permutation of , where . Argue that after PARTITION , the two
resulting subarrays are random permutations of their respective elements.
Solution:
Using our solution from part (e), we know that after PARTITION is run on
all values in the set y³W£ ²x4·´
. Therefore we get all permutations of the
, we get
ones and all J4^ y³£ ²
permutations of the negative ones. Furthermore, we get each sub-array permu-
tations an equal number of times and so the subarrays are also random permutations.
6 Handout 12: Problem Set 2 Solutions
(g) Use induction to show that, under the assumption that all input permutations are
$ q'rm+')pM* y
longing to monnip3
equally likely, at each recursive call of Q UICKSORT
is equally likely to be the pivot .
, every element of be-
T>·/ m?
Solution:
$]2* $¢*
The base case for the initial array; we know that it is randomly permuted, and so by
part and
$r2*
each of its subarrays will also be randomly permuted after PARTITION .
Therefore we can inductively apply at each partition to prove that every subarray
will also be randomly permuted.
l L$ NC E +*
(h) Use the analysis of R ANDOMIZED -Q UICKSORT to conclude that the average-case
running time of Q UICKSORT on elements is .
Solution:
By part (g) we know that under the assumption that the input pattern is random every
element is equally likely to be chosen as the pivot at each recursive call which then pro-
duces the same random distribution of quicksort traces as R ANDOMIZED -Q UICKSORT .
Therefore as their distribution is the same, the expected-case analysis for R ANDOMIZED -Q UICKSORT
l $ FC ¸(E +*
will apply to the average case of Q UICKSORT . Therefore the average case of Q UICKSORT
also takes time.
(a) How would you represent a -ary heap in an array?
Solution:
The -ary heap would be similar to a binary heap with the parent and child indexes
¹ 1pM
- > BD- :<1G
calculated as follows:
Alternate Solution: A -ary heap can be represented in a -dimensional array as
0.#
/0 6
follows. The root is kept in
6 3.
, its children are kept in order in
, their children are kept in order in through
through
, and so on. 1U 6 6
ov - t
t v
The two procedures that map a node with index to its parent and to its th child (for
), respectively, are;
Handout 12: Problem Set 2 Solutions 7
$-*
return º $ - 4K3*
:M(»
D - ARY-PARENT
D - ARY-C HILD - t
$' *
return $ - 4O3* 6%t56
CORRECTION
A -ary heap would have a height of S $ NC ¸(E(¼ +* . We know that
6 6 U 6 nnn 6 1½ ¨© ¾v 6 6 U 6 nnn 6 1½
½ 4O ¾v ½9¿ 4^
o4K q4K
½ {$]q4K7* 6 ·v 1½¿
CF¸(E(¼ ${$]o4K3* 6 3*v 6
which solves to ¬> º $ CN¸(E ¼ $L[$q4K3* 6 3*=4O3*,» .
(c) Give an efficient implementation of E XTRACT-M AX in a -ary max-heap. Analyze its
running time in terms of and .
Solution:
H EAPIFY $L' - ')=')¢*
t/ ¤-
A ÁtoÀ o4K¬ vK /0À w^/
1
2 for
3 if -6 >zÀ and - 6 t
then t -6
t> Â -
4
5 if
-
the maximum child is greater than the th node, we switch the two nodes and recurse
on the child.
"J1TX /Ã
E XTRACT-M AX (A, n)
1
/
2
3 Ä>
J4O
"J(T
4 H EAPIFY (A, 1, n, d)
5 return
l $/ÀL!7 ¼ 2*
The running time of this algorithm, is clearly constant work plus the time of H EAPIFY
which as shown above is . E XTRACT-M AX works by storing the value
of the maximum element, moving the minimum element into the root of the heap, and
then calling heapify to restore the heap property.
(d) Give an efficient implementation of I NSERT in a -ary max-heap. Analyze its running
time in terms of and .
Solution:
L$ ' ')='*
Ä 6
I NSERT
1
>¥4³Å
2
3 I NCREASE -K EY $L' - '')2*
From the following problem part, we know I NCREASE -K EY runs in time, l $ NC ¸($ E ¼ 2* +*
therefore since I NSERT only adds constant time operations it is also . It l FC ¸(E ¼
is rather trivially correct as the algorithm has not changed because all calculations
involving the number of children are performed by I NCREASE K EY .
Solution:
Handout 12: Problem Set 2 Solutions 9
L$ ' - 'x*
- Æ®Ç#È2$L - ]'x*
I NCREASE -K EY
1
W>z/ -
- g ,É ¼7Ê wO/ -
2 if
3 while and
/ - /&É ¼ Ê
4 do
5 Exchange
6 - É ¼Ê
l $ L!3 ¼ +*
Our implementation loops proportionally to at most the depth of the tree, therefore it
runs in time. I NCREASE -K EY loops, at each step comparing the increased
node to its parent and exchanging them if the heap property is violated. Therefore,
once the algorithm terminates we know that the node once again satisfies the heap
property and has the correct value.
(f) When might it be better to use a -ary heap instead of a binary heap?
Solution:
It would be better to use a -ary heap when it is predicted that the heap will do
many more I NSERT s and I NCREASE -K EY s than E XTRACT-M AX s because I NSERT
and I NCREASE -K EY are faster algorithms as increases while E XTRACT-M AX gets
slower.