Dynamic Planning for algorithm analysis
First, let's give an example. 01. A specific example of a backpack: Assume that there are 4 other items, namely A1, A2, A3, and A4, with a capacity of 15kg. Item A1 has a weight of 3 kg and a value of 4. Item A2 has a weight of 4 kg and a value of 5. Item A3 has a weight of 5 kg and a value of 6. Item A4 has a weight of 6 kg and a value of 7. What items can be put into a backpack to maximize the total value of the backpack?
For such a problem, if the data involved is relatively small, we can calculate it by listing it. For example, the answer to the above example is: put A4, A3, and A2 in the backpack. The total weight is 6 + 5 + 4 = 15, and the total value is 5 + 6 + 7 = 18. The total value is the largest. However, if there are many conditions given above, we will never be able to look at it with our eyes, so we should use the idea of dynamic planning.
The idea of dynamic planning is how to establish it. If beginners are still confused about dynamic planning, you can open the following article link.
Click Open link http://blog.csdn.net/u014028070/article/details/39695669
The basic idea of dynamic planning is similar to the divide and conquer method. It also breaks down the problem to be solved into several subproblems (stages) and solves the substages in sequence and the solutions of the previous subproblems, it provides useful information for solving the next subproblem. When solving any subproblem, list various possible local solutions, reserve the local solutions that may reach the optimum through decision-making, and discard other local solutions. Solve each sub-problem in sequence. the last sub-problem is the solution of the initial problem.
Most of the problems solved by Dynamic Planning overlap subproblems. To reduce repeated computing, each subproblem is resolved only once, save different States of different stages in a two-dimensional array.
The biggest difference from the divide and conquer method is that it is suitable for the problem solved by dynamic programming, subproblems obtained after decomposition are often not independent of each other (that is, the next substage is based on the solutions of the previous substage for further solving ).
We use the 01 backpack as an example:
Idea: First generalize the original problem, and want to obtain the total value of the backpack, that is, to put the first I object into the capacity of M (kg) the maximum value of a backpack C [I] [m] -- use an array to store the maximum value. The first I put an object into the capacity of M (kg) of the backpack, but also can be converted into the first (I-1) an object into the backpack. The following uses mathematical expressions to describe the specific relationship between them.
The specific meaning of each symbol in the expression.
W [I]: the weight of the I-th object;
P [I]: the value of the I object;
C [I] [m]: maximum value of putting the first I object into a backpack with a capacity of m;
C [I-1] [m]: The maximum value of putting the first I-1 objects into a backpack with a capacity of m;
C [I-1] [M-W [I]: The maximum value of putting the first I-1 objects into a backpack with a capacity of M-W [I;
Therefore, you can obtain the following information:
C [I] [m] = max {C [I-1] [M-W [I] + Pi, C [I-1] [m]} (recursion is used at this time)
Referencing a picture on the Internet can better illustrate the situation:
Problem Analysis: V (I, j) indicates that the capacity of the items in the previous I (1 <= I <= N) is J (1 <= j <= C) to obtain the following dynamic planning function:
(1) V (I, 0) = V (0, j) = 0
(2) V (I, j) = V (I-1, j) j <wi
V (I, j) = max {v (I-1, J), V (I-1, J-WI) + VI)} j> wi
(1) indicates that if the weight of the I-th item is greater than the size of the backpack, the maximum value obtained by the first item of the loader is the same as the maximum price obtained by the first item of the I-1, that is, the item I cannot be loaded into the backpack; the (2) formula indicates: if the Weight of item I is smaller than the size of the backpack, there will be two situations: (a) If you load item I into the backpack, then the value of the backpack item is equal to the value of the first I-1 item loaded into the capacity space J-wi backpack plus the value of the first item VI; (B) if the I-th item is not loaded into a backpack, the value of the item in the backpack is equal to the value obtained by loading the previous I-1 item into the backpack with a capacity of J. Obviously, the greatest value of the two is used as the optimal solution for loading the first I item into a backpack with a capacity of J.
# Include <stdlib. h> # include <stdio. h> int V [200] [200]; // The maximum value int max (int A, int B) obtained by loading the first I item into a backpack with a capacity of J) // a size comparison function, used when the total weight is greater than the I-th row {if (a> = B) return a; else return B;} int knap (INT N, int W [], int V [], int X [], int c) {int I, j; for (I = 0; I <= N; I ++) V [I] [0] = 0; For (j = 0; j <= C; j ++) V [0] [J] = 0; for (I = 0; I <= n-1; I ++) for (j = 0; j <= C; j ++) if (j <W [I]) V [I] [J] = V [I-1] [J]; else V [I] [J] = max (V [I-1] [J], V [I-1] [J-W [I] + V [I]); j = C; for (I = n-1; I> = 0; I --) {If (V [I] [J]> V [I-1] [J]) {x [I] = 1; j = J-W [I];} else X [I] = 0;} printf ("the selected item is \ n"); for (I = 0; I <n; I ++) printf ("% d", X [I]); printf ("\ n"); Return V [n-1] [c];} int main () {int s; // The maximum value obtained int W [4]; // The weight value of the item and the state of the item are stored in the array, the item starts from 1. Int V [4]; // The value of the item int X [4]; // The value of the selected item is 1, which is not selected as 0 int N, I; int C; // maximum backpack capacity n = 4; printf ("Enter the maximum backpack capacity: \ n"); scanf ("% d", & C ); printf ("item count: \ n"); scanf ("% d", & N); printf ("Enter the item weight separately: \ n "); for (I = 0; I <n; I ++) scanf ("% d", & W [I]); printf ("Enter the value of the item separately: \ n "); for (I = 0; I <n; I ++) scanf (" % d ", & V [I]); s = knap (n, w, V, X, c); // call the core function printf ("Maximum item value: \ n"); printf ("% d \ n", S ); system ("pause"); Return 0 ;}
The result is:
So much understanding is being learned.
Detailed analysis of dynamic planning (01 knapsack problem)