Description
1. (50 points) Part 1 : Programming exercise
. We want to compare the naive and Karatsuba divide-and-conquer methods to multiply two
integers x and y. Download the java file multiply.java from the course web page. Here,
your task is to implement a recursive version of these algorithms in the methods naive(int
size, int x, int y) and karatsuba(int size, int x, int y), and to use
them to compare the efficiency of each algorithm (i.e. number of arithmetic operations). The
variable size is the size of the integers x and y, and is defined as the number of bits used to
encode them (Note: we assume that x and y have the same size).
Each method (i.e. naive and karatsuba) will return an integer array result that stores
the value of the multiplication in the first entry of the array (i.e. result[0]), and the cost of
this computation in the second entry (i.e. result[1]). We define the cost as the number of
brute force arithmetic operations of the (addition, subtraction, or multiplication) executed by
the algorithm multiplied by the size (in bits) of the integers involved in this operation (Note:
We ignore the multiplication by powers of 2 which can be executed using a bit shift. Of course,
this is a crude approximation).
In particular, for the base case (i.e. when the size of the integers is 1 bit), this cost will be 1
(brute force multiplication of two integers of size 1). In the induction case, the naive method
executes 3 arithmetic operations of integer of size m (i.e. cost is 3 ·m), in addition of the number of operations executed by each recursive call to the function. By contrast, the Karatsuba
algorithm requires 6 arithmetic operations of size m on the top of the cost of the recursion.
The output of your program will print a list of numbers such that, the first number of each row
is the size of the integers that have been multiplied, the second number is the cost of the naive
method, and the third number the cost of the Karatsuba method.
You will test your methods on integers of size 1 to 15. Complete the provided CSV file,
karatsuba.csv with your results for both methods on integers of each size. Plot those
results, and write a short report describing and explaining what you observe. Include the plot
and the report in a file named 260xxxxxx.pdf, editing the name to match your student ID.
Part 2 : write your computations with all the steps and justified results in
a PDF file named 260xxxxxx.pdf. If this PDF is hand-written, make sure
you scan it. Pictures will not be accepted.
Enter your results on the MyCourses quiz
2. (25 points) We remind the master method for determining the asymptotical behaviour of a
recursive function.
Theorem 1 (Master method) Let a ≥ 1 and b ≥ 1 be two constants, and f(n) a function.
∀n ∈ N
+ we define T(n) as:
T(n) = aTn
b
+ f(n),where n
b
is interpreted as b
n
b
c or d
n
b
e.
Then, we can find asymptotical bounds for T(n) such that:
1. If f(n) = O(n
logb a−
) with > 0, then T(n) = Θ(n
logb a
).
2. If f(n) = Θ(n
logb a
· logp n), then T(n) = Θ(n
logb a
logp+1 n).
3. If f(n) = Ω(n
logb a+
) with > 0, and a · f
n
b
≤ cf(n), ∀n > n0 with c < 1 and
n0 > 0. Then T(n) = Θ(f(n)).
When possible, apply the master theorem to find the asymptotic behaviour of T(n) from the
following recurrences. Show your work and justify your answer for each recurrence in the
PDF file. Answer in the MyCourses quiz.
(a) (5 points) T(n) = 25 · T(
n
5
) + n
(b) (5 points) T(n) = 2 · T(
n
3
) + n · log(n)
(c) (5 points) T(n) = T(
3n
4
) + 1
(d) (5 points) T(n) = 7 · T(
n
3
) + n
3
(e) (5 points) T(n) = T(n/2) + n(2 − cos n)
3. (25 points) Let TA and TB be two function returning the running time of algorithms A and B,
defined by the recusions TA(n) = 7TA(
n
2
) + n
2
and TB(n) = αTB(
n
4
) + n
2
. Find the largest
integer value of α for which algorithm B is asymptotically faster than A. Show your work and
justify your answer in the PDF file. Answer in the MyCourses quiz.
You will submit multiply.java, karatsuba.csv and student_id.pdf in a single
zip file in the MyCourses submission folder for Assignment 4. You will enter
your solutions to question 2 and 3 on the MyCourses assignment 4 quiz. Your
final answers must be entered in the quiz for you to receive a grade.