Professional Documents
Culture Documents
Part A
Please only submit give your responses to the questions in this part.
3. T (n) = 5T (n/5) + n/5; T (1) = 0 (You may assume that n is a power of 5.)
5. T (n) = T ( 2n 3n
5 ) + T ( 5 ) + n; T (n) = 0 if n ≤ 1
1
√
2. T (n) = T ( n) + 2
1
= T (n 2 ) + 2
1
= T (n 22 ) + 2 ∗ 2
1
= T (n 23 ) + 2 ∗ 3
1 k
= T (n 2k ) + 2 ∗ k (Assume that n = 22 )
1
= T (n 2log2 log2 n ) + 2 ∗ (log2 log2 n)
= T (2) + 2 ∗ (log2 log2 n)
= 0 + 2 ∗ (log2 log2 n) → Θ(log log n)
n n
3. T (n) = 5T ( ) +
5 5
n n n
= 5[5T ( 2 ) + 2 ] +
5 5 5
2 n n
= 5 T( 2) + ∗ 2
5 5
2 n n n
= 5 [5T ( 3 ) + 3 ] + ∗ 2
5 5 5
3 n n
= 5 T( 3) + ∗ 3
5 5
n n
= 5 T ( k ) + ∗ k (Assume that n = 5k )
k
5 5
log5 n n n
=5 T ( log n ) + ∗ log5 n
5 5 5
log5 n n
=5 T (1) + ∗ log5 n
5
n
= ∗ log5 n → Θ(n log n)
5
4. T (n) = T (n − 1) + log2 n
= [T (n − 2) + log2 (n − 1)] + log2 n
= [T (n − 3) + log2 (n − 2)] + log2 (n − 1) + log2 n
= T (n − 3) + log2 (n − 2) + log2 (n − 1) + log2 n
= log2 1 + log2 2 + ... + log2 (n − 2) + log2 (n − 1) + log2 n
= log2 n! → Θ(n log n)
Proof of log2 n! → Θ(n log n): Since it is obvious that nn >= n!. We can see that n! = 1∗2∗
..∗(n/2)∗(n/2+1)∗...∗n >= (n/2)∗(n/2+1)∗...∗n >= (n/2)∗(n/2)∗...∗(n/2) = (n/2)n/2 ).
So log nn >= log n! >= log((n/2)n/2 ), we have n log n >= log n! >= (n/2) log(n/2).
2n 3n
5. T (n) = T ( ) + T ( ) + n ( level 1 in Figure 1)
5 5
2 2 6n 6n 3 2
= T (n ∗ ( ) ) + T ( ) + T ( ) + T (n ∗ ( ) ) + n ∗ 2
5 25 25 5
n n n n
= T( 2) + T( 5 5 ) + T( 5 5 ) + T( 5 2) + n ∗ 2 (level 2 in Figure 1)
5
(2) ( 2 ) ∗ ( 3 ) ( 3 ) ∗ ( 2 ) (3)
= ...
We can expand the recurrence equation as Figure 1. More details of the recursion tree
can be found at chapter 2 and chapter 4 in “Introduction to Algorithms” by Leiserson,
Cormen, Rivest and Stein, MIT press.
By observation, each T (n) can split into two new T (n)s plus a single n according to the
recurrence equation. In the last level of the recursion tree where all T (n) = T (1) = 0, we
2
Figure 1: Recursion Tree
can see the equation is only related to the n part. It is easy to find that, at each level, the
term that only contain n is n*(the current level). Assume k is the total number of levels
of the recursion tree, then Θ bound of T (n) is Θ(nk)
n k
At the k-th level, the left-most T ( k) has the biggest denominator ( 25 ) than any other
( 52 )
term relates to T (n). So it will be reach to T (1) earlier than other terms. Similarity, the
right-most T is T ( 5n k ) has smallest denominator and will be the last one to reach T (1).
(3)
k k
The middle T s have denominators between ( 35 ) and ( 52 ) .
n
So the final level k will be larger than the level when the left-most T ( k) = T (1).
( 52 )
n
Similarly, k will be smaller than or equals to the level when the right-most T( 5 k) = T (1).
(3)
Then, we obtain ⌊log 5 n⌋ ≤ k ≤ ⌈log 5 n⌉. The lower Θ bound is Θ(n ∗ log 5 n) when
2 3 2
k = ⌊log 5 n⌋, and upper Θ bound is Θ(n ∗ log 5 n) when k = ⌈log 5 n⌉.
2 3 3
1. You are given c identical mooncakes and p identical plates. Fill in the details in the
following pseudo-code of a function S (indicated by the dotted line). The function S(c,
p) computes the number of possible ways of distributing c mooncakes among p plates.
S(c, p):
if p == 1 or c == 0:
return 1
if p > c :
return S(c, c);
return ______________________
3
2. Take a look at these two Python functions, funcA and funcB. The parameters for the
function funcA are e, a list of strings, and lst, a list of lists of strings. A list of lists of
strings lst is the only parameter required by the other function, funcB.
Solution:
4
Question 3 Mathematical Induction [20%]
Prove the following equations with mathematical induction.
1. In each step, the object can either move 1 or 2 units along either axis.
3. The object cannot move backward. That is, it must make progress either along the x-axis
or the y-axis towards the destination D in each step and it cannot regress towards O.
5
Design a recursive algorithm calculate route that takes the destination coordinates D(x, y)
(x, y are non-negative integers and O ̸= D) as input and outputs the number of distinct routes
from O to D. For example, if the input coordinates are D(2, 2), your algorithm should
return 12 because there are 12 possible routes from O to D:
(0, 0) → (0, 1) → (0, 2) → (2, 2)
(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2)
(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2)
(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)
(0, 0) → (0, 1) → (2, 1) → (2, 2)
(0, 0) → (1, 0) → (2, 0) → (2, 2)
(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)
(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2)
(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)
(0, 0) → (1, 0) → (1, 2) → (2, 2)
(0, 0) → (0, 2) → (1, 2) → (2, 2)
(0, 0) → (0, 2) → (2, 2) (Exclude due to violation of Rule 2)
(0, 0) → (2, 0) → (2, 1) → (2, 2)
(0, 0) → (2, 0) → (2, 2) (Exclude due to violation of Rule 2)
Give the pseudocode for calculate route(x, y, z), where z can be utilized to indicate
whether a 2-unit move is carried out in a previous step.
You should verbally explain your idea and make your algorithm clear and understandable.
Solution: See Algorithm 1.
Part B
For your practice only. Do NOT submit your answer to this part.
1. Solve the following recurrence formulae and give a Θ bound for each of the recurrences.
6
(b) Θ(n) if a = 1
(c) Θ(an ) if a > 1
def test(i,j):
if i==0: return j
else: return test(i-1,i+j)
print (test(4,7))
4. The following pseudocode calculates the greatest common divisor of two positive integers
a and b. Use M.I. to prove the correctness the gcd function.
gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a mod b)
5. Is there a faster way to compute the n-th Fibonacci number than by the iterative algorithm
(Chapter 2, slide p.91)? One idea involves matrices. We start by writing the equations
F1 = F1 and F2 = F0 + F1 in matrix notation:
F1 0 1 F0
=
F2 1 1 F1
Similarly,
2
F2 0 1 F1 0 1 F0
= =
F3 1 1 F2 1 1 F1
In general, n
Fn 0 1 F0
=
Fn+1 1 1 F1
So, in order to compute Fn , it suffices to raise this 2 × 2 matrix, call it X, to the n-th
power.
(a) Show that two 2 × 2 matrices can be multiplied using 4 additions and 8 multiplica-
tions.
(b) Show that O(log n) matrix multiplications suffice for computing X n . (Hint: Think
about computing X 8 .)
Thus the number of arithmetic operations needed by our matrix-based algorithm, is just
O(log n), as compared to O(n) for the iterative method. Have we broken another barrier?
(c) Briefly explain why or why not the new algorithm will be faster than the iterative
algorithm.
7
Solution:
3. 17
The recursive processes are as follows: test(4,7) → test(3,4+7) → test(2,3+11)
→ test(1,2+14) → test(0,1+16) → 17
4. [Base Case]
Case 1: When a = 1 and b = 1, gcd(1,1) = gcd(1,0) = 1. 1 is the greatest common divisor
of 1 and 1.
Case 2: When a = 1 and b > 1, gcd(1,b) = gcd(b,1) = gcd(1,0) = 1. 1 is the greatest
common divisor of 1 and b.
Case 3: When a > 1 and b = 1, gcd(a,1) = gcd(1,0) = 1. 1 is the greatest common divisor
of a and 1.
Case 4: When b = q ∗ a where q > 1 and q is an integer, gcd(a,q*a) = gcd(q*a,a) =
gcd(a,0) = a. a is the greatest common divisor of a and b = q ∗ a.
Case 5: When a = q ∗ b where q > 1 and q is an integer, gcd(q*b,b) = gcd(b,0) = b. b is
the greatest common divisor of a = q ∗ b and b.
8
[Inductive Step] Assume that when a ≤ k and b ≤ k, gcd(a,b) return the greatest common
divisor of a and b. Then we want to prove gcd(a,b) still holds when a ≤ k +1 and b ≤ k +1.
That means we need to prove the extra cases of new range which are gcd(k, k+1), gcd(k+1,
k) and gcd(k+1, k+1) can return the correct answer.
Case 1: We know gcd(a, b) = gcd(b, a mod b), so gcd(k+1, k) = gcd(k, (k+1) mod k).
k ≤ k and (k + 1) mod k ≤ k, so gcd(k+1, k) = gcd(k, (k+1) mod k) returns the greatest
common divisor of k+1 and k.
Case 2: Similarly, k ≤ k and (k + 1) mod k ≤ k, gcd(k, k+1) = gcd(k+1, k mod (k+1))
= gcd(k+1, k) = gcd(k, (k+1) mod k) return the greatest common divisor of k and k+1.
Case 3: gcd(k+1, k+1) = gcd(k+1, 0) = k+1 return the greatest common divisor of k+1
and k+1.
Therefore, we showed by mathematical induction that for any positive integer a and b,
gcd(a,b) return the greatest common divisor of a and b.
Submission
Please submit your assignment (one PDF file) to moodle by the deadline. Make sure the
content is readable. Feel free to post your questions on moodle forum, or contact TAs if you
encounter any difficulty in this assignment. We are happy to help.