# Dynamic programming method of common algorithm

Source: Internet
Author: User

Previous Blog We talked about the division of the law, followed by the dynamic planning method: Dynamic Programming and division of the same method, it is to solve the problem of decomposition into sub-problem, the optimal solution , the difference is that if the decomposition of the sub-problem has a lot of the same, using the same sub-treatment method of the same problem will be solved many times , it affects the efficiency; dynamic programming, it will save the answers to the sub-problems solved , and then have the same sub-problem directly with the saved answer on the line, saving a lot of computational time.

As a picture shows:

Cases:

Solution: We ask first F (5) the solution, as below, is represented by the structure of a two-fork tree

through the binary tree, we notice that F (n) is through computes its two overlapping sub-problems F (n-1) and F (n-2) in the form of a representation, so that a table can be designed to fill in n+1 f (n) values. The following table finds: The last number equals the sum of the preceding two digits. (This is the famous Fibonacci number)

Therefore, using the dynamic programming method, the properties of a problem can be summarized as: Optimal substructure, overlapping sub-problem

Applicable situation:

(1) Optimization principle: If the optimal solution of the problem contains sub-problem solution is also optimal, it is said that the problem has the optimal sub-structure, that is, to meet the optimization principle.

(2) No effect: that is, once a stage state is determined, it is not affected by the decision after this state. In other words, the subsequent process of a State does not affect the previous state, only the current state.

(3) There are overlapping sub-problems: That is, sub-problems are not independent, a sub-problem in the next stage of decision-making may be used more than once. (This nature is not a necessary condition for dynamic programming, but without this nature, the dynamic programming algorithm has no advantage over other algorithms)

application Examples:

`<span style= "FONT-SIZE:14PX;"            > public class Coinschange {/** * coin change: Dynamic Programming Algorithm * * @param values * : An array that holds the value of each type of coin * @param valuekinds *: Number of coins of different currencies, i.e. coinvalue[] array size * @           Param Money *: The face value of the change required * @param coinsused *: The minimum number of coins required to save the change of banknotes with a face value of I                    */public static void Makechange (int[] values, int valuekinds, int. money, int[] coinsused) {              Coinsused = 0; For every penny of the change, that is, the solution to save the sub-problem to spare, that is, fill in for (int cents = 1; cents <=; cents++) {//When using the minimum currency value                       Coins to find the maximum number of coins required int mincoins = cents;                                   Traverse each denomination of the coin to see if it can be used as one of the change for (int kind = 0; kind < valuekinds; kind++) {                   If the current value of the coin is less than the current cents then the problem is decomposed and the table is checked if (Values[kind] <= cents) {       int temp = Coinsused[cents-values[kind]] + 1;                          if (Temp < mincoins) {mincoins = temp;                       }}}//save minimum number of coins coinsused[cents] = mincoins;              System.out.println ("+ (cents) +" minimum number of Coins: "+ coinsused[cents]);              }} public static void Main (string[] args) {//Coin value pre-sorted in descending order              int[] Coinvalue = new int[] {25, 21, 10, 5, 1};              The face value of the change required int money = 63;                   Save the minimum number of coins required to change each face value, Unit No. 0 is discarded, so add 1 int[] coinsused = new Int[money + 1];          Makechange (Coinvalue, Coinvalue.length, Money, coinsused); }} </span>`

Dynamic programming method of common algorithm

Related Keywords:

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.