Skip to content

Instantly share code, notes, and snippets.

@gandroz
Last active May 4, 2021 17:51
Show Gist options
  • Save gandroz/949575f6c9332b0c5e2832a0f834ff7e to your computer and use it in GitHub Desktop.
Save gandroz/949575f6c9332b0c5e2832a0f834ff7e to your computer and use it in GitHub Desktop.
Minimum Levenshtein distance optimized
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
int levenshtein(char *source, char *target, int ins_cost, int del_cost, int rep_cost)
{
/*
Input:
source: a string corresponding to the string you are starting with
target: a string corresponding to the string you want to end with
ins_cost: an integer setting the insert cost
del_cost: an integer setting the delete cost
rep_cost: an integer setting the replace cost
Output:
the minimum edit distance (med) required to convert the source string to the target
*/
// use deletion and insert cost as 1
int nb_row = strlen(source);
int nb_col = strlen(target);
int row, col, diag, temp;
// initialization
unsigned int line[nb_col + 1];
for (col=1; col<nb_col + 1; col++) {
line[col] = col;
}
// iterate over rows
for (row=1; row<nb_row + 1; row++) {
line[0] = row;
for (col=1, diag=row - 1; col<nb_col + 1; col++) {
temp = line[col];
line[col] = MIN3(line[col-1]+ins_cost, line[col]+del_cost, diag + (source[row-1] == target[col-1] ? 0 : rep_cost));
diag = temp;
}
}
return line[nb_col];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment