Академический Документы
Профессиональный Документы
Культура Документы
1 + OPT(j)
We want the option that gives the longest length: the maximum of all the lengths given by
theses options.
OPT(i) = 1 + max OPT(j)
j>i
if aj >ai
length of the longest increasing going through each following length of the longest increasing
subsequence starting at ai item aj greater than ai subsequence starting at aj
Pseudocode.
// base case
OPT(n) = 1
// main loop
for i from n downto 1:
OPT[i] = 1 + max([OPT[j] for j > i if a[i] < a[j]])
// final result
return max([OPT[i] for i from 1 to n])
1
CSE 101 Winter 12 Dynamic Programming examples
Time complexity.
and for each subproblem i, we go through all the following items to get the maximum,
which is O(n i).
Finally, getting the maximum of all OPTs takes O(n) operations. Therefore the total com-
plexity is O(n2 ).
S - N O W Y - S N O W - Y
S U N N - Y S U N - - N Y
cost 3 cost 5
Subproblems definition. The subproblems that we consider are the minimum edit distance
OPT(i, j) between some prefix of the first string, x[1...i], and some prefix of the second, y[1...j].
x[i] or - or x[i]
- y[j] y[j]
First option: (x[i], -). The cost of this alignment would be:
1 + OPT(i 1, j)
Second option: (-, y[j]). The cost of this alignment would be:
1 + OPT(i, j 1)
Third option: (x[i], y[j]). The cost of this alignment would be:
2
CSE 101 Winter 12 Dynamic Programming examples
We want the option that gives the smallest cost: it will be the minimum between all the
costs given by these 3 options.
OPT(i, j) = min 1 + OPT(i 1, j) , 1 + OPT(i, j 1) , diff(x[i], y[j]) + OPT(i 1, j 1)
We can now fill the matrix of values for OPT(i, j) and return the last value OPT(m, n).
Pseudocode.
// base case
for i from O to m:
OPT[i, 0] = i
for j from 0 to n:
OPT[0, j] = j
// main loop
for i from 1 to m:
for j from 1 to n:
E[i, j] = min(1 + E[i-1, j], 1 + E[i, j-1], diff(i, j) + E[i-1, j-1])
// final result
return OPT[m, n]
Complexity.
3
CSE 101 Winter 12 Dynamic Programming examples
Recursive formulation. What was the last item put in the burglars knapsack? If we knew
it was wi then we would know the maximum value achievable with a knapsack of capacity w
would be:
OPT(w) = OPT(w wi ) + vi
As we dont know which item is the last one put in the knapsack, we have to try all the
possibilities and take the one that gives the maximum value:
Pseudo-code.
// base case
OPT[0] = 0
// main loop
for w = 1...W:
OPT[w] = max([OPT[w - w[i]] + v[i] for i = 1...n if w[i] <= w])
// final result
return OPT[W]
Complexity.
4
CSE 101 Winter 12 Dynamic Programming examples
either item i is needed in the optimal solution, then the value would be
OPT(w wi , i 1) + vi
or the item i is not needed, then the value would be OPT(w, i 1).
As we try to maximise the value of the knapsack, we take the maximum of these values:
i, OPT(0, i) = 0
w, OPT(w, 0) = 0
Pseudo-code.
// base cases
for i = 1...n:
OPT[0, i] = 0
for w = 1...W:
OPT[w, 0] = 0
// main loop
for i = 1...n:
for w = 1...W:
if w[i] > w:
OPT[w, i] = OPT[w, i-1]
else:
OPT[w, i] = max(OPT[w - w[i], i-1] + v[i], OPT[w, i-1])
// final result
return OPT[W, n]
Complexity.
5
CSE 101 Winter 12 Dynamic Programming examples
either location i is not used, then the maximum profit OPT(i) would be OPT(i 1),
or the location i is used, then maximum profit would be pi + OPT(i ) with i the largest
index j such that mj mi k (the location preceding i and at least k miles apart from
it).
Because we are interested in the maximum profit, we take the maximum of these values:
Pseudo-code.
// base case
OPT[0] = 0
// main loop
for i = 1...n:
for j = i...1:
if m[j] <= m[i] - k:
i_star = j
break
OPT[i] = max(OPT[i-1], p[i] + OPT[i_star])
// final result
return OPT[n]
Complexity.
6
CSE 101 Winter 12 Dynamic Programming examples
Recursive formulation. The longest substring terminating at xi and yj must include xi and
yj . Hence:
So we get
OPT(i 1, j 1) + 1 if xi = yj
OPT(i, j) =
0 otherwise
And the base case are:
OPT(0) = 0
i, OPT(i, 0) = 0
j, OPT(0, j) = 0
Pseudo-code.
// base case
OPT[0] = 0
for i = 1...n:
OPT[i, 0] = 0
for j = 1...m:
OPT[0, j] = 0
// main loop
for i = 1...n:
for j = 1...m:
if x[i] = y[j]:
OPT[i, j] = OPT[i-1, j-1] + 1
else:
OPT[i, j] = 0
// final result
return OPT[n, m]
Complexity.
7
CSE 101 Winter 12 Dynamic Programming examples
Recursive formulation. What was the last coin to be chosen? If we knew it was xi then
we would know that the maximum value achievable with a capacity of k would be:
OPT(k) = OPT(k xi ) + xi
As we dont know which coin is the last one to be chosen, we have to try all the possibilities
and take the one that gives the maximum value:
Pseudo-code.
// base case
OPT[0] = 0
// main loop
for k = 1...v:
OPT[k] = max([OPT[k - x[i]] + x[i] for i = 1...n if x[i] <= k])
// final result
return (OPT[v] == v)
Complexity.