Exercise 3
Exercise 3
These exercises are for your own benefit. Feel free to collaborate and share your answers
with other students. There are many problems on this set, solve as many as you can and ask
for help if you get stuck for too long. Problems marked * are more difficult but also more
fun :).
These problems are taken from various sources at EPFL and on the Internet, too numer-
ous to cite individually.
Algorithm A solves problems of size n by dividing (in constant time) them into two sub-
problems each of size n/2, recursively solving each subproblem, and then combining the
solutions in Θ(n3 ) time.
Algorithm B solves problems of size n by dividing (in constant time) them into nine sub-
problems each of size n/3, recursively solving each subproblem, and then combining the
solutions in Θ(n2 ) time.
Algorithm C solves problems of size n by dividing (in constant time) them into ten subproblems
each of size n/3, recursively solving each subproblem, and then combining the solutions in
Θ(n) time.
Algorithm D solves problems of size n by dividing (in constant time) them into eight sub-
problems each of size n/2, recursively solving each subproblem, and then combining the
solutions in constant time.
Algorithm E solves problems of size n by dividing (in constant time) them into two subprob-
lems each of size n − 1, recursively solving each subproblem, and then combining the
solutions in constant time.
What are the running times of each of these algorithms (in Θ notation), and which would
you choose?
Page 1 (of 4)
-1 4 -1 -1 2 -5 2 1
Matrix Multiplication
4 (Exercise 4.2-1 in the book)
Use Strassen’s algorithm to compute the matrix product
1 3 6 8
7 5 4 2
Show your work.
Page 2 (of 4)
Power(a, n)
1. if n = 0
2. return 1
3. if n = 1
4. return a
5. q = b n4 c + 1
6. return Power(a, q)· Power(a, n − q)
7a (10 pts) Let T (n) be the time it takes to invoke Power(a, n). Then the recurrence relation
of T (n) is
(
Θ(1) if n = 0 or n = 1,
T (n) =
T (bn/4c + 1) + T (n − bn/4c − 1) + Θ(1) if n ≥ 2.
Page 3 (of 4)
A solution that only works when n is a power of 2, i.e., n = 2k for some integer k ≥ 0,
gives partial credits.
(Note that an is not a basic instruction and should therefore not be used.)
8 (∗, Exercise 4.1-5 in the book) Use the following ideas to develop a nonrecursive, linear-time
algorithm for the maximum-subarray problem. Start at the left end of the array, and progress
toward the right, keeping track of the maximum subarray seen so far. Knowing a maximum
subarray of A[1 . . j], extend the answer to find a maximum subarray ending at index j + 1
by using the following observation: a maximum subarray of A[1 . . j + 1] is either a maximum
subarray of A[1 . . j] or a subarray of the form A[i . . j + 1], for some 1 ≤ i ≤ j + 1.
Determine a maximum subarray of the form A[i . . j + 1] in constant time based on knowing a
maximum subarray ending at index j.
Page 4 (of 4)