Sei sulla pagina 1di 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..n]. The top-level function in your algorithm should be called LIS. It should take a
parameter i and return the length of the longest increasing subsequence of A[1..i].
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
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?
Ans 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)
2:
if n == 1 then
3:
return 1
4:
Set maxEndingHere = 1
5:
for i = 1 to n do
6:
temp = LIS(A, i, max)
7:
if A[i 1] < A[n 1] and temp + 1 > maxEndingHere then
8:
maxEndingHere = temp + 1
9:
if (max < maxEndingHere) then
10:
max = maxEndingHere
11:
return maxEndingHere

Complexity of algorithm:The time complexity of the above algorithm would be O(n2 ). 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]f oronce. 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 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)+....+1.
This is equal to n (n 1)/2 expansions.
Hence the time complexity of the memoized version will come O(n2 ).
2. A contiguous subsequence of a list S is a subsequence made up of consecutive elements of S. For
instance, if S is 5, 15, 30, 10, 5, 40, 10, then 15, 30, 10 is a contiguous subsequence but 5, 15, 40
is not. Give a linear-time algorithm for the following task: Input: A list of numbers, a1 , a2 , ..., an .
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
Ans Algorithm function: SubSeq (S, n)
Input: S - S represents the array of numbers x1 , ..., xn
n - number of elements in the S.
Output: returns maximum sum with the continuous subsequence

Algorithm 2 Algorithm to return contiguous subsequence of maximum sum.


1: procedure SubSeq (S, n)
2:
set maxEndingHere = S[0]
3:
set maxSum = S[0]
4:
for i = 1 to n do
5:
maxEndingHere = M AX(maxEndingHere + S[i], S[i])
6:
maxSum = M AX(maxEndingHere, maxSum)
7:
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
2

complexity is linear.
3. Given an unlimited supply of coins of denominations x1 , x2 , ..., xn , 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: x1 , ..., xn ; v.
Question: Is it possible to make change for v using coins of denominations x1 , ..., xn ?
Ans Algorithm function: ChangePossible (coin[], v)
Input: coin[] - represents the denomination array of size n having values x1 , ..., xn
v - given sum.
Output: Return T rue if the given sum can be formed by available denominations otherwise returns
F alse

Algorithm 3 Algorithm to check if the sum can be formed from available denominations.
1: procedure ChangePossible (coin[], v)
2:
Create an array Table[v + 1][n + 1]
3:
for i = 0 to v do
4:
for j = 0 to n do
5:
if (i == 0) then
6:
Table[i][j] = True
7:
else if (i 6= 0) and (j == 0) then
8:
Table[i][j] = False
9:
else
10:
X = ((i - Coin[j-1]) 0) ? Table[i- Coin[j-1]][j] : False)
11:
Y = j 0 ? Table[i][j-1] : False
12:
Table[i][j] = X U Y
13:
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.

Potrebbero piacerti anche