Sunteți pe pagina 1din 3

CSE 548 Analysis of Algorithms Homework 7

Anshul Anshul (aanshul) Amit Khandelwal (amkhandelwal)

October 15, 2014

1. Write down a recursive, memoized version of longest increasing subsequence algorithm. Your algorithm should take the form of pseudocode similar to what you see in lecture slides. The input to your algorithm

is an array A[1

parameter i and return the length of the longest increasing subsequence of A[1 Unlike the version discussed in class, do not explicitly construct the DAG. In fact, do not use any auxiliary data structure at all your program should not use any arrays, list, or other data structures beyond the input array A. Assume that memoization is handled implicitly, i.e., once you call LIS(i), the returned value will be saved using some mechanism that you dont need to know about, so that

The top-level function in your algorithm should be called LIS. It should take a



repeat calls to LIS with the same i will immediately return with the value computed by the first call. What is the complexity of your recursive version?

Algorithm function: LIS (A, n, max) Input: A - represents the array of numbers A[1 n] n - number of elements in the A. max - pointer max will contain the maximum length till now. Initially its set to zero Output: returns length of the longest increasing subsequence of A[1 n]

Algorithm 1 Algorithm to return length of longest increasing subsequence.

1: procedure LIS (A, n, max)


if n == 1 then


return 1


Set maxEndingHere = 1



i = 1 to n do


temp = LIS(A, i, max)


if A[i 1] < A[n 1] and temp + 1 > maxEndingHere then


maxEndingHere = temp + 1


if (max < maxEndingHere) then


max = maxEndingHere


return maxEndingHere

Complexity of algorithm:- The time complexity of the above algorithm would be O(n 2 ). The node at top level will have (n 1) nodes below it. Then each node at next level will have further nodes below it. But we will be expanding each LIS[k]foronce. The expansion will be like as show below in the image.


Figure 1: At the 2nd level, there will be n − 1 expansions. Then at

Figure 1:

At the 2nd level, there will be n 1 expansions. Then at the next level, there will be only n 2

expansions. This will continue till the last level. So the total expansions would be (n1)+(n2)+ This is equal to n (n 1)/2 expansions. Hence the time complexity of the memoized version will come O(n 2 ).


2. A contiguous subsequence of a list S is a subsequence made up of consecutive elements of S. For

subsequence but 5, 15, 40

a n .

Output: The contiguous subsequence of maximum sum (a subsequence of length zero has sum zero).

For the preceding example, the answer would be 10, 5, 40, 10, with a sum of 55

Algorithm function: SubSeq (S, n) Input: S - S represents the array of numbers x 1 , n - number of elements in the S. Output: returns maximum sum with the continuous subsequence

is not.

instance, if S is 5, 15, 30, 10, 5, 40, 10, then 15, 30, 10 is a contiguous

Give a linear-time algorithm for the following task: Input: A list of numbers, a 1 , a 2 ,






Algorithm 2 Algorithm to return contiguous subsequence of maximum sum.

1: procedure SubSeq (S, n)


set maxEndingHere = S[0]


set maxSum = S[0]


for i = 1 to n do


maxEndingHere = MAX(maxEndingHere + S[i], S[i])


maxSum = MAX(maxEndingHere, maxSum)


return maxSum

Complexity of algorithm:- The time complexity of the above algorithm would be O(n) since there is only single for loop. So the


complexity is linear.

, x n , we wish to make change for a value

v; that is, we wish to find a set of coins whose total value is v. This might not be possible: for instance, if the denominations are 5 and 10 then we can make change for 15 but not for 12. Give an O(nv) dynamic-programming algorithm for the following problem.

Input: x 1 ,

Question: Is it possible to make change for v using coins of denominations x 1 ,

Algorithm function: ChangePossible (coin[], v)

Input: coin[] - represents the denomination array of size n having values x 1 ,

v - given sum.

Output: Return T rue if the given sum can be formed by available denominations otherwise returns

F alse

3. Given an unlimited supply of coins of denominations x 1 , x 2 ,


x n ; v.

, x n ?


x n


Algorithm 3 Algorithm to check if the sum can be formed from available denominations.

1: procedure ChangePossible (coin[], v)


Create an array Table[v + 1][n + 1]


for i = 0 to v do


for j = 0 to n do


if (i == 0) then


Table[i][j] = True


else if (i

= 0) and (j == 0) then


Table[i][j] = False




X = ((i - Coin[j-1]) 0) ? Table[i- Coin[j-1]][j] : False)


Y = j 0 ? Table[i][j-1] :



Table[i][j] = X U Y


return Table[v][n]

Complexity of algorithm:- The time complexity of the above algorithm would be O(nv) since there are two nested for loops of size n and v. Space complexity would be O(nv) because we are using an array of size n v.