13 - 06 Feb - Dynamic Programming
-
Upload
neeldhara-misra -
Category
Education
-
view
88 -
download
0
Transcript of 13 - 06 Feb - Dynamic Programming
CS 321. Algorithm Analysis & Design Lecture 13
Dynamic Programming
Edit DistanceKnapsacks
Shortest Paths
Items with weights wi
and value vi
Knapsack with capacity C
Items with weights wi
and value vi
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn… …
(What we want)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn… …
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn… …
A[i] stores the best value for the first i items.
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn… …
A[i] stores the best value for the first i items.
A[i] = max(A(i-1), A[i-1] + vi)
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn… …
A[i] stores the best value for the first i items.
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn… …
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn… …
A[i,j] stores the best value for the first i items.
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn… …
that fits in a knapsack of size j.
A[i,j] stores the best value for the first i items.
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(What we want)
(Build up the array bottom to top.)
w1
v1
w2
v2
wi
vi
wn-1
vn-1
wn
vn… …
that fits in a knapsack of size j.
1 2 3 4 5
1
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
1 2 3 4 5
1
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3 0 6 7 9 9
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3 0 6 7 9 9
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3 0 6 7 9 9
4
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
1 2 3 4 5
1 0 3 3 3 3
2 0 6 6 9 9
3 0 6 7 9 9
4 4 6 10 11 13
A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)
(2,3), (2,6), (3,7), (1,4) - weight/value pairs
Running time = # of subproblems x time/subproblem
Running time = # of subproblems x time/subproblem
nC x O(1) = O(nC) (n = #items, C = Capacity)
Running time = # of subproblems x time/subproblem
nC x O(1) = O(nC) (n = #items, C = Capacity)
Is this polynomial?
Running time = # of subproblems x time/subproblem
nC x O(1) = O(nC) (n = #items, C = Capacity)
Is this polynomial?
pseudo-polynomial
Edit Distance
Edit Distance
The cost of changing one word to another, using insertions/deletions/changes.
Edit Distance
The cost of changing one word to another, using insertions/deletions/changes.
Each operation can have a certain cost.
A C G T
A 0 1 1 3
C 2 0 1 1
G 1 2 0 2
T 3 2 1 0
SUNNY DEOL
SUNNY LEONE
SUNNY DEOL
SUNNY LEONE
SUNNY DEOL
SUNNY LEONE
SUNNY LEOL
SUNNY DEOL
SUNNY LEONE
SUNNY LEOL
SUNNY LEON
SUNNY DEOL
SUNNY LEONE
SUNNY LEOL
SUNNY LEON
SUNNY LEONE
Work out the solution for pairs x[i:] and y[j:].
(What we want)
(Build up the array bottom to top.)
(What we want)
(Build up the array bottom to top.)
c[i,j] stores the edit distance between x[1…i] and y[1…j].
(What we want)
(Build up the array bottom to top.)
c[i,j] stores the edit distance between x[1…i] and y[1…j].
c[i,j] = min(c[i-1,j] + w1, c[i,j-1] + w2, c[i-1,j-1] + w3)
(What we want)
(Build up the array bottom to top.)
c[i,j] stores the edit distance between x[1…i] and y[1…j].