Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Dynamic programming.
Curs 2015
Fibonacci Recurrence.
n-th Fibonacci TermINPUT: n ∈ natQUESTION: ComputeFn = Fn−1 + Fn−2
Recursive Fibonacci (n)if n = 0 then
return 0else if n = 1 then
return 1else
(Fibonacci(n − 1)++Fibonacci(n − 2))
end if
Computing F7.
As Fn+1/Fn ∼ (1 +√
5)/2 ∼ 1.61803 then Fn > 1.6n, and tocompute Fn we need 1.6n recursive calls.
F(0)
F(7)
F(6)
F(5)
F(4)
F(3)
F(2)
F(1)
F(1)
F(2)
F(1)
F(3)
F(2) F(1)
F(4)
F(5)
F(4)F(3)
F(0)
F(0)F(0)
F(0)
A DP algorithm.
To avoid repeating multiple computations of subproblems, carrythe computation bottom-up and store the partial results in a table
DP-Fibonacci (n) Construct tableF0 = 0F1 = 1for i = 1 to n doFi = Fi−1 + Fi−2
end for
13
F[0]
F[1]
F[2]
F[3]
F[4]
F[5]
F[6]
F[7]
0
1
1
2
3
5
8
To get Fn need O(n) time and O(n) space.
F(1)
F(7)
F(6)
F(5)
F(4)
F(3)
F(2)
F(1)
F(0)
F(1)
F(2)
F(0)F(1)
F(3)
F(2) F(1)
F(4)
F(5)
F(4)F(3)
F(0)
I Recursive (top-down) approach very slow
I Too many identical sub-problems and lots of repeated work.
I Therefore, bottom-up + storing in a table.
I This allows us to look up for solutions of sub-problems,instead of recomputing. Which is more efficient.
Dynamic Programming.
Richard Bellman: An introduction to thetheory of dynamic programming RAND, 1953Today it would be denoted Dynamic Planning
Dynamic programming is a powerful technique of divide andconquer type, for efficiently computing recurrences by storingpartial results and re-using them when needed.
Explore the space of all possible solutions by decomposing thingsinto subproblems, and then building up correct solutions to largerproblems.
Therefore, the number of subproblems can be exponential, but ifthe number of different problems is polynomial, we can get apolynomial solution by avoiding to repeat the computation of thesame subproblem.
Properties of Dynamic Programming
Dynamic Programming works when:
I Optimal sub-structure: An optimal solution to a problemcontains optimal solutions to subproblems.
I Overlapping subproblems: A recursive solution contains asmall number of distinct subproblems. repeated many times.
Difference with greedyGreedy problems have the greedy choice property: locally optimalchoices lead to globally optimal solution.For DP problems greedy choice is not possible globally optimalsolution requires back-tracking through many choices.
Guideline to implement Dynamic Programming
1. Characterize the structure of an optimal solution: make surespace of subproblems is not exponential. Define variables.
2. Define recursively the value of an optimal solution: Find thecorrect recurrence formula , with solution to larger problem asa function of solutions of sub-problems.
3. Compute, bottom-up, the cost of a solution: using therecursive formula, tabulate solutions to smaller problems, untilarriving to the value for the whole problem.
4. Construct an optimal solution: Trace-back from optimal value.
Implementtion of Dynamic Programming
Memoization: technique consisting in storing the results ofsubproblems and returning the result when the same sub-problemoccur again. Technique used to speed up computer programs.
I In implementing the DP recurrence using recursion could bevery inefficient because solves many times the samesub-problems.
I But if we could manage to solve and store the solution tosub-problems without repeating the computation, that couldbe a clever way to use recursion + memoization.
I To implement memoization use any dictionary data structure,usually tables or hashing.
Implementtion of Dynamic Programming
I The other way to implement DP is using iterative algorithms.
I DP is a trade-off between time speed vs. storage space.
I In general, although recursive algorithms ha exactly the samerunning time than the iterative version, the constant factor inthe O is quite more larger because the overhead of recursion.On the other hand, in general the memoization version iseasier to program, more concise and more elegant.
Top-down: Recursive and Bottom-up: Iterative
Weighted Activity Selection Problem
Weighted Activity Selection ProblemINPUT: a set S = 1, 2, . . . , n of activities to be processed by asingle resource. Each activity i has a start time si and a finish timefi , with fi > si , and a weight wi .QUESTION: Find the set of mutually compatible such that itmaximizes
∑i∈S wi
Recall: Greedy strategy not always solved this problem.
6
1 5
10
6
10
Notation for the weighted activity selection problem
We have 1, 2, . . . , n activities with f1 ≤ f2 ≤ · · · ≤ fn andweights wi.
Define p(j) to be the largest integer i < j such that i and j aredisjoints (p(j) = 0 if no disjoint i < j exists).
Let Opt(j) be the value of the optimal solution to the problemconsisting of activities in the range 1 to j . Let Oj be the set ofjobs in optimal solution for 1, . . . , j.
13
1
2
3456
p(1)=0
p(2)=0p(3)=1p(4)=0p(5)=3
p(6)=3
1
2
23
1
2
0 1 2 3 4 5 6 7 8 9 10 11 12
Recurrence
Consider sub-problem 1, . . . , j. We have two cases:1.- j ∈ Oj :
I wj is part of the solution,
I no jobs p(j) + 1, . . . , j − 1 are in Oj ,
I if Op(n) is the optimal solution for 1, . . . , p(n) thenOj = Op(n) ∪ j (optimal substructure)
2.- j 6∈ Oj : then Oj = Oj−1
Opt(j) =
0 if j = 0
max(Opt(p(j)) + wj),Opt(j − 1) if j ≥ 1
Recursive algoritm
Considering the set of activities A, we start by a pre-processingphase: sorting the activities by increasing fini=1 and computingand tabulating P[j ].The cost of the pre-computing phase is: O(n lg n + n)
Therefore we assume A is sorted and all p(j) are computer andtabulated in P[1 · · · n]
R-Opt (j)if j = 0 then
return 0else
return max(wj + R-Opt(p(j)),R-Opt(j − 1))end if
Recursive algorithm
Opt(2)
Opt(3)
Opt(1)
Opt(1)
Opt(2)
Opt(6)
Opt(5)
Opt(4)Opt(3)
Opt(3)
Opt(1)
Opt(1) Opt(1)
Opt(1)
What is the worst running time of this algorithm?: O(2n)
Iterative algorithm
Assuming we have as input the set A of n activities sorted byincreasing f , each i with si ,wi and the values of p(j) tabulated,
I-Opt (n)Define a 2× n table M[]for j = 1 to n do
M[j ] = max(M[P[j ]] + wj ,M[j − 1])end forreturn M[n]
Notice: this algorithm gives only the numerical max. weight
Time complexity: O(n)(not counting the O(n lg n) pre-process)
Iterative algorithm: Example
i si fi wi P
1 1 5 1 02 0 8 2 03 7 9 2 14 1 11 3 05 9 12 1 36 10 13 2 3
5
0 1 2 3 4 5 6
M 3320 1 3
Iterative algorithm: Returning the selected activities
List-Sol (n)Define a 2× n table M[]for j = 1 to n doM[j ] = max(M[P[j ]] + wj ,M[j − 1])if M[P[j ]] + wj > M[j − 1] then
return jend if
end forreturn M[n]
In the previous example we get : 1,3,6 and value=5
The DP recursive algorithm: Memoization
Notice we only have to solve a limited number of n differentsubproblems: from Opt(1) to Opt(n).A memoize recursive algorithm stores the solution for eachsub-problem solution , using a dictionary DS. Therefore it coulduse tables, hashing,. . .In the algorithm below we use a table M[]
Mem-Find-Sol (j)if j = 0 then
return 0else if M[j ] 6= ∅ then
return M[j ]elseM[j ] = max(Mem-Opt(P[j ]) + wj ,Mem-Opt(j − 1))return M[j ]
end if
Time complexity: O(n)
The DP recursive algorithm: Memoization
To get also the list of selected activities:
Mem-Opt (j)if j = 0 then
return 0else if M[P[j ]] + wj > M[j − 1] then
return j together with Mem-Find-Sol(P[j ])else
return Mem-Find-Sol(j − 1)end if
Time complexity: O(n)
0-1 Knapsack
0-1 KnapsackINPUT:a set I = in1 of items that can NOT be fractioned, each iwith weight wi and value vi . A maximum weight W permissibleQUESTION: select the items to maximize the profit.
Recall that we can NOT take fractions of items.
Characterize structure of optimal solution and definerecurrence
Need a new variable wLet v [i ,w ] be the maximum value (optimum) we can get fromobjects 1, 2, . . . , i and taking a maximum weight of w (theremaining free weight).We wish to compute v [n,W ].
To compute v [i ,w ] we have two possibilities:I That the i-th element is not part of the solution, then we go
to compute v [i − 1,w ],I or that the i-th element is part of the solution. we add vi and
call
This gives the recurrence,
v [i , j ] =
0 if i = 0 or w = 0
v [i − 1,w − wi ] + vi if i is part of the solution
v [i − 1,w ] otherwise
Iterative algorithm
Define a table M = v [1 . . . n, 0 . . .W ],
Knapsack(I ,W )for i = 1 to n − 1 dov [i , 0] := 0
end forfor i = 1 to n do
for w = 0 to W doif wi > w thenv [i ,w ] := v [i − 1,w ]
elsev [i ,w ] := maxv [i − 1,w ], v [i − 1,w − wi ] + vi
end ifend for
end forreturn v [n,W ]
The number of steps is O(nW ).
Example.
i 1 2 3 4 5
wi 1 2 5 6 7
vi 1 6 18 22 28W = 11.
w0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0 0 01 0 1 1 1 1 1 1 1 1 1 1 12 0 1 6 7 7 7 7 7 7 7 7 7
I 3 0 1 6 7 7 18 19 24 25 25 25 254 0 1 6 7 7 18 22 24 28 29 29 295 0 1 6 7 7 18 22 28 29 34 35 35
For instance,v [5, 11] = maxv [4, 10], v [4, 11− 7] + 28 = max7, 0 + 18 = 18.
Recovering the solution
a table M = v [1 . . . n, 0 . . .W ],Sol-Knaps(I ,W )Initialize MDevine a list L[i ,w ]for every cell [i ,w ]for i = 1 to n do
for w = 0 to W dov [i ,w ] := maxv [i − 1,w ], v [i − 1,w − wi ] + viif v [i − 1,w ] < v [i − 1,w − wi ] + vi then
Add vi ∪ L[i − 1,w − wi ] to L[i−,w ]end if
end forend forreturn v [n,W ] and the list of [i ,w ]
Complexity: O(nW ) + cost(L[i ,w ]).
Question: How would you implement the L[i ,w ]’s?
Solution for previous example
0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0 0 01 0 1 1(1) 1 1 1 1 1 1 1 1 12 0 1 6 7 7(1,2) 7 7 7 7 7 7 73 0 1 6 7 7(1,2) 18 19 24 25 25 25 254 0 1 6 7 7(1,2) 18 22 24 28 29 29 295 0 1 6 7 7 18 22 28 29 34 35 35 (1,2,5)
Question: could you come with a memoized algorithm?(Hint: use a hash data structure to store solution to sub-problems)
Complexity
The 0-1 Knapsack is NP-complete. Does it mean P=NP?
I An algorithm runs in pseudo-polynomial time if its runningtime is polynomial in the numerical value of the input, but itis exponential in the length of the input,
I Recall that given n ∈ Z the value is n but the length of therepresentation is dlg ne bits.
I 0-1 Knapsack, has complexity O(nW ), and its length isO(lg n + lgW ).
I If W = 2n the problem is exponential its the length. Howeverthe DP algorithm works fine when W = Θ(n).
I Consider the unary knapsack problem, where all integers arecoded in unary (7=1111111). In this case, the complexity ofthe DP algorithm is polynomial on the size. I.e.unary knapsack ∈P.
Multiplying a Sequence of Matrices
Multiplication of n matricesINPUT: A sequence of n matrices (A1 × A2 × · · · × An)QUESTION: Minimize the number of operation in thecomputation A1 × A2 × · · · × An
Recall that Give matrices A1,A2 with dim(A1) = p0 × p1 anddim(A2) = p1 × p2, the basic algorithm to A1 × A2 takes timep0 × p1 × p2 2 3
3 44 5
× [2 3 43 4 5
]=
13 18 2318 25 3223 32 41
Recall that matrix multiplication is NOT commutative, so we cannot permute the order of the matrices without changing the result,but it is associative, so we can put parenthesis as we wish.In fact, the problem of given A1, . . . ,An with dim (Ai ) = pi−1× pi ,how to multiply them to minimize the number of operations isequivalent to the problem of how to parenthesize the sequenceA1, . . .An.Example Consider A1 × A2 × A3, where dim (A1) = 10× 100dim (A2) = 100× 5 and dim (A3) = 5× 50.((A1A2)A3) = (10× 100× 5) + (10× 5× 50) = 7500 operations,(A1(A2A3)) = (100×5×50) + (10×100×50) =75000 operations.The order makes a big difference in real computation’s time.
How many ways to paranthesize A1, . . .An?A1 × A2 × A3 × A4:(A1(A2(A3A4))), ((A1A2)(A3A4)), (((A1(A2A3))A4),(A1((A2A3)A4))), (((A1A2)A3)A4))Let P(n) be the number of ways to paranthesize A1, . . .An. Then,
P(n) =
1 if n = 1∑n−1
k=1 P(k)P(n − k) si n ≥ 2
with solution P(n) = 1n+1
(2nn
)= Ω(4n/n3/2)
The Catalan numbers. Brute force will take too long!
1.- Structure of an optimal solution and recursive solution.
Let Ai−j = (AiAi+1 · · ·Aj).The parenthesization of the subchain (A1 · · ·Ak) within theoptimal parenthesization of A1 · · ·An must be an optimalparanthesization of Ak+1 · · ·An.
Notice,∀k , 1 ≤ k ≤ n, cost(A1−n) = cost(A1−k) + cost(Ak+1−n) + p0pkpn.
Let m[i , j ] the minimum cost of Ai × . . .× Aj . Then, m[i , j ] will begiven by choosing the k , i ≤ k ≤ j s.t. minimizesm[i , k] + m[k + 1, j ] + cost (A1−k × Ak+1−n).That is,
m[i , j ] =
0 if i = j
min1≤k≤jm[i , k] + m[k + 1, j ] + pi−1pkpj otherwise
2.- Computing the optimal costs
Straightforward implementation of the previous recurrence:As dim(Ai ) = pi−1pi , the imput is given by P =< p0, p1, . . . , pn >,
MCR (P, i , j)if i = j then
return 0end ifm[i , j ] :=∞for k = i to j − 1 do
q := MCR(P, i , k) + MCR(P, k + 1, j) + pi−1pkpjif q < m[i , j ] thenm[i , j ] := q
end ifend forreturn m[i , j ].
The time complexity if the previous is given byT (n) ≥ 2
∑n−1i=1 T (i) + n ∼ Ω(2n).
Dynamic programming approach.Use two auxiliary tables: m[1 . . . n, 1 . . . n] and s[1 . . . n, 1 . . . n].
MCP (P)for i = 1 to n do
m[i , i ] := 0end forfor l = 2 to n do
for i = 1 to n − l + 1 doj := i + l − 1m[i , j ] :=∞for k = i to j − 1 do
q := m[i , k] + m[k + 1, j ] + pi−1pkpjif q < m[i , j ] then
m[i , j ] := q, s[i , j ] := kend if
end forend for
end forreturn m, s.
T (n) = Θ(n3), and space = Θ(n2).
Example.
We wish to compute A1,A2,A3,A4 with P =< 3, 5, 3, 2, 4 >m[1, 1] = m[2, 2] = m[3, 3] = m[4, 4] = 0
i \ j 1 2 3 4
1 0
2 0
3 0
4 0
l = 2, i = 1, j = 2,k = 1 : q = m[1, 2] = m[1, 1] + m[2, 2] + 3.5.3 = 45 (A1A2)s[i , 2] = 1l = 2, i = 2, j = 3,k = 2 : q = m[2, 3] = m[2, 2] + m[3, 3] + 5.3.2 = 30 (A2A3)s[2, 3] = 2l = 2, i = 3, j = 4,k = 3 : q = m[3, 4] = m[3, 3] + m[4, 4] + 3.2.4 = 24 (A3A4)s[3, 4] = 3
i \ j 1 2 3 4
1 0 45
2 1 0 30
3 2 0 24
4 3 0
l = 3, i = 1, j = 3 :
m[1, 3] = min
(k = 1)m[1, 1] + m[2, 3] + 3.5.2 = 60 A1(A2A3)
(k = 2)m[1, 2] + m[3, 3] + 3.3.2 = 63 (A1A2)A3
s[1, 3] = 1, l = 3, i = 2, j = 4 :
m[2, 4] = min
(k = 2)m[2, 2] + m[3, 4] + 5.3.4 = 84 A2(A3A4),
(k = 3)m[2, 3] + m[4, 4] + 5.2.4 = 70 (A2A3)A4.
s[2, 4] = 3
i \ j 1 2 3 4
1 0 45 60
2 1 0 30 70
3 1 2 0 24
4 3 3 0
l = 4, i = 1, j = 4 :
m[1, 4] = min
(k = 1)m[1, 1] + m[2, 4] + 3.5.4 = 130 A1(A2A3A4),
(k = 2)m[1, 2] + m[3, 4] + 3.3.4 = 105 (A1A2)(A3A4),
(k = 3)m[1, 3] + m[4, 4] + 3.2.4 = 84 (A1A2A3)A4.
i \ j 1 2 3 4
1 0 45 60 84
2 1 0 30 70
3 1 2 0 24
4 3 3 3 0
3.- Constructing an optimal solutionWe need to construct an optimal solution from the information ins[1, . . . , n, 1, . . . , n]. In the table, s[i , j ] contains k such that theoptimal way to multiply:
Ai × · · · × Aj = (Ai × · · · × Ak)(Ak+1 × · · · × Aj).
Moreover, s[i , s[i , j ]] determines the k to get Ai−s[i ,j] ands[s[i , j ] + 1, j ] determines the k to get As[i ,j]+1−j . Therefore,A1−n = A1−s[1,n]As[1,n]+1−n.
Multiplication(A, s, i , j)if j > 1 then
X :=Multiplication (A, s, i , s[i , j ])Y :=Multiplication (A, s, s[i , j ] + 1, j)return X × Y
elsereturn Ai
end if
Therefore (A1(A2A3))A4.
Evolution DNA
Insertion
CT A A G T A C G
CT A A T A C G
CT A G A C G
A A C G
C
T C A G A C G
GACT
Mutation
Delete
Sequence alignment problem
?
CT A A G T A C G
A A C GGACT
Formalizing the problem
Longest common substring: Substring = chain of characterswithout gaps.
A
T C A GT T A G A
C T A T C A G
Longest common subsequence: Subsequence = ordered chain ofcharacters with gaps.
A
T C A GT T A G A
C T A T C A G
Edit distance: Convert one string into another one using a givenset of operations.
?
CT A A G T A C G
A A C GGACT
String similarity problem: The Longest CommonSubsequence
LCSINPUT: sequences X =< x1 · · · xm > and Y =< y1 · · · yn >QUESTION: Compute the longest common subsequence.
A sequence Z =< z1 · · · zk > is a subsequence of X if there is asubsequence of integers 1 ≤ i1 < i2 < . . . < ik ≤ m such thatzj = xij . If Z is a subsequence of X and Y , the Z is a commonsubsequence of X and Y .
Given X = ATATAT , then TTT is a subsequence of X
Greedy approach
LCS: Given sequences X =< x1 · · · xm > and Y =< y1 · · · yn >.Compute the longest common subsequence.
Greedy X ,YS := ∅for i = 1 to m do
for j = i to n doif xi = yj then
S := S ∪ xiend iflet yl such that l = mina > j |xi = yalet xk such that k = mina > i |xi = yaif ∃i , l < k then
do S := S ∪ xi, i := i + 1;j := l + 1S := S ∪ xk, i := k + 1; j := j + 1
else if not such yl , xk thendo i := i + 1 and j := j + 1.
end ifend for
end for
Greedy approach do not workFor X =A T C A C andY =C G C A C A C A C Tthe result of greedy is A T butthe solution is A C A C.
Greedy approach
LCS: Given sequences X =< x1 · · · xm > and Y =< y1 · · · yn >.Compute the longest common subsequence.
Greedy X ,YS := ∅for i = 1 to m do
for j = i to n doif xi = yj then
S := S ∪ xiend iflet yl such that l = mina > j |xi = yalet xk such that k = mina > i |xi = yaif ∃i , l < k then
do S := S ∪ xi, i := i + 1;j := l + 1S := S ∪ xk, i := k + 1; j := j + 1
else if not such yl , xk thendo i := i + 1 and j := j + 1.
end ifend for
end for
Greedy approach do not workFor X =A T C A C andY =C G C A C A C A C Tthe result of greedy is A T butthe solution is A C A C.
Dynamic Programming approach
Characterization of optimal solution and recurrenceLet X =< x1 · · · xn > and Y =< y1 · · · ym >.Let X [i ] =< x1 · · · xi > and Y [i ] =< y1 · · · yj >.Define c[i , j ] = length de la LCS of X [i ] and Y [j ].Want c[n,m] i.e. solution LCS X and YWhat is a subproblem?
Characterization of optimal solution and recurrence
Subproblem = something that goes part of the way in convertingone string into other.Given X =C G A T C and Y =A T A Cc[5, 4] = c[4, 3] + 1If X =C G A T and Y =A T A to find c[4, 3]:either LCS of C G A T and A Tor LCS of C G A and A T Ac[4, 3] = max(c[3, 3], c[4, 2])Given X and Y
c[i , j ] =
c[i − 1, j − 1] + 1 if xi = yj
max(c[i , j − 1], c[i − 1, j ]) otherwise
c[i , j ] =
c[i − 1, j − 1] + 1 if xi = yj
max(c[i , j − 1], c[i − 1, j ]) otherwise
c[0,1]
c[3,2]
c[3,1] c[2,2] c[2,1]
c[3,0] c[2,1] c[2,0] c[2,1] c[1,2] c[1,1]
c[2,0] c[1,1] c[1,0]
c[1,0] c[0,1] c[0,0]
c[1,1] c[0,2]
The direct top-down implementation of the recurrence
LCS (X ,Y )if m = 0 or n = 0 then
return 0else if xm = yn then
return 1+LCS (x1 · · · xm−1, y1 · · · yn−1)else
return maxLCS (x1 · · · xm−1, y1 · · · yn)end ifLCS (x1 · · · xm, y1 · · · yn−1)
The algorithm explores a tree of depth Θ(n + m), therefore thetime complexity is T (n) = 3Θ(n+m).
Bottom-up solution
Avoid the exponential running time, by tabulating the subproblemsand not repeating their computation.To memoize the values c[i , j ] we use a table c[0 · · · n, 0 · · ·m]Starting from c[0, j ] = 0 for 0 ≤ j ≤ m and from c[i , 0] = 0 from0 ≤ i ≤ n go filling row-by-row, left-to-right, all c[i , j ]
j
c[i,j-1]
c[i-1,j-1]i-1i
j-1
c[i-1,j]
c[i,j]
Use a field d [i , j ] inside c[i , j ] to indicate from where we use thesolution.
Bottom-up solution
LCS (X ,Y )for i = 1 to n do
c[i , 0] := 0end forfor j = 1 to m do
c[0, j ] := 0end forfor i = 1 to n do
for j = 1 to m doif xi = yj then
c[i , j ] := c[i − 1, j − 1] + 1, b[i .j ] :=else if c[i − 1, j ] ≥ c[i , j − 1] then
c[i , j ] := c[i − 1, j ], b[i , j ] :=←else
c[i , j ] := c[i , j − 1], b[i , j ] :=↑.end if
end forend for
Time and space complexity T = O(nm).
Example.
X=(ATCTGAT); Y=(TGCATA). Therefore, m = 6, n = 7
0 1 2 3 4 5 6T G C A T A
0 0 0 0 0 0 0 0
1 A 0 ↑0 ↑0 ↑0 1 ←1 1
2 T 0 1 ←1 ←1 ↑1 2 ←2
3 C 0 ↑1 ↑1 2 ←2 ↑2 ↑24 T 0 1 ↑1 ↑2 ↑2 3 ←3
5 G 0 ↑1 2 ↑2 ↑2 ↑3 ↑36 A 0 ↑1 ↑2 ↑2 3 ↑3 4
7 T 0 1 ↑2 ↑2 ↑3 4 ↑4
Construct the solution
Uses as input the table c[n,m].The first call to the algorithm is con-LCS (c, n,m)
con-LCS (c , i , j)if i = 0 or j = 0 then
STOP.else if b[i , j ] = then
con-LCS (c , i − 1, j − 1)return xi
else if b[i , j ] =↑ thencon-LCS (c , i − 1, j)
elsecon-LCS (c , i , j − 1)
end if
The algorithm has time complexity O(n + m).
Edit Distance.
Generalization of LCSThe edit distance between strings X = x1 · · · xn and Y = y1 · · · ymis defined to be the minimum number of edit operations needed totransform X into Y , where the set of edit operations is:
I insert(X , i , a) = x1 · · · xiaxi+1 · · · xn.
I delete(X , i) = x1 · · · xi−1xi+1 · · · xnI modify(X , i , a) = x1 · · · xi−1axi+1 · · · xn.
Example: x = aabab and y = babbaabab = XX ′ =insert(X , 0, b) baababX ′′ =delete(X ′, 2) bababY =delete(X ′′, 4) babb
A shorter distance.
X = aabab → Y = babbaabab = XX ′ =modify(X , 1, b) bababY =delete(X ′, 4) babbUse dynamic programming.
1.- Characterize the structure of an optimal solution andset the recurrence.
Assume want to find the edit distance from X = TATGCAAGTAto Y = CAGTAGTCConsider the prefixes TATGCA and CAGTLet E [6, 4] = edit distance between TATGCA and CAGT• Distance between TATGCA and CAGT is E [5, 4] + 1 (delete A)• Distance between TATGCA and CAGT is E [6, 5] + 1 (add A)• Distance between TATGCA and CAGTA is E [5, 4] (keep last Aand compare TATGC and CAGT ).
To compute the edit distance from X = x1 · · · xn to Y = y1 · · · ym:Let X [i ] = x1 · · · xi and Y [j ] = y1 · · · yjlet E [i , j ] = edit distance from X [i ] to Y [j ]If xi 6= yj the last step from X [i ]→ Y [j ] must be one of:
1. put yj at the end x : x → x1 · · · xiyj , and then transformx1 · · · xi into y1 · · · yj−1.
E [i , j ] = E [i , j − 1] + 1
2. delete xi : x → x1 · · · xi−1, and then transform x1 · · · xi−1 intoy1 · · · yj .
E [i , j ] = E [i − 1, j ] + 1
3. change xi into yj : x → x1 · · · xn−1ym, and then transformx1 · · · xi−1 into y1 · · · yj−1
E [i , j ] = E [i − 1, j − 1] + 1
Moreover, E [0, j ] = j (converting λ→ Y [j ])E [i , 0] = j (converting X [i ]→ λ)Therefore, we have the recurrence
E [i , j ] =
i if j = 0
j if i = 0
minE [i − 1, j ] + 1,E [i , j − 1] + 1,E [i − 1, j − 1] + d(xi , yj) otherwise
where
d(xi , yj) =
0 if xi = yj
1 otherwise
2.- Computing the optimal costs.
Edit X = x1, . . . , xn, Y = y1, . . . , ynfor i = 0 to n do
E [i, 0] := iend forfor j = 0 to m do
E [0, j] := jend forfor i = 1 to n do
for j = 1 to m doif xi = yj then
d(xi , yj ) = 0else
d(xi , yj ) = 1end ifE [i, j] := E [i, j − 1] + 1if E [i − 1, j − 1] + d(xi , yj ) < E [i, j]then
E [i, j] := E [i − 1, j − 1] + d(xi , yj ),b[i, j] :=
else if E [i − 1, j] + 1 < E [i, j] thenE [i, j] := E [i − 1, j] + 1, b[i, j] :=↑
elseb[i, j] :=←
end ifend for
end for
Time and space complexity T = O(nm).
Example:X=aabab; Y=babb. Therefore,n = 5,m = 4
0 1 2 3 4λ b a b b
0 λ 0 1 2 3 41 a 1 1 1 ← 2 ← 32 a 2 2 1 2 33 b 3 2 ↑ 2 1 24 a 4 ↑ 3 2 ↑ 2 25 b 5 4 ↑ 3 ↑ 2 2
3.- Construct the solution.
Uses as input the table E [n,m].The first call to the algorithm is con-Edit (E , n,m)
con-Edit (E , i , j)if i = 0 or j = 0 then
STOP.else if b[i , j ] = and xi = yj then
change(X , i , yj)con-Edit (E , i − 1, j − 1)
else if b[i , j ] =↑ thendelete(X , i) , con-Edit (c , i − 1, j)
elseinsert(X , i , yj), con-Edit (c , i , j − 1)
end if
This algorithm has time complexity O(nm).
Sequence Alignment.
Finding similarities between sequences is important inBioinformaticsFor example,• Locate similar subsequences in DNA• Locate DNA which may overlap.Similar sequences evolved from common ancestorEvolution modified sequences by mutations• Replacement.• Deletions.• Insertions.
Sequence Alignment.
Given two sequences over same alphabetG C G C A T G G A T T G A G C G AT G C G C C A T T G A T G A C C AAn alignment- GCGC- ATGGATTGAGCGATGCGCCATTGAT -GACC- A
Alignments consist of:• Perfect matchings.• Mismatches.• Insertion and deletions.There are many alignmentsof two sequences.Which is better?
To compare alignments define a scoring function:
s(i) =
+1 if the ith pair matches
−1 if the ith mismatches
−2 if the ith pair InDel
Score alignment =∑
i s(i)Better = max. score between all alignments of two sequences.Very similar to LCS, but with weights.
Typesetting justification.
We have a textT = w1,w2, . . . ,wn
Given we have a width of line= M, the goal is to place theline breaks causing theminimum of stretching(present an aesthetic look).
Typesetting justificationTypesettingINPUT: a text w1,w2, . . . ,wn and width MQUESTION: Compute the line breaks j1, j2, . . . , j` that minimizesthe cost function of stretching.
The DP we explain here isvery similar to that LaTexsoftware for doingtypesetting justification.
Greedy approach
Fill each line as much as possible before going to next line.
abcd, abcd, abcd, abcd
abcd i ab
abracadabraarbadacabra
abcd, abcd, abcd
abcd, abcd i ab
abracadabraarbadacabra
DP solutionIf average number of words is 10 per line and there are n words thenumber of possible solutions is(
n
n/10
)= 2Ω(n).
Key: once we break each line it breaks the paragraph into twoparts that can be optimized independently.
Greedy approach
Fill each line as much as possible before going to next line.
abcd, abcd, abcd, abcd
abcd i ab
abracadabraarbadacabra
abcd, abcd, abcd
abcd, abcd i ab
abracadabraarbadacabra
DP solutionIf average number of words is 10 per line and there are n words thenumber of possible solutions is(
n
n/10
)= 2Ω(n).
Key: once we break each line it breaks the paragraph into twoparts that can be optimized independently.
Characterize optimal solution.
We have `+ 1 lines in a page, and let j1, j2, . . . , j` be the linebreaks, so at the ji -th line we have wji−1+1, . . . ,wji .Let E [i , j ] be the extra-space of putting wi , . . . ,wj in one line.Then
E [i , j ] = M − (j − i)−j∑
k=i
|wk |.
Let c[i , j ] be the cost of putting wi , . . . ,wj in one line. Then
c[i , j ] =
∞ if M − E [i , j ] < 0
0 if j = n & M − E [i , j ] ≥ 0
E [i , j ]3 otherwise
Want to minimize the sum of c over all lines.Consider an optimal arrangement of words 1, . . . , j , where 1 ≤ j ≤ n.
If we have `+ 1 lines consider the line from word i = j`−1 + 1 to word j`.The proceedings words 1 to j`−1 must be break in an optimal way
Recursive definition.
Let OC [j ] the optimal cost of the (optimal) arrangement of words1, . . . , jIf we know the last line in those words contains i , . . . , j we get therecurrence:
OC [j ] = OC [i − 1] + c[i , j ].
So,
OC [j ] =
0 if j = 0,
min1≤i≤j OC [i − 1] + c[i , j ] if j > 0.
We apply DP to compute OC [n].
Algorithm to compute the solution.
Justify |w1|ni=1, n, Mfor i = 1 to n do
E [i, i ] := M − wifor j = i + 1 to n do
E [i, j] := E [i, j − 1]− wj − 1end for
end forfor i = 1 to n do
for j = i to n doif E [i, j] < 0 then
c[i, j] :=∞else if j = n and E [i, j] ≥ 0 then
c[i, j] := 0else
c[i, j] := (E [i, j])3
end ifend for
end forc[0] := 0for j = 1 to n do
c[j] :=∞for i = 1 to j do
if c[i − 1] + OC [i, j] < c[j] thenc[j] := c[i − 1] + OC [i, j]p[j] := i
end ifend for
end forprint c, p
Define n × n table:for empty space per line E [i , j ]cost from wi to wj in one line:c[i , j ]Define n + 1 array of optimalcosts:OC [i ]Define n array of pointersindicating optimal breaking oflines `i : p[i ]
Complexity O(n2).
Example.
Consider |w1| = 2, |w2| = 4, |w3| = 5, |w4| = 1, |w5| = 4, |w6| =1, |w7| = 2, |w8| = 4, |w9| = 3, |w10| = 5, |w11| = 2, |w12| = 4 withM = 20.
The table of extra space E = M − j + i −∑
wk :
E 1 2 3 4 5 6 7 8 9 10 11 12
1 18 13 7 5 0 -2
2 16 10 8 3 1 -2
3 15 13 8 6 3 -2
4 19 14 12 9 4 0 -6
5 16 14 11 6 2 -4
6 19 16 11 7 1 -2
7 18 13 9 3 0 -5
8 16 12 6 3 -1
9 17 11 8 4
10 15 12 7
11 18 13
12 16
Example.
The table of costs c[i , j ]
c 1 2 3 4 5 6 7 8 9 10 11 121 5832 2197 343 125 0 ∞ 02 4096 1000 512 27 1 ∞ 03 3375 2197 512 216 27 ∞ 04 6859 2744 1728 729 64 0 ∞ 05 4096 2744 1331 216 8 ∞ 06 6859 4096 1331 343 1 ∞ 07 5832 2197 729 27 0 08 4096 1728 216 27 09 4913 1331 512 64
10 3375 1728 34311 5832 219712 4096
Optimal cost array of pointers and Typesetting
The optimal cost array OC [i , j ]0 1 2 3 4 5 6 7 8 9 10 11 120 5832 2197 343 125 0 6859 1072 341 8 1 853 351
The array of pointers p[i ]1 2 3 4 5 6 7 8 9 10 11 121 1 1 1 1 4 4 5 5 5 9 10
p[12] = 10, p[p[12]] = 5, p[p[p[12]]] = 1 ⇒w1w2w3w4w5
w6w7w8w9w10 +1w11w12
Dynamic Programming in Trees
Trees are nice graphs to bound the number of subproblems.Given T = (V ,A) with |V | = n, recall that there are n subtrees inT .
Therefore, when considering problems defined on trees, it is easy tobound the number of subproblems
This allows to use Dynamic Programming to give polynomialsolutions to ”difficult” graph problems when the input is a tree.
The Maximum Weight Independent Set (MWIS).
INPUT: G = (V ,E ), together with a weight w : V → RQUESTION: Find the largest S ⊆ V such that no two vertices in Sare connected in G .
For general G , the problem is difficult, asthe case with all weights =1 is alreadyNP-complete.
2
5
1
2
1
2
6
Given a rooted tree T = (V ,A) asinstance for the MWIS. is rooted.Then ∀v ∈ V , v defines a subtreeTv .
j
6
4 8 8
79
2
3
8
25 6
5 4 6
a
b dc
e f g h i k
l m n o
Characterization of the optimal solution
Key observation: An WIS S in T can’t contain vertices which arefather-son.
For v ∈ V , let Tv be the subtree rooted at v and let Sv be the sizeof the MWIS in Tv . Define:
M(v) = maxw(Sv )| v ∈ Sv & M ′(v) = maxw(Sv )| v 6∈ S.
M(v) and M ′(v) are the max weights of the independent sets inTv that contain v and not contain v
Recursive definition of the optimal solution
For v ∈ V and u a son of v (in Tv ):M(v) = w(v) +
∑u M
′(u) andM ′(v) = w(v) +
∑u maxM(u),M ′(u)
In the case v is a leaf M(v) = w(v) and M ′(v) = 0
Algorithm to find MWIS on a rooted tree T with size n.
Define 2 n × n tables to store the values of M and M ′.The optimal solution will be given by the following procedure thatexpres T first bottom-up and after top-down:
1. For every v ∈ V compute M(v) and M ′(v)
1.1 If v is a leaf M(v) := w(v), M ′(v) := 01.2 Otherwise for u a son of v , M(v) := w(v) +
∑v M′(u),
M ′(v) :=∑
u maxM(u),M ′(u)2. In a top-dow manner, do
2.1 S = ∅2.2 for root r , if M(r) > M ′(r), then S := S ∪ r2.3 for u 6= r , if v 6∈ S and M(u) > M ′(u) then S := S ∪ r
3. return S
Bottom-upM(l) = 8,M(n) = 4,M(n) = 6,M(e) = 5M(f ) = 6,M(o) = 2,M(k) = 7M(b) = 4, M ′(b) = 11M(h) = 8, M ′(h) = 15M(j) = 9, M ′(h) = 2M(d) = 10, M ′(d) = 16M(c) = 23, M ′(c) = 20M(a) = 6 + M ′(b) + M ′(c) + M ′(d) = 53M ′(a) = 6 + M ′(b) + M(c) + M ′(d) = 50
j
6
4 8 8
79
2
3
8
25 6
5 4 6
a
b dc
e f g h i k
l m n o
Top downM(l) = 8,M(n) = 4,M(n) = 6,M(e) = 5M(f ) = 6,M(o) = 2,M(k) = 7M(b) = 4, M ′(b) = 11M(h) = 8, M ′(h) = 15M(j) = 9, M ′(h) = 2M(d) = 10, M ′(d) = 16M(c) = 23, M ′(c) = 20M(a) = 6 + M ′(b) + M ′(c) + M ′(d) = 53M ′(a) = 6 + M ′(b) + M(c) + M ′(d) = 50
j
6
4 8 8
79
2
3
8
25 6
5 4 6
a
b dc
e f g h i k
l m n o
Maximum Weighted Independent Set:S = a, e, f , g , i , j , k , l ,m, n w(S) = 53.
Complexity.
Space complexity: 2n2
Time complexity:Step 1 To update M and M ′ we only need to look at the sons ofthe vertex into consideration. As we cover all n vertices, O(n)Step 2 From the root down to the leaves, for each vertex vcompare M(v) with M ′(v), if M(v) > M ′(v) include v in S ,otherwise do not include. Θ(n)
As we have to consider all the vertices at least one, we have alower bound in the time complexity of Ω(n), therefore the totalnumber of steps is Θ(n).