8. Dynamic Planning (1) -- The distance between string editing and string Planning

Source: Internet
Author: User

8. Dynamic Planning (1) -- The distance between string editing and string Planning

The algorithm questions of dynamic planning are often frequent visitors to the pen questions of major companies. Among the public accounts of many algorithms, articles on "Dynamic Planning" are not uncommon. They are all trying to describe dynamic planning in the most easy-to-understand text, and some even use cartoons to explain it, read every article pushed by the public account carefully. In fact, you can understand it and have a rough idea about dynamic planning.

What is dynamic planning? In a general sense, the solution to a problem is known at first glance (poor), but it cannot be counted one by one. You have to find the optimal solution, in other words, there will be a reference like "maximum", "minimum", and "How many kinds of questions". These questions can be solved using dynamic planning in theory.The dynamic planning method is similar to the partitioning method. It solves the original problem by combining the subproblem solutions. However, it only solves each subproblem once and saves it in the table without re-calculation, usually used to solve optimization problems-Introduction to Algorithms.

Edit Distance: This article refers to Levenshtein Distance, that is, the number of times that string S1 can be changed to string S2 through the insert, modify, and delete operations. For example, S1 = abc, S2 = abf, and the editing distance is d = 1 (you only need to change c to f ). In this article, we will use the dynamic programming algorithm to solve the string editing distance.

Definition: S1 and S2 indicate two strings, S1 (I) indicates the first character of S1, d [I, j] indicates the I-th prefix of S1 to the j-th prefix of S2 (for example, S1 = "abc", S2 = "def ", the editing distance from S1 to S2 is d [3, 3]).

1) At the end of the S1 stringInsertThe character "f". In this case, S1 = "abcf", S2 = "def", that is, S1 [I] = S2 [j, the editing distance from S1 to S2 is d [4, 3] = d [3, 2]. ThereforeD [I, j] = d [I, j-1] + 1. (+ 1 is because S1 adds "f ")

2) insert the character "c" at the end of the S2 string. In this case, S1 = "abc", S2 = "defc", that is, S1 [I] = S [j, the editing distance from S1 to S2 is d [3, 4] = d [2, 3]. ThereforeD [I, j] = d [I-1, j] + 1In fact, this is done for S1.Delete. (+ 1 is because S2 adds "c ")

3) Replace the last character of the S1 stringModifyIs "f". In this case, S1 = "abf", S2 = "def", that is, S1 [I] = S [j, the editing distance from S1 to S2 is d [3, 3] = d [2, 2]. ThereforeD [I, j] = d [I-1, j-1] + 1. (+ 1 is because S1 modifies "c ")

In conclusion, the recursive formula is obtained:

=>

Use a table to show the dynamic programming process for solving S1 = "abc" and S2 = "def.

We can see that the Red Square is the final editing distance, and the whole solution process is to fill the table with a two-dimensional array. The following describes how to dynamically plan and solve the string editing distance in Java and Python.

Java

 

1 package com. algorithm. dynamicprogramming; 2 3/** 4 * dynamic planning -- the distance between string editing and 5 * s1 = "abc", s2 = "def" 6 * calculation formula: 7 * | 0 I = 0, j = 0 8 * | j I = 0, j> 0 9 * d [I, j] = | I> 0, j = 0 10 * | min (d [I, J-1] + 1, d [I-1, j] + 1, d [I-1, J-1]) s1 (I) = s2 (j) 11 * | min (d [I, J-1] + 1, d [I-1, j] + 1, d [I-1, J-1] + 1) s1 (I) =s2 (j) 12 * defines two-dimensional arrays [4] [4]: 13 * d e f 14 * | x | 0 | 1 | 2 | 3 | 15 * a | x | => a | 1 | 1 | 2 | 3 | => editing Logical distance d = [3] [3] = 3 16 * B | x | B | 2 | 2 | 3 | 17 * c | x | x | c | 3 | 3 | 3 | 18*19 * Created by yulinfeng on 6/29/17. 20 */21 public class Levenshtein {22 23 public static void main (String [] args) {24 String s1 = "abc"; 25 String s2 = "def "; 26 int editDistance = levenshtein (s1, s2); 27 System. out. println (the distance between "s1 =" + s1 + "and s2 =" + s2 + "is:" + editDistance ); 28} 29 30/** 31 * editing distance solving 32 * @ pa Ram s1 String s1 33 * @ param s2 String s2 34 * @ return edit distance from 35 */36 private static int levenshtein (String s1, String s2) {37 int I = 0; // character subscript 38 int j = 0 in s1 string; // character subscript 39 char s1i = 0 in s2 string; // I character 40 char s2j = 0 in s1 string; // s2 string j characters 41 int m = s1.length (); // s1 String Length 42 int n = s2.length (); // s2 String Length 43 if (m = 0) {// s1 string length is 0, the editing distance is s2 String Length 44 return n; 45} 46 if (n = 0) {47 return m; // s2 character The length of the string is 0. The editing distance is 48} 49 int [] [] solutionMatrix = new int [m + 1] [n + 1]. // solution matrix 50/** 51 * d e f 52 * | 0 | x | 53 * a | 1 | x | 54 * B | 2 | x | 55 * c | 3 | x | 56 */57 for (I = 0; I <m + 1; I ++) {58 solutionMatrix [I] [0] = I; 59} 60/** 61 * d e f 62 * | 0 | 1 | 2 | 3 | 63 * a | x | 64 * B | x | x | 65 * c | x | 66 */67 for (j = 0; j <n + 1; j ++) {68 solutionMatrix [0] [j] = j; 69} 70/*** after the above two operations, the solution matrix is changed to 72 * d e f 73 * | 0 | 1 | 2 | 3 | 74 * a | 1 | x | 75 * B | 2 | x | x | 76 * c | 3 | x | 77 * fill in the remaining Table 78 */79 for (I = 1; I <m + 1; I ++) {// I = 1, j = 1, 2, 3, fill in 80 s1i = s1.charAt (I-1) Starting with rows ); 81 for (j = 1; j <n + 1; j ++) {82 s2j = s2.charAt (j-1); 83 int flag = (s1i = s2j )? 0: 1; // according to the formula, if s1 [I] = s2 [j], then d [I, j] = d [I-1, J-1], if s1 [I] ≠ s2 [j], one of the formulas is d [I, j] = d [I-1, j-1] + 1 84 solutionMatrix [I] [j] = min (solutionMatrix [I] [J-1] + 1, solutionMatrix [I-1] [j] + 1, solutionMatrix [I-1] [J-1] + flag); 85} 86} 87 return solutionMatrix [m] [n]; 88} 89 90/** 91 * according to the formula, the editing distance is 92 * @ param insert s1 insert operation 93 * @ param delete s1 delete operation 94 * @ param edit s1 modify operation 95 * @ return: The editing distance is 96 */97 private sta. Tic int min (int insert, int delete, int edit) {98 int tmp = insert <delete? Insert: delete; 99 return tmp <edit? Tmp: edit; 100} 101}

Python3

1 ''' 2 dynamic planning -- string editing distance 3 s1 = "abc", s2 = "def" 4 formula: 5 | 0 I = 0, j = 0 6 | j I = 0, j> 0 7 d [I, j] = | I> 0, j = 0 8 | min (d [I, j-1] + 1, d [I-1, j] + 1, d [I-1, J-1]) s1 (I) = s2 (j) 9 | min (d [I, j-1] + 1, d [I-1, j] + 1, d [I-1, J-1] + 1) s1 (I) ≠ s2 (j) 10 define a two-dimensional array [4] [4]: 11 d e f d e f12 | x | 0 | 1 | 2 | 3 | 13 a | x | => a | 1 | 1 | 2 | 3 | => editing distance d = [4] [4] = 314 B | x | B | 2 | 2 | 3 | 15 c | x | c | 3 | 3 | 3 | 16 '''17 def levenshtein (s1, s2 ): 18 I = 0 # character subscript 19 j = 0 # character subscript 20 s1i = "" # s1 string I character 21 s2j = "" # s2 string I character 21 s2j = "# s2 string j character 22 m = len (s1) # s1 String Length 23 n = len (s2) # s2 String Length 24 if m = 0: 25 return n # s1 String Length 0, the editing distance is the s2 String Length 26 if n = 0: 27 return m # s2 String Length 0, the editing distance is the s1 String Length 28 solutionMatrix = [[0 for col in range (n + 1)] for row in range (m + 1)] # The length is m + 1, 29 ''' 30 d e f31 | 0 | x | 32 a | 1 | x | 33 B | 2 | x | x | 34 c | 3 | x | 35 ''' 36 for I in range (m + 1): 37 solutionMatrix [I] [0] = i38 ''' 39 d e f40 | 0 | 1 | 2 | 3 | 41 a | x | 42 B | x | 43 c | x | 44 45''' 46 for j in range (n + 1): 47 solutionMatrix [0] [j] = j48 ''' 49 after the preceding two operations, solution matrix changed to 50 d e f51 | 0 | 1 | 2 | 3 | 52 a | 1 | x | 53 B | 2 | x | 54 c | 3 | x | 55 then fill in the remaining Table 56 ''' 57 for x in range (1, m + 1): 58 s1i = s1 [x-1] 59 for y in range (1, n + 1 ): 60 s2j = s2 [y-1] 61 flag = 0 if s1i = s2j else 162 solutionMatrix [x] [y] = min (solutionMatrix [x] [Y-1] + 1, solutionMatrix [x-1] [y] + 1, solutionMatrix [x-1] [Y-1] + flag) 63 64 return solutionMatrix [m] [n] 65 66 def min (insert, delete, edit): 67 tmp = insert if insert <delete else delete68 return tmp if tmp <edit else edit69 70 s1 = "abc" 71 s2 = "def" 72 distance = levenshtein (s1, s2) 73 print (distance)

 

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.