Dynamic programming 4
Dynamic programming 4
Jianxi Gao
Definitions
A sequence of numbers with n elements: 𝑆 = {𝑎1 , 𝑎2 , … , 𝑎𝑛 }
A subsequence of 𝑆 with m elements: Ss = {𝑎𝑗1 , 𝑎𝑗2 , … , 𝑎𝑗𝑚 }, where 𝑗1 < 𝑗2 < ⋯ < 𝑗𝑚
An increasing subsequence of 𝑆: Ss = {𝑎𝑗1 , 𝑎𝑗2 , … , 𝑎𝑗𝑚 }, where 𝑎𝑗1 < 𝑎𝑗2 < ⋯ < 𝑎𝑗𝑚 .
Longest common subsequence: Given two sequences, find the length of the
longest subsequence presented in both of them.
Examples
A sequence: 𝑆 = 5, 2, 8, 6, 3, 6, 9, 7 .
subsequences of S: 5, 2, 8 , 5, 8, 6,6 , 2, 6, 9 , 2
Increasing subsequences of S: 5,8 , 2,3,6,9 , 2 5,6,7
Longest Increasing subsequences of S: 2,3,6,9 .
For example: “attaaaggtt” and “cacgcagtat”
The longest common sequences = ”at”, “ag”, “ta”, “gt”
Longest increasing subsequence algorithm
Iterator j i
S 5 2 8 6 3 6 9 7
LIS 1 1 2 2 2 3 3, 4 1
LCS Empty A T A G C
Initialize:
T 0 L(i-1,j-1) L(i,j-1)
If L1(i) and L2(j) match,
L(i,j) = L(i-1,j-1) +1
G 0 L(i-1,j) L(i,j)
If L1(i) and L2(j) do not match,
L(i,j) = max(L(i-1,j), L(I,j-1)).
A 0
C 0
Introduction
I love her.
I loeve har.
What is the distance between “loeve” and “love”, and what is the distance between
“loeve” and “leave”? How to give the suggestions to the writers?
Introduction
The natural measure of the distance between two strings is the extent to which
they can be aligned, or matched.
String 1 L O E V E
String 2 L O V E
Approach 1 Cost: 3
String 1 L O E, V V, E E
String 2 L O V E
Approach 2 Cost: 1
String 1 L O E V E
String 2 L O V E
Edit distance
Definitions
Given two strings String 1 and string 2 and below operations that can be
performed on string 1. Find the minimum number of operations or edits
required to convert string 1 to string 2.
Edits are:
1. Insert String 1 H A R
2. Delete String 2 H E R Cost: 1
3. Modify Edits H A, E R
Examples
String 1 L O E V E String 1 L O E V E
String 2 L E V E E String 2 L E V E E
Edits L O,E E,V V,E E Edits L O E V E E
Cost: 3 Cost: 2
The Approach
1. If the last letters of the two strings match, we do not change anything
and recur for length m-1 and n-1.
D(VEVE,VE)
Recursive solution
D(LOEVE,LOVE) Choice 2,
Choice 1,
Insert V in string 1 to Delete E in string 1.
match the V in string D(OEVE,OVE)
2. The cost is 1.
D(EVE,VE)
D(VEVE,VE) D(VE,VE)
Recursive solution
D(LOEVE,LOVE) Choice 2, Choice 3,
Choice 1,
Delete E in string 1. Modify E to be V.
Insert V in string 1 to
The cost is 1. The cost is 1.
match the V in string D(OEVE,OVE)
2. The cost is 1.
D(EVE,VE)
D(EVE,VE)
D(EVE,E)
Recursive solution
D(LOEVE,LOVE)
E matches E.
Cost 0.
D(OEVE,OVE)
D(EVE,VE)
D(EVE,E)
D(VE,[])
Recursive solution
D(LOEVE,LOVE)
Delete V
Cost 1.
D(OEVE,OVE)
D(EVE,VE)
D(EVE,E)
D(VE,[])
D(E,[])
Recursive solution
D(LOEVE,LOVE)
Delete E
Cost 1.
D(OEVE,OVE)
D(EVE,VE)
D(EVE,E)
D(VE,[])
D(E,[])
D([],[])
Recursive solution
D(LOEVE,LOVE) x Means number of edits.
D(OEVE,OVE)
D(EVE,VE)
D(EVE,E)
D(VE,[])
D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE) x Means number of edits.
D(OEVE,OVE)
D(EVE,VE)
D(EVE,E)
D(VE,[])
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE) x Means number of edits.
D(OEVE,OVE)
D(EVE,VE)
D(EVE,E)
2 D(VE,[])
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE) x Means number of edits.
D(OEVE,OVE)
D(EVE,VE)
2 D(EVE,E)
2 D(VE,[])
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE) x Means number of edits.
D(OEVE,OVE)
D(EVE,VE)
2 D(EVE,E)
2 D(VE,[])
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE)
V matches V.
Cost 0.
D(OEVE,OVE)
D(EVE,VE)
2 D(EVE,E) D(E,E)
2 D(VE,[])
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE)
E matches E.
Cost 0.
D(OEVE,OVE)
D(EVE,VE)
2 D(EVE,E) D(E,E)
2 D(VE,[]) D([],[])
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE)
E matches E.
Cost 0.
D(OEVE,OVE)
D(EVE,VE)
2 D(EVE,E) D(E,E)
2 D(VE,[]) 0 D([],[])
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE)
E matches E.
Cost 0.
D(OEVE,OVE)
D(EVE,VE)
2 D(EVE,E) 0 D(E,E)
2 D(VE,[]) 0 D([],[])
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE)
V matches V.
Cost 0.
D(OEVE,OVE)
D(EVE,VE)
2 D(VE,[]) 0 D([],[])
1 D(E,[])
0 D([],[])
Recursive solution
“V” does not match “E”, D(LOEVE,LOVE)
we have three choices .
But we are not sure D(OEVE,OVE)
which one is the best.
D(EVE,VE)
2 D(VE,[]) 0 D([],[])
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE)
Choice 1,
Insert E in string 1 to
match the E in string D(OEVE,OVE)
2. The cost is 1.
D(EVE,VE)
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE) Choice 2,
Choice 1,
Insert E in string 1 to Delete V in string 1.
match the E in string D(OEVE,OVE) The cost is 1.
2. The cost is 1.
D(EVE,VE)
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE) Choice 2, Choice 3,
Choice 1,
Delete V in string 1. Modify V to be E.
Insert E in string 1 to
The cost is 1 The cost is 1.
match the E in string D(OEVE,OVE)
2. The cost is 1.
D(EVE,VE)
1 D(E,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE)
E matches E.
Cost 0.
D(OEVE,OVE)
D(EVE,VE)
1 D(E,[]) L(VE,[])
0 D([],[])
Recursive solution
D(LOEVE,LOVE)
Delete V.
The cost is 1.
D(OEVE,OVE)
D(EVE,VE)
1 D(E,[]) L(VE,[])
0 D([],[]) D(E,[])
Recursive solution
D(LOEVE,LOVE)
Delete E.
The cost is 1.
D(OEVE,OVE)
D(EVE,VE)
1 D(E,[]) L(VE,[])
D(OEVE,OVE)
D(EVE,VE)
1 D(E,[]) L(VE,[])
D(OEVE,OVE)
D(EVE,VE)
1 D(E,[]) L(VE,[])
D(OEVE,OVE)
D(EVE,VE)
1 D(E,[]) 2 L(VE,[])
D(OEVE,OVE)
D(EVE,VE)
1 D(E,[]) 2 L(VE,[])
D(EVE,VE)
D(OEVE,OVE)
D(EVE,VE)
D(EVE,VE)
D(EVE,VE)
D(OEVE,OVE)
D(EVE,VE)
D(OEVE,OVE)
D(EVE,VE)
D(OEVE,OVE)
D(EVE,VE)
D(EVE,VE)
D(OEVE,OVE)
D(EVE,VE)
1 D(EVE,VE)
1 D(OEVE,OVE)
1 D(EVE,VE)
1 D(OEVE,OVE)
1 D(EVE,VE)
Empty 0
O
String 1
E
Dynamic programming (Bottom-Up)
Empty 0 1 2 3 4
O
String 1
E
Dynamic programming (Bottom-Up)
Empty 0 1 2 3 4
L 1
O 2
String 1
E 3
V 4
E 5
Dynamic programming (Bottom-Up)
String 2
EDist Empty L O V E
If String2(i) matches String1(j)
Empty 0 1 2 3 4 D(i,j) = D(i-1,j-1).
Because the cost is 0 when match.
L 1 If String2(i) DOES NOT match
String1(j)
String 1
L 1 0 Cost = 0
O 2
E 3
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 Cost = 1
O 2 D(i,j) = D(i-1,j) +1
Insert “O”
E 3
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 Cost = 2
O 2 D(i,j) = D(i-1,j) +1
Insert “O” and “V”
E 3
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 3
O 2 D(i,j) = D(i-1,j) +1
Insert “O”, “V”, and
E 3 “E”.
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 1
O 2 1 D(i,j) = D(i,j-1) +1
E 3 Delete “O”
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 0
O 2 1 0 D(i,j) = D(i-1,j-1)
E 3 Match
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 1
O 2 1 0 1 D(i,j) = D(i-1,j) +1
E 3 Insert V.
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 2
O 2 1 0 1 2 D(i,j) = D(i-1,j) +1
E 3 Insert V and E.
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 2
O 2 1 0 1 2 D(i,j) = D(i,j-1) +1
E 3 2 Delete O and E.
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 1
O 2 1 0 1 2 D(i,j) = D(i,j-1) +1
E 3 2 1 Delete E.
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 1
O 2 1 0 1 2 D(i,j) = D(i-1,j-1) +1
E 3 2 1 1 Modify E to V.
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 1
O 2 1 0 1 2 D(i,j) = D(i-1,j-1)
E 3 2 1 1 1
V 4
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 3
O 2 1 0 1 2 D(i,j) = D(i,j-1) +1
E 3 2 1 1 1 Delete O E V.
V 4 3
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 2
O 2 1 0 1 2 D(i,j) = D(i,j-1) +1
E 3 2 1 1 1 Delete E V.
V 4 3 2
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 1
O 2 1 0 1 2 D(i,j) = D(i,j-1) +1
E 3 2 1 1 1 Delete E.
V 4 3 2 1
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 2
Two ways
O 2 1 0 1 2
D(i,j) = D(i,j-1) +1
E 3 2 1 1 1 1. Delete E before V
and insert E after V.
V 4 3 2 1 2 2. Modify E to V
and Modify V to E.
E 5
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 4
Delete “OEVE”
O 2 1 0 1 2
E 3 2 1 1 1
V 4 3 2 1 2
E 5 4
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 3
Delete “EVE”
O 2 1 0 1 2
E 3 2 1 1 1
V 4 3 2 1 2
E 5 4 3
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 2
E 3 2 1 1 1
V 4 3 2 1 2
E 5 4 3 2
Dynamic programming (Bottom-Up)
L 1 0 1 2 3 Cost = 1
Delete “E”
O 2 1 0 1 2
E 3 2 1 1 1
V 4 3 2 1 2
E 5 4 3 2 1
Dynamic programming (Bottom-Up)
EDist Empty L O V E
Initialize:
Empty 0 1 2 3 4
L 1 0 1 2 3
O 2 1 0 1 2
E 3 2 1 1 1
V 4 3 2 1 2
E 5 4 3 2 1
Edit distance algorithm (Bottom-Up store dynamic programming)
D(VEVE,VE)
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) ?
D(EVE,E) ?
D(VEVE,VE)
D(EVE,E)
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) ?
D(EVE,E) ?
D(VEVE,VE)
D(VE,[]) ?
D(EVE,E)
D(VE,[])
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) ?
D(EVE,E) ?
D(VEVE,VE)
D(VE,[]) ?
D(EVE,E)
D(E,[]) 1
D(VE,[])
D(E,[])
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) ?
D(EVE,E) ?
D(VEVE,VE)
D(VE,[]) 2
D(EVE,E)
D(VE,[])
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) ?
D(EVE,E) 2
D(VEVE,VE)
D(VE,[]) 2
D(EVE,E)
D(VE,[])
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) 2
D(EVE,E) 2
D(VEVE,VE)
D(VE,[]) 2
D(EVE,E)
D(VE,[])
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) 2
D(EVE,E) 2
D(VEVE,VE) D(VE,VE)
D(VE,[]) 2
D(EVE,E)
D(VE,VE) ?
D(VE,[])
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) 2
D(EVE,E) 2
D(VEVE,VE) D(VE,VE)
D(VE,[]) 2
D(EVE,E) D(E,E)
D(VE,VE) ?
D(E,E) 0
D(VE,[])
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) 2
D(EVE,E) 2
D(VEVE,VE) D(VE,VE)
D(VE,[]) 2
D(EVE,E) D(E,E)
D(VE,VE) 0
D(E,E) 0
D(VE,[])
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) 2
D(EVE,E) 2
D(VEVE,VE) D(VE,VE) D(VVE,VE)
D(VE,[]) 2
D(EVE,E) D(E,E)
D(VE,VE) 0
D(E,E) 0
D(VE,[]) D(VVE,VE) ?
Dynamic programming (Top-Down)
D(LOEVE,LOVE) Table size m*n rows EiditDist #
D(LOEVE,LOVE) ?
D(OEVE,OVE) D(OEVE,OVE) ?
D(EVE,VE) ?
D(EVE,VE)
D(VEVE,VE) 2
D(EVE,E) 2
D(VEVE,VE) D(VE,VE) D(VVE,VE)
D(VE,[]) 2