Preliminary introduction to knapsack problems-backpack 1

Source: Internet
Author: User

Introduction: I love and hate backpacks and understand the code, but it does not mean that you have mastered backpacks. I know that modeling is difficult! So I am cheering for my backpack headache. perseverance can overcome all problems.

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 capacity 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

After reading this table, I understand the equation. The initial model of the backpack, the X axis represents the incremental capacity, the Y axis represents the number of items, and the yellow part represents the value. The corresponding code of this table is easy to understand.

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? From the size of the backpack to 0, the first test of item 1, 0, 1, 2, cannot be placed. therefore, set 0 and put 4 in 3 for the backpack capacity. in this way, the size 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 size of the backpack is 3, the best solution is the best price solution in the last row. C is 4. when the backpack capacity is 5, the best solution is 5 of its weight. when the backpack capacity 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 size of the 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?

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 current item capacity is smaller than the backpack capacity */
{
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 ");
}

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.