Dynamic Programming to solve the coin seeking problem -- JAVA Implementation

Source: Internet
Author: User

Basic Idea of Dynamic PlanningIs to break down the problem to be solved into several subproblems, first solve the subproblems, and save the solutions to these subproblems, if you need to solve these subproblems in the future, you can directly retrieve these computed solutions without repeated operations. You can use the table Filling Method to save the sub-problem, for example, saving it in an array.

The main difficulty of dynamic planning lies in the theoretical design, that is, the determination of the above four steps. Once the design is completed, the implementation part will be very simple. When using dynamic planning to solve a problem, the most important thing is to determine the three elements of dynamic planning: the stage of the problem, the status of each stage, and the recurrence relationship between the previous stage and the next stage. The recursive relationship must be a conversion from a small problem to a large problem. From this perspective, dynamic planning can often be implemented using recursive Programs, however, because recursion can take full advantage of the solutions of the previously stored sub-problems to reduce repeated computing, there is an incomparable advantage of recursion for large-scale problems, which is also the core of Dynamic Planning Algorithms. After determining the three elements of dynamic planning, you can useOptimal Decision TableThe optimal decision table is a two-dimensional table with rows representing the decision stage and columns representing the problem status, the data to be filled in the table usually corresponds to the optimal value (such as the shortest path, Longest Common subsequence, and maximum value) of the problem in a certain stage ), the table filling process starts from one row and one column according to the recursive relationship, and forms are filled in order of row or column priority, finally, the optimal solution is obtained through simple trade-offs or operations based on the data in the entire table.

The following uses an actual example to demonstrate the idea of Dynamic Planning Algorithms-coin seeking.

 

Coin change Problem description: There are a pile of faces with V1, V2, V3... Number of coins per unit. Ask how many coins are needed to find the total value
T unit change? Assume that the heap face values are 1, 2, 5, 21, and 25 yuan respectively, you need to find the change with the total T value of 63 yuan.

 

Obviously, as long as three 21 Yuan coins are taken out, 63 yuan is enough.

 

Based on the above dynamic planning idea, we can calculate the minimum number of coins from 1 yuan, and then ask for 2 yuan, 3 yuan... Each Obtained result is saved in an array. You can retrieve the result directly when it is used later. So when do we need to solve these subproblems? How can we solve a major problem by solving subproblems?

 

In fact, when we start from 1 yuan to find the zero value in sequence, we can try to find the current zero-value (here 1 yuan) whether the number of coins needed to be split into another resolved face value, plus the sum of a face value in the heap of coins, if the number of coins finally decomposed is the least, then the answer is displayed.

 

The preceding text description is too abstract. Assume the following variables first:

 

Values []: an array of currency values for each coin

Valuekinds: Number of coin types with different currency values, that is, the size of the Values [] Array

Money: the nominal value to be zero

Coinsused []: Minimum number of coins required for saving the I-oriented banknotes

 

Algorithm Description:

 

When we solve the coinsused [I] coin with a minimum value of I, we break it into a coinsused [I-cents] coin with a value of cents, because I-cents <I, its solution coinsused [I-cents] already exists. If the coin with a nominal value of cents meets the meaning of the question, then the Final Solution coinsused [I] is equal to the coinsused [I-cents] plus the 1 (that is, the nominal value is cents) coin.


Algorithm Implementation:

Package COM. dynamic;/*** @ author sjmei * @ date 2012-10-30 */public class coinschange {/***** coin change: Dynamic Planning Algorithm * @ Param values: array for saving the currency value of each coin ** @ Param valuekinds: number of coins with different currency values, that is, the size of the coinvalue [] array ** @ Param money: face value to be changed ** @ Param coinsused: Minimum number of coins required to save the banknotes whose face value is I */public static void makechange (INT [] values, int valuekinds, int money, int [] coinsused, int [] cointrack) {coinsused [0] = 0; int last = 0; // you can change the value of zero for every penny, that is, save the sub-problem solution for backup, enter the table for (INT cents = 1; cents <= money; cents ++) {// The maximum number of coins required for int mincoins = 999 when the coin with the minimum value is found to be zero; // traverse each coin with a nominal value to see if it can be used as one of the zeros for (INT kind = 0; kind <valuekinds; kind ++) {// If the coin with the current face value is smaller than the current cents, the problem is decomposed and the table is queried. If (Values [kind] <= cents) {int temp = coinsused [cents-Values [kind] + 1; if (temp <mincoins) {mincoins = temp; last = kind ;}}} // minimum number of coins saved: coinsused [cents] = mincoins; cointrack [cents] = values [last]; system. out. print ("minimum coin count:" + (cents) + "+ coinsused [cents]); system. out. print ("coin:"); trackprint (cents, cointrack); system. out. println () ;}} Private Static void trackprint (int m, int [] cointrack) {If (M = 0) {return;} else {system. out. print (cointrack [m] + ""); trackprint (m-cointrack [m], cointrack) ;}} public static void main (string [] ARGs) {// coin face values are pre-sorted in descending order int [] coinvalue = new int [] {25, 21, 10, 5, 1 }; // int money = 65; // Save the minimum number of coins required for each change. Units 0 are not needed, therefore, we need to add 1int [] coinsused = new int [money + 1]; int [] cointrack = new int [money + 1]; for (INT I = 1; I <= money; I ++) {coinsused [I] = 0; cointrack [I] = 0;} makechange (coinvalue, coinvalue. length, money, coinsused, cointrack );}}

Program running result:

Minimum number of coins with a nominal value: 1: Minimum number of coins with a nominal value of 2: Minimum number of coins with a 2: 1: Minimum number of coins with a nominal value of 3: 3 Coins: 1 1 1 par value: 4 min number of coins: 4 coins: 1 1 1 1 par value: 5 min number of coins: 1 coin: 5. Minimum number of coins with a nominal value of 6: 2 Coins: 5. Minimum number of coins with a nominal value of 7: 3 Coins: 5. 1. The nominal value is: 8 min coins: 4 coins: 5 1 1 1 min coins: 9 coins: 5 coins: 5 1 1 1 1 1 1 par: 10 min coins: 1 coin: 10 min coins: 11 min coins: 2 Coins: 10 1 par: 12 min coins: 3 Coins: 10 1 1 minimum number of coins with a nominal value of 13: 4 coins: 10 1 1 minimum number of coins with a nominal value of 14: 5 coins: 10 1 1 1 1 nominal value: 15 min coins: 2 Coins: 10 5 nominal value: 16 min coins: 3 Coins: 10 5 1 nominal value: 17 min coin count: 4 coin: 10 5 1 1 min coin count: 18 min coin count: 5 coin: 10 5 1 1 1 1 par value: 19 min coins: 6 coins: 10 5 1 1 1 1 par value: 20 min coins: 2 Coins: 10 10 10 par value: 21 min coins: 1 coin: 21; nominal value: 22; min coin count: 2; coin count: 21; 1; nominal value: 23; min coin count: 3; coin count: 21; 1; nominal value: min number of coins in 24: 4 coins: 21 1 1 min number of coins with a nominal value of 25: 1 coin with a nominal value of 25: min number of coins with a nominal value of 26: 2 Coins: 25 1 nominal value: 27 min number of coins: 3 Coins: 25 1 nominal value: 28 min number of coins: 4 coins: 25 1 1 1 nominal value: 29 min coins: 5 coins: 25 1 1 1 1 nominal value: 30 min coins: 2 Coins: 25 5 nominal value: minimum coin count of 31: 2 coin: 21 10 par value: 32 min coin count: 3 coin: 21 10 1 par value: 33 min coin count: 4 coins: 21 10 1 1 minimum number of coins with a nominal value of 34: 5 coins: 21 10 1 1 1 minimum number of coins with a nominal value of 35: 2 Coins: 25 10 nominal value: 36 min coin count: 3 coin: 25 10 1 nominal value: 37 min coin count: 4 coin: 25 10 1 nominal value: 38 min coins: 5 coins: 25 10 1 1 1 min coins: 39 coins: 6 coins: 25 10 1 1 1 1 1 1 1 nominal value: minimum coin count of 40: 3 coin: 25 10 5 min coin count of 41: 3 coin: 21 10 10 min coin count of 42: 2 Coins: 21 21; nominal value: 43; min number of coins: 3 Coins: 21; 21; 1; nominal value: 44; min number of coins: 4 coins: 21 21 1 1 nominal value: 45 min coins: 3 Coins: 25 10 10 nominal value: 46 min coins: 2 Coins: 25 21 nominal value: minimum number of coins for 47: 3 Coins: 25 21 1 minimum number of coins with a nominal value of 48: 4 coins: 25 21 1 minimum number of coins with a nominal value of 49: 5 coins: 25 21 1 1 1 min number of coins with a nominal value of 50: 2 Coins: 25 25 min number of coins with a nominal value of 51: 3 Coins: 25 25 1 nominal value: 52 min coins: 3 Coins: 21 21 10 nominal value: 53 min coins: 4 coins: 21 21 10 1 nominal value: min number of coins in 54: 5 coins: 21 21 10 1 min number of coins in nominal value: 55: 3 Coins: 25 25 5 min number of coins in nominal value: 56: 3 coin: 25 21 10 min number of coins with a nominal value of 57: 4 coin: 25 21 10 1 min number of coins with a nominal value of 58: 5 coin: 25 21 10 1 1 nominal value: 59 min coins: 6 coins: 25 21 10 1 1 1 nominal value: 60 min coins: 3 Coins: 25 25 10 nominal value: 61 min coins: 4 coins: 25 25 10 1 nominal value: 62 min coins: 4 coins: 21 21 10 10 nominal value: 63 min coins: 3 Coins: 21 21 21 min coins: 64 min coins: 4 coins: 21 21 21 1 min coins: 65 min coins: 4 coins: 25 25 10 5

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.