Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Wagner-Fisher algorithm for calculating Levenshtein distance
- int WagnerFisher(char *A, int lenA, char *B, int lenB){
- int distance = 0;
- int **matrix;
- int j, k, cost, temp;
- //allocating 2D array with Calloc
- matrix = calloc(lenA, sizeof(int *));
- for(j = 0; j < lenA; j++) {
- matrix[j] = calloc(lenB, sizeof(int));
- }
- //Wagner-Fisher Alhorithm
- for(j = 0; j < lenA; j++){
- matrix[j][0] = j;
- }
- for(k = 0; k < lenB; k++){
- matrix[0][k] = k;
- }
- for(j = 1; j < lenA; j++){
- for(k = 1; k < lenB; k++){
- if(A[j - 1] == B[k - 1]){
- cost = 0;
- }else{
- cost = 1;
- }
- temp = minimum((matrix[j - 1][k] + 1), (matrix[j][k - 1] + 1));
- matrix[j][k] = minimum(temp, (matrix[j - 1][k - 1] + cost));
- cost = 0;
- }
- }
- for(j = 0; j< lenA; j++){
- for(k = 0; k < lenB; k++){
- printf(" %d",matrix[j][k]);
- }
- printf("\n");
- }
- distance += matrix[lenA - 1][lenB - 1];
- //freeing 2D array
- for(j = 0; j < lenA; j++) {
- free(matrix[j]);
- }
- free(matrix);
- return distance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement