Advertisement
Guest User

Wagner fisher

a guest
Mar 28th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. //Wagner-Fisher algorithm for calculating Levenshtein distance
  2.  
  3. int WagnerFisher(char *A, int lenA, char *B, int lenB){
  4.  
  5. int distance = 0;
  6. int **matrix;
  7. int j, k, cost, temp;
  8.  
  9. //allocating 2D array with Calloc
  10.  
  11. matrix = calloc(lenA, sizeof(int *));
  12. for(j = 0; j < lenA; j++) {
  13. matrix[j] = calloc(lenB, sizeof(int));
  14. }
  15.  
  16.  
  17. //Wagner-Fisher Alhorithm
  18.  
  19. for(j = 0; j < lenA; j++){
  20. matrix[j][0] = j;
  21. }
  22. for(k = 0; k < lenB; k++){
  23. matrix[0][k] = k;
  24. }
  25.  
  26. for(j = 1; j < lenA; j++){
  27. for(k = 1; k < lenB; k++){
  28. if(A[j - 1] == B[k - 1]){
  29. cost = 0;
  30. }else{
  31. cost = 1;
  32. }
  33.  
  34. temp = minimum((matrix[j - 1][k] + 1), (matrix[j][k - 1] + 1));
  35. matrix[j][k] = minimum(temp, (matrix[j - 1][k - 1] + cost));
  36. cost = 0;
  37.  
  38. }
  39. }
  40.  
  41. for(j = 0; j< lenA; j++){
  42. for(k = 0; k < lenB; k++){
  43. printf(" %d",matrix[j][k]);
  44. }
  45. printf("\n");
  46. }
  47.  
  48. distance += matrix[lenA - 1][lenB - 1];
  49.  
  50. //freeing 2D array
  51.  
  52. for(j = 0; j < lenA; j++) {
  53. free(matrix[j]);
  54. }
  55. free(matrix);
  56.  
  57. return distance;
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement