Edit Distance Problem
Source: http://people.csail.mit.edu/bdean/6.046/dp/
Problem:
Solution:
Highlight the part between the * symbols for the answer.
* This is the classic Levenshtein DP. Let D(i, j) = edit distance between the first i characters of A and the first j characters of B. Then:
D(0, j) = j, D(i, 0) = i, and
D(i, j) = min of:
D(i-1, j-1) + (0 if A[i] = B[j] else 1) (match or substitute)
D(i-1, j) + 1 (delete A[i])
D(i, j-1) + 1 (insert B[j])
Fill the (n+1) x (m+1) table row by row - O(nm) time and space; D(n, m) is the edit distance. To recover the PATH (the actual sequence of operations), store a back-pointer with each cell recording which of the three moves achieved the minimum, then walk back from (n, m) to (0, 0). Space can be cut to O(min(n, m)) for the distance alone by keeping only two rows (keep the full table if you need the path).
Solution by nick from the comments. *
Problem:
Given two text strings A of length n and B of length m, you want to transform A into B with a minimum number of operations of the following types: delete a character from A, insert a character into A, or change some character in A into a new character. The minimal number of such operations required to transform A into B is called the edit distance between A and B.
Given two strings A and B, what is the path from A to B with minimum edit distance?
Given two strings A and B, what is the path from A to B with minimum edit distance?
Solution:
Highlight the part between the * symbols for the answer.
* This is the classic Levenshtein DP. Let D(i, j) = edit distance between the first i characters of A and the first j characters of B. Then:
D(0, j) = j, D(i, 0) = i, and
D(i, j) = min of:
D(i-1, j-1) + (0 if A[i] = B[j] else 1) (match or substitute)
D(i-1, j) + 1 (delete A[i])
D(i, j-1) + 1 (insert B[j])
Fill the (n+1) x (m+1) table row by row - O(nm) time and space; D(n, m) is the edit distance. To recover the PATH (the actual sequence of operations), store a back-pointer with each cell recording which of the three moves achieved the minimum, then walk back from (n, m) to (0, 0). Space can be cut to O(min(n, m)) for the distance alone by keeping only two rows (keep the full table if you need the path).
Solution by nick from the comments. *
I think it will be Levenshtein distance if the distance for insertion is same for all character pairs[replacing A with B has same cost as replacing A with G] else Dynamic Time Warp Distance.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteInitial Condition: Edit distance for m = 0 , n= 0 is 0 ie (0,0) = 0
ReplyDeletefor each (i,j) where i belongs to m and j belongs to n
check = min { i.(i - 1 , j- 1) + cost of replacement of ith and jth element(here if a[i] == b[j] then the cost will be zero)
ii.(i,j-1) + cost of insertion of the jth element
iii.(i-1,j) + cost of the insertion of the ith element
}
This problem if solved recursively will take exponential time however can be done in O(m*n) by using dynamic programming with additional O(m*n) spaces.
eg: APPLE and CAPE
Considering the cost of addition, deletion or replacement is each 1.
A P P L E
C 1 2 3 4 5
A 1 2 3 4 5
P 2 1 2 3 4
E 3 3 2 3 3
Space Optimization:
The problem can be further optimized for space by using only the last array. Thus, only additional space required will be 2* min(m,n).