SLIDE 1
TU/e
Algorithms (2IL15) – Lecture 4
1
Algorithms (2IL15) – Lecture 4 DYNAMIC PROGRAMMING II
SLIDE 2 TU/e
Algorithms (2IL15) – Lecture 4
2
Techniques for optimization
- ptimization problems typically involve making choices
backtracking: just try all solutions
- can be applied to almost all problems, but gives very slow algorithms
- try all options for first choice,
for each option, recursively make other choices greedy algorithms: construct solution iteratively, always make choice that seems best
- can be applied to few problems, but gives fast algorithms
- only try option that seems best for first choice (greedy choice),
recursively make other choices dynamic programming
- in between: not as fast as greedy, but works for more problems
SLIDE 3 TU/e
Algorithms (2IL15) – Lecture 4
3
5 steps in designing dynamic-programming algorithms 1. define subproblems [#subproblems] 2. guess first choice [#choices] 3. give recurrence for the value of an optimal solution [time/subproblem treating recursive calls as Θ(1))] i. define subproblem in terms of a few parameters ii. define variable m[..] = value of optimal solution for subproblem
- iii. relate subproblems by giving recurrence for m[..]
4. algorithm: fill in table for m [..] in suitable order (or recurse & memoize) 5. solve original problem Running time: #subproblems * time/subproblem Correctness: (i) correctness of recurrence: relate OPT to recurrence (ii) correctness of algorithm: induction using (i) Today more examples: longest common subsequence and optimal binary search trees
SLIDE 4
TU/e
Algorithms (2IL15) – Lecture 4
4
5 steps: Matrix chain multiplication 1. define subproblems [Θ(n2)] given i,j with 1≤i<j ≤n, compute Ai • … • Aj in cheapest way 2. guess first choice final multiplication [Θ(n)] 3. give recurrence for the value of an optimal solution [Θ(n)] 4. algorithm: fill in table for m [..] in suitable order (or recurse & memoize) 5. solve original problem: split markers s[i,j] Running time: #subproblems * time/subproblem = Θ(n3)
i≤k<j
m [i,j ] = 0 if i = j min { m [i,k ] + m [k+1,j ] + pi-1 pk pj } if i< j Blackboard example
SLIDE 5
TU/e
Algorithms (2IL15) – Lecture 4
5
Longest Common Subsequence
SLIDE 6 TU/e
Algorithms (2IL15) – Lecture 4
6
Comparing DNA sequences DNA: string of characters A = adenine C = cytosine G = guanine T = thymine Problem: measure the similarity of two given DNA sequences X = A C C G T A A T C G A C G Y = A C G A T T G C A C T G How to measure similarity?
- edit distance: number of changes to transform X into Y
- length of longest common subsequence (LCS) of X and Y
- …
SLIDE 7
TU/e
Algorithms (2IL15) – Lecture 4
7
Longest common subsequence substring of a string: string that can be obtained by omitting zero or more characters string: A C C A T T G example substrings: A C C A T T G or A C C A T T G or … LCS(X,Y) = a longest common subsequence of strings X and Y = a longest string that is a subsequence of X and a subsequence of Y X = A C C G T A A T C G A C G Y = A C G A T T G C A C T G LCS(X,Y) = ACGTTGACG (or ACGTTCACG …)
SLIDE 8
TU/e
Algorithms (2IL15) – Lecture 4
8
The LCS problem Input: strings X = x1 , x2 , … , xn and Y = y1 , y2 , … , ym Output: (the length of) a longest common subsequence of X and Y So we have valid solution = common subsequence profit = length of the subsequence (to be maximized)
SLIDE 9 TU/e
Algorithms (2IL15) – Lecture 4
9
X = A C C G T A A T C G A C G Y = A C G A T T G C A C T G LCS for x1…xn-1 and y1…ym-1 so: optimal substructure x1 x2 xn y1 y2 ym Let’s study the structure of an optimal solution
- what are the choices that need to be made?
- what are the subproblems that remain? do we have optimal substructure?
first choice: is last character of X matched to last character of Y ?
- r is last character of X unmatched ?
- r is last character of Y unmatched ?
greedy choice ?
- verlapping subproblems ?
- ptimal solution
SLIDE 10
TU/e
Algorithms (2IL15) – Lecture 4
10
5 steps in designing dynamic-programming algorithms 1. define subproblems 2. guess first choice 3. give recurrence for the value of an optimal solution 4. algorithm: fill in table for c [..] in suitable order (or recurse & memoize) 5. solve original problem
SLIDE 11 TU/e
Algorithms (2IL15) – Lecture 4
11
X = A C C G T A A T C G A C G Y = A C G A T T G C A C T G x1 x2 xn y1 y2 ym
X0 is empty string
3. Give recursive formula for the value of an optimal solution i. define subproblem in terms of a few parameters Find LCS of Xi := x1 … xi and Yj := y1 … yj , for parameters i, j with 0 ≤ i ≤ n and 0 ≤ j ≤ m ? ii. define variable c [..] = value of optimal solution for subproblem c [i,j ] = length of LCS of Xi and Yj
- iii. give recursive formula for c [..]
SLIDE 12
TU/e
Algorithms (2IL15) – Lecture 4
12
X = A C C G T A A T C G A C G Y = A C G A T T G C A C T G x1 x2 xn y1 y2 ym Xi := x1 … xi and Yj := y1 … yj , for 0 ≤ i ≤ n and 0 ≤ j ≤ m we want recursive formula for c [i,j ] = length of LCS of Xi and Yj Lemma: c [i,j ] = if i = 0 or j = 0 if i,j > 0 and xi = yj if i,j > 0 and xi ≠ yj c[i-1,j-1] + 1 max { c[i-1,j], c[i,j-1] }
SLIDE 13 TU/e
Algorithms (2IL15) – Lecture 4
13
Proof: i =0 or j = 0: trivial (at least one of Xi and Yj is empty) i,j > 0 and xi = yj : We can extend LCS (Xi-1,Yj-1) by appending xi, so c [i,j ] ≥ c [i-1,j-1] + 1. On the other hand, by deleting the last character from LCS(Xi ,Yj) we
- btain a substring of Xi-1 and Yj-1. Hence, c [i-1,j-1] ≥ c [i,j ] - 1.
It follows that c [i,j ] = c [i-1,j-1] + 1. i,j > 0 and xi ≠ yj : If LCS(Xi ,Yj) does not end with xi then c [i,j ] ≤ c[i-1,j ] ≤ max { .. }. Otherwise LCS(Xi ,Yj) cannot end with yj, so c [i,j ] ≤ c[i,j-1] ≤ max { .. }. Obviously c [i,j ] ≥ max { .. }. We conclude that c [i,j ] = max { .. }. Lemma: c [i,j ] = if i = 0 or j = 0 if i,j > 0 and xi = yj if i,j > 0 and xi ≠ yj c[i-1,j-1] + 1 max { c[i-1,j], c[i,j-1] }
SLIDE 14
TU/e
Algorithms (2IL15) – Lecture 4
14
5 steps in designing dynamic-programming algorithms 1. define subproblems 2. guess first choice 3. give recurrence for the value of an optimal solution 4. algorithm: fill in table for c [..] in suitable order (or recurse & memoize) 5. solve original problem
SLIDE 15
TU/e
Algorithms (2IL15) – Lecture 4
15
i j m n solution to original problem c [i,j ] = if i = 0 or j = 0 if i,j > 0 and xi = yj if i,j > 0 and xi ≠ yj c[i-1,j-1] + 1 max { c[i-1,j], c[i,j-1] } 4. Algorithm: fill in table for c [..] in suitable order
SLIDE 16 TU/e
Algorithms (2IL15) – Lecture 4
16
LCS-Length(X,Y)
- 1. n ← length[X]; m ← length[Y]
- 2. for i ← 0 to n do c[i,0] ← 0
- 3. for j ← 0 to m do c[0,j] ← 0
- 4. for i ← 1 to n
- 5. do for j ← 1 to m
- 6. do if X[i] = Y[j]
- 7. then c[i,j] ← c[i-1,j-1] + 1
- 8. else c[i,j] ← max { c[i-1,j], c[i,j-1] }
- 9. return c[n,m]
c [i,j ] = if i = 0 or j = 0 if i,j > 0 and xi = yj if i,j > 0 and xi ≠ yj c[i-1,j-1] + 1 max { c[i-1,j], c[i,j-1] } 4. Algorithm: fill in table for c [..] in suitable order
j m i n
SLIDE 17 TU/e
Algorithms (2IL15) – Lecture 4
17
Θ(n) Θ(m) Θ(1) Θ(1) Lines 4 – 8: ∑1≤i≤n ∑1≤j≤m Θ(1) = Θ(nm) LCS-Length(X,Y)
- 1. n ← length[X]; m ← length[Y]
- 2. for i ← 0 to n do c[i,0] ← 0
- 3. for j ← 0 to m do c[0,j] ← 0
- 4. for i ← 1 to n
- 5. do for j ← 1 to m
- 6. do if X[i] = Y[j]
- 7. then c[i,j] ← c[i-1,j-1] + 1
- 8. else c[i,j] ← max { c[i-1,j], c[i,j-1] }
- 9. return c[n,m]
Analysis of running time
SLIDE 18
TU/e
Algorithms (2IL15) – Lecture 4
18
5 steps in designing dynamic-programming algorithms 1. define subproblems 2. guess first choice 3. give recurrence for the value of an optimal solution 4. algorithm: fill in table for c [..] in suitable order (or recurse & memoize) 5. solve original problem Approaches for going from optimum value to optimum solution: i. store choices that led to optimum (e.g. s[i,j] for matrix multiplication) ii. retrace/deduce sequence of solutions that led to optimum backwards (next slide)
SLIDE 19
TU/e
Algorithms (2IL15) – Lecture 4
19
Print-LCS(c,X,Y,i,j) 1. if i=0 or j=0 2. then skip 3. else if X[i] = Y[j] 4. then Print-LCS(c,X,Y,i-1,j-1); print xi 5. else if c[i-1,j] > c[i,j-1] 6. then Print-LCS(c,X,Y,i-1,j) 7. else Print-LCS (c,X,Y,i,j-1) Advantage of avoiding extra table: saves memory Disadvantage: extra work to construct solution Initial call: Print-LCS(c,X,Y,n,m) 5. Extend algorithm so that it computes an actual solution that is optimal, and not just the value of an optimal solution. Usual approach: extra table to remember choices that lead to optimal solution It’s also possible to do without the extra table.
SLIDE 20
TU/e
Algorithms (2IL15) – Lecture 4
20
5 steps in designing dynamic-programming algorithms 1. define subproblems 2. guess first choice 3. give recurrence for the value of an optimal solution 4. algorithm: fill in table for c [..] in suitable order (or recurse & memoize) 5. solve original problem Approaches for going from optimum value to optimum solution: i. store choices that led to optimum (e.g. s[i,j] for matrix multiplication) ii. retrace/deduce sequence of solutions that led to optimum backwards (next slide)
SLIDE 21
TU/e
Algorithms (2IL15) – Lecture 4
21
Optimal Binary Search Trees
SLIDE 22
TU/e
Algorithms (2IL15) – Lecture 4
22
Observation: balanced binary search tree does not necessarily have best average search time, if certain keys are searched for more often than others porcupine is the Example: dictionary for automatic translation of texts (keys are words) porcupine the is balanced better average search time
SLIDE 23 TU/e
Algorithms (2IL15) – Lecture 4
23
The problem of constructing optimal binary search trees Input:
- keys k1 , k2 , … , kn with k1 < k2 < … < kn
- probabilities p1 , p2 , … , pn with pi = probability that query key is ki
- dummy keys d0 , d1 , … , dn
- probabilities q0 , q1 , … , qn with qi = probability that query key lies between
ki and ki+1 (k0 = - ∞ en kn+1 = ∞), that is, that search ends in dummy di Note: ∑ pi + ∑ qi = 1 d0 d1 d2 d3 dn
0.001 0.02
k1 k2 k3 kn
0.03 0.04 0.072 0.008 0.002 0.002 0.001
SLIDE 24
TU/e
Algorithms (2IL15) – Lecture 4
24
Output: tree that minimizes expected search time (expected = weighted average) d0 d1 d2 d3 k1 k2 k3 k4 d0 d1 d2 k1 k2 k3 d4 d3 k4 d4 depth = 0 depth = 1 expected cost of a query = ∑ pi ∙ (1 + depth of ki ) + ∑ qi ∙ (1 + depth of di) = 1 + ∑ pi ∙(depth of ki ) + ∑ qi ∙(depth of di ) this is what we want to minimize
0.05 0.08 0.25 0.1 0.15 0.3 0.3 0.25 0.1 0.15 0.04 0.02 0.05 0.08 0.04 0.02 0.01 0.01
SLIDE 25 TU/e
Algorithms (2IL15) – Lecture 4
25
kr
- ptimal tree for d0 ,k1 ,…,dr-1
- ptimal tree for dr,kr+1 ,…,dn
Let’s study the structure of an optimal solution
- what are the choices that need to be made?
- what are the subproblems that remain? do we have optimal substructure?
d0 d1 d2 d3 dn k1 k2 k3 kn
SLIDE 26
TU/e
Algorithms (2IL15) – Lecture 4
26
5 steps in designing dynamic-programming algorithms 1. define subproblems 2. guess first choice 3. give recurrence for the value of an optimal solution 4. algorithm: fill in table for m [..] in suitable order (or recurse & memoize) 5. solve original problem
SLIDE 27 TU/e
Algorithms (2IL15) – Lecture 4
27
3. Give recursive formula for the value of an optimal solution i. define subproblem in terms of a few parameters Find tree for di-1 ,ki ,…,dj that minimizes expected search cost for parameters i, j with 0 ≤ i -1≤ j ≤ n? ii. define variable m [..] = value of optimal solution for subproblem m [i,j ] = expected search cost of optimal tree for di-1 ,ki ,…,dj
- iii. give recursive formula for m [..]
kr d0 ,ki ,…,dr-1 dr ,kr-1,…,dn
SLIDE 28
TU/e
Algorithms (2IL15) – Lecture 4
28
recursive formula for m [i,j ] = expected search cost of optimal tree for di-1 ,ki ,…,dj
kr di-1 ,ki ,…,dr-1 dr ,kr-1,…,dj
Lemma: m [i,j ] = if j = i-1 if j > i -1 qi-1 min { m[i,r-1] + m[r+1,j] + w(i,j) } i ≤ r ≤ j expected search cost = pr + (cost of left subtree) + (cost of right subtree) = pr + ( m [i,r-1 ] + ∑i≤s ≤ r-1 ps + ∑ i-1≤s≤r-1 qs ) + ( m [r+1,j ] + ∑r+1≤s ≤ j ps + ∑ r≤s≤j qs ) = m [i,r-1 ] + m [r+1,j ] + ∑i≤s ≤ j ps + ∑ i-1≤s≤j qs define w(i,j) = ∑i≤s≤j ps + ∑ i-1≤s≤j qs
SLIDE 29
TU/e
Algorithms (2IL15) – Lecture 4
29
5 steps in designing dynamic-programming algorithms 1. define subproblems 2. guess first choice 3. give recurrence for the value of an optimal solution 4. algorithm: fill in table for m [..] in suitable order (or recurse & memoize) 5. solve original problem
SLIDE 30 TU/e
Algorithms (2IL15) – Lecture 4
30
OptimalBST-Cost ( p, q ) // p and q: arrays with probabilities p1,..,pn and q0,…,qn
- 1. n ← length[p]
- 2. for i ← 1 to n+1
- 3. do m[i,i-1] ← qi-1
- 4. for i ← n downto 1
- 5. do for j ← i to n
- 6. do m[i,j] ← min { m[i,r-1] + m[r+1,j] + w(i,j) }
- 7. return m[1,n]
i ≤ r ≤ j
precompute
i j 1 n n+1
4. Algorithm: fill in table for m [..] in suitable order m [i,j ] = if j = i-1 if j > i -1 qi-1 min { m[i,r-1] + m[r+1,j] + w(i,j) } i ≤ r ≤ j
SLIDE 31
TU/e
Algorithms (2IL15) – Lecture 4
31
Analysis of running time OptimalBST-Cost ( p, q ) // p and q: arrays with probabilities p1,..,pn and q0,…,qn 1. n ← length[p] 2. for i ← 1 to n+1 3. do m[i,i-1] ← qi-1 4. for i ← n downto 1 5. do for j ← i to n 6. do m[i,j] ← min { m[i,r-1] + m[r+1,j] + w(i,j) } 7. return m[1,n]
precompute
i j 1 n n+1 i ≤ r ≤ j
Θ(n3): similar to algorithm for matrix-chain multiplication
SLIDE 32
TU/e
Algorithms (2IL15) – Lecture 4
32
5 steps in designing dynamic-programming algorithms 1. define subproblems 2. guess first choice 3. give recurrence for the value of an optimal solution 4. algorithm: fill in table for m [..] in suitable order (or recurse & memoize) 5. solve original problem
SLIDE 33
TU/e
Algorithms (2IL15) – Lecture 4
33
5 steps in designing dynamic-programming algorithms 1. define subproblems 2. guess first choice 3. give recurrence for the value of an optimal solution 4. algorithm: fill in table for m [..] in suitable order (or recurse & memoize) 5. solve original problem Try it!
SLIDE 34 TU/e
Algorithms (2IL15) – Lecture 4
34
5 steps in designing dynamic-programming algorithms 1. define subproblems [#subproblems] 2. guess first choice [#choices] 3. give recurrence for the value of an optimal solution [time/subproblem treating recursive calls as Θ(1))] i. define subproblem in terms of a few parameters ii. define variable m[..] = value of optimal solution for subproblem
- iii. relate subproblems by giving recurrence for m[..]
4. algorithm: fill in table for m [..] in suitable order (or recurse & memoize) 5. solve original problem Running time: #subproblems * time/subproblem Correctness: (i) correctness of recurrence: relate OPT to recurrence (ii) correctness of algorithm: induction using (i) A problem to think about: Knapsack with integer weights
- items with values v1, …, vn and integer weights w1, …, wn
- select items to pack of total weight < W, maximizing total value
SLIDE 35 TU/e
Algorithms (2IL15) – Lecture 4
35
Summary of part I of the course
we want to find valid solution minimizing cost (or maximizing profit) techniques for optimization
- backtracking: just try all solutions (pruning when possible)
- greedy algorithms: make choice that seems best, recursively solve
remaining subproblem
- dynamic programming: give recursive formula, fill in table
always start by investigating structure of optimal solution