Assign1 Solutions
Assign1 Solutions
Assignment 1
Due Date: Thu Jan 31 6:00pm
http://www.ece.cmu.edu/∼pueschel/teaching/18-645-CMU-spring08/course.html
Submission instructions: If you have an electronic version of your assignment (preferably made using
LATEX, but other forms, including scanned copies are okay), email them to: <schellap+18645-assign1@andrew.cmu.edu>.
Paper submissions need to be dropped off with one of the TAs at PH-B10 or with Carol Patterson at PH-B15.
Late submissions will not be graded.
1. (9 pts) Show that the following identities hold by determining the explicit constants c and n0 that are
a part of the definition of O.
(a) n + 1 = O(n)
(b) n3 + 2n2 + 3n + 4 = O(n3 )
(c) n5 = O(nlog2 n )
Solution:
(a) n + 1 = O(n)
For all n ≥ 1,
n + 1 ≤ n + n = 2n.
Thus, for c = 2, n0 = 1, we have n + 1 ≤ cn for all n ≥ n0 .
(b) n3 + 2n2 + 3n + 4 = O(n3 )
The trick is to get rid of all lower terms by converting them into multiples of n3 .
For all n ≥ 1:
n5 ≤ nlog2 (n) .
2. (14 pts) You know that O(n + 1) = O(n). Similarly, simplify the following as much as possible and
briefly justify.
(a) O(100)
(b) O(100 + (1/n))
√
(c) O(3n2 + n)
(d) O(log3 (n))
(e) O(n2.1 + n2 log(n))
(f) O(mn + n)
(g) O(m log(n) + n log(m) + n)
Solution:
(a) O(100)
Constants don’t matter, so O(100) = O(1).
c · 1 ≤ 1/n, for n ≥ n0 ,
4. (18 pts) Give asymptotic bounds (O, Ω, or Θ) for T (n) in each of the following recurrences. Make
your bounds as tight as possible. Justify your answers.
Solution:
uk = 2uk−1 + 22k
= 2(2uk−2 + 22k−2 ) + 22k
= 22 uk−2 + 22k + 22k−1
= 23 uk−3 + 22k + 22k−1 + 22k−2
uk = 2k + 22k + · · · + 2k+1
= 2k + 2k+1 (1 + · · · + 2( k − 1))
1 − 2k
= 2k + 2k+1
1−2
= 2k + 2k+1 (2k − 1)
= 22k+1 − 2k
(a) by definition;
(b) using the Karatsuba algorithm, recursively applied, assuming n = 2k .
Solution:
(a) By definition, it is necessary to multiply each factor by all others. Therefore, Cm (n) = n2 .
One needs to figure out that there are as many additions as there are multiplications except that
one of the multiplication goes for free in each monomials of the final polynomial(of degree 2n − 2).
Therefore Ca (n) = n2 − 2n + 1.
(b) As seen in class, C(2) = (4, 3) with Karatsuba’s algorithms.
We now denote pm (x) is a polynomial of degree m in the variable x. To compute the recursive
Karatsuba, there are two methods that are equivalent in the number of computations. Namely
p2m+1 (x) = pm (x) + xm+1 pm x or p2m+1 (x) = pm (x2 ) + xpm x2 . We take the first one but we
would find the same results with the other one.
From here, we can see that we need 3 multiplications of degree m polynomials, two addtions of
degree m polynomials, two additions of degree 2m polynomials and 2m scalar additions (first
term and third term don’t overlap and their sum has all terms except a term in x2m+1 . Third
term has 2m + 1 monomials, between xm+1 and x3m+1 ).
Therefore, a multiplication of two degree 2m + 1 polynomials require 3 multiplications of degree
m polynomials and 2(m + 1) + 2(2m + 1) + 2m = 8m + 4 scalar additions.
Therefore, a multiplication of two degree 2k − 1 polynomials require 3 multiplications of degree
2k−1 − 1 polynomials and 2k+2 − 4 scalar additions.
By induction, it is clear that Cm (2k ) = 3k . Therefore Cm (n) = nlog2 (3)
X
h≤k−2 X
h≤k−2
= (3h 22+k−h ) − (3h 4) + 3k−1 ∗ 4
h=0 h=0
X
h≤k−2 X
h≤k−2
= 22+k (3/2)h − 4 (3h ) + 3k−1 ∗ 4
h=0 h=0
k−1
1 − (3/2) 1 − 3k−1
= 22+k −4 + 3k−1 ∗ 4
1 − 3/2 1−3
= 23+k ((3/2)k−1 − 1) − 2(3k−1 − 1) + 3k−1 ∗ 4
= 16 ∗ 3k−1 − 2 ∗ 3k−1 + 2 + 4 ∗ 3k−1 − 23+k
= 18 ∗ 3k−1 − 23+k + 2
= 6 ∗ 3k − 8 ∗ 2k + 2
Therefore Ca (n) = 6nlog2 (3) − 8n + 2.
7. (15 pts) Solve the recurrence f0 = 1, f1 = 1, fn = fn−1 + 2fn−2 , using the method of generating
functions.
Solution: Our generating function is:
X
F (x) = fn xn
n≥0
Step 1: X X X
fn xn = fn−1 xn + 2 fn−2 xn
Step 2:
X X X
fn xn = fn−1 xn + 2 fn−2 xn
n≥2 n≥2 n≥2
X
F (x) = f0 + f1 + fn xn
n≥2
X X
F (x) = 1+x+ fn−1 xn + 2 fn−2 xn
n≥2 n≥2
X X
n−1
F (x) = 1+x+x fn−1 x + 2x2 fn−2 xn−2
n≥2 n≥2
X X
k 2
F (x) = 1+x+x fk x + 2x fk xk
k≥1 k≥0
X X
k 2
F (x) = 1+x+x fk x + 2x fk xk − xf0 x0
k≥0 k≥0
2
F (x) = 1 + xF (x) + 2x F (x)
Step 3:
F (x) = 1 + xF (x) + 2x2 F (x)
1 1
F (x) = 2
=
(1 − x − 2x ) (1 + x)(1 − 2x)
Step 5:
1X 2X n n
F (x) = (−1)n xn + 2 x
3 3
n≥0 n≥0
Step 6:
1 2 1
fn = (−1)n + 2n = (2n+1 + (−1)n )
3 3 3
8. (10 pts) Consider a polynomial of the third degree: a(x) = a0 + xa1 + x2 a2 + x3 a3 .
(a) i. By definition, there is 3 additions and 6 multiplications (without reusing already computed
powers of x) or 3 additions and 5 multiplications (with reuse of powers of x)
ii. With this expression it goes down to 3 additions and 3 multiplications.
(b) By induction, Horner’s scheme evaluates a polynom of degree n in n additions and n multiplica-
tions.