Dynamic planning_longest common subsequence LCs

Source: Internet
Author: User

I have been learning dynamic planning for a while. I feel like I can understand the problem, but I am sweating when I get a new question! I know that this is actually not a deep understanding. Dynamic Planning is a solution to a type of problem, rather than a solution to a problem. Today, I try to feel how to think about the dynamic programming solution of LCS.

Question: Given two sequences x = <x1, x2, x3 ......, XM> and Y = <Y1, Y2, y3 ......., YN>. You want to find out the longest common sequence and LCs of X and Y ).

SoWhat is the first problem?Analyze how X and Y generate LCS, and then use this relationship to directly write the dynamic planning transfer equation, as the idea given in the introduction to algorithms? No. At least I am not so smart. My problem is:What is the solution?Or in myAre there any ideas for solving similar problems in the brain database?, Or more directly,Why is it associated with dynamic planning??

My answer to this question is a bit clever. First of all, you must understand that this problem has been solved. Generally, you will not encounter a godebach conjecture without your knowledge. Therefore, you must first increase your confidence in solving this problem. Then, we made full use of the exam-oriented education that Chinese students have trained for decades,Question locating prompt! I remember that when I was in high school, it was very clear whether to use trigonometric function or analytic ry, because the question contains either a large number of sin, cos, or curve equations, and even most of the resolution geometric questions provide coordinate diagrams of the elliptic or hyperbolic shapes. These are question signals. Generally, the keyword "best", "maximum", "minimum", or "most economical" exists for questions that can be answered through dynamic planning. The maximum length of the LCS question is the prompt. Prompt me"Maybe you can try dynamic planning."Thinking is a process of trying to get at least one direction to walk down. Finally, it is a personal feeling that the problems that can be solved through dynamic planning usually make people feel that they have no clue in the face of a bunch of troubles,Making people unable to start. Why do we say this? Because dynamic planning usually faces a lot of problems, it is difficult to find a breakthrough immediately. Let you eat the whole watermelon without cutting it. Anyone will be thirsty and helpless (except for the eight rings ). In addition, dynamic planning usually requires reverse thinking. The existence of such anti-human thinking is a headache, and it will not cause people to think about problems in this way at the first time. Therefore, it is also a kind of reminder to be confused when you get some questions.

Since we decided to use dynamic planning, how can we proceed? NaturallyFinding State and State Transfer Equations. Now, we should keep an impulse in the subconscious: I want to be keenly aware of the relationship between a long LCs and its sub-LCs? How to describe the existence status of LCS? What we can think of now is that an LCS is composed of a certain length of X sequence and a certain length of Y sequence common elements, and the LCS sub-LCS (that is, sub-problems) is composed of a shorter x series and a public element of the Y series. So how are the two converted? The status transfer here is not very obvious, and it is easy to find some problems, such as the company gathering problem (Professor Stewart is a consultant to the president of a company, which plans a company gathering. The company has a hierarchical structure, that is, the management relationship forms a tree rooted in the President. The Ministry of Human Resources ranks every employee as fond of gathering. This is a real number. The President does not want an employee and his/her immediate supervisor to participate in this gathering in order to make everyone enjoy it. Professor Stewart faces a tree that describes the company structure and uses the left child and right brother notation. In addition to pointers, each node in the tree also contains the employee's name and ranking of the employee's favorite parties. Describes an algorithm that generates a list of guests, maximizing the total number of guests who like the party.) in this question, whether or not a person participates in a gathering directly affects the elements of the upper and lower levels, therefore, when considering the problem, it is natural to start from the President, from top to bottom, and the choice is obvious, that is, whether the person participates in the two situations, the two situations are the best. When I started to think about LCS, I found that I did not follow the idea of introducing algorithms. I chose a self-destruction approach, let's talk about how I got into a dead end.

I have found an LCS sequence Z (t) from x (1 to I) and Y (1 to J ). The next step is how to find the longer LCS sequence Z (t + 1) in X (k> I) and Y (k> J ). If X (I + 1) = y (J + 1), then LCS (t + 1) = LCS (t) + 1 and increase I to I + 1, J is added to J + 1 and transferred successfully. But if x (I + 1 )! = Y (J + 1), then we need to push an element behind the X [or Y] sequence to search for X (I + 1) and Y (j)
[Or the LCS of X (I) and Y (J + 1)], the key problem arises. How to Write this recursive equation! The current situation is like the tree chart below. I want to push and export the relationship between A and B, but I find that to export a, c, d, e must be known, in this way of thinking, I cannot use mathematical formulas to represent C, D, and E, so I will fail here.

However, there is no harm in thinking this way, because the analyzed Conversion Relationship is actually very similar to the correct Dynamic Planning Idea. Before trying to think correctly, it is worth reflecting on why you fall into the maze of thinking without any suspense. The most important thing is that this is a habitual way of thinking, and it is difficult to use dynamic planning to solve problems. A key aspect of dynamic planning is,Dynamic Planning is a top-down ProblemNote that I am talking about the way to think about the problem, not solving the problem. Dynamic Planning uses the optimal sub-structure to solve the problem from the bottom up. That is to say, focusing on big problems, thinking about how to break down into small problems, and finding out how big problems are merged from small problems, the academic point is the idea of divide governance, and the popular point is the reverse thinking.

When learning dynamic planning, I emphasized that the problem should be resolved over a thousand times. But when solving the problem, I immediately forgot to think about it. In fact, my thinking is also inert, thinking will become lazy and watch yourself make mistakes with evil spirits.

Return to the question and try reverse thinking. There is a hypothesis before thinking, firstAssume that the optimal solution in a certain state has been found.,You don't have to worry about how this optimal solution comes from.. The previous failed thinking is actually the process of seeking the optimal solution. Think about it carefully. If we want to look for two sequences of LCS, we will also follow that idea.

Assume that the length of the longest common subsequence Z (1 to K) of x (1 to I) and Y (1 to J) is L (I, j ), so how can we resolve this big problem into a small one? It is necessary to analyze how the public subsequences are generated. Note,Reverse Thinking. View the obtained optimal subsequence,

If X (I) = y (j) = z (K), it means that X (I) and Y (j) are just public sequences Z (1 to K) the last element, then z (k-1) must exist in X (1 to i-1) and Y (1 to J-1), that is, L (I-1, J-1) and L (I, j) difference 1:L (I, j) = L (I-1, J-1) + 1;

If X (I )! = Y (j) indicates that Z (k) exists in X (M <= I) and Y (n <= J ).L (I, j) = max {L (m, n), m <= I, n <= J and M, N are not equal },Note that L (x, n)> = L (k <X, n) must be true, because the LCS of the X sequence and Y sequence must be longer than the LCS of the X subsequence and Y sequence, for example, LCS ()> LCS () must be true. Similarly, L (n, y)> = L (n, k <Y) must be true, So rewrite the above formulaL (I, j) = max {
L (I-1, J), L (I, J-1 )}

Now we get the recursive equation of the LCS problem. Now we can say that this idea has been successfully tried, but there is still a last bit of work and boundary condition for solving the problem. The Boundary Condition of LCS is relatively simple: L (0, 0) = 0.

The LCS solution is obtained by combining the boundary condition with the two Recursive formulas above.

The above are purely my personal ideas. If you have any mistakes, please leave it blank.

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.