a4_solution

# D provide pseudocode for the dynamic programming

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ans that the answer is yes. d Provide pseudocode for the dynamic programming algorithm that builds the solution table L(i, s) and returns the correct answer to the ﬁnal problem. This needs to be 1, otherwise, the 1 L(i, 0) = 0 for i = 1, · · · , n L(0, s) = 0 for s = 1, · · · , t for s = 1, · · · , t algorithm will never find a 1 value for any i and s. 1 for i = 1, · · · , n s = max{0, s − ai } L(i, s) = max{L(i − 1, s), L(i − 1, s )} end for end for replace with if a_i > s L(i,s) = L(i-1,S) else L(i,s) = max{L(i-1,s), L(i-1, s-a_i)} e Modify the pseudocode such that it will not only return the correct “yes”, “no” answer, but also return the exact subset if the answer is “yes”. L(i, 0) = 0 for i = 1, · · · , n L(0, s) = 0 for s = 1, · · · , t for s = 1, · · · , t for i = 1, · · · , n s = max{0, s − ai } if L(i − 1, s ) > L(i − 1, s) then A(i, s) = 1 otherwise A(i, s) = 0 L(i, s) = max{L(i − 1, s), L(i − 1, s )} end for end for replace with if a_i > s L(i,s) = L(i-1,s); A(i, s) = 0 else if L(i-1,s) > L(i-1, s-a_i) L(i,s) = L(i-1,s); A(i,s) = 0 else L(i,s) = L(i-1, s-a_i); A(i,s)=1 if L(n, t) is 1, i.e., there is a subset that sums up to the target sum t, the following code could be used to ﬁnd the selected subset. s = t; i = n; selected = {} while s > 0 and i > 0 if A(s, i) == 1 add i to selected i=i−1 s = s − ai else i = i − 1 end if 2 min {(200-(a_i-a_j))^2 + D(j)} 0<=j<i...
View Full Document

## This note was uploaded on 11/05/2013 for the course CS 325 taught by Professor Staff during the Winter '08 term at Oregon State.

Ask a homework question - tutors are online