Dynamic Planning-0/1 knapsack problems

Source: Internet
Author: User

Dynamic Planning is the abstraction of a method for changing the space for time. The key is to discover sub-problems and record their results. Then use these results to reduce the computational workload.
For example, 01.

/* A traveler has a backpack that can use up to M kilograms and now has n items,
Their weights are W1, W2,..., Wn,
Their values are P1, P2,..., Pn.
If each item has only one item, the traveler can obtain the maximum total value.
Input Format:
M, n
W1, p1
W2, p2
......
Output Format:
X
*/

The maximum weight m of the backpack is unknown. Therefore, our program should be tested one by one from 1 to M. For example, you can choose one of N items. Look at the corresponding M backpack, can put in, if can put in, and there is a lot of space, then, out of the space can put the maximum value of N-1 items. How can we ensure that the total choice is the greatest value? See the following table.
Test data:
10, 3
3, 4
4, 5
5, 6

The C [I] [J] array stores the maximum value of items 1, 2, and 3 selected in sequence.

How can we get the greatest value? Starting from the weight of the backpack being 0, the weight of the first item, 0, 1, 2, cannot be placed. so set it to 0, and put it in 3 when the weight of the backpack is 3. in this way, the weight of this backpack is 4, 5, 6 ,.... when 10, the best solution is to put 4. assume that item 1 is placed in a backpack. then let's look at item 2 again. when the weight of the backpack is 3, the best solution is the best price solution in the last row. C is 4. when the weight of a backpack is 5, the best solution is 5. when the weight of the backpack is 7, it is obviously 5 plus a value. Who should I add ?? Obviously, when 7-4 = 3, the optimal solution for the previous C3 row is 4. So. In general, the best solution is 5 + 4 for 9. In this way, a row is pushed down. The rightmost decentralized data is the greatest value. (Note that when the weight of a 3rd-row backpack is 7, the best solution is not its own 6. but 9 in the last row. this indicates that item 3 is not selected at this time. item 1 and 2 are selected. so 9 .)

We can see from the above construction process of the maximum value.

F (n, m) = max {f (n-1, m), F (n-1, M-W [N]) + P (n, m )} this is the dynamic planning equation written in books. is this clear?

 

Follow the steps described in "computer algorithm design and analysis" to Solve the Problem Based on Dynamic Planning:

1. analyze the problem

For an Object Sequence A1 ~ An, each choice may only be 0 or 1, so the problem is converted to finding a 0/1 integer sequence X1 ~ XN, xi = 0 or xi = 1.

2. Find the optimal sub-structure.

Assume that ~ Xi is in the Object Sequence A1 ~ An Optimal Selection sequence selected in AI, then, X1 ~ X (I-1) must be from Object Sequence A1 ~ A (I-1) selects the most selective sequence.

However, it should be noted that the Object Sequence A1 ~ A (I-1) and A1 ~ When AI is selecting, they have different constraints, that is, they require different maximum weight limits. Therefore, you cannot simply consider the General Sub-problem (that is, set C [I] ~ The maximum value of the optimal choice in AI, then C [I] = C [I-1] + VI, so that the maximum weight limit is not taken into account ).

When considering the maximum weight limit, set C [I] [J] To "From Object Sequence A1 ~ In AI, select an integer sequence X1 ~ XI, and make the total weight not exceed the maximum total value of J ".

Reconsider the optimal sub-structure: X1 ~ X (I-1) is from Object Sequence A1 ~ The optimal integer sequence selected in a (I-1) and ensures:

(1) If xi = 1, that is, the object AI is selected, the total weight must not exceed J-WI.

(2) If xi = 0, that is, no AI is selected, the total weight must not exceed J.

Therefore, C [I] [J] =

(J> = ai)

Max (C [I-1] [J]), // do not select AI

C [I-1] [J-wi] + VI) // select AI

(J <AI time)

C [I-1] [J]

The following is the actual program:

# Include <stdio. h>
Int C [10] [100];/* maximum value for each situation */
Int knapsack (int m, int N)
{
Int I, j, W [10], p [10];
For (I = 1; I <n + 1; I ++)
Scanf ("/n % d, % d", & W [I], & P [I]);
For (I = 0; I <10; I ++)
For (j = 0; j <100; j ++)
C [I] [J] = 0;/* Initialize an array */
For (I = 1; I <n + 1; I ++)
For (j = 1; j <m + 1; j ++)
{
If (W [I] <= J)/* if the weight of the current item is smaller than the weight of the backpack */
{
If (P [I] + C [I-1] [J-W [I]> C [I-1] [J])

/* If the value of this item is added with the value of the items that can be placed in the space left by the backpack */

/* Update C [I] [J] if it is greater than the best solution selected last time */
C [I] [J] = P [I] + C [I-1] [J-W [I];
Else
C [I] [J] = C [I-1] [J];
}
Else C [I] [J] = C [I-1] [J];
}
Return (C [N] [m]);

}
Int main ()
{
Int M, N; int I, J;
Scanf ("% d, % d", & M, & N );
Printf ("input each one:/N ");
Printf ("% d", knapsack (m, n ));
Printf ("/N");/* The following is the test array, which can be deleted */
For (I = 0; I <10; I ++)
For (j = 0; j <15; J ++)
{
Printf ("% d", C [I] [J]);
If (j = 14) printf ("/N ");
}
System ("pause ");
}

 

Http://blog.csdn.net/adcxf/archive/2008/08/07/2784156.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.