Sie sind auf Seite 1von 5

CS 241-001, Fall 2007, Exam 2

Prof. David Nassimi

Solution
(Time: 1.5 Hours)

Number of students who took the exam: 18


Median = 48
Grades:
0, 10, 10, 12, 15, 18, 20, 30, 43 52, 52, 54, 60, 62, 70, 70, 72, 92
1. Prove the following function is (n4 ).
T (n) = 2n4 20n3 200n2 + 500n + 5000

(a) Prove O(n4 ):


T (n)

2n4 + 500n + 5000


n4 (2 + 500/n3 + 5000/n4 )
n4 (2 + 500/103 + 5000/104 ),
n4 (2 + 0.5 + 0.5)
3n4 , n 10.

n 10

T (n)

2n4 20n3 200n2


n4 (2 20/n 200/n2 )
n4 (2 20/100 200/1002 ),
n4 (2 0.2 0.02)
1.78n4 , n 100.

n 100

(b) Prove (n4 ) :

Notes:
For () proof, n 10 would not work as it would result in negative constant C. (The
constants C and n0 must be positive.)
For O(), we picked n 10 and for () we picked n 100. So we see that the n0 values
for the two cases need not be the same. Why? Let n1 = max{10, 100}. Then both proofs
would still be valid for n n1 .

CS 241-001, Fall 2007, Exam 2 (Solution)


2. The following divide-and-conquer algorithm performs some mysterious function on an array
A[s..t]. The initial call to this recursive function is Find(A, 0, n1) for an array of n elements.
boolean Find (dtype A[ ], int s, int t) {
if (s == t) return true; //n=1 returns with a true value.
if (A[s] 6= A[t]) return false;
int m = b(s + t)/2c;
if (Find(A, s, m) and Find(A, m + 1, t))
return true;
else
return false;
}
(a) (7 pts.) What does this function accomplish? When does it return a true value for an
array of size n?
Claim: The function returns true if and only if all n elements are equal. Why? First observe
that all n elements are equal if and only if:
i. all elements in the left half of the array are equal; and
ii. all elements in the right half of the array are equal; and
iii. one element from the first half is equal to one element from the second half.
Now, we may easily prove the claim to be correct by induction on n.
(b) (7 pts.) Let f (n) be the worst-case number of key comparisons performed by this function
for an array of n elements. Assuming n is a power of 2, write a recurrence for f (n).
The number of key comparisons for each of the recursive calls is f (n/2). Therefore,
(

f (n) =

0,
n=1
2f (n/2) + 1, n > 1

(c) (6 pts.) Find the solution of the recurrence by repeated substitution. Then express the
asymptotic time complexity of the algorithm.

f (n) = 1 + 2f (n/2) = 1 + 2(1 + 2f (n/4))


= 1 + 2 + 4f (n/4)
..
.
=
=
=
=

1 + 2 + 4 + + 2k1 + 2k f (n/2k )
1 + 2 + 4 + + 2k1 + 2k f (1)
1 + 2 + 4 + + 2k1 (geometric series sum)
2k 1 = n 1.

CS 241-001, Fall 2007, Exam 2 (Solution)


3. Towers-of-Hanoi is an interesting problem that is easily solved with recursion. There are
three towers: A, B, and C. Initially, n discs are stacked on tower A, and each disc is smaller
than the one below it. The object of the game is to move the stack of discs to tower B in
the same order. (The third tower is used as intermediate storage.) There are two rules for
moving the discs: Only one disc can be moved at a time from the top of one tower to the
top of another, and a larger disc can never be placed on top of a smaller one. The following
recursive function solves this problem.
Towers (A, B, n, C) //Move n disc from tower A to B. Use C as temp storage.
{
if (n == 1)
MOVE (A, B) //Move one disc from A to B
else {
Towers (A, C, n 1, B)
MOVE (A, B)
Towers (C, B, n 1, A) }
}
(a) (10 pts.) Let f (n) be the number of single-disc moves this algorithm makes to solve the
n-disc problem. Write a recurrence for f (n).
(

f (n) =

1,
n=1
2f (n 1) + 1, n > 1.

(b) (10 pts.) Solve the recurrence by repeated substitution.

f (n) = 1 + 2f (n 1) = 1 + 2(1 + 2f (n 2))


= 1 + 2 + 4f (n 2)
= 1 + 2 + 4 + 8f (n 3)
..
.
= 1 + 2 + 4 + + 2n2 + 2n1 f (1)
= 1 + 2 + 4 + + 2n2 + 2n1
= 2n 1.

CS 241-001, Fall 2007, Exam 2 (Solution)


4. Find the exact solution of the following recurrence. (Assume n = 2k .) You may use either
the repeated substitution method, or the master-theorem.
(

T (n) =

1,
n=1
8T (n/2) + n2 , n > 1

(a) Master Theorem: a = 8, b = 2, = 2.


Since h = log2 8 = 3 6= , the solution form is:
T (n) = Anh + Bn = An3 + Bn2 .
We may find the constants A and B using T (1) and T (2). (We dont need induction.)
T (1) = 1 = A + B
T (2) = 8T (1) + 22 = 12 = A23 + B22 = 8A + 4B.
This gives A = 2 and B = 1. Therefore,
T (n) = 2n3 n2 .
(b) Repeated Substitution Method:
T (n) =
=
=
=
=
=
=
=

n2 + 8T (n/2)
n2 + 8((n/2)2 + 8T (n/4))
n2 + 2n2 + 82 T (n/22 )
n2 + 2n2 + 22 n2 + 83 T (n/23 )
..
.
n2 (1 + 2 + 22 + + 2k1 ) + 8k T (n/2k )
n2 (2k 1) + (2k )3 T (1)
n2 (n 1) + n3
2n3 n2 .

(1)

CS 241-001, Fall 2007, Exam 2 (Solution)


5. Consider the following recurrence, where n is a power of 2.
(

T (n) =

0,
n=1
2T (n/2) + log n, n > 1

Prove by induction that the solution is


T (n) = An + B log n + C
and find the constants A, B, C.
Proof: For the base, n = 1, T (1) = 0 = A + C.
Next, for any n 2, suppose that
T (n/2) = An/2 + B log(n/2) + C.
Then,
T (n) =
=
=
=
=

2T (n/2) + log n
2(An/2 + B log(n/2) + C) + log n, (from hypothesis)
An + 2B(log n log 2) + 2C + log n
An + (2B + 1) log n 2B + 2C
An + B log n + C,

where the latter equality requires


2B + 2C = C.

2B + 1 = B,
So, we have:

A+C = 0
2B + 1 = B
2B + 2C = C.
Thus, A = 2, B = 1, C = 2. Therefore,
T (n) = 2n log n 2.