You are on page 1of 7

Name:

Unity ID :

CSC 505 Fall 2011 Midterm 1


Time: 75 Minutes
Instructions: You have the entire period of the class to complete this exam. Extension will not
be granted. Please work carefully and print answers neatly. Good luck!
Honor Pledge: I pledge that I have neither given nor received any aid while taking this exam.
Except for a non-programmable calculator (no cell phone!) and writing utensils no further tools
are allowed to be used while taking the exam.

Problem
1 10 points
2 8 points
3 11 points
4 10 points
5 11 +1 points
50+1 points

Score

Name:
Problem 1. (12 points) Prove rigorously (ie give values for c and n0 that will make your
argument work, and justify the values) each of the following statements using the formal
definitions of O, , and . Warning: testing individual values for n is NOT a proof.
a) (5 points)

nlg(n) + sqrt(n) + n-1 ( nlg(n) )

1. nlg(n) + sqrt(n) + n-1 O( nlg(n) ) : 0 < nlg(n) + sqrt(n) + n-1 < 3 nlg(n) for n > 2 since
n < nlg(n) for n > 2, sqrt(n) ( < n ) < nlg(n) for n >2, and n-1 (< 1 <n) < nlg(n) for n >2
=> choose c=3, n0=2 to show the bigOh claim
2. nlg(n) + sqrt(n) + n-1 ( nlg(n) ) : 0 < nlg(n) < nlg(n) + sqrt(n) + n-1 for n > 1 since
n + sqrt(n) + n-1 > 0 for n > 1.
=> choose c=1, n0=1 to show the bigOmega claim
Since all terms are positive, the first inequality is fulfilled in both cases.
1 & 2 together results in nlg(n) + sqrt(n) + n-1 ( nlg(n) )

b) (5 points) n3 - n2 + 2nlog(n) O( n3log(n) )


0 < n3 - n2 + 2nlog(n) for n > 2
n3 - n2 + 2nlog(n)
< n3 + 2nlog(n) for n > 0 since - n2 < 0 for n>0
< n3log(n) + 2 n3log(n) for n>2 since n3 < n3log(n) and
2nlog(n) < 2n3log(n) for n > 2
Together: n3 - n2 + 2nlog(n) < 3n3log(n) for n > 2
=> choose c=3, n0=2 to show the bigOh claim

Name:
Problem 2. (8 +1 points) If it is not stated differently, give the asymptotic big-theta bound for
T(n) of each of the following recurrences. If not explicitly stated, please assume that small
instances need constant time c. If possible, use the Master Theorem. To receive full marks justify
your answers.
a) (4 points) T(n) = T(2n/3) + 1
Use Master Theorem: a=1, b=3/2, f(n)= 1, nlogba= n0 = 1. Since f(n) ( nlogba(=1)) Master
Theorem case 2 applies, and we get: T(n)= ( lg(n) ).

b) (4 points) Solve the recurrence T(n) = 2T( n-1 ) + 1 for n>1 ; T(1) = 1 exactly, i.e. compute
T(n) as a function of n. Show your work! Tip: use forward iteration.
T(1) = 1
T(2) = 2+1
T(3) = 4+2+1
T(4) = 8+4+2+1

T(n) = 2n-1+2n-2++1 = 2n - 1

Name:
Problem 3. (11 points)
Consider the following algorithm.
(1) s 1
(2) for i 1 to n do
(3)
if i is even then
(4)
for j 1 to 2 do
(5)
ss*2
(6)
else
(7)
ss*3
(8) return s
a) (5 points) For n = 1,2,3,4,5 what function values are returned in line 8? How many
multiplications (*) does the algorithm perform for these n values?

N
Return value
(s)

# multiplications
(*)

3*4

9*4

9*16

5
27*16

b) (3 points) As a function of n, what is the exact number of multiplications (*) performed by


the above algorithm?

#Multiplica tions n n

c) (3 point) As a function of n, what is the value of s returned in line 8?

s 3

( n 1) / 2

n / 2

Name:
Problem 4. (10 points)
a) (3 points) Consider the following recursive procedure:
F(n):
if n = 1 then
return 1
else
return F(n-1) + 2*F(n-1)
What value does F(n) return? Tip: start with computing some small instances.
F(n) = 3n-1

b) (4 points) Write down the recurrence for the running time T(n) of F.

T(n)

= c1, for n=1


= 2T(n-1) + c2, otherwise

c) (3 points) How do the answers to (c) and (e) change if we replace the last line of F(n) by
return 3*F(n-1)
c) does not change, e) changes to T(n)

= c1, for n=1


= T(n-1) + c3, otherwise

Name:
Problem 5 (11+1 points). Mark the correct answer (T: true, F: false). Beware of guessing:
correct answer +1.5pt. incorrect answer -.5pt. no answer 0pts.
a) [ T F ]

f(n) + g(n) = (max (f(n), g(n))).

b) [ T F ]

f(n) = (g(n)) and at the same time f(n) = o(g(n)).

c) [ T F ]

2n+400000 (2n)

d) [ T F ]

2n+400000 (2n)

e) [ T F ]

2n+400000 (2n)

g) [ T F ]

If a function f(n) is in O(nk) with k1, we call f exponential.


.

i) (3 points) Please complete the definition of little-oh below.


f(n) o( g(n) ) iff _______________________ there is ____________________________

such that __________________________________________________________________

for __________________________ .
See lecture 2 page 11.

Name:

You might also like