Practice Quiz 1 Solutions: Introduction To Algorithms
Practice Quiz 1 Solutions: Introduction To Algorithms
Practice Quiz 1 Solutions: Introduction To Algorithms
(a)
. Therefore, we have
. Lower bound is obvious.
*
(b)
!
Solution: Master method does not apply directly. But is much smaller than ,
!
therefore ignore the lower order term and guess that the answer is .
Check by substitution.
(c) / 1 3 4
7 : < > ?
.
(d)
!
1 3
(e) / 1 3 4
.
(f)
!
A
>
>
1 C 3 .
Handout 11: Practice Quiz 1 Solutions 2
(g)
.
(h)
Solution: . This is
! # % ! ) ) $
whence . Thus, $ .
(b) T F The worst-case running time and expected running time are equal to within con-
stant factors for any randomized algorithm.
.
0
1 1
0 5 0 7 0 : 0 0
- 3 - %
. . .
3 %
%
% % %
%
Handout 11: Practice Quiz 1 Solutions 3
Solution: True. A hash family that maps a universe of keys into slots is
universal if for each pair of distinct keys , the number of hash functions
. Therefore, for any pair of the four distinct keys, exactly hash function
only for
(a) Argue that any comparison based sorting algorithm can be made to be stable, without
affecting the running time by more than a constant factor.
Solution: To make a comparison based sorting algorithm stable, we just tag all
elements with their original positions in the array. Now, if , then we
compare and , to decide the position of the elements. This increases the running
(b) Argue that you cannot have a Priority Queue in the comparison model with both the
following properties.
Solution:
If such priority queues existed, then we could sort by running BUILD -H EAP ( )
(c) Given a heap in an array with as the maximum key (the heap is a max
heap), give pseudo-code to implement the following routine, while maintaining the
max heap property.
D ECREASE -K EY – Decrease the value of the key currently at by . Assume
.
Solution:
D ECREASE -K EY
M AX -H EAPIFY
(d) Given a sorted array of distinct integers, some of which may be negative, give an
algorithm to find an index such that and provided such an index
exists. If there are many such indices, the algorithm can return any one of them.
Solution:
and , then . So if we look at the middle element of the array, then half
of the array can be eliminated. The algorithm below (I NDEX -S EARCH ) is similar to
I NDEX -S EARCH
if
return -1
if
then return
if
Problem -4. Suppose you are given a complete binary tree of height with leaves, where
each node and each leaf of this tree has an associated “value” (an arbitrary real number).
If is a leaf, we denote by
That is, consists of , ’s parent, grandparent, etc. up to the root of the tree.
Similarly, if
and
or . That
is,
Handout 11: Practice Quiz 1 Solutions 5
36
21 6
15 30 11 10
10 19 20 14 5 9 2 7
x y
A(x,y) shown in bold
f(x,y) = 19+15+21+36+20+30 = 141
/
/
.
Give an algorithm (pseudo-code not necessary) that efficiently finds two leaves ;
( and
( such that
is as large as possible. What is the running time of your algorithm?
;
(
/
(
Solution:
There are several different styles of solution to this problem. Since we studied divide-and-conquer
algorithms in class, we just give a divide-and-conquer solution here. There were also several
different quality algorithms, running in , , and . These were worth up to
solution given here just finds the maximum value, but it is not any harder to return the leaves
giving this value as well.
We define a recursive function M AX 1 to return the maximum value of —the sum of the
;
Handout 11: Practice Quiz 1 Solutions 6
in ’s subtree. Calling
subtree, so we end up with a straightforward divide and conquer algorithm given as:
M AX 1
For M AX 2 , we note that there are three possible types of solutions: the two leaves are in ’s left
subtree, the two leaves are in ’s right subtree, or one leaf is in each subtree. We have the following
pseudocode:
M AX 2
Analysis:
For M AX 1, we have the following recurrence
(1)
(2)
answer to M AX 1 and the answer to M AX 2. With this simple change, the recurrence is the same as
M AX 1
. Our goal is to sort this array in time faster than . We will do so in two phases.
In the first phase, we will compute a sorted array that contains the distinct keys occuring in .
In the second phase we will sort the array using the array to help us.
Handout 11: Practice Quiz 1 Solutions 7
Note that might be very small, like a constant, and your running time should depend on
as well
as . The objects have satellite data in addition to the keys.
Your goal is to design and analyse efficient algorithms and analyses for the two phases. Remember,
the more efficient your solutions, the better your grade!
(a) Design an algorithm for the first phase, that is computing the sorted array of length
containing the distinct keys. The value of is not provided as input to the algorithm.
Solution:
also provides the location where should be inserted into array to maintain in
sorted order. All elements in to the right of this position are shifted by one place to
Solution:
Binary search in array for each element of array takes time since size of
into array exactly times, and the total time over all such insertions is
since .
(c) Design an algorithm for the second phase, that is, sorting the given array , using the
array that you created in part (a). Note that since the objects have satellite data, it
is not sufficient to count the number of elements with a given key and duplicate them.
Hint: Adapt Counting Sort.
Solution:
Build the array as in counting sort, with containing the number of elements in
that have values less than or equal to . Counting sort will not work as is since
Handout 11: Practice Quiz 1 Solutions 8
is necessarily an integer. Or, it may be some integer of very large value (there is
no restriction on our input range). Therefore is an invalid index into our array .
What we would like to do is assign an integral “label” for the value . The label we
choose is the index of the value in the array calculated in the last part of the
problem.
How do we find this index? We could search through from beginning to end, looking
for the value , then returning the index of that contains . This would take
time. But, since is already sorted, we can use B INARY-S EARCH to speed this
and an item within the array, and returns such that . The modified version
C OUNTING -S ORT
For to do ; /* Initialize */
C[Location] C[Location] ;
;
;
Out-Location D[Location];
D[Location] D[Location] ;
-out[Out-Location] ;
Output( -out);
Solution:
The running time of the modification to C OUNTING -S ORT we described can be bro-
ken down as follows:
First Loop: .
Third Loop: .
The running time is dominated by the second and fourth loops, so the total running
time is .