This is a Java Program to implement Wagner Fischer Algorithm. Wagner–Fischer algorithm is a dynamic programming algorithm that measures the Levenshtein distance between two strings of characters.
Here is the source code of the Java Program to implement Wagner Fischer Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/**
** Java Program to implement Wagner Fischer Algorithm
**/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/** Class WagnerFischer **/
public class WagnerFischer
{
/** Function to get levenshtein distance between 2 strings **/
public int getLevenshteinDistance(String str1, String str2)
{
int len1 = str1.length();
int len2 = str2.length();
int[][] arr = new int[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++)
arr[i][0] = i;
for (int i = 1; i <= len2; i++)
arr[0][i] = i;
for (int i = 1; i <= len1; i++)
{
for (int j = 1; j <= len2; j++)
{
int m = (str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0:1;
arr[i][j] = Math.min(Math.min(arr[i - 1][j] + 1, arr[i][j - 1] + 1), arr[i - 1][j - 1] + m);
}
}
return arr[len1][len2];
}
/** Main Function **/
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Wagner Fischer Test\n");
/** Accept two strings **/
System.out.println("\nEnter string 1 :");
String str1 = br.readLine();
System.out.println("\nEnter string 2 :");
String str2 = br.readLine();
/** make object and call function **/
WagnerFischer wf = new WagnerFischer();
int lDist = wf.getLevenshteinDistance(str1, str2);
System.out.println("\nLevenshtein Distance = "+ lDist);
}
}
Wagner Fischer Test Enter string 1 : sunday Enter string 2 : saturday Levenshtein Distance = 3
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.
Related Posts:
- Check Java Books
- Apply for Java Internship
- Practice BCA MCQs
- Practice Programming MCQs
- Practice Information Technology MCQs