You are on page 1of 7

Divide and Conquer, polynomial multiplication and FFT

Problems

This box indicates hints.

Question 1
Determine the asymptotic growth rate of the solutions to the following recurrences. If possible,
you can use the Master Theorem, if not, find another way of solving it.

1.1 T (n) = 2T ( n2 ) + n(2 + sin n)

1 ≤ 2 + sin n ≤ 3.


1.2 T (n) = 2T ( n2 ) + n + log n

Which term grows faster, n or log n?

1.3 T (n) = T (n − 1) + n

Does Master Theorem apply here? If not, just try unwinding the recurrence and see if you
can sum up the linear overheads.

Question 2
In each case determine the exact asymptotic growth rate of the solution T (n) to the recurrence.

2.1 T (n) = 16T ( n4 ) + n(2 + sin n)

2.2 T (n) = 2T ( n2 ) + n + 1
n

2.3 T (n) = 8T ( n2 ) + nlog n

Use algebra of logs.

2.4 T (n) = 2T ( n2 ) + (1 + (−1)blog nc )n2

1
COMP3821/9801 21T1 — Algorithms Practice Problems: Divide and Conquer (UNSW)

Question 3
You are given an array A containing a very long sequence of N integers which form an arithmetic
progression, except that one number has been repeated twice (consecutively). Thus, the sequence
looks like this:

a, a + b, a + 2b, . . . , a + (k − 1)b, a + kb, a + kb, a + (k + 1)b, . . . , a + M b

You are also given the length N of the array.

3.1 If only one number has been repeated in such a sequence, what is the value of M ? Keep in
mind that the starting element is a = a + 0 × b.

3.2 What would have been the value of M in a sequence corresponding to such an arithmetic
progression of length N if no element were repeated twice?

3.3 Find the repeated value by accessing only O(log2 N ) many entries of A. (Hint: you might
find (a) and (b) useful.)

Question 4
You are given a 2n × 2n board with one of its cells missing (i.e., the board has a hole); the position
of the missing cell can be arbitrary. You are also given a supply of “dominoes” each containing 3
such squares; see the figure below.

Your task is to design an algorithm for covering the entire board with such “dominoes”, except for
the hole which should remain uncovered.

Question 5
You and a friend find yourselves on a TV game show! The game works as follows. There is a
hidden N × N table A. Each cell A[i, j] of the table contains a single integer and no two cells
contain the same value.
At any point in time, you may ask the value of a single cell to be revealed.
To win the big prize, you need to find the N cells each containing the maximum value in its
row. Formally, you need to produce an array M [1..N ] so that A[r, M [r]] contains the maximum
value in row r of A, i.e., such that A[r, M [r]] is the largest integer among A[r, 1], A[r, 2], . . . , A[r, N ].
In addition, to win, you should ask at most O(N log N) many questions. For example, if the hidden
grid looks like this:

Column 1 Column 2 Column 3 Column 4


Row 1 10 5 8 3
Row 2 1 9 7 6
Row 3 -3 4 -1 0
Row 4 -10 -9 -8 2

then the correct output would be M = [1, 2, 2, 4].

2
COMP3821/9801 21T1 — Algorithms Practice Problems: Divide and Conquer (UNSW)

Your friend has just had a go, and sadly failed to win the prize because they asked N 2 many
questions which is too many. However, they whisper to you a hint: they found out that M is
non-decreasing. Formally, they tell you that M [1] ≤ M [2] ≤ · · · ≤ M [N ] (this is the case in the
example above).
Design an algorithm which asks at most O(N log N) many questions that produces the array
M correctly, even in the very worst case.

Note that you do not have enough time to find out the value of every cell in the grid! Try
determining M [N/2] first, and see if divide-and-conquer is of any assistance.

Question 6
Design an algorithm which multiplies any polynomial of degree 16 with any polynomial of degree
8 using only 25 multiplications in which both operands can be arbitrarily large.

Question 7
Multiply the following pair of polynomials using at most 7 multiplications of large numbers (large
numbers are those which depend on the coefficients and thus can be arbitrarily large). P (x) =
a0 + a2 x2 + a4 x4 + a6 x6 and Q(x) = b0 + b2 x2 + b4 x4 + b6 x6 .

Question 8
Multiply the following pairs of polynomials using at most the prescribed number of multiplications
where both numbers multiplied are large (large numbers are those which depend on the coefficients
and thus can be arbitrarily large).

8.1 P (x) = a0 + a2 x2 + a4 x4 + a6 x6 and Q(x) = b0 + b2 x2 + b4 x4 + b6 x6 + b8 x8 using at most 8


multiplications of large numbers;

8.2 P (x) = a0 + a100 x100 and Q(x) = b0 + b100 x100 with at most 3 multiplications of large
numbers.

Question 9
9.1 Multiply two complex numbers (a + ı̇ b) and (c + ı̇ d) (where a, b, c, d are all real numbers)
using only 3 real number multiplications and any number of real number additions.

9.2 Find (a + ı̇ b)2 using only two multiplications of real numbers and any number of real number
additions.

9.3 Find the product (a + ı̇ b)2 (c + ı̇ d)2 using only five real number multiplications.

Question 10
13 11 k
Describe all k which satisfy ı̇ ω64 ω32 = ω64 (ı̇ is the imaginary unit).

Question 11
π
What are the real and the imaginary parts of eı̇ 4 ? Compute the DFT of the sequence A =
(0, 1, 2, 3, 4, 5, 6, 7) by applying the FFT algorithm by hand.

3
COMP3821/9801 21T1 — Algorithms Practice Problems: Divide and Conquer (UNSW)

Question 12
Describe how you would compute all elements of the sequence F (0), F (1), F (2), . . . , F (2n) in time
O(n log n) where
[A] X
F (m) = i3 j 2
i+j=m
0≤i,j≤n

[B] X
F (m) = log(j + 1)i
i+j=m
0≤i,j≤n

Question 13
Compute by any method you wish the (linear) convolution s ∗ s of the sequence s = h1, 2, 0, 4i with
itself. (Note that there is no requirement on the efficiency of your method, and that the sequence
is really short!)

Question 14
If a sequence x has n terms and sequence y has k terms, how many terms does the convolution
x ∗ y of these two sequences have?

Question 15
Is it true that s ∗ t = t ∗ s for any two sequences s and t? Explain why or why not.

Question 16
Describe how we compute efficiently the convolution of two (long) sequences?

Question 17
17.1 Revision: Describe how to multiply two n-degree polynomials together in O(n log n) time,
using the Fast Fourier Transform (FFT). You do not need to explain how FFT works—you may
treat it as a black box.

Remember that FFT does not multiply polynomials, what does it do? Refer to the lecture
slides.

17.2 In this part we will use the Fast Fourier Transform (FFT) algorithm described in class to
multiply multiple polynomials together (not just two).
Suppose you have K polynomials P1 , . . . , PK so that degree(P1 ) + · · · + degree(PK ) = S > 1.
Show that you can find the product of these K polynomials in O(KS log S) time.

How many points do you need to uniquely determine an S-degree polynomial?

17.3 Show that you can find the product of these K polynomials in O(S log S log K) time.

Consider using divide-and-conquer; a tree which you used in the previous assignment might

4
COMP3821/9801 21T1 — Algorithms Practice Problems: Divide and Conquer (UNSW)

be helpful here as well. Also, remember that if x, y, z are all positive, then log(x + y) <
log(x + y + z)

Question 18
You have a set of N coins in a bag, each having a value between 1 and M , where M ≥ N . Some
coins may have the same value. You pick two coins (without replacement) and record the sum
of their values. Determine what possible sums can be achieved, in O(M log M ) time.
For example, if there are N = 3 coins in the bag with values 1, 4 and 5 (so we could have
M = 5), then the possible sums are 5, 6 and 9.

If the coins have values v1 , . . . , vN , how might you use the polynomial xv1 + · · · + xvN ?

Question 19
Let us define the Fibonacci numbers as F0 = 0, F1 = 1 and Fn = Fn−1 + Fn−2 for all n ≥ 2. Thus,
the Fibonacci sequence looks as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, . . .

19.1 Show, by induction or otherwise, that


   n
Fn+1 Fn 1 1
=
Fn Fn−1 1 0

for all integers n ≥ 1.

19.2 Give an algorithm that finds Fn in O(log n) time.

You may wish to refer to Example 1.5 on page 28 of the “Review Material” found as lecture
notes for Topic 0 on the Course Resources page of the course website.

Question 20
0 1 2 63
Consider the polynomial P (x) = (x − ω64 )(x − ω64 )(x − ω64 ) . . . (x − ω64 )

20.1 Compute P (0);

20.2 What is the degree of P (x)? What is its coefficient of the highest degree of x present in
P (x)?

20.3 What are the values of P (x) at the roots of unity of order 64?

20.4 Can you represent P (x) in the coefficient form without any computation?

Question 21
To apply the FFT to the sequence (a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 ) we apply recursively FFT and ob-
tain F F T (a0 , a2 , a4 , a6 ) and F F T (a1 , a3 , a5 , a7 ). Proceeding further with recursion, we obtain
F F T (a0 , a4 ) and F F T (a2 , a6 ) as well as F F T (a1 , a5 ) and F F T (a3 , a7 ). Thus, from bottom up,
F F T (a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 ) is obtained using permutation (a0 , a4 , a2 , a6 , a1 , a5 , a3 , a7 ) as the
leaves of the recursion tree of the original input sequence. Given any input (a0 , a1 , a2 , . . . , a2n −1 )
describe the permutation of the leaves of the recursion tree.

5
COMP3821/9801 21T1 — Algorithms Practice Problems: Divide and Conquer (UNSW)

Write indices in binary and see what the relationship is of the bits of the ith element of the
original sequence and the ith element of the resulting permutation of elements as they appear
on the leaves on the recursion tree.

Question 22
You are given a sequence A = ha0 , a1 , . . . , an−1 i of length n and sequence B = h1, 0, 0, . . . , 0, −1i
| {z }
k
n
of length k + 2, where 1 ≤ k ≤ 4.

22.1 Compute the DFT of sequence B.

22.2 Compute the convolution sequence C = A ∗ B in terms of the elements of sequence A.

22.3 Show that in this particular case such a convolution can be computed in time O(n).

Question 23
Compute the DFT of the sequence (1, −1, −1, 1) by any method you wish.

Question 24
Compute the linear convolution of sequences (1, −1, −1, 1) and (−1, 1, 1, −1) by any method you
wish.

Question 25
Assume that you are given a polynomial P (x) = a0 + a1 x + a2 x2 + a3 x3 and a polynomial Q(x) =
b0 +b1 x+b2 x2 +b3 x3 whose coefficients can be arbitrarily large numbers. Let R(x) = P (x)2 −Q(x)2 .
Compute the coefficients of R(x) using only 7 large number multiplications.

Question 26
Recall that the DFT of a sequence
ha0 , a1 , . . . , an−1 i
is the sequence of values
hP (ωn0 ), P (ωn1 ), P (ωn2 ), . . . , P (ωnn−1 )i
of the associated polynomial P (x) = a0 + a1 x + a2 x2 + . . . + an−1 xn−1 . Compute directly (i.e.,
without using the FFT, to spare you the trouble) and maximally simplify the DFT of the following
sequences:

26.1 h1, 0, . . . , 0 i
| {z }
n−1

26.2 h 0, . . . , 0, 1i
| {z }
n−1

26.3 h 1, . . . , 1 i
| {z }
n

6
COMP3821/9801 21T1 — Algorithms Practice Problems: Divide and Conquer (UNSW)

Question 27
Assume that you are given an n × n table; each square of the table contains a distinct number. A
square is a local minimum if the number it contains is smaller than the numbers contained in all
neighbouring squares which share an edge with that square. Thus, each of the four corner squares
has only two neighbours sharing an edge; 4(n − 2) non corner squares along the edges of the table
have three neighbouring squares and all internal squares have four neighbouring squares. A square
is a local minimum if the number it contains is smaller than the numbers contained in all of its
neighbouring squares. You can only make queries which given numbers m and k, 1 ≤ m, k ≤ n,
return the value in the square (m, k).

27.1 Show by an example that if you search for a local minimum by picking a square and then
moving to a neighbouring square with a smaller value (if there is such) might take Θ(n2 ) many
queries.

27.2 Design an algorithm which finds a local minimum and makes only O(n) many queries.

A surface z = f (x, y) where (x, y) belongs to a bounded closed subset S of the plane attains
its minimal height z either along the curve which is the edge of S or strictly in the interior of
S. Can you use this fact to design a divide-and-conquer algorithm?

Question 28
Assume you are given two data bases A and B; A contains n numbers, B contains n + 1 numbers,
all numbers are distinct. You can query each database only in the following way: you specify a
number k and the database (A or B); the query returns the value of the k th smallest element in
that database. Design an algorithm which produces the median of the set of all elements which
belong to either A or B using at most O(log n) queries.

Let m be the median of all elements which belong to either A or B; then if k many elements
of A are smaller than m, then exactly n − k elements of B must be smaller than m. How can
you find such m efficiently?

You might also like