Classical Algorithm Research Series: 3. dynamic planning algorithm solving Microsoft interview questions [56th]

Source: Internet
Author: User

Dynamic Planning Algorithm

 

Author July December 31, 2010

This article is for reference: Microsoft interview 100 series V0.1 19th, 56, Introduction to algorithms, Wikipedia.

 

OK. Let's first understand what a dynamic planning algorithm is.

Dynamic Planning can only be applied to the problem of optimal sub-structure. The optimal sub-structure means that the local optimal solution can determine the global optimal solution.

(For some problems, this requirement cannot be fully met, so some approximation is sometimes required ).

Simply put, the problem can be resolved into sub-problems.

 

The dynamic planning algorithm consists of the following four steps:

1. describe the structure of the Optimal Solution

2. recursively define the value of the Optimal Solution

3. Calculate the value of the optimal solution based on the bottom-up method. // The three steps constitute the foundation of the dynamic planning solution.

4. Construct an optimal solution based on the calculated results. // Skip this step if you only need to calculate the value of the optimal solution.

 

Okay. Next, let's discuss the two elements that best suit the dynamic planning method:

Optimal substructure, overlapping with subproblems.

 

1. Optimal sub-structure.

If the solution of the subproblem included in the optimal solution is also optimal, we call it an optimal sub-structure (that is, satisfying the optimization principle ).

That is to say, there are many issues, and the solutions to these subproblems are optimal.

Ii. Overlapping subproblems.

Subproblem overlap refers to the subproblem that is not always a new problem when a top-down problem is solved using a recursive algorithm,

Some sub-problems are repeatedly calculated. The dynamic planning algorithm uses the overlapping nature of this seed issue to calculate each subproblem only once,

Then, save the calculation results in a table. When you need to calculate the subproblems that have been computed again, simply view the results in the table,

To achieve high efficiency.

 

OK. Let's immediately go to the FAQ 56th, that is, using the classic Dynamic Planning Algorithm:

56. Longest Common subsequence.
Question: If all the characters in string 1 appear in the order of the strings in another string,
Then, string 1 is called a substring of string 2.

Note that the character of a substring (string 1) must appear in string 2 consecutively.
Compile a function, enter two strings, calculate their longest public substrings, and print the longest public substrings.
For example, input two strings: BDCABA and ABCBDAB. Both BCBA and BDAB are their longest common substrings,
The output length is 4 and any substring is printed.

 

Analysis: finding the Longest Common Subsequence (LCS) is a very classic dynamic planning question,

Therefore, some companies that place importance on algorithms, such as MicroStrategy, regard it as an interview question.

 

Step 1. Describe a Longest Common subsequence

First introduce the nature of LCS problems: Note Xm = {x0, x1 ,... Xm-1} and Yn = {y0, y1 ,..., Yn-1} is two strings,

Set Zk = {z0, z1 ,... Zk-1} is any LCS of X and Y, three properties can be obtained:

1. If the xm-1 = yn-1, then the zk-1 = xm-1 = yn-1, And the Zk-1 is an lcs of the Xm-1 and Yn-1;
2. If the xm-1 is not yn-1, then when the zk-1 is not xm-1, Z is Xm-1 and y lcs;
3. If xm-1 is less than yn-1, then when zk-1 is less than yn-1, Z is the LCS of X and Yn-1;

 

Below is a simple proof of these properties derived from the above conditions:
1. if the zk-1 is not xm-1, then we can add the xm-1 (yn-1) to Z to get Z', so that we can get a length of X and Y is k + 1 of the Public substring Z '.

This is in conflict with Z whose length is k and LCS of X and Y. So there must be zk-1 = xm-1 = yn-1.
Since zk-1 = xm-1 = yn-1, if we delete the zk-1 (xm-1, yn-1) to get the Zk-1, Xm-1 and Yn-1, apparently the Zk-1 is a public substring of the Xm-1 and Yn-1, now we prove that the Zk-1 is Xm-1 and the Yn-1 of LCS. It is not difficult to prove it by using the reverse verification method. Suppose there is a Xm-1 and a Yn-1 with a public substring W longer than the K-1, then we add it to W to get W', then W' is the public substring of X and Y, and the length exceeds k, which is in conflict with known conditions.
2. Verify it by Reverse verification. If Z is not the Xm-1 and Y of LCS, there is a length more Than k W is the Xm-1 and Y of LCS, then W must also X and y of the public substring, in the known conditions, the maximum length of the Public substrings X and Y is k. Conflict.
3. The proof is the same as 2.

 

Step 2. a recursive Solution

Based on the above nature, we can come up with the following ideas:

Evaluate the two strings Xm = {x0, x1 ,... Xm-1} and Yn = {y0, y1 ,..., Yn-1} LCS,

If the xm-1 = yn-1, then just get the Xm-1 and the Yn-1 of LCS, and add the xm-1 (yn-1) after it (the above properties 1 );

If the xm-1 is not yn-1, we obtain the LCS of Xm-1 and Y and the LCS of Yn-1 and X respectively, in addition, the long LCS of the two LCS is X and Y (the above properties are 2 and 3 ).

 

According to the above conclusions, the following formula can be obtained,

If we remember that the length of the LCS of string Xi and Yj is c [I, j], we can recursively calculate c [I, j]:

/0 if I <0 or j <0
C [I, j] = c [I-1, J-1] + 1 if I, j> = 0 and xi = xj
Max (c [I, J-1], c [I-1, j] if I, j> = 0 and xi = xj

 

The above formula is not difficult to obtain using recursive functions. Naturally, we can see from the solution to the n-th question (question 100 of the 19th question series, such as Microsoft, V0.1) of the Fibonacci,

Direct recursion involves a lot of repeated computations. Therefore, it is more efficient to use the bottom-up and upward-loop solution.

 

In order to be able to use the idea of loop solution, we use a matrix (refer to LCS_length in the code at the end of the following section) to save the computed c [I, j],

When the subsequent computation requires the data, the data can be directly read from the matrix.

 

In addition, c [I, j] can be calculated from c [I-1, J-1], c [I, J-1] or c [I-1, j,

It is equivalent to moving one of the two in the matrix LCS_length from c [I-1, J-1], c [I, J-1] or c [I-1, j] to c [I, j],

Therefore, there are three different moving directions in the matrix: left, up, and top left. Only moving to the top left indicates that one character in LCS is found.

So we need to use another matrix (refer to LCS_direction in the code at the end of the following) to save the moving direction.

 

Step 3: Calculate the LCS Length

LCS-LENGTH (X, Y)
1 m bytes length [X]
2 n bytes length [Y]
3 for I between 1 to m
4 do c [I, 0] defaults 0
5 for j defaults 0 to n
6 do c [0, j] defaults 0
7 for I between 1 to m
8 do for j between 1 to n
9 do if xi = yj
10 then c [I, j] ← c [I-1, j-1] + 1
11 B [I, j] Random ""
12 else if c [I-1, j] ≥c [I, j-1]
13 then c [I, j] Objective c [I-1, j]
14 B [I, j] Province "Province"
15 else c [I, j] Objective c [I, j-1]
16 B [I, j] regression Regression
17 return c and B

The LCS-LENGTH in this process is input in two sequences X = <x1, x2,..., xm> and Y = <y1, y2,..., yn>.

It fills in the value of c [I, j] in a table c [0 m, 0 m n] That calculates table items by row. It also maintains B [1 m, 1 limit n] to simplify the structure of the optimal solution.

Intuitively, B [I, j] points to a table item, which corresponds, j] the solutions for the selected optimal subproblem are the same.

This program returns the length of an LCS in the table B and c, c [m, n] that contains X and Y.

 

Step 4: Construct an LCS,

PRINT-LCS (B, X, I, j)
1 if I = 0 or j = 0
2 then return
3 if B [I, j] = ""
4 then PRINT-LCS (B, X, I-1, j-1)
5 print xi
6 elseif B [I, j] = "Hangzhou"
7 then PRINT-LCS (B, X, I-1, j)
8 else PRINT-LCS (B, X, I, j-

Related Article

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.