You are on page 1of 2

University of Toronto

ECE-345: Algorithms and Data Structures


Solutions to Midterm Examination (Fall 2013)

1. (a) For n = 1, log1! = 1log1 = 0


Assume true for n, then log(n!) cn logn, for some c.
Then log((n + 1)!) = log(n + 1) + log(n!) log(n + 1) + cn logn c log(n + 1) + cn logn c log(n +
1) + cn log(n + 1) c(n + 1)log(n + 1). For c 1. QED
n
P
(b) The while loop takes log x, as it halves x repeatedly. Run time is log x = log(n!) = (n logn)
1
x2 1
(c) Base: When n = 1 we have = x + 1. So P (1) is correct. Hypothesis: Assume that P (k) is
x1
correct for some positive integer k. That means that

xk+1 1 xk+1 1
= 1 + x + x2 . . . xk = + xk+1 by inductive hypothesis
x1 x1
xk+1 1 + xk+1 (x 1)
=
x1
xk+2 1
=
x1

The proposition is true for all n 1. QED


Inductive Step: We will now show that P (k + 1) is correct. 1 + x + x2 + xk+2 by the induction
hypothesis. 2 2 k > 2(k + 1), as required.
(d) (i) Case 2 of the Master Method - T (n) = (n3 logn).
n1
2i = 2n 1.
P
(ii) T (n) = 2T (n1)+1 = 2(2T (n2)+1)+1 = 4T (n2)+2+1 = 8T (n3)+4+2+1 =
i=0
T (n) = O(2n )
(e) Independently, each key has a 1/m probability of hashing into the first slot, or (m 1)/m probability
 of
 n
m1
not hashing into the first slot. Thus, the probability that no key hashes into the first slot is: m

(f) To start with, we need to determine an interval in which k lives in O(log k) time. To do this, we simply
look for a 1 at indices 1, 2, 4, . . . , 2dlog ke . Index k lives somewhere in the interval [2dlog ke1 , 2dlog ke ]
We can now search this interval for an index which contains 0 and which is followed by a 1, using binary
search. The interval has no more than k items, so binary search incurs an additional O(log k) cost.

1
2. The idea is to keep the smallest element from each list in a heap; each element is augmented with the index
of the lists where it comes from. We can perform a DeleteMin on the heap to find and delete the smallest
element and insert the next element from the corresponding list.
Analysis: It takes O(k) to build the heap; for every element, it takes O(logk) to DeleteMin and O(logk) to
insert the next one from the same list. In total it takes O(k + n logk) = O(n logk).
3. (i) For each i = 1, . . . , k we will compute(solve) two sub-problems:
F (i) : the maximum amout of work that can be done during minutes {1, . . . , i} assuming that minute
i is spent at First Cup.
N (i) : the maximum amout of work that can be done during minutes {1, . . . , i} assuming that minute
i is spent at Jim Nortons.
Overall we want to compute max(F (k), N (k))
(ii) Given that I spend minute i at First Cup there are two possible situations: either I spent the previous
minute at First Cup or I spent minute i 11 at Jim Nortons (I was switching during {i 10, . . . i 1}).
In the first case F (i) = fi + F (i 1). In the second case where I switched, we have F (i) = fi + N (i 11).
Thus:
F (i) = fi + max(F (i 1), N (i 11)), if 1 i k and F (i) = 0, if i 0.
N (i) = ni + max(N (i 1), F (i 11)), if 1 i k and N (i) = 0, if i 0.
(iii) Allocate two arrays of real numbers F and N of length k with indices staring from 10 until k. Intialize
them to 0 for all entries. Then incrementallty compute the functions in the previous question. Clearly
O(k) both time and space complexity.

You might also like