Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. function LevenshteinDistance(char s[1..m], char t[1..n]):
  2.  
  3. // create two work vectors of integer distances
  4. declare int v0[n + 1]
  5. declare int v1[n + 1]
  6.  
  7. // initialize v0 (the previous row of distances)
  8. // this row is A[0][i]: edit distance for an empty s
  9. // the distance is just the number of characters to delete from t
  10. for i from 0 to n:
  11. v0[i] = i
  12.  
  13. for i from 0 to m-1:
  14. // calculate v1 (current row distances) from the previous row v0
  15.  
  16. // first element of v1 is A[i+1][0]
  17. // edit distance is delete (i+1) chars from s to match empty t
  18. v1[0] = i + 1
  19.  
  20. // use formula to fill in the rest of the row
  21. for j from 0 to n-1:
  22. if s[i] = t[j]:
  23. substitutionCost := 0
  24. else:
  25. substitutionCost := 1
  26. v1[j + 1] := minimum(v1[j] + 1, v0[j + 1] + 1, v0[j] + substitutionCost)
  27.  
  28. // copy v1 (current row) to v0 (previous row) for next iteration
  29. swap v0 with v1
  30.  
  31. // after the last swap, the results of v1 are now in v0
  32. return v0[n]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement