Docsity

Edit Distance and Approximate Matching: Algorithms and Complexity - Prof. Sugih Jamin, Study notes of Algorithms and Programming

Partial preview of the text

Download Edit Distance and Approximate Matching: Algorithms and Complexity - Prof. Sugih Jamin and more Study notes Algorithms and Programming in PDF only on Docsity!

Outline

Today:

  • Edit Distance and Approximate Matching
  • Knuth-Morris-Pratt and Boyer-Moore pattern matching

Edit Distance

Edit (a.k.a. Levenshtein) distance of two words s and t, dist(s,t), is the smallest number of operations selected from R(), D(), I() that turns s into t

Three allowed operations, each at unit cost:

  • R(x,y): replace character x with y
  • D(x): delete x
  • I(x): insert x

Example: dist("ghost", "house") = 3

g h o s t h o s t ; D(g) h o u s t ; I(u) h o u s e ; R(t,e)

Edit Distance Computation Example

Example: s = presto, t = peseta

D 0 1 2 3 4 5 s\t p e s e t a 0 p 0 1 2 3 4 5 1 r 1 1 2 3 4 5 2 e 2 1 3 s 3 2 4 t 4 3 2 5 o 5 4 3

First row: dist(s[0],t[0]) = dist("p", "p") = 0 dist(s[0],t[0,1]) = dist("p", "pe") = 1, i.e., p+I(e) dist(s[0],t[0..2]) = dist("p", "pes") = 2, i.e., p+I(e)+I(s)

... dist(s[0],t[0..j]) = dist("p", "p...") = j, i.e., p+I(...) Second column: dist(s[0..i],t[0,1]) = i-1, i >= 2, i.e., p+D(r)+e+D(s)+D(...) Second row: dist(s[0,1],t[0..j]) = j, j >= 1, i.e., p+R(r,e)+I(s)+I(e)+I(...) D[4,3]: dist(s[0..4],t[0..3]) = dist("prest", "pese") = 2, i.e., p+D(r)+e+s+R(t,e) D[5,5]: dist("presto", "peseta") = 3, i.e., p+D(r)+e+s+I(e)+t+R(o,a)

DP for Edit Distance

Let D[i, j] = dist(s[0..i], t[0..j]), in other words, dist(s, t) = D[|s| − 1 , |t| − 1]

How to compute the D[] table algorithmically, using dynamic programming?

Running time: O(mn), m = |s|, n = |t|

Wagner-Fischer (more formally)

D[i, j] =

    

D[i − 1 , j − 1] emit s[i] if s[i] == t[j],

1 + MIN

  

D[i − 1 , j − 1] do R(s[i],t[j]) D[i − 1 , j] do D(s[i]) D[i, j − 1] do I(t[j])

otherwise

D -1 0 1 2 3 4 5

s\t p e s e t a -1 0 1 2 3 4 5 6 0 p 1 0 1 2 3 4 5 1 r 2 1 1 2 3 4 5 2 e 3 2 1 2 2 3 4 3 s 4 3 2 1 2 3 4 4 t 5 4 3 2 2 2 3 5 o 6 5 4 3 3 3 3

D[− 1 , j] = smallest number of edit operations to turn an empty string into t[0..j] = j + 1, similarly, D[i, −1] = i + 1, D[0, 0] = D[− 1 , −1] = 0, D[0, 1] = 1 + MIN(0, 1 , 2) = 1, D[1, 1] = 1 + MIN(0, 1 , 1) = 1, D[1, 4] = 1 + MIN(3, 3 , 4) = 4, D[2, 1] = D[1, 0] = 1.

Edit Operations

dist("presto", "peseta") = 3, which 3 edit operations?

D[i, j] =

    

D[i − 1 , j − 1] emit s[i] if s[i] == t[j],

1 + MIN

  

D[i − 1 , j − 1] do R(s[i],t[j]) D[i − 1 , j] do D(s[i]) D[i, j − 1] do I(t[j])

otherwise

D -1 0 1 2 3 4 5

s\t p e s e t a -1 0 1 2 3 4 5 6 0 p 1 0 1 2 3 4 5 1 r 2 1 1 2 3 4 5 2 e 3 2 1 2 2 3 4 3 s 4 3 2 1 2 3 4 4 t 5 4 3 2 2 2 3 5 o 6 5 4 3 3 3 3

D[5, 5] = 1 + D[4, 4] ⇒ R(s[5],t[5]) = R(o,a) D[4, 4] = D[3, 3] ⇒ s[4] = t D[3, 3] = 1 + D[3, 2] ⇒ I(t[3]) = I(e) D[3, 2] = D[2, 1] ⇒ s[3] = s D[2, 1] = D[1, 0] ⇒ s[2] = e D[1, 0] = 1 + D[0, 0] ⇒ D(s[1]) = D(r) D[0, 0] = D[− 1 , −1] ⇒ s[0] = p

Best Approximate Match

Brute force:

  • compute the edit distance between P and all substrings of T
  • choose the substring with the smallest edit distance

For m = |P |, n = |T |, there are O(n^2 ) substrings of T , and each substring takes O(mn) time to compare

Running time: O(mn^3 )

Computing Best Approximate Match

Observations:

  1. the BF method recomputes some comparisons multiple times: P [0]? = T [0], P [0]? = T [0, 1], P [0, 1]? = T [0], P [0, 1]? = T [0, 1], etc. ⇒ Use dynamic programming
  2. computing the edit distance between P [0..i] and T [0..j] already compares P [0..i] against all prefixes of T [0..j], we only need to further compare P [0..i] against suffixes of T [0..j], i.e., against T [l..j], 0 ≤ l ≤ j + 1 (l = j + 1 is the empty string).

AD[i, j] = MIN{dist(P [0..i], T [l..j])| 0 ≤ l ≤ j + 1}

Best Approximate Match Example

AD[i, j] =

    

AD[i − 1 , j − 1] if s[i] == t[j],

1 + MIN

  

AD[i − 1 , j − 1] AD[i − 1 , j] AD[i, j − 1]

otherwise

AD -1 0 1 2 3 4 5 6 7 8 9 A

j a m , p i e , t e a -1 0 0 0 0 0 0 0 0 0 0 0 0 0 p 1 1 1 1 1 0 1 1 1 1 1 1 1 e 2 2 2 2 2 1 1 1 2 2 1 2 2 i 3 3 3 3 3 2 1 2 2 3 2 2

Best match (AD[m, j] minimal ): w = T [4, 5] = pi

Multiple Best Approximate Matches

AD -1 0 1 2 3 4 5 6 7 8 9 A a p l i a n t p l o t -1 0 0 0 0 0 0 0 0 0 0 0 0 0 p 1 1 0 1 1 1 1 1 0 1 1 1 1 l 2 2 1 0 1 2 2 2 1 0 1 2 2 a 3 2 2 1 1 1 2 3 2 1 1 2 3 i 4 3 3 2 1 2 2 3 3 2 2 2 4 n 5 4 4 3 2 2 2 3 4 3 3 3

Best matches (AD[m, j] minimal ): w 1 = T [1, 3] = pli: D(n),D(a) w 2 = T [1, 4] = plia: R(n,a), D(a) w 3 = T [1, 5] = plian: D(i), I(i)

Knuth-Morris-Pratt Algorithm: Idea

Both the Brute-Force and Simplified-BM algorithms completely throw away comparisons already made and re-start from scratch

KMP’s idea: why not save the comparisons we have made and not redo them whenever possible?

Knuth-Morris-Pratt: Intuition

T: c a d c a d c c a d c a c a d c a d b P: c a d c a d b ; left-to-right 1 2 3 4 5 6 7 ; "cadcad" matches prefix "cad" c a d c a d b 8 9 ; "cadc" matches prefix "c" c a d c a d b 10 ; "c" matches only itself c a d c a d b 11... .16 ; "cadca" matches prefix "ca" c a d c a d b 17 ; "ca" matches only itself c a d c a d b 18..... 24 ; match found

kmp match()

int /* index of matching start in T */ kmp_match(char T[], char P[]) { int i; int j = 0; int n = sizeof(T); int m = sizeof(P); int *prefixlen = compute_prefixlen(P); for (i = 0; i < n; i++) { while ((j > 0) && (T[i] != P[j])) j = prefixlen[j-1]; if (T[i] == P[j]) { j++; if (j == m) return (i-m+1); } } return NOTFOUND; }

KMP prefixlen[] Construction

How many of the tail elements in P match its head (unlike when matching T , which matches only up to T [i − 1], here matches all the way to P [j])

For P : c a d c a d b ; prefixlen[] = { 0 , 0 , 0 , 1 , 2 , 3 , 0 } P: c ; by def., prefixlen[0]= P: c a c ; no match, prefixlen[1]= P: c a d c ; no match, prefixlen[2]= P: c a d c c ; match, prefixlen[3]= P: c a d c a c a ; match, prefixlen[4]= P: c a d c a d c a d ; match, prefixlen[5]= P: c a d c a d b c a d c ; no match, c a d ; no match, (if brute force!) c a ; no match, c ; no match, prefixlen[6]=