Java Dynamic Programming-Example code for distance editing and java sample code

Source: Internet
Author: User

Java Dynamic Programming-Example code for distance editing and java sample code

The dynamic planning process is: each decision depends on the current state, and then causes the state transfer. A decision sequence is generated in a changed state. Therefore, this multi-stage optimization decision-making process is called dynamic planning.

Dynamic Planning is actually a general term for a category of questions, not a fixed algorithm. The significance of dynamic planning is to solve the overall practice by adopting a recursive (or divide and conquer) strategy to solve the subproblems of big problems. The core idea of dynamic planning is to skillfully split the problem into multiple sub-problems and solve the overall problem by calculating sub-problems. Sub-problems can be split into more sub-problems, so as to solve the required problems using a method similar to recursive iteration. Problem description:

For the sequence S and T, the distance between them is defined as: perform the following operations on either of them: 1, delete a character; 2, insert a character; 3, change a character. each operation increases the count by 1. the minimum count for converting S and T into equal sequences is the editdistance or similarity between the two. provide the corresponding algorithm and its implementation.

Analysis:

Assume that the length of the sequence S and T is m and n respectively, and the editing distance between the two is represented as edit [m] [n]. There are several situations when operating the sequence:

A. When the end characters of S and T are equal, no one of the above definition operations (I .e. "edit") is required for the end characters, that is, no additional count is required. the condition is met: edit [m] [n] = edit [M-1] [n-1].

B. When the end characters of S and T are not equal, edit the end of one of the two. The corresponding count will increase by 1.

B1: Modify the end of S or T to make it equal to T or S. In this case, edit [m] [n] = edit M-1] [n-1] + 1;

B2: Delete the element at the end of S to make S equal to T. In this case, edit [m] [n] = edit M-1] [n] + 1;

B3: Delete the element at the end of T to make T equal to S. In this case, edit [m] [n] = edit [m] [n-1] + 1;

B4: add the tail element T at the end of S to make S and T equal. Then, the length of S is changed to m + 1, but at this time, the end elements of S and T are equal, only the first m elements of S need to be compared with the first n-1 elements of T. Therefore, edit [m] [n] = edit [m] [n-1] + 1 is required;

B5: add the end element of S at the end of T to make T and S equal. In this case, it is the same as b4, meeting the requirements of edit [m] [n] = edit [M-1] [n] + 1;

C. In special cases, when S is null, edit [0] [n] = n; when T is null, edit [m] [0] = m; this is easy to understand. For example, for sequences "" and "abc", the minimum operation of the two is 3, that is, the sequence "" Performs 3 insert operations, or the sequence "abc" Performs 3 Delete operations.

Therefore, it is not difficult to introduce the dynamic planning equation for editing distance as follows:

Therefore, the Recursive Implementation of the Dynamic Programming Algorithm for string editing distance can be expressed using the following Java code:

public static int editDistance(String a, String b) {    if (a == null || b == null) {      return -1;    }    return editDistance(a, a.length() - 1, b, b.length() - 1);  }  public static int editDistance(String a, int m, String b, int n) {    if (m < 0 || n < 0) {      return 1;    } else if (a.charAt(m) == b.charAt(n)) {      return editDistance(a, m - 1, b, n - 1);    } else {      return Math.min(Math.min(editDistance(a, m - 1, b, n) + 1, editDistance(a, m, b, n - 1) + 1), editDistance(a, m - 1, b, n - 1) + 1);    }  }

UPDATE:

At the same time, we can see from the dynamic planning equation of the editing distance that edit [m] [n] can be changed from edit [m-1] [n-1], edit [m-1] [n], edit [m] [n-1] is obtained. If edit is a two-dimensional array, edit [m] [n] can be determined by conditions from the elements at the upper, left, and left positions. that is, we can traverse the two-dimensional array and calculate the current value through backtracking.

For example, for strings S = "sailn" and T = "failing", initialize the two-dimensional array as follows:

M \ n   F A I L I N G
  0 1 2 3 4 5 6 7
S 1 1            
A 2              
I 3              
L 4              
N 5              

Because S [0] = s, T [0] = f, S [0]! = T [0], corresponds to the preceding two-dimensional matrix. edit [1] [1] = min (edit [0] [0], edit [0] [1], edit [1] [0]) + 1: edit [1] [1] = min (0, 1, 1) + 1: edit [1] [1] = 0 + 1 = 1.

M \ n   F A I L I N G
  0 1 2 3 4 5 6 7
S 1 1 2 3 4 5 6 7
A 2 2 1          
I 3              
L 4              
N 5              

For S [1] = a, T [1] = a, S [1] = T [1], it corresponds to a two-dimensional matrix, edit [2] [2] = edit [1] [1], so edit [2] [2] = 1. according to this rule, the above two-dimensional matrix is filled as follows:

M \ n   F A I L I N G
  0 1 2 3 4 5 6 7
S 1 1 2 3 4 5 6 7
A 2 2 1 2 3 4 5 6
I 3 3 2 1 2 3 4 5
L 4 4 3 2 1 2 3 4
N 5 5 4 3 2 2 2 3

Therefore, the editing distance between the two is edit [m] [n] = edit [5] [7] = 3.

Therefore, the Java version of the Backtracking solution dynamically planned based on the above idea can be as follows:

public static int editDistance(String a, String b) {    if (a == null || b == null) {      return -1;    }    int[][] matrix = new int[a.length() + 1][b.length() + 1];    for (int i = 0; i < a.length() + 1; i++) {      for (int j = 0; j < b.length() + 1; j++) {        if (i == 0) {          matrix[i][j] = j;        } else if (j == 0) {          matrix[i][j] = i;        } else {          if (a.charAt(i - 1) == b.charAt(j - 1)) {            matrix[i][j] = matrix[i - 1][j - 1];          } else {            matrix[i][j] = 1 + Math.min(Math.min(matrix[i - 1][j], matrix[i][j - 1]), matrix[i - 1][j - 1]);          }        }      }    }    return matrix[a.length()][b.length()];  }

Summary

The above is the full content of the sample code for Java Dynamic Programming editing distance, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.