Wednesday, August 4, 2010

Hirschberg's algorithm

Hirschberg's algorithm, named after its inventor, Dan Hirschberg, is a dynamic programming algorithm that finds the least cost sequence alignment between two strings, where cost is measured as Levenshtein edit distance, defined to be the sum of the costs of insertions, replacements, deletions, and null actions needed to change one string to the other. Hirschberg's algorithm is simply described as a divide and conquer version of the Needleman-Wunsch algorithm.

The Needleman-Wunsch algorithm finds an optimal alignment in O(nm) time, using O(nm) space. Hirschberg's algorithm is a clever modification of the Needleman-Wunsch Algorithm which still takes O(nm) time, but needs only O(min{m,n}) space.

  • The edit distance Edit(x,y) is the least cost of changing x into y by using the operations Insert, Substitute, and Delete, where the cost of each kind of operation is given. 
  • We write Ins(a) for the cost of inserting a symbol a into a string, Sub(a,b) for the cost of substituting a symbol b for a in a string, and Del(a) for the cost of deleting a symbol a from a string.
  • Pref[x,i] denotes the prefix of x of length i.
The "standard" choice of those costs is Ins(a) = Del(a) = 1 for each symbol a, Sub(a,a) = 0, and Sub(a,b) = 1 if a is not equal to b. The Needleman-Wunsch algorithm computes Edit(x,y) by computing Edit(Pref[x,i],Pref[y,j]) for all i and j dynamically, where  That algorithm requires O(nm) time and O(nm) space, where n = |x| and m = |y|.

Understanding algorithm
To understand Hirschberg's algorithm, it is important to first understand that edit distances can be computed using linear space.

What we call the "forward subprogram" computes the values of Edit(Pref[x,i],Pref[y,j]) for all i and j, just as the Needleman-Wunsch and returns the array {Edit(x,Pref[y,j])}0 ≤ j ≤ m. The "backward subprogram" is similar, except that the dynamic program is done in the opposite direction, i.e., starting from the right ends of the strings. It returns the array {Edit(x,Suff[y,j])}0 ≤ j ≤ m, where Suff[y,j] is the suffix of y of length j.

The forward and backward subprograms compute values in the matrix by using previously computed values, but save space by erasing values that will no longer be needed during that execution of the subprogram. Unfortunately, the erased values will need to be recomputed later; thus, Hirschberg's algorithm takes roughly twice as much time as Needleman-Wunsch.


Pseudocode

Forwards[x,y]
  1. Forwards[x,y] is
  2. 
    
  3. 1.  n = |x|;  m = |y|
  4. 2.  For all i from 1 to n:
  5. Edit[Pref[x,i],Pref[y,0]] = 0
  6. 3.  For all j from 1 to m:
  7. A.  Edit[Pref[x,0],Pref[y,j]] = Edit[Pref[x,0],Pref[y,j-1]] + Ins(y_j)
  8. B.  For all i from 1 to n, execute the following steps:
  9. i.  Edit[Pref[x,i],Pref[y,j]] =
  10. min{Edit[Pref[x,i-1],Pref[y,j]] + Del(x_i),
  11. Edit[Pref[x,i-1],Pref[y,j-1]] + Sub(x_i,y_j),
  12. Edit[Pref[x,i],Pref[y,j-1]] + Ins(y_j)}
  13. ii.  Erase Edit[Pref[x,i-1],Pref[y,j-1]]
  14. C.  Erase Edit[Pref[x,i-1],Pref[y,j]]
  15. 4.  RETURN Edit[x]  %% an array of length m+1


Backwards[x,y]

  1. 1.  n = |x|;  m = |y|
  2. 2.  For all i from 1 to n:
  3. Edit[Suff[x,i],Suff[y,0]] = 0
  4. 3.  For all j from 1 to m:
  5. A.  Edit[Suff[x,0],Suff[y,j]] = Edit[Suff[x,n],Suff[y,j-1]] + Ins(y_{m-j+1})
  6. B.  For all i from 1 to n:
  7. i.  Edit[Suff[x,i],Suff[y,j]] =
  8. min{Edit[Suff[x,i-1],Suff[y,j]] + Del(x_{n-i-1}),
  9. Edit[Suff[x,i-1],Suff[y,j-1]] + Sub(x_{n-i-1},y_{m-j+1}),
  10. Edit[Suff[x,i],Suff[y,j-1]] + Ins(y_{m-j+1})}
  11. ii.  Erase Edit[Suff[x,i-1],Suff[y,j-1]]
  12. C.  Erase Edit[Suff[x,i-1],Suff[y,j]]
  13. 4.  RETURN Edit[x] %% an array of length m+1



//High level description of Hirschberg's algorithm:

Hirschberg(x,y)

  1. 1.  n = |x|;  m = |y|
  2. 2.  If n <= 1 or m <= 1:
  3. OUTPUT Alignment(x,y) using Needleman-Wunsch.
  4. Else:
  5. A.  mid = floor(n/2)
  6. B.  x^left = Pref[x,mid]
  7. C.  x^right = Suff[x,n-mid]
  8. D.  Edit[x^left] = Forwards(x^left,y)  %% an array of length m+1
  9. E.  Edit[x^right] = Backwards(x^right,y) %% an array of length m+1
  10. F.  cut = ArgMin{Edit[x^left,Pref[y,j]] + Edit[x^right,Suff[y,m-j]]}  %% j ranges from 1 to m-1
  11. G.  Hirschberg(x^left,Pref[y,cut])
  12. H.  Hirschberg(x^right,Suff[y,m-cut])

Reactions:

0 comments:

Post a Comment