Using Induction to design use inductive design algorithm [11/14]

Source: Internet
Author: User

Link: http://blog.csdn.net/jj12345jj198999/article/details/6624864

 

The basic situation is very simple. If there is only one item of K size, there is a solution. If there is a solution to P (n-1, k), that is to say, if there is a way to put n-1 items into a backpack, we can do it. We only need to simply not use the nth item. However, if there is no solution to P (n-1, k), can we use this negative conclusion? The answer is yes, which means that the nth item must be included. In this case, the remaining items must be packed into a smaller backpack with a capacity of K-kn. We simplify the problem to two smaller problems: If P (n, k) has a solution, P (n-1, k) or P (n-1, K-kn) there must be a solution between the two. To complete this algorithm, we need to solve not only the problem of the backpack with a capacity of K, but also the problem of all the backpacks with a maximum capacity of K. (The size of the backpack is limited to some size after the Ki is subtracted, but this restriction may not exist)

Stronger induction hypothesis: we already know how to solve P (n-1, k) for all 0 ≤ k ).

The preceding simplification does not depend on a specific K value. It is effective for all K values. We can use this inductive hypothesis to solve all the P (n, k) Problems of 0 ≤ k. We simplify P (n, k) to P (n-1, k) and P (n-1, K-kn. (If K-kn <0, we will ignore the second problem.) both problems can be solved by induction. This is an effective simplification. We can also find an algorithm, but the efficiency of this algorithm is not high. We get only two subproblems with a smaller size. Therefore, the number of simplified neutron problems in each step of scale also doubles. Because the simplification of the scale may not be so precise, the algorithm may grow exponentially. (Determined based on the value of KI)

Fortunately, in many cases, it is feasible to improve the running efficiency of such problems. The main observation is that the total number of possible problems is not large. In fact, we introduce the P (n, k) Mark to indicate this problem. There are n possibilities for the first parameter and K possibilities for the second parameter. In general, there are only N * k different possibilities. After each simplification, doubling the number of problems will lead to exponential running time. However, if there are only N * k different problems, we must have solved the same problem multiple times. If we write down all the answers, we do not need to solve the same problem twice. This is actually a combination of enhanced induction assumptions and stronger induction (using assumptions for all known smaller-scale questions, not just answers to n-1 questions. The following describes how to implement this method.

We store all known results in a matrix of n × K. The IJ position in the matrix contains the answer information of P (I, j. After using the above stronger induction hypothesis, the problem is simplified to basically the nth row value of the matrix. Each entry in row N is calculated based on the two entries on it. If we are interested in finding the actual set, we can add a tag field to each position, which can indicate whether the corresponding item is selected in that step. The marked field can be pushed from the nth position (n, k), and the set can be restored. This algorithm column is at the top of the next column.

Knapsack Algorithm {K1, k2... kN, K: integer };
{If there is a solution for the first I element and the backpack with the size of J, then P [I, j]. exist = true. If the I-th element is included in the solution, P [I, j]. belong = true}
Begin
P [0, 00]. exist: = true;
For J: = 1 to n do
P [0, J]. exist: = false;
For I: = 1 to n do
For J: = 0 to k do
P [I, j]. exist: = false; {default value}
If P [I-1, J]. exist then
P [I, j]. exist: = true;
P [I, j]. belong: = false;
Else if J-ki ≥ 0 then
P [I-1, J-Ki]. exist then
P [I, j]. exist: = true;
P [I, j]. belong: = true;
End;

Complexity: The matrix contains n × K entries. It takes constant time to calculate an entry from two different entries. Therefore, the total running time is O (NK ). If the size of an item is not very large, K cannot be too large and NK is much smaller than the index of N. (If K is very large or a real number, this method is not efficient.) If we only care about whether there is a solution, then the answer is in P [N, K. If we are interested in finding the actual set, we can reverse push from the (n, k) entries, for example, using the belong flag in the backpack program, at O (N) restore the set within the specified time.

Conclusion: We just used a specific example of a common technical method called dynamic planning. Its essence is to create a huge table, enter the known answers in the table. This table is constructed using iterative methods. Each entry in the matrix is calculated from other entries on top or left. The main problem is to use the most effective method to construct a table. Dynamic Planning is only effective when the problem can be simplified to a few subproblems that are not small enough.

Largest counterexample [Title 2]

A special and powerful technique in proving a mathematical theorem is to assume that the opposite side of the theorem is true and then find a contradiction. This is usually implemented in an unconstructive way, which is not very useful for our analogy. Although sometimes a conflict can be found through a method similar to induction. Suppose we want to prove that a definite Variable P (in a given problem) can obtain a definite value n. The first step is to give p a small value (basic ). In the second step, assume that P cannot get n, and we can consider the maximum value K that it can get <n. The last and most important step is to give a contradiction, usually aiming at the maximization hypothesis. An example of algorithm design is provided. In this example, this technology is very useful.

 

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.