Understanding the difference between Dynamic Planning Algorithms and greedy algorithms-money-seeking

Source: Internet
Author: User

I. Description

 

Having struggled with the similarities and differences between dynamic programming and greedy algorithms for a long time, I simply looked at the description in the theoretical text and did not fully understand what the difference was. The most cited

The difference between the two is to find money. To solve this issue, we can understand the differences between dynamic programming of French greedy algorithms.

 

 

Ii. Problems

Currently, only three types of RMB are available: 11 RMB, 5 RMB, and 1 RMB.

How can we use the RMB of the three denominations to find a RMB with the minimum amount of money?

For example, given 10 yuan, we can find the following method:

Two 5 yuan denominations

1 RMB 5 denomination + 5 RMB 1 denomination

10 RMB 1 denomination

Select the first method. Only two RMB is used.

 

 

Iii. Analysis

 

Dynamic programming can be used to find the optimal solution.

The greedy algorithm can be used to find the optimal solution (when the problem meets the greedy selective quality. This issue does not meet the requirements of three payment options: 11, 5, and 1)

Or find the approximate optimal solution (in the case of the three denominations set in this question)

 

If you want to open $15

1. According to the idea of solving the problem in the dynamic programming method, the optimal solution is 3 RMB 5, a total of 3

2. According to the greedy algorithm's problem-solving idea, the approximate optimal solution is one piece of 11 yuan denomination plus four yuan of 1 yuan denomination, a total of five

 

From this, we can see the differences between the two.

 

 

4. Comparison between the dynamic programming method and greedy algorithm for money-seeking in code implementation

/*************************************** * ****************** <Br/> * question: there are three RMB with a minimum denomination of 11, 5, and 1, and you need to find the minimum amount of money. <br/> * Description: Comparison between dynamic planning and greedy algorithm to solve the problem <br/> * Author: jarvisChu <br/> *********************************** * *********************/<br/> # include <stdio. h> <br/> # define N 4 <br/> # define VALUE1 11 // RMB 11 for the denomination (which can be modified) <br/> # define VALUE2 5 // RMB with a denomination of 5 RMB (which can be modified) <br/> # define VALUE3 1 // RMB with a denomination of 1 RMB (do not modify it, otherwise, it will be unavailable.) <br/> # Define MAX_MONEY 1000 // maximum amount of money that can be found </p> <p> /********************* ******************************** <br/> * method: <br/> * int Num [MAX_MONEY]; // Num [I] Save the I Yuan You Want To open, minimum RMB required <br/> * int Num_Value [N] [MAX_MONEY]; // Num_Value [I] [j] indicates j RMB, RMB <br/> * Num [I] = I; 0 <= I <= 4 <br/> * Num [I] = min (Num [i-VALUE1] + 1, Num [i-VALUE2] + 1, Num [i-VALUE3] + 1) <br/> */</p> <p> //----------------- -------- Minimum value --------------------------------- <br/> int min (int a, int B, int c) {<br/> return a <B? (A <c? A: c) :( B <c? B: c); <br/>}< br/> // ------------------------- obtain the optimal value --------------------------------- <br/> int DP_Money (int money, int Num []) {<br/> // obtain the total number of RMB required to open the money. <br/> int I; <br/> for (I = 0; I <= VALUE2; I ++) {// 0 ~ 4 $1 <br/> Num [I] = I; <br/>}< br/> for (I = VALUE2; I <= money; I ++) {// collect money starting from 5 yuan <br/> if (i-VALUE1> = 0) {// if it is larger than 11 yuan, it indicates that an additional 11 yuan denomination may be used <br/> // select a small number of Num [I] = min (Num [i-VALUE1] + 1, num [i-VALUE2] + 1, Num [i-VALUE3] + 1 ); <br/>}< br/> else {// select a small number of elements from 5 yuan, 1 <br/> Num [I] = (Num [i-VALUE2] + 1) <(Num [i-VALUE3] + 1 )? (Num [i-VALUE2] + 1) :( Num [i-VALUE3] + 1); <br/> // Num [I] = min (Num [i-VALUE2] + 2, num [i-VALUE2] + 1, Num [i-VALUE3] + 1); <br/>}< br/> return Num [money]; <br/>}< br/> // ----------------------- calculate the optimal solution <br/> void BestChoice (int money, int Num [], int Num_Value [N] [MAX_MONEY]) {<br/> // for money, the total number of RMB is stored in Num [money]. <br/> // Num [1 ~ 3] [money] Save the three denominations respectively <br/> int I; <br/> for (I = 0; I <VALUE2; I ++) {<br/> Num_Value [1] [I] = 0; <br/> Num_Value [2] [I] = 0; <br/> Num_Value [3] [I] = I; <br/>}< br/> for (I = VALUE2; I <= money; I ++) {<br/> if (I> = VALUE1) & (Num [I] = (Num [i-VALUE1] + 1 ))) {// I is composed of a I-11 + 11 I is composed of a I-11 plus a denomination of 11 RMB. <br/> Num_Value [1] [I] = Num_Value [1] [i-VALUE1] + 1; // an additional 11 yuan denomination RMB <br/> Num_Value [2] [I] = Num_Value [2] [i-VALUE1]; // 5 yuan denomination RMB As many as a number <br/> Num_Value [3] [I] = Num_Value [3] [i-VALUE1]; // 1 RMB with the same amount of RMB <br/>}< br/> else if (Num [I] = (Num [i-VALUE2] + 1 )) {// I is composed of I-5 + 5 <br/> Num_Value [1] [I] = Num_Value [1] [i-VALUE2]; // 11 yuan with the same amount of RMB <br/> Num_Value [2] [I] = Num_Value [2] [i-VALUE2] + 1; // an additional 5 yuan denomination RMB <br/> Num_Value [3] [I] = Num_Value [3] [i-VALUE2]; // 1 RMB with the same amount of RMB <br/>}< br/> else if (Num [I] = (Num [i-VALUE3] + 1 )) {// I is composed of I-1 + 1 <br/> N Um_Value [1] [I] = Num_Value [1] [i-VALUE3]; // 11 yuan with the same amount of RMB <br/> Num_Value [2] [I] = Num_Value [2] [i-VALUE3]; // 5 RMB with the same amount of RMB <br/> Num_Value [3] [I] = Num_Value [3] [i-VALUE3] + 1; // one more RMB 1 <br/>}< br/> else {< br/>}</p> <p> ********* * ********************** <br/> * method: <br/> * Num_Value [I] indicates the number of RMB in which the denomination is VALUEi. <br/> * you can use a large denomination in RMB, try to use a large denomination <br/> */<br/> int Greed (in T money, int Num_Value []) {<br/> // you need to open the money RMB, Num_Value [1 ~ 3] The number of sheets that store three denominations of RMB <br/> int total = 0; // The total number of sheets. The returned value is the total number of sheets. <Br/> Num_Value [1] = 0; <br/> Num_Value [2] = 0; <br/> Num_Value [3] = 0; <br/> for (int I = money; I >=1;) {<br/> if (I> = VALUE1) {<br/> Num_Value [1] ++; <br/> I-= VALUE1; <br/> total ++; <br/>}< br/> else if (I >= VALUE2) {<br/> Num_Value [2] ++; <br/> I-= VALUE2; <br/> total ++; <br/>}< br/> else if (I >= VALUE3) {<br/> Num_Value [3] ++; <br/> I-= VALUE3; <br/> total ++; <br/>}< br/> else {<br/>}< br/> return total; <br/>}< br/> void main () {<br/> // test the Dynamic Programming Method <br/>/* int I; <br/> int money = 23; <br/> int Num [MAX_MONEY]; // Num [I] Save the I RMB to open, minimum Required RMB <br/> int Num_Value [N] [MAX_MONEY]; // Num_Value [I] [j] indicates j RMB, RMB <br/> printf ("% d/n", DP_Money (money, Num); <br/> printf ("---------------------------------------------------/n "); <br/> BestChoice (money, Num, Num_Value); <br/> printf ("-------------------------------------------/n"); <br/> for (I = 0; I <= money; I ++) {<br/> printf ("Num [% d] = % 4d, % 3d, % 3d, % 3d/n", I, num [I], Num_Value [1] [I], Num_Value [2] [I], Num_Value [3] [I]); <br/>}< br/> */</p> <p> // test the greedy algorithm <br/>/* int I; <br/> int Num_Value_Greed [4]; <br/> for (I = 0; I <= 40; I ++) {// method of finding money from 0 RMB to 40 RMB <br/> Greed (I, Num_Value_Greed); <br/> printf ("% d ----> % d, % d, % d/n ", I, Num_Value_Greed [1], Num_Value_Greed [2], Num_Value_Greed [3]); <br/>}< br/> */</p> <p> // compare two algorithms <br/> int I; <br/> int dp, grd; // Save the total number of RMB sheets obtained by Dynamic Planning and greedy algorithms respectively <br/> int money; // The amount of money to be searched <br/> int Num [MAX_MONEY]; // Num [I] saves the number of silver coins to be used to find I <br/> int Num_Value [N] [MAX_MONEY]; // Num_Value [I] [j] indicates the number of coins whose face value is VALUEi. <br/> int Num_Value_Greed [N]; // Num_Value_Greed [I] indicates the number of RMB whose nominal value is VALUEi <br/> money = 15; // can be any non-negative integer value (15 yuan is a typical value that can be distinguished between the two algorithms) <br/> dp = DP_Money (money, Num ); // Dynamic Programming Method <br/> BestChoice (money, Num, Num_Value); <br/> grd = Greed (money, Num_Value_Greed ); // greedy algorithm <br/> printf ("$: % d/n", money ); <br/> printf ("algorithm cost: 11 RMB 5 RMB 1 RMB/n "); <br/> printf ("Dynamic Planning %-4d %-4d %-3d %-3d/n", dp, Num_Value [1] [money], num_Value [2] [money], Num_Value [3] [money]); <br/> printf ("greedy algorithm %-4d %-4d %-3d %-3d/n", grd, Num_Value_Greed [1], Num_Value_Greed [2], num_Value_Greed [3]); <br/>} 

 

 

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.