Divide & Conquer problems [00PM]
- October 19, 2026
[DC1] Modular exponentiation (2 pts) [00PN]
- October 19, 2026
Describe a recursive algorithm which computes the modular exponentiation b^e \bmod {m}, that is, the remainder when dividing b^e by m, in only O(\lg e) time. You may assume that 0 \leq b, e, m < 2^{32}, and that arithmetic operations such as addition and multiplication on 64-bit numbers take constant time.
Be sure to analyze the running time of the algorithm, and prove it is correct by (strong) induction on e.
Recall that (a \cdot b) \equiv _m (a \bmod m) \cdot (b \bmod m), so it does not matter whether you reduce modulo m before or after multiplication. For example, b^4 \bmod m \equiv _m (b \bmod m)^4.
[DC2] Majority elements (3 pts) [00PO]
- October 19, 2026
An array A[1\ldots n] is said to have a majority element if more than half of its entries are the same. Given an array, the task is to design an efficient algorithm to tell whether the array has a majority element, and, if so, to find that element. The only thing you may assume about the elements of the array is that you can test whether two of them are equal (in constant time). In particular, the elements of the array are not necessarily from some ordered domain like the integers, and so there can be no comparisons of the form A[i] > A[j]. You also may not assume that there is a hash function for the elements, so they cannot be used as the keys of a dictionary/hash table.
- What is a brute-force algorithm for this problem? How long does it take to run?
- Show how to solve this problem in O(n \log n) time. Make sure to prove that your algorithm is correct (via induction) and give a recurrence relation for the running time of your algorithm.
[DC3] n-bit multiplication (1 pt) [00PW]
- October 22, 2026
Explain how to generalize the divide-and-conquer n-bit multiplication algorithm from class to work for any value of n, not just powers of two.
[DC4] Divide & conquer comparison (1 pt) [00Q2]
- October 23, 2026
Suppose you are choosing between the following three algorithms:
- Algorithm A solves problems by dividing them into five subproblems of half the size, recursively solving each subproblem, and then combining the solutions in linear time.
- Algorithm B solves problems of size n by recursively solving two subproblems of size n-1 and then combining the solutions in constant time.
- Algorithm C solves problems of size n by dividing them into nine subproblems of size n/3, recursively solving each subproblem, and then combining the solutions in O(n^{2}) time.
[DC5] Efficient Fibonacci calculation (3 pts) [00Q3]
- October 23, 2026
Recall the Fibonacci numbers F_n:
The Fibonacci numbers are the sequence F_n defined by the recurrence
\begin {align*}
F_0 &= 0 \\
F_1 &= 1 \\
F_n &= F_{n-1} + F_{n-2}.
\end {align*}Definition Fibonacci numbers [00OZ]
As you may know, directly turning the definition of Fibonacci numbers into a recursive implementation is a terrible idea; the resulting algorithm takes \Theta (\varphi ^n) time (where \varphi = (1 + \sqrt 5)/2 \approx 1.618\dots ).
The usual iterative algorithm to repeatedly calculate the next Fibonacci number from the previous two seems like it would take \Theta (n) time to compute F_n: it just loops from 1 to n and does one addition each loop, right? Well... yes, it does one addition each loop, but we can’t really assume that these additions take constant time, because the Fibonacci numbers involved can get quite large! Let’s analyze the situation more carefully.
- Recall from a previous homework problem that the size of the nth Fibonacci number F_n is \Theta (\varphi ^n). Given this fact, approximately how many bits (in terms of \Theta ) are needed to represent F_n? Simplify your answer as much as possible. (Note, you need note have completed GCD4 to do this problem.)
- Now suppose we implement the usual iterative algorithm as follows: initialize an array F of size n, which can hold arbitrary-size integers. Initialize F[0] = 0 and F[1] = 1. Then loop i from 2 to n, and at each iteration, set F[i] \gets F[i-1] + F[i-2]. Taking into account the time needed to add integers of a given size, what is the running time of this algorithm?
- We can do better! In addition to their definition, Fibonacci numbers satisfy the following recurrences (you can just take my word for it, or see this exercise):
\begin {align*}
F_{2n+1} & = F_n^2 + F_{n+1}^2 \\
F_{2n} &= 2F_n F_{n+1} - F_n^2
\end {align*} For example, F_7 = 13 = F_3^2 + F_4^2 = 2^2 + 3^2, and F_8 = 21
= 2F_4F_5 - F_4^2 = 2 \cdot 3 \cdot 5 - 3^2.
Explain how to turn these recurrences into a recursive algorithm for computing Fibonacci numbers, and implement this algorithm in a programming language of your choice. Be careful not to make more recursive calls than necessary!
- Analyze the running time of this algorithm. Be sure to include the time needed to do any additions or multiplications. Assume we will use Karatsuba’s algorithm for multiplication.
[DC6] Fibonacci recurrence (1 pt) [00Q4]
- October 23, 2026
This problem guides you through a derivation of the Fibonacci recurrences in the previous problem.
- Consider the 2\times 2 matrix M = \begin {bmatrix} 1 & 1 \\ 1 & 0 \end {bmatrix}. Compute M^2, M^3, and M^4. What do you notice?
- State a conjecture about the values of M^n for n \geq 1 and prove your conjecture by induction on n.
- In practice, of course, we could compute M^n using an algorithm like the one in DC1. Expand the equation M^{2n} = (M^n)^2 and use it to derive the Fibonacci recurrences in DC5.