Assignment 2
posted: 14.05.2021 due: midnight 28.05.2021
You can work in groups of up to three people. One group member should submit a copy of the solutions on
Brightspace, with all members’ names and banner numbers on it; the other group members should submit text
files with all members’ names and banner numbers (otherwise Brightspace won’t let us assign them marks!).
You may consult with other people but each group should understand the solutions: after discussions with
people outside the groups, discard any notes and do something unrelated for an hour before writing up your
solutions; it’s a problem if no one in a group can explain one of their answers. For programming questions
you should submit your code, which should compile and run correctly to receive full marks.
1. Given a tree on n vertices, we can always find a single vertex whose removal leaves a forest in which
no tree has more than n/2 vertices. Suppose we use our divide-and-conquer algorithm to count the
3-colourings of a tree on n vertices; about how long does it take? How fast can you compute the
answer?
You can assume n is a power of 2 and the tree is always split into exactly two pieces of size n/2 (even
though the two pieces together should have n − 1 vertices instead of n, since we removed a vertex to
split the tree).
2. In the lecture, we saw that implementing Euclid’s algorithm on positive integers a and b with a > b
by repeated subtraction takes Ω(a) time in the worst case but implementing it by mod takes O(log a)
time, assuming subtraction and mod each take constant time. Now suppose subtracting two n-digit
numbers takes n time but taking their mod takes n2 time; comparing two numbers takes time 1.
About how much bigger does a have to be than b in order for it to be faster to compute a mod b with
mod directly than with repeated subtraction?
For example, if a = 1523 and b = 0427, then computing a mod b = 242 by repeated subtraction means
subtracting 0427 from 1523 to get 1096 in 4 time units, checking 1096 is still bigger than 0427 in 1 time
unit, subtracting 0427 from 1096 to get 0669 in 4 time units, checking 0669 is still bigger than 0427 in 1
time unit, subtracting 0427 from 0667 to get 0242 in 4 time units, and checking whether 0240 is bigger
than 0427 in 1 time unit (and finding it’s not). That takes a total of 4 + 1 + 4 + 1 + 4 + 1 = 15 time
units, whereas computing a mod b = 242 directly takes 42 = 16 time units, so in this case repeated
subtraction is faster.
3. Describe how to build a circuit consisting of AND, OR and NOT gates that takes two n-bit binary
numbers x and y and outputs the (n + 1)-bit binary number x + y. Your circuit should be a directed
acyclic graph (a DAG) whose size is at most polynomial in n and whose depth is constant (where
“depth” means the length of the longest directed path); the fan-in and fan-out are not bounded (where
“fan-in” and “fan-out” mean the maximum in- and out-degree of any vertex).
4. Toom-3 is like Karatsuba’s algorithm but divides x and y into 3 parts each, and then multiplies them
using 5 multiplications of (n/3)-digit numbers, plus additions and subtractions. What is the maximum
number of such multiplications that it could use while still be asymptotically faster than Karatsuba’s
algorithm? Explain your answer.
I messed up and thought the exponent in the time bound for Karatsuba was 1.85. . . instead
of 1.58. . . . This question is now a bonus; you can figure out what the maximum number
of multiplications should be to beat either 1.58 or 1.85 (but please say which one you’re
beating).
Continued on Next Page!
1
5. Give a divide-and-conquer program for https://leetcode.com/problems/maximum-subarray (you
don’t have to pay for a membership!) and explain how to use your solution to solve https://leetcode.
com/problems/maximum-sum-circular-subarray neatly.
(If you don’t use divide-and-conquer or your solution looks like it’s been copied, you will not get the
mark and you may be reported to FCS.)
6. Suppose you didn’t understand Max’s lecture on the FFT but you still want to multiply degree-n
polynomials in time subquadratic in n. Show how to use Karatsuba’s algorithm to do it in O(nlg 3)
time, assuming arithmetic operations on coefficients take constant time.
For example, consider multiplying the two polynomials
A(x) = 8×7 + 5×6 + 4×5 + x4 + 9×3 + 6×2 + 2x + 1
B(x) = 7×7 + 5×6 + 3×5 + 3×4 + 9×3 + 4×2 + 5 .
Notice
(8×3 + 5×2 + 4x + 1)(9×3 + 4×2 + 5) + (9×3 + 6×2 + 2x + 1)(7×3 + 5×2 + 3x + 3)
= (8×3 + 5×2 + 4x + 1 + 9×3 + 6×2 + 2x + 1)(7×3 + 5×2 + 3x + 3 + 9×3 + 4×2 + 5) −
(8×3 + 5×2 + 4x + 1)(7×3 + 5×2 + 3x + 3) − (9×3 + 6×2 + 2x + 1)(9×3 + 4×2 + 5) ;
does this look familiar?
2
https://leetcode.com/problems/maximum-subarray
https://leetcode.com/problems/maximum-sum-circular-subarray
https://leetcode.com/problems/maximum-sum-circular-subarray