Java Program to Implement Wagner Fischer Algorithm

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.

  1. /**
  2.  **  Java Program to implement Wagner Fischer Algorithm
  3.  **/
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. import java.io.IOException;
  8.  
  9. /** Class WagnerFischer **/
  10. public class WagnerFischer
  11. {
  12.     /** Function to get levenshtein distance between 2 strings **/
  13.     public int getLevenshteinDistance(String str1, String str2)
  14.     {    
  15.         int len1 = str1.length();
  16.         int len2 = str2.length();
  17.         int[][] arr = new int[len1 + 1][len2 + 1];
  18.         for (int i = 0; i <= len1; i++)
  19.             arr[i][0] = i;
  20.         for (int i = 1; i <= len2; i++)
  21.             arr[0][i] = i;
  22.         for (int i = 1; i <= len1; i++)
  23.         {
  24.             for (int j = 1; j <= len2; j++)
  25.             {
  26.                 int m = (str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0:1;                        
  27.                 arr[i][j] = Math.min(Math.min(arr[i - 1][j] + 1, arr[i][j - 1] + 1), arr[i - 1][j - 1] + m);
  28.             }
  29.         }
  30.         return arr[len1][len2];
  31.     }
  32.  
  33.     /** Main Function **/
  34.     public static void main(String[] args) throws IOException
  35.     {    
  36.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  37.         System.out.println("Wagner Fischer Test\n");
  38.  
  39.         /** Accept two strings **/
  40.         System.out.println("\nEnter string 1 :");
  41.         String str1 = br.readLine();
  42.         System.out.println("\nEnter string 2 :");
  43.         String str2 = br.readLine();
  44.  
  45.         /** make object and call function **/
  46.         WagnerFischer wf = new WagnerFischer();
  47.         int lDist = wf.getLevenshteinDistance(str1, str2);    
  48.  
  49.         System.out.println("\nLevenshtein Distance = "+ lDist);    
  50.     }
  51. }

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.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.