Difference between greedy method and Dynamic Programming Method

Source: Internet
Author: User
Differences between dynamic planning and greedy Algorithms
Dynamic Programming and greedy algorithms are both recursive algorithms that use local optimal solutions to derive global optimal solutions.

Differences:
Greedy Algorithm:
1. In the greedy algorithm, the greedy decision made in each step cannot be changed, because the greedy policy derives the optimal solution of the next step from the optimal solution in the previous step, and the optimal solution in the previous step is not reserved.
2. According to the introduction in (1), the correct condition of the greedy method is that the optimal solution of each step must include the optimal solution of the previous step.

Dynamic Planning Algorithm:
1. The global optimal solution must contain a certain local optimal solution, but not necessarily the previous local optimal solution. Therefore, we need to record all the previous optimal solutions.
2. The key to dynamic planning is the state transition equation, that is, how to derive the global optimal solution from the obtained local optimal solution.
3. boundary condition: the simplest local optimal solution that can be obtained directly
========================================================== ==============================================
Greedy Algorithm and Dynamic Planning
The basic idea of greedy method:

Starting from an initial solution of the problem, we gradually approach the given goal to obtain a better solution as quickly as possible. When a certain step in an algorithm is reached, the algorithm stops.
This algorithm has the following problems:
1. There is no guarantee that the final solution obtained is optimal;
2. cannot be used to find the maximum or limit the problem;
3. only feasible solutions that meet certain constraints can be obtained. The process of implementing this algorithm:
Starting from an initial solution to the problem;

While can move one step forward to the given overall goal do
Find a solution element of the feasible solution;
A feasible solution that combines all solution elements into a problem

The most classic example of greedy algorithms is money.
For example, China's currency only looks at yuan, with 1 yuan, 2 yuan, 5 yuan, 10 yuan, 20, 50, and 100 yuan.

If I want 16 yuan, I can get 16 yuan 1 yuan and 8 yuan 2 yuan, but how can I get at least?
If you use greedy calculation, it is the biggest one I can get at a time.
For example 16, I couldn't afford 20 for the first time. I got 10 yuan, OK, 6 yuan for the rest, 5 yuan for the other, and 1 yuan for the rest.
That is, 3 images: 10, 5, and 1.

The greatest thing you can get each time is greed.

However, it must be noted that what greedy gets is not the optimal solution. That is to say, greedy is not necessarily the least number of images.
Greedy can only get a better solution, and the greedy algorithm is very easy to get.
Note: Why can we use greed for money? Because our country's money size design can make the greedy algorithm calculate the optimal solution (generally, coins in a country should be designed in this way ). It would be different if it was designed to be something else.
For example, coins in a country are divided into 1 RMB, 3 RMB, and 4 RMB.
How can I get 6 yuan for it? If you are greedy, take 4 first and then take two, one for three.
What is the actual optimum? Two three yuan tickets are enough.

The problem of finding the optimal solution is basically a traversal of the solution space. The most direct brute-force analysis is easy to obtain, and the space for solving the optimal solution is usually exponential growth. Therefore, brute-force exhaustion is not feasible.
Most of the optimal solution problems can be divided into sub-problems, and the traversal of the solution space is considered as the traversal of the sub-problem tree. Then, the optimal solution can be obtained through traversing the entire tree in some form, this analysis is not feasible.
Greedy and dynamic planning are essentially pruning of the Child problem tree. One of the problems required by the two algorithms is the "subproblem is the optimum ". That is, the solution of each subproblem that constitutes the optimal solution is certainly the best for this subproblem itself. If we look at the problem tree in the top-down direction (the original problem is used as the root), we only need to traverse down the child tree representing the optimal solution each time to guarantee the overall optimal solution. In the image, you can simply use a value (optimal value) to represent the entire subtree, instead of finding all the values that this subtree may represent.
The dynamic planning method represents a general solution to this type of problem. We construct a subproblem solution from the bottom up (from the leaves to the root). For the root of each subtree, we can find the values of each leaf below and use the optimal values as our own values, discard other values. The cost of dynamic planning depends on the number of options (the number of forks in the tree) and the number of subproblems (the number of nodes in the tree, or the height of the tree ?).
Greedy algorithms are a special case of dynamic planning. It can be proved that the root value of each subtree does not depend on the values of the following leaves, but only on the current problem. In other words, you can obtain the value of a node without knowing all the Subtrees of a node. Generally, this value is an obvious "Optimal" Condition for the current problem. Therefore, we use "greedy" to describe the essence of this algorithm. Because of the greedy algorithm, it does not need to traverse the spatial tree from the bottom up, but only needs to start from the root, select the optimal path, and continue to the end. In this way, compared with dynamic planning, the cost only depends on the number of subproblems, and the total number of choices is 1. ------------------------------

Looking for money can help us to understand the difference 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

View plaincopy to clipboardprint?
01. /*************************************** *******************
02. * question: There are three RMB with a minimum denomination of 11 5, and the minimum amount is used to find money.
03. * Description: Comparison between dynamic planning and greedy algorithm for solving problems
04. * OPERATOR: JarvisChu
05. **************************************** ******************/
06. # include <stdio. h>
07. # define N 4
08. # define VALUE1 11 // RMB of the denomination of 11 RMB (which can be modified)
09. # define VALUE2 5 // RMB 5 with a denomination (which can be modified)
10. # define VALUE3 1 // RMB with a denomination of 1 RMB (do not modify it, or else it may fail to be found)
11. # define MAX_MONEY 1000 // The maximum amount of money that can be found
12.
13. ********** **********************
14. * method:
15. * int Num [MAX_MONEY]; // Num [I] Save the minimum number of RMB required to open the I RMB
16. * int Num_Value [N] [MAX_MONEY]; // Num_Value [I] [j] indicates the number of RMB required for VALUEi.
17 .*
18. * Num [I] = I; 0 <= I <= 4
19. * Num [I] = min (Num [i-VALUE1] + 1, Num [i-VALUE2] + 1, Num [i-VALUE3] + 1)
20 .*/
21.
22. // ------------------------- obtain the minimum value ---------------------------------
23. int min (int a, int B, int c ){
24. return a <B? (A <c? A: c) :( B <c? B: c );
25 .}
26. // ------------------------- obtain the optimal value ---------------------------------
27. int DP_Money (int money, int Num []) {
28. // obtain the total number of RMB required to open the money
29. int I;
30. for (I = 0; I <= VALUE2; I ++) {// 0 ~ 4 full use 1 RMB
31. Num [I] = I;
32 .}
33. for (I = VALUE2; I <= money; I ++) {// collect money starting from 5 yuan
34. if (i-VALUE1> = 0) {// if it is larger than 11 yuan, it indicates the possibility of an additional 11 yuan denomination.
35. // select a small number of items from 11 RMB, 5 RMB, and 1 RMB.
36. Num [I] = min (Num [i-VALUE1] + 1, Num [i-VALUE2] + 1, Num [i-VALUE3] + 1 );
37 .}
38. else {// select a small number
39. Num [I] = (Num [i-VALUE2] + 1) <(Num [i-VALUE3] + 1 )? (Num [i-VALUE2] + 1) :( Num [i-VALUE3] + 1 );
40. // Num [I] = min (Num [i-VALUE2] + 2, Num [i-VALUE2] + 1, Num [i-VALUE3] + 1 );
41 .}
42 .}
43. return Num [money];
44 .}
45. // ------------------------- obtain the optimal solution ---------------------------------
46. void BestChoice (int money, int Num [], int Num_Value [N] [MAX_MONEY]) {
47. // for money, put the total RMB in Num [money]
48. // Num [1 ~ 3] [money] Save the number of three denominations respectively
49. int I;
50. for (I = 0; I <VALUE2; I ++ ){
51. Num_Value [1] [I] = 0;
52. Num_Value [2] [I] = 0;
53. Num_Value [3] [I] = I;
54 .}
55. for (I = VALUE2; I <= money; I ++ ){
56. if (I> = VALUE1) & (Num [I] = (Num [i-VALUE1] + 1 ))) {// I is composed of I-11 + 11 I is composed of I-11 yuan plus a denomination of 11 yuan
57. Num_Value [1] [I] = Num_Value [1] [i-VALUE1] + 1; // an additional 11 yuan denomination RMB
58. Num_Value [2] [I] = Num_Value [2] [i-VALUE1]; // the same amount of RMB 5
59. Num_Value [3] [I] = Num_Value [3] [i-VALUE1]; // The number of RMB in one denomination is the same
60 .}
61. else if (Num [I] = (Num [i-VALUE2] + 1) {// I is composed of I-5 + 5
62. Num_Value [1] [I] = Num_Value [1] [i-VALUE2]; // the same amount of RMB 11
63. Num_Value [2] [I] = Num_Value [2] [i-VALUE2] + 1; // an additional 5 yuan denomination RMB
64. Num_Value [3] [I] = Num_Value [3] [i-VALUE2]; // The number of RMB in the same amount
65 .}
66. else if (Num [I] = (Num [i-VALUE3] + 1) {// I is composed of I-1 + 1
67. Num_Value [1] [I] = Num_Value [1] [i-VALUE3]; // the same amount of RMB 11
68. Num_Value [2] [I] = Num_Value [2] [i-VALUE3]; // the same amount of RMB 5
69. Num_Value [3] [I] = Num_Value [3] [i-VALUE3] + 1; // an additional 1 RMB denomination
70 .}
71. else {
72 .}
73 .}
74 .}
75.
76. ********** **********************
77. * method:
78. * Num_Value [I] indicates the number of RMB in which the denomination is VALUEi.
79. * use a large denomination of RMB as much as possible
80 .*/
81. int Greed (int money, int Num_Value []) {
82. // find the RMB for money, Num_Value [1 ~ 3] storage of three denominations of RMB
83. int total = 0; // The total number of sheets. The returned value is the total number of sheets.
84. Num_Value [1] = 0;
85. Num_Value [2] = 0;
86. Num_Value [3] = 0;
87. for (int I = money; I> = 1 ;){
88. if (I> = VALUE1 ){
89. Num_Value [1] ++;
90. I-= VALUE1;
91. total ++;
92 .}
93. else if (I> = VALUE2 ){
94. Num_Value [2] ++;
95. I-= VALUE2;
96. total ++;
97 .}
98. else if (I> = VALUE3 ){
99. Num_Value [3] ++;
100. I-= VALUE3;
101. total ++;
102 .}
103. else {
104 .}
105 .}
106. return total;
107 .}
108. void main (){
109. // test the Dynamic Programming Method
110./* int I;
111. int money = 23;
112. int Num [MAX_MONEY]; // Num [I] Save the minimum number of RMB required to open the I RMB
113. int Num_Value [N] [MAX_MONEY]; // Num_Value [I] [j] indicates the number of RMB required for VALUEi.
114. printf ("% d \ n", DP_Money (money, Num ));
115. printf ("--------------------------------------------- \ n ");
116. BestChoice (money, Num, Num_Value );
117. printf ("--------------------------------------------- \ n ");
118. for (I = 0; I <= money; I ++ ){
119. printf ("Num [% d] = % 4d, % 3d, % 3d, % 3d \ n", I, Num [I], Num_Value [1] [I], num_Value [2] [I], Num_Value [3] [I]);
120 .}
121 .*/
122.
123. // test the greedy algorithm
124./* int I;
125. int Num_Value_Greed [4];
126. for (I = 0; I <= 40; I ++) {// method of finding money from 0 RMB to 40 RMB
127. Greed (I, Num_Value_Greed );
128. printf ("% d ---- >>> % d, % d, % d \ n", I, Num_Value_Greed [1], Num_Value_Greed [2], Num_Value_Greed [3]);
129 .}
130 .*/
131.
132. // compare two algorithms
133. int I;
134. int dp, grd; // Save the total number of RMB Obtained by Dynamic Programming and greedy algorithms respectively
135. int money; // The money to seek
136. int Num [MAX_MONEY]; // Num [I] saves the number of silver coins to be spent on searching for I
137. int Num_Value [N] [MAX_MONEY]; // Num_Value [I] [j] indicates the number of coins whose face value is VALUEi.
138. int Num_Value_Greed [N]; // Num_Value_Greed [I] indicates the number of RMB whose nominal value is VALUEi.
139. money = 15; // can be any non-negative integer value (15 RMB is a typical value that can be distinguished between the two algorithms)
140. dp = DP_Money (money, Num); // Dynamic Programming Method
141. BestChoice (money, Num, Num_Value );
142. grd = Greed (money, Num_Value_Greed); // Greedy Algorithm
143. printf ("the money is: % d \ n", money );
144. printf ("the number of algorithms is 11 RMB 5 RMB 1 RMB \ n ");
145. printf ("Dynamic Planning %-4d %-4d %-3d %-3d \ n", dp, Num_Value [1] [money], Num_Value [2] [money], num_Value [3] [money]);
146. printf ("greedy algorithm %-4d %-4d %-3d %-3d \ n", grd, Num_Value_Greed [1], Num_Value_Greed [2], Num_Value_Greed [3]);
147 .}

 

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/JarvisChu/archive/2010/12/05/6056963.aspx

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.