{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

old-midterm2-solutions

# old-midterm2-solutions - CS 173 Spring 2010 Midterm 2...

This preview shows pages 1–3. Sign up to view the full content.

CS 173, Spring 2010 Midterm 2, Solutions Problem 1: Short answer (11 points) The following require only short answers. Justification/work is not required, but may increase partial credit if your short answer is wrong. (a) Prof. Moriarty has discovered an algorithm to sort n numbers in decreasing order in O ( n 1 . 6 ) time. Is this the fastest such sorting algortithm? Solution: No. Mergesort runs in O ( n log n ) which is faster. (b) What is the big-theta running time of the algorithm that solves the towers of Hanoi problem? Solution: Θ(2 n ) (c) If the root node in a full m -ary tree is an internal node, what is the minimum number of leaves in the tree? Solution: m . The root must have at least one child, so it must have m children (since the tree is full). (d) (3 points) If a binary tree T has n leaves, what are the minimum and maximum height possible heights for T ? Solution: Minimum height is log 2 n . There is no maximum height, because some large piece of the tree might be a vertical sequence of nodes with a single child, which increases the height without adding leaves. (e) Is the worst-case running time of linear search Ω(log n )? Solution: Yes. Its worst-case running time is Θ( n ) which is Ω(log n ).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Problem 2: Short proof I (7 points) (a) (4 points) Prove that n i =0 2 i is O (2 n ). Prove this directly from the definition of what it means for a function f to be O ( g ) (where g is another function), being careful to justify your algebraic steps and put them into logical order. Solution: Let k = 1 and C = 2. Then, for any n k , n i =0 2 i = 2 n +1 1 2 n +1 = C (2 n ). Since n i =0 2 i C (2 n ) for any n k , n i =0 2 i is O (2 n ). (b) (3 points) Suppose that g : A B and f : B C . Use a concrete counter-example to disprove the claim that if f g is one-to-one, then f is one-to-one. Solution: Suppose that A = { 1 } , B = { a, b, c } and C = { 7 , 8 } . Now, let g (1) = a and f ( a ) = f ( b ) = f ( c ) = 8. Then f g is one-to one, but f is not. [A large range of different examples would work here, but it is critical to use a specific concrete example rather than a general argument.] Problem 3: Short proof II (8 points) (a) (4 points) Let g : N N be onto, and let f : N 2 Z be defined by f ( n, m ) = ( m 1) g ( n ) Prove that f is onto. Solution: Suppose that x is any integer. Let z = | x | . Since g is onto, there is a natural number y such that g ( y ) = z . Then if x is non-negative, f ( y, 2) = (2 1) g ( y ) = g ( y ) = z = x . So f ( y, 2) = x . If x is negative, then f ( y, 0) = (0 1) g ( y ) = g ( y ) = z = x .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}