xref: /openbmc/linux/tools/perf/util/levenshtein.c (revision 175729fc)
186470930SIngo Molnar #include "levenshtein.h"
2175729fcSArnaldo Carvalho de Melo #include <errno.h>
3175729fcSArnaldo Carvalho de Melo #include <stdlib.h>
4175729fcSArnaldo Carvalho de Melo #include <string.h>
586470930SIngo Molnar 
686470930SIngo Molnar /*
786470930SIngo Molnar  * This function implements the Damerau-Levenshtein algorithm to
886470930SIngo Molnar  * calculate a distance between strings.
986470930SIngo Molnar  *
1086470930SIngo Molnar  * Basically, it says how many letters need to be swapped, substituted,
1186470930SIngo Molnar  * deleted from, or added to string1, at least, to get string2.
1286470930SIngo Molnar  *
1386470930SIngo Molnar  * The idea is to build a distance matrix for the substrings of both
1486470930SIngo Molnar  * strings.  To avoid a large space complexity, only the last three rows
1586470930SIngo Molnar  * are kept in memory (if swaps had the same or higher cost as one deletion
1686470930SIngo Molnar  * plus one insertion, only two rows would be needed).
1786470930SIngo Molnar  *
1886470930SIngo Molnar  * At any stage, "i + 1" denotes the length of the current substring of
1986470930SIngo Molnar  * string1 that the distance is calculated for.
2086470930SIngo Molnar  *
2186470930SIngo Molnar  * row2 holds the current row, row1 the previous row (i.e. for the substring
2286470930SIngo Molnar  * of string1 of length "i"), and row0 the row before that.
2386470930SIngo Molnar  *
2486470930SIngo Molnar  * In other words, at the start of the big loop, row2[j + 1] contains the
2586470930SIngo Molnar  * Damerau-Levenshtein distance between the substring of string1 of length
2686470930SIngo Molnar  * "i" and the substring of string2 of length "j + 1".
2786470930SIngo Molnar  *
2886470930SIngo Molnar  * All the big loop does is determine the partial minimum-cost paths.
2986470930SIngo Molnar  *
3086470930SIngo Molnar  * It does so by calculating the costs of the path ending in characters
3186470930SIngo Molnar  * i (in string1) and j (in string2), respectively, given that the last
3286470930SIngo Molnar  * operation is a substition, a swap, a deletion, or an insertion.
3386470930SIngo Molnar  *
3486470930SIngo Molnar  * This implementation allows the costs to be weighted:
3586470930SIngo Molnar  *
3686470930SIngo Molnar  * - w (as in "sWap")
3786470930SIngo Molnar  * - s (as in "Substitution")
3886470930SIngo Molnar  * - a (for insertion, AKA "Add")
3986470930SIngo Molnar  * - d (as in "Deletion")
4086470930SIngo Molnar  *
4186470930SIngo Molnar  * Note that this algorithm calculates a distance _iff_ d == a.
4286470930SIngo Molnar  */
4386470930SIngo Molnar int levenshtein(const char *string1, const char *string2,
4486470930SIngo Molnar 		int w, int s, int a, int d)
4586470930SIngo Molnar {
4686470930SIngo Molnar 	int len1 = strlen(string1), len2 = strlen(string2);
4786470930SIngo Molnar 	int *row0 = malloc(sizeof(int) * (len2 + 1));
4886470930SIngo Molnar 	int *row1 = malloc(sizeof(int) * (len2 + 1));
4986470930SIngo Molnar 	int *row2 = malloc(sizeof(int) * (len2 + 1));
5086470930SIngo Molnar 	int i, j;
5186470930SIngo Molnar 
5286470930SIngo Molnar 	for (j = 0; j <= len2; j++)
5386470930SIngo Molnar 		row1[j] = j * a;
5486470930SIngo Molnar 	for (i = 0; i < len1; i++) {
5586470930SIngo Molnar 		int *dummy;
5686470930SIngo Molnar 
5786470930SIngo Molnar 		row2[0] = (i + 1) * d;
5886470930SIngo Molnar 		for (j = 0; j < len2; j++) {
5986470930SIngo Molnar 			/* substitution */
6086470930SIngo Molnar 			row2[j + 1] = row1[j] + s * (string1[i] != string2[j]);
6186470930SIngo Molnar 			/* swap */
6286470930SIngo Molnar 			if (i > 0 && j > 0 && string1[i - 1] == string2[j] &&
6386470930SIngo Molnar 					string1[i] == string2[j - 1] &&
6486470930SIngo Molnar 					row2[j + 1] > row0[j - 1] + w)
6586470930SIngo Molnar 				row2[j + 1] = row0[j - 1] + w;
6686470930SIngo Molnar 			/* deletion */
6786470930SIngo Molnar 			if (row2[j + 1] > row1[j + 1] + d)
6886470930SIngo Molnar 				row2[j + 1] = row1[j + 1] + d;
6986470930SIngo Molnar 			/* insertion */
7086470930SIngo Molnar 			if (row2[j + 1] > row2[j] + a)
7186470930SIngo Molnar 				row2[j + 1] = row2[j] + a;
7286470930SIngo Molnar 		}
7386470930SIngo Molnar 
7486470930SIngo Molnar 		dummy = row0;
7586470930SIngo Molnar 		row0 = row1;
7686470930SIngo Molnar 		row1 = row2;
7786470930SIngo Molnar 		row2 = dummy;
7886470930SIngo Molnar 	}
7986470930SIngo Molnar 
8086470930SIngo Molnar 	i = row1[len2];
8186470930SIngo Molnar 	free(row0);
8286470930SIngo Molnar 	free(row1);
8386470930SIngo Molnar 	free(row2);
8486470930SIngo Molnar 
8586470930SIngo Molnar 	return i;
8686470930SIngo Molnar }
87