Você está na página 1de 5

CS 610, Sec 102, Spring 2007, Midterm Exam Prof.

David Nassimi

Solution
(Time: 2 Hours)

Exam Distribution: Number of Students = 39 Median = 60 90 s 80 s 70 s 60 s 50 s 40 s 30 s < 30 0 2 5 13 4 3 6 6 1. (a) (10 pts.) Prove the following function is (n3 ). P (n) = 5n3 100n2 + 1000n P (n) 5n3 100n2 discard positive terms 3 n (5 100/n) n3 (5 100/100), n 100 4n3 , n 100.

(b) (10 pts.) Consider the following pseudo-code. x = 0; for i = 1 to n for j = 1 to 4*i-3 x = x + 1; i. Find the exact number of times the innermost statement (increment x ) is executed. That is, what is the nal value of x?

n 4i3

x =
i=1 j=1 n

(1) (4i 3)
i=1

= = = = =

n (rst term + last term) /2 n ((4 3) + (4n 3))/2 n(4n 2)/2 2n2 n.

ii. Express the time complexity T (n) of this code in O() form. T (n) = O(2n2 n) = O(n2 ).

CS 610, Sec 102, Spring 2007, Midterm Exam (Solution) 2. (a) (15 pts.) Use induction to prove that every postage of 32 cents or more can be achieved using only 9-cent stamps and 5-cent stamps. That is, prove that for every integer n 32, there exist some non-negative integers A and B such that n = 9A + 5B.

Proof: Induction Base: n = 32 = 9 3 + 5 1. Induction Step: We will prove that for any n 32, if n = 9A + 5B for some non-negative integers A and B, then n + 1 = 9A + 5B for some non-negative integers A and B . We consider two cases: Case 1: A 1: n + 1 = 9(A 1) + 5(B + 2). Case 2: A = 0. Since n 32, this implies that B 7. Then, n + 1 = 9(A + 4) + 5(B 7). (b) (5 pts.) Explain whether the proof can be extended to show that all postages of 30 cents or more are obtainable. If not, explain exactly which part of the proof will not be applicable. Answer: The base is easily proved for n = 30, since 30 = 9 0 + 5 6. But, in the induction step, Case-2 is not applicable. That is, when A = 0 and n 30, it is not necessarily true that B 7. (In fact, when n = 30, B = 6.) So, the transformation of Case 2 will not be valid.

CS 610, Sec 102, Spring 2007, Midterm Exam (Solution) 3. (a) (10 pts.) Find the exact solution of the following recurrence. (Assume n = 2k .) T (1) = 1, T (n) = 4T (n/2) + n, n > 1. Solution by Master Theorem: a = 4, b = 2, = 1; Since h = , the solution form is: T (n) = Anh + Bn = An2 + Bn. To nd the constants A, B: T (1) = 1 = A + B. T (2) = 4T (1) + 2 = 4 1 + 2 = 6 = 4A + 2B. Solving the two equations, A + B = 1 and 4A + 2B = 6, gives A = 2, B = 1. T (n) = 2n2 n. Solution by Repeated Substitutions: T (n) = = = = n + 4T (n/2) n + 4(n/2 + 4T (n/4)) = n + 2n + 42 T (n/22 ) n + 2n + 42 (n/22 + 4T (n/23 )) n + 2n + 22 n + 43 T (n/23 ) . . . h = logb a = log2 4 = 2.

= n(1 + 2 + 22 + + 2k1 ) + 4k T (n/2k ) = n(2k 1) + 4k T (1) = n(2k 1) + 4k = n(n 1) + n2 = 2n2 n.

(b) (10 pts.) Consider the Fibonacci sequence: F1 = 1, F2 = 1, and Fn = Fn1 + Fn2 for n 3. Prove by induction that for all n 1, Fn 2n1 . Proof: First, we prove the inequality for two base cases, n = 1 and n = 2. F1 = 1 20 , F2 = 1 21 . (1)

Next, we prove (1) is true for n = m, m 3, supposing that it is true for n = m 1 and n = m 2. Fm = Fm1 + Fm2 2m2 + 2m3 = 3 2m3 = 3 2m1 < 2m1 . 4

CS 610, Sec 102, Spring 2007, Midterm Exam (Solution) 4. Consider a 2n 2n board, with one of its four quadrants missing. That is, the board consists of only three quadrants, each of size 2n1 2n1 . Lets call such a board a quad-decient board. For n = 1, such a board becomes an L-shape 3-cell piece called a tromino, as shown below.

(a) (12 pts.) Use a divide-and-conquer technique to prove by induction that a quad-decient board of size 2n 2n , n 1 can always be covered using some number of trominoes. (By covering we mean that every cell of the board must be covered by a tromino piece, and the pieces must not overlap or extend outside of the board.) Use a diagram to help describing your algorithm and proof.

Solution: For the base, n = 1, the borad is in the shape of a single tromino, thus it can be covered by a single tromino. For n 2, we will prove that if a 2n1 2n1 quad-decient board can be covered, then a 2n 2n quad-decient board can also be covered. Consider a 2n 2n quad-decient board. The board can be divided into four 2n1 2n1 quad-decient boards, as shown below. By the hypothesis, each of these smaller boards can be covered. Therefore the 2n 2n board can be covered.

(b) (8 pts.) Illustrate the covering produced by the algorithm for n = 3 (that is, 23 23 board).

Initial Board

TopLevel Divide

NextLevel Divide

Polished Final Tiling

CS 610, Sec 102, Spring 2007, Midterm Exam (Solution) 5. (a) (10 pts.) Insert the following sequence of elements into an AVL tree, starting with an empty tree. Show the result after each balancing operation. 70, 40, 90, 50, 45, 60, 55, 52

70 40 50 45 90 40 45

70 90 50 40 45

70 90 50 60 40 45

50 70 60 90 40 45

50 70 60 55 52 90 40 45

50 70 55 52 60 90

(b) (10 pts.) Show the nal AVL tree from part (a) below. Then, delete 40. Show the AVL tree resulting from this deletion. (Show the work.)

50 45 40 52 55 60 70 90 45

50 70 55 52 60 90 45 50 52

55 70 60 90

Você também pode gostar