You are on page 1of 5

CS325 Winter 2014: HW1

1. 0.2 from text book. Show that, if c is a positive real number, then g(n) =
1 + c + c2 + + cn is
(a) (1) if c < 1
(b) (n) if c = 1
(c) (cn ) if c > 1
The moral: in big- terms, the sum of a geometric series is simply the first
term if the series is strictly decreasing, the last term if the series is strictly
increasing, of the number of terms if the series is unchanging. (Question
0.2 from the text.)
Solution: First, we note that a geometric series has a closed-form solution:
cn+1 1
g(n) = 1 + c + c2 + ... + cn =
c1
If c < 1, we have:
lim g(n) =

01
1
=
(since limn cn+1 = 0)
c1
1c

From this we conclude that g(n) = (1) for c < 1.


If c = 1, we have g(n) = n + 1 = (n).
if c > 1, we have:
g(n)
cn+1 1
=
lim
n cn+1 cn
n cn
c c1n
= lim
n c 1
c
=
c1
lim

(divide by cn simultaneously top and bottom)


(limn

1
cn

= 0 for c > 1)

This allows us to conclude that g(n) = O(cn ) for c > 1.


Similar we can show that
cn
c1
=
n g(n)
c
lim

This allows us to conclude that g(n) = (cn ) for c > 1. Thus, we have
g(n) = (cn ).
1

2. 0.3(a) from text book.


The Fibonacci numbers F0 , F1 , ..., are defined by the rule
F0 = 0, F1 = 1, Fn = Fn1 + Fn2
Use induction to prove that Fn 20.5n for n 6. (Question 0.3a from
the text.)
Solution:
(Inductive assumption): Assuming that the statement is true for 6 k <
n
(Base cases:)
if n = 6, we have F6 = 8 26/2 = 8.
if n = 7, we have F7 = 13 27/2 = 11.31.
(Inductive case:) if n 8, the inductive assumption implies that Fn1
20.5(n1) and Fn2 20.5(n2) .
As such, we have: Fn+1 = Fn + Fn1 2n/2 + 2(n1)/2 = 2(n1)/2 (21/2 +
1) 2(n1)/2 2 = 2(n+1)/2
Thus, for all n 6, we have Fn 20.5n .
3. 2.3 from text book.
In class, we saw a method for solving recurrence relations which is based
on analyzing the recursion tree and deriving a formula for the work done
at each level. Another (closely related) method is to expand out the
recurrence a few times, until a pattern emerges. For instance, lets start
with the familiar T (n) = 2T (n/2)+O(n). Think of O(n) as being cn for
some constant c, so: T (n) 2T (n/2) + cn. By repeatedly applying this
rule, we can bound T (n) in terms of T (n/2), then T (n/4), then T (n/8),
and so on, at each step getting closer to the value of T () we do know,
namely T (1) = O(1):
T (n)

2T (n/2) + cn

2[2T (n/4) + cn/2] + cn = 4T (n/4) + 2cn

4[2T (n/8) + cn/4] + 2cn = 8T (n/8) + 3cn

8[2T (n/16) + cn/8] + 3cn = 16T (n/16) + 4cn

A pattern is emerging... the general term is


T (n) 2k T (n/2k ) + kcn
Plugging in k = log2 n (the depth of the recursion) we get T (n) nT (1) +
cn log2 n = O(n log n).

(a) Do the same thing for the recurrence T (n) = 3T (n/2) + O(n). What
is the general kth term in this case? And what value of k should be
plugged in to get the answer?
(b) Now try the recurrence T (n) = T (n 1) + O(1), a case which is not
covered by the master theorem. Can you solve this too?
Solution:
(a) From the recurrence relation, T (n) 3T (n/2) + cn for some constant
c. So:
T (n)

3T (n/2) + cn

3(3T (n/4) + cn/2) + cn = 32 T (n/4) + (3/2 + 1)cn

32 (3T (n/8) + cn/4) + (3/2 + 1)cn = 33 T (n/23 ) + (32 /22 + 3/2 + 1)cn

So the general term is:


T (n) 3k T (n/2k ) + cn

k
X
(3/2)i
i=0

Since n is reduced by a factor of 2 in each step, the depth of the


recursion tree is log2 n. Setting k = log2 n and using the fact that
Pk
i
k
i=0 (3/2) = O((3/2) ) by the Geometric Series Lemma,
T (n) 3log2 n T (1) + cn3log2 n /2log2 n
Since 3log2 n = nlog2 3 and T (1) c,
T (n) 2cnlog2 3 = O(nlog2 3 )
(b) This one is easier:
T (n) T (n 1) + c for some constant c
T (n 2) + c + c = T (n 2) + 2c
And the general term is:
T (n) = T (n k) + ck
Since n is reduced by 1 in each iteration, setting k = n results in:
T (n) = O(n)
4. 2.4 from text book. The recursive relations for each of the three algorithms are:
A: T (n) = 5T (n/2) + O(n)
B: T (n) = 2T (n 1) + c
3

C: T (n) = 9T (n/3) + O(n2 )


Using master theorem to solve for A and C, we have:
A: T (n) = O(nlog2 5 )
C: T (n) = O(n2 log n)
As for case B, we will use the technique introduced in problem 2.3:
T (n) = 2T (n 1) + c = 2(2T (n 2) + c) + c = 4T (n 2) + (2 + 1)c =
Pk1
4(2T (n3)+c)+(2+1)c = 8T (n3)+(4+2+1)c = 2k T (nk)+c i=0 2i
The depth k = n, thus we have T (n) = O(2n )
Among the three, algorithm C has the lowest time complexity.
5. 2.17 from text book.
Given a sorted array of distinct integers A[1, . . . , n], you want to find out
whether there is an index i for which A[i] = i. Give a divide and conquer
algorithm that runs in time O(log n).
Solution:
algo(A[1, . . . , n])
1.
if n == 1, return (A[n]==n)
2.
m = d n2 e
3.
if A[m] = m return true
4.
else if A[m] > m, return algo(A[1, . . . , m 1])
5.
else return algo(A[m + 1, . . . , n] m)
Each division reduce the problem size by half, and we do a constant
amount of work in each function call. This gives us the following recurrence relation:
T (n) = T (n/2) + O(1)
which solves to T (n) = O(log n).
6. Proof by induction. Show that for every full binary tree, the number of
leave nodes is exactly one more than the number of the internal nodes.
We showed a proof by doing induction on the number of nodes in the tree.
In this assignment, please prove this statement by doing induction on the
number of internal nodes.
Solution: The only difference between induction on the number of internal nodes vs. on the number of total nodes in the tree is the base case.
For internal nodes, the basecase is n = 0. For number of total nodes, the
base case is n = 1. We can use the same inductive arguments for both.

Sample questions The following questions are provided with solutions to


help you review the contents.
0.1 from textbook
f (n)
a
n 100
b
n1/2
c 100n + log n
d
n log n
e
log 2n
f
10 log n
g
n1.01
2
h
n / log n
i
n0.1
j
(log n)log n

k
n
1/2
l
n
m
n2n
n
2n
o
n!
log n
p
(log
Pnn) k
q
i=1 i

g(n)
n 200
n2/3
n + (log n)2
10n log 10n
log 3n
log(n2 )
n(log n)2
n(log n)2
(log n)10
n/ log n
(log n)3
5log2 n
3n
n+1
2
2n
(log2 n)2
2
nk+1

f = O(g)?
yes
yes
yes
yes
yes
yes
no
no
no
no
no
yes
yes
yes
no
yes
yes

f = (g)?
yes
no
yes
yes
yes
yes
yes
yes
yes
yes
yes
no
no
yes
yes
no
yes

f = (g)?
yes
no
yes
yes
yes
yes
no
no
no
no
no
no
no
yes
no
no
yes

2.5 from textbook


a.
b.
c.
d.
e.
f.
g.
h.
i.
j.

T (n) = O(nlog3 2 )
T (n) = O(nlog4 5 )
T (n) = O(n log7 n)
T (n) = O(n2 log3 n)
T (n) = O(n3 log2 n)
T (n) = O(n3/2 log n)
T (n) = O(n)
Pn
T (n) = nc + (n 1)c + ... + 1c = i=1 ic = O(nc+1 )
Pn
T (n) = i=1 ci = O(cn )
T (n) = O(2n )
k

k. T (n) = T (n1/2 ) + 1 = T (n1/4 ) + 2 = T (n(1/2) ) + k Lets assume


that it stops the recursive call if the input size is a small constant b.
k
Solving n1/2 = b leads to k = O(log log n) and T (n) = O(log log n).
2.12 from textbook.
The recurrence relation is: T (n) = 2T (n/2) + c. Solving this recurrence
relation using the master theorem, we have case 3 and T (n) = (nlog2 2 ) =
(n)

=
<
=
=
=
=
>
>
>
>
>
<
<
=
>
<
=

You might also like