Algorithms Computer Science Practice Exam
Algorithms Computer Science Practice Exam
Do not turn this page until you have received the signal to start. (In the meantime, please ll out the identication section above.)
Aids allowed: NONE
# 1: # 2: # 3: # 4: TOTAL:
Good Luck!
Total Pages = 6 Page 1
PLEASE HAND IN
contd. . .
CMPUT204
Term Test
Question 1.
[10 marks]
Consider the following recursive denition of function f : if n = 0 1 3 if n = 1 f (n) = f (n 1) + 2f (n 2) if n > 2 Using induction, prove that for all values of n: f (n) 3n .
Total Pages = 6
Page 2
contd. . .
CMPUT204
Term Test
Question 2.
[20 marks]
Find asymptotic tight bounds (i.e. ) for the following recurrences. Assume that in each case, T (n) is some positive constant for suciently small n. You can use the master theorem or iterated substitution, but must show how it applies. If you use iterated substitution, you do not need to prove your closed-form is correct. a) T (n) = 8T ( n 2 ) + 5 n.
2 b) T (n) = 5T ( n 5) + n . c) T (n) = 8T ( n 4 ) + n n.
d) T (n) = T (n 1) + n log n.
Total Pages = 6
Page 3
contd. . .
CMPUT204
Term Test
Question 3.
n3 10
[10 marks]
Indicate (without proof) whether the following statements are true. + 5n2 (n3 ) lg(n4 n n) ((lg n)4 ) 23n O(6n ) n3 4n2 lg n (n3 )
5n2 lg n
O(n lg n)
Total Pages = 6
Page 4
contd. . .
CMPUT204
Term Test
Question 4.
[20 marks]
Describe an algorithm that takes an array A and a number x and returns two elements from the array that sum to x, if they exist. The procedure should run in O(n log n) in the worst case.
Total Pages = 6
Page 5
contd. . .
CMPUT204
Term Test
Notes on Logarithms
Denition of logb n (b, n > 0): blogb n = n logb n as a function in n: increasing, one-to-one logb 1 = 0 logb xp = p logb x logb (xy ) = logb x + logb y xlogb y = y logb x logb x = (logb c)(logc x) ln n = loge n (natural logarithm) (logb n) = (log{whatever
d dx positive} n)
= (log n)
ln x =
1 x
Master Theorem
Let a 1 and b > 1 be constants, let f (n) be a function, and let T (n) be dened by the recurrence n T (n) = aT ( ) + f (n). b Then: 1. If f (n) O(nlogb a ), for some > 0 then T (n) (nlogb a ),
2. if f (n) (nlogb a logk n) for some k 0 then T (n) (nlogb a logk+1 n), 3. if f (n) (nlogb a+ ) for some constant > 0, and if af ( n b ) f (n) for some constant < 1 and all suciently large n, then T (n) (f (n)).
Common Summations
n
i=1 n
i=
n(n + 1) 2
i=0 n
2i = 2n+1 1 3n+1 1 2
i=0
3i =
End of Examination