Algorithm diagram--the Nineth chapter of dynamic programming

Source: Internet
Author: User

The Nineth Chapter Dynamic Planning

1 Dynamic planning-knapsack problem

Formula:

Practice
9.1 Suppose you can also steal another item--mp3 player, which weighs 1 pounds and is worth $1000. You want to steal it?

To. In this case, you can steal the MP3 player and iphone and guitar with a total value of $4500

What happens when a row's order of arrangement changes? The answer has not changed. In other words, the order of the rows is irrelevant.

Can I fill the grid line by row instead of by column? This has no effect on this issue, but it may have implications for other issues.

How about adding a smaller piece of merchandise? You need to consider finer granularity, so you have to adjust the grid.

Can you steal part of the product? The answer is impossible to deal with. When using dynamic planning, consider taking away the entire product, or consider not taking it, without judging the part of the product that should not be taken away. But greedy algorithms can be easily handled!

2 Maximum Travel itinerary

Dynamic planning grid According to the list plot:

How do you deal with interdependent situations?

Dynamic planning is powerful, it solves sub-problems and uses these answers to solve big problems. However, dynamic planning works only when each sub-problem is discrete, that is, independent of other sub-problems.

Does it involve more than two sub-backpacks when calculating the final solution?

To obtain the optimal solution of the knapsack problem, it may be necessary to steal more than two items. However, according to the design of the dynamic programming algorithm, a maximum of two sub-backpacks can be combined, that is, no more than two sub-backpacks are involved. But these backpacks may also contain sub-backpacks.

Can the optimal solution cause the backpack not to fill? Of course

Practice
9.2 Suppose you're going camping. You have a backpack with a capacity of 6 lbs and need to decide what to carry underneath. Each of these things has a corresponding value, the greater the value means the more important:

Water (weighs 3 lbs, value 10);

Book (weighs 1 lbs, value 3)

Food (weighs 2 lbs, value 9);

Jacket (weighs 2 lbs, value 5);

Camera (weighs 1 lbs, value 6).

What is the most valuable thing to bring with me? Use dynamic planning to do chant, water, food, camera

3 Longest common substring

The revelation of dynamic programming:

① Dynamic Planning can help you find the optimal solution under a given constraint condition.

② can be solved by using dynamic programming when the problem can be decomposed into separate and discrete sub-problems.

③ Each dynamic planning solution involves a grid.

The value in cell ④ is usually the value you want to optimize.

⑤ Each cell is a sub-problem, so you should consider how to divide the problem into sub-problems, which will help you find the axis of the grid.

Give me a name. : Alex entered Hish, so did he have to type fish or vista?

3.1 Drawing grids

The grid to solve the problem above should be structured to consider the following points:

What is the value in cell ①? In this example, you need to find the longest common substring of two words, which is the value you want to calculate.

② How do you divide this problem into sub-problems? You may need to compare substrings: not compare hish and fish, but compare his and FIS first.

What is the axis of the ③ grid? Each cell will contain the length of the longest common substring of the two substrings. This also gives you clues to make you feel that the axes are likely to be these two words.

Therefore, the grid may resemble the following:

3.2 Fill Grid

What formula is used when filling? the Feynman algorithm (Feynman algorithm) steps are as follows: (This is afraid not to say nonsense = =!) Waste slow algorithm? )

①, write down the problem.

②, think hard.

③ write down the answers.

3.3 Announce the answer

When looking for the longest common substring of the word hish and Vista, the grid is as follows:

For knapsack problems, the final answer is always in the last cell, but for the longest common substring, the answer is the largest number in the grid-not necessarily in the last cell.

So the answer to the question comes out, the longest common substring of hish and fish contains three letters, while Hish and Vista's longest common substring contains two letters. So Alex is probably going to be importing fish.

3.4 Longest common sub-sequence

Suppose Alex accidentally entered Fosh, did he want to enter fish or fort? We use the longest common substring formula to compare them:

So all are 2 and the same length, but fosh and fish are more like.

the longest common subsequence should be compared here: the number of letters contained in the sequence of two words .

Solutions for the longest common subsequence of 3.5

The formula:

Practical applications of dynamic planning:

Biologists determine the similarity of the DNA chain according to the longest common sequence, and then determine how similar the two animals or diseases are. The longest common sequence is also used to look for multiple sclerosis treatment options.

Have you ever used commands such as Git diff? They point out the differences between the two files and are implemented using dynamic planning.

The similarity of the strings is discussed earlier in . The editing distance (Levenshtein distance) indicates how similar the two strings are, and is calculated using dynamic programming. The editing distance algorithm has many uses, from the spelling checker to judging whether the user uploads the information is pirated, in which.

Have you ever used an application that has hyphenation features such as Microsoft Word? How do they determine where to hyphenate to make sure the president is consistent? Use dynamic planning!

Practice
9.3 Draw and populate the grid used to calculate the longest common substrings of blue and clues.

4 Summary

Dynamic planning is useful when ① needs to optimize a certain indicator under a given constraint condition.

When ② problems can be decomposed into discrete sub-problems, dynamic planning can be used to solve them.

③ Each dynamic planning solution involves a grid.

The value in cell ④ is usually the value you want to optimize.

⑤ Each cell is a sub-problem, so you need to consider how to decompose the problem into sub-problems.

⑥ does not have a universally-accurate formula for calculating dynamic programming solutions.

Algorithm diagram--the Nineth chapter of dynamic programming

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.