The Wagner-Fischer algorithm is a dynamic programming method for calculating the
Levenshtein distance (edit distance) between two strings. It determines the
minimum number of single-character edits (insertions, deletions, or
substitutions) needed to transform one string into another by filling a matrix
where each cell represents the edit distance between prefixes of the two
strings. The algorithm builds the solution incrementally, ensuring optimal
substructure and overlapping subproblems for efficient computation.
// @file Wagner-Fischer Edit Distance
// @author Dustin Boston
// @see R. Wagner, and M. Fischer, "The String-to-String Correction Problem,"
// ACM, vol. 21, no. 1, pp. 168–173, 1974.
// Calculates the edit distance between two strings, indicating how many
// operations (insert, delete, substitute) are required to transform the
// start word into the target word. This function implements the Wagner-Fischer
// algorithm.
exportfunctioneditDistance(startTerm: string,targetTerm: string):number[][]{constdistance: number[][]=[[0]];for(leti=1;i<=startTerm.length;i++){distance[i]=[distance[i-1][0]+cost(startTerm[i-1],undefined)];}for(letindex=1;index<=targetTerm.length;index++){distance[0][index]=distance[0][index-1]+cost(undefined,targetTerm[index-1]);}for(leti=1;i<=startTerm.length;i++){for(letindex=1;index<=targetTerm.length;index++){constm1=distance[i-1][index-1]+cost(startTerm[i-1],targetTerm[index-1]);constm2=distance[i-1][index]+cost(startTerm[i-1],undefined);constm3=distance[i][index-1]+cost(undefined,targetTerm[index-1]);distance[i][index]=Math.min(m1,m2,m3);}}returndistance;}/**
* Determines the cost of an edit operation between two characters or undefined
* values. The cost is used in the calculation of the edit distance between
* two strings.
*
* @param a - Char from the first string or undefined if it's an insert/delete.
* @param b - Char from the second string or undefined if it's an insert/delete.
* @returns Cost of the edit operation. 0 for no change, 1 for all others.
* @throws Error if both parameters are undefined.
*/exportfunctioncost(a: string|undefined=undefined,b: string|undefined=undefined):number{if(a===b)return0;if(a!==undefined&&b!==undefined)return1;// Change
if(a===undefined&&b!==undefined)return1;// Insert
if(a!==undefined&&b===undefined)return1;// Delete
thrownewError("Invalid cost arguments");}