Lecture 24
Lecture 24
Analysis of
Algorithm
Lecture 24: Dynamic Programming
Dr. Tauheed
Contents
Edit Distance
2
Edit Distance: Introduction
One measure of similarity between two strings is their edit distance.
This is a measure of the number of operations required to transform the first string into the other.
The edit distance between two strings is defined as the minimum number of edit
operations needed to transform the first into the second. Matches are not included in
the count.
Definitions : Optimal Answers
Let = {I, D, R, M} be the edit alphabet
An optimal transcript is an edit transcript with the minimal number of edit operations
for transforming one string into another.
The edit distance problem entails computing the edit distance between two strings
along with an optimal transcript.
String Alignment
A global alignment of strings and is obtained by:
1. Inserting dashes/spaces into or at the ends of and .
2. Aligning the two strings s.t. each character/space in either string is opposite a
unique character/space in the other string.
Example 1: Example 2:
S1 = qacdbd S2 = qawxb S1 = vintner S2 = writers
qac-dbd v-intner-
qawx-b– wri-t-ers
Edit Distance
D(i,j), the edit distance of S1[1..i] and S2[1..j] is the minimum number of edit operations
needed to transform the first i characters of S1 into the first j characters of S2.
• Observation : There are many possible ways to transform one string into another.
Edit Distance: Approach
• The general recurrence is given by:
Here
3. D(i - 1, j - 1) + t (i,j)
A B C D
Use following recurrence to fill other values 0 1 2 3 4
A 1 0 1 2 3
D(i,j) = min[D(i - 1, j) + 1, D(i, j - 1) + 1, D(i - 1, j - 1) + t (i,j)]
E 2 1 1 2 3
where C 3 2 2 1 2
D 4 3 3 2 1
B 5 4 3 3 2