Вы находитесь на странице: 1из 8

CSE 101 Winter 12 Dynamic Programming examples

Section 6.2. Longest increasing subsequence (lots of de-


tails)
Problem. The input is a sequence of numbers a1 , . . . , an . A subsequence is any subset of
these numbers taken in order. An increasing subsequence is one in which the numbers are
getting strictly larger. The task is to find the length of the longest increasing subsequence.

Subproblems definition. The subproblems that we


consider are the suffixes of the the given input.
Method

a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 We are going to find the solutions


to all the subproblems. Then we
We call OPT(i) the length of the longest increasing will return the last one.
subsequence starting at ai . We assume we know already the
Then we will return the biggest OPT(i). solutions of some subproblems
(OPT(j), in blue) to find the
Recursive formulation. Starting at ai , the next solution to the next subproblem
item can be any element: (OPT(i), in red).
following ai ,
and greater than ai .
Lets consider one option: let aj be the next item in the longest increasing subsequence
starting at ai . Then the length of this sequence ai aj . . . will be:

1 + OPT(j)

for the step length of the longest increasing


ai aj subsequence starting at aj

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.

We have n subproblems (main loop in the algorithm),

and for each subproblem i, we go through all the following items to get the maximum,
which is O(n i).

So the total complexity of the main loop is


n n
!  
X X n(n 1)
O(i) = O i =O = O(n2)
i=1 i=1
2

Finally, getting the maximum of all OPTs takes O(n) operations. Therefore the total com-
plexity is O(n2 ).

Section 6.3. Edit distance (lots of details)


Problem. The cost of an alignment of 2 strings is the number of columns in which the letters
differ. And the edit distance between two strings is the cost of their best possible alignment.
The task is to find the minimum edit distance between 2 given strings x[1..m] and y[1..n].

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].

Recursive formulation. For the rightmost letter alignment, we have 3 possibilities:

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)

for the alignment of edit distance of


the rightmost column x[1...i 1] and y[1...j]

Second option: (-, y[j]). The cost of this alignment would be:

1 + OPT(i, j 1)

for the alignment of edit distance of


the rightmost column x[1...i] and y[1...j 1]

Third option: (x[i], y[j]). The cost of this alignment would be:

diff(x[i], y[j]) + OPT(i 1, j 1)

2
CSE 101 Winter 12 Dynamic Programming examples

for the alignment of edit distance of


the rightmost column: x[1...i 1] and y[1...j 1]
1 if x[i] 6= y[j]
0 if x[i] = y[j]

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)

edit distance between First option Second option Third option


x[1...i] and y[1...j]

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.

There are mn cells in the table,

and for each cell we perform 3 constant time operations.

So the overall complexity will be O(mn).

Section 6.4. Knapsack with repetition


Problem. During a robbery, a burglar finds much more loot than he had expected and has
to decide what to take. His bag (or knapsack) will hold a total weight of at most W pounds.
There are n items to pick from, of weight w1 , ...wn and dollar value v1 , ..., vn . Whats the most
valuable combination of items he can fit into his bag?
With repetition: we suppose there is an infinite number of each item (we can repeat each
item as many time as we want).

3
CSE 101 Winter 12 Dynamic Programming examples

Suproblems definition. Let OPT(w) be maximum value achievable with a knapsack of


capacity w.

1 2 ... ... w ... ... W

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:

OPT(w) = max {OPT(w wi ) + vi }


i
wi w

And the base case is OPT(0) = 0.

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.

We have O(W ) subproblems,

each subproblem takes time O(n).

Therefore the overall complexity is O(nW ).

Section 6.4. Knapsack without repetition


Problem. Same as previous except that there is only one item of each type (without repeti-
tion).
Note that if there was a limited number of each item, we would just have to deal with each
item as it was unique.

Suproblems definition. Let OPT(w, i) be maximum value achievable with a knapsack of


capacity w with items 1, ..., i.

1 2 ... ... w ... ... W


1 2 ... ... i ... ... n

4
CSE 101 Winter 12 Dynamic Programming examples

Recursive formulation. We have 2 possibilities:

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:

OPT(w) = max {OPT(w wi , i 1) + vi , OPT(w, i 1)}

And the base cases are:

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.

We have O(nW ) subproblems,

each subproblem takes time O(1).

Therefore the overall complexity is O(nW ).

Exercise 6.3. Yuckdonalds maximum profit


Suproblems definition. Let OPT(i) be the maximum profit which Yuckdonalds can obtain
from locations 1 to i.

p1 p2 ... ... pi ... ... pn

5
CSE 101 Winter 12 Dynamic Programming examples

Recursive formulation. There is 2 options:

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:

OPT(i) = max {OPT(i 1), pi + OPT(i )}


And the base case is OPT(0) = 0.

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.

We have O(n) subproblems,

each subproblem takes time O(n).

Therefore the overall complexity is O(n2 ).

Exercise 6.8. Longest common substring


Suproblems definition. Let OPT(i, j) be the length of the longest common substring of x
and y terminating at xi and yj .

x1 x2 ... ... ... xi ... xn


y1 y2 ... ... ... yj ... ym

6
CSE 101 Winter 12 Dynamic Programming examples

Recursive formulation. The longest substring terminating at xi and yj must include xi and
yj . Hence:

the length will be 0 if these characters are different,

and OPT(i 1, j 1) + 1 if they are equal.

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.

We have O(n2 ) subproblems,

each subproblem takes time O(1).

Therefore the overall complexity is O(n2 ).

Exercise 6.17. Money change


This problem reduces to Knapsack with repetitions. The total capacity is v and there is an
item i of value xi and weight xi for each coin denomination. It is possible to make change for
value v if and only if the maximum value we can fit in v is v.

7
CSE 101 Winter 12 Dynamic Programming examples

Suproblems definition. Let OPT(k) be maximum value achievable with a capacity of k.

1 2 ... ... k ... ... v

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:

OPT(k) = max {OPT(k xi ) + xi }


i
xi k

And the base case is OPT(0) = 0.

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.

We have O(v) subproblems,

each subproblem takes time O(n).

Therefore the overall complexity is O(nv).

Вам также может понравиться