This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: COMP 271 Design and Analysis of Algorithms 2004 Fall Semester Solutions to Question Bank Number 3 (Selected Problems) 4. (a) Consider the case where wt [ i ] = 1 for all i (the worst case must be at least as bad as this special case). The proof boils down to observing that the recursion tree is a complete binary tree whose height is essentially h = min( n, W ). The number nodes of will be 2 h . More formally, let T ( i, W ) denote the running time of the algorithm for a given pair i and W . We can see that we have the following recurrence (up to constant factors): T ( i, W ) = braceleftBigg 1 if i = 0 or w < T ( i 1 , W ) + T ( i 1 , W 1) otherwise. It is an easy induction proof that T ( i, W ) ≥ 2 min( i,W ) . The basis case i = 0 or W = 0 is trivial. For the induction step we have T ( i, W ) ≥ T ( i 1 , W ) + T ( i 1 , W 1) ≥ 2 min( i 1 ,W ) + 2 min( i 1 ,W 1) ≥ 2 · 2 min( i 1 ,W 1) = 2 · 2 min( i,W ) 1 = 2 · 2 min( i,W ) / 2 = 2 min( i,W ) . (b) The problem with the recursive version is that it recomputes many of the same function values over and over again. Again assume that wt [ i ] = 1 for all i . Let R ( i, W ) be a shorthand for the call with parameters i and W . R ( i, W ) calls R ( i 1 , W ) and R ( i 1 , W 1). Both of these call R ( i 2 , W 1). As you trace the algorithm deeper, you will see that the same procedure is invoked over and over again. The dynamic programming version avoids this duplication, since once a value has been computed for a given i and W , this effort is never repeated. 5. Let X = < x 1 , . . ., x n > be the given sequence of n numbers. We need to find the longest increasing subsequence in X ....
View
Full
Document
This note was uploaded on 10/18/2009 for the course COMP 271 taught by Professor Arya during the Spring '07 term at HKUST.
 Spring '07
 ARYA
 Algorithms, C Programming

Click to edit the document details