Java Program to Implement Wagner and Fisher Algorithm for Online String Matching
The Wagner-Fischer Algorithm is a dynamic programming algorithm that measures the Levenshtein distance or the edit distance between two strings of characters. Levenshtein Distance(LD) calculates how similar are the two strings. The distance is calculated by three parameters to transform the string1 to string2.
The parameters are:
- Number of deletions
- Number of insertions
- Number of Substitutions
Input: str1="cat" str2="cat" Output: 0 The levenshtein distance i.e. LD(str1,str2)=0, because both the strings are equal and no changes are needed. Input: str1="bat" str2="cat" Output: 1 The levenshtein distance i.e. LD(str1,str2)=1, because we need to substitute 'b' with 'c' to transform the string1 to string2. Input: str1="bat" str2="ball" Output: 2 The levenshtein distance i.e. LD(str1,str2)=2, because there is one substitution of 't' to 'l' and one insertion of 'l' needed to transform "bat" to "ball".
- Store lengths of both the strings str1 and str2 in some variables say n and m respectively.
- If n==0 return m
- If m==0 return n
- Construct a matrix of m rows and n columns and initialize the first row to 0 to n and the first column to 0 to m.
- Check each character of str1 and each character to str2.
- If the character at str1[i] is equal to the character at str2[j] then m is 0 and if both are not equal then m is 1.
- Set the element at arr[i][j] of the matrix as the minimum of the following: (arr[i-1][j]+1, arr[i][j-1]+1, arr[i-1][j-1]+m)
- After iterating through the steps 5, 6 ,7 ,the distance is found at arr[n][m].
Time complexity: O(m*n).