01 Backpack (dp)

Problem Description:

There are n items, they have their own weight and value, there is a knapsack with a given capacity, how to make the items in the knapsack have the largest sum of value?

There are two ways to solve this problem, as with other dynamic programming problems

Array w[] is the weight of the item, v[] is the value of the item

One is a recursive idea. Considering from the back to the front, the backpack decides whether to put an item or not based on the size of two values ​​(one value is the value of the backpack without this item, and the other value is the backpack put this item, In addition, the backpack capacity reduces the value of the item weight), and goes to the maximum of the two values. The recursive end condition is that the items are exhausted or the backpack capacity is less than or equal to 0.

dp

Next is the idea of ​​dynamic programming. Dynamic programming is to think from front to back.

To find this knapsack problem, first draw a table

The horizontal axis is the capacity, and the vertical axis is the item id

The process of solving the problem is the process of maintaining this table, and the value to be solved is the value of the last space

First for item 0

When the capacity is 0, since its own weight is 1, it cannot be put in, so fill in the blank with 0. After the capacity is 1, the item can be put in, so it is 6.

For item No. 1

For the position (2,1), since the backpack capacity is 2, I can put item No. 1, take out item No. 0, or not put item No. 1, and judge the maximum value of the two cases, which is the first one Condition.

For this position (3,1), the backpack capacity is 3, and you can put both items at 1,0, or keep (2,1), and the result is the maximum value for both items.

For item No. 2

(3,2) This position is the value of (0,1)+2 item and the size of (3,1), the result is that (3,1) is relatively large, so it is still 16

(4,2) This position is the value of (1,1)+2 item and (4,1) is larger than the size, the result is (1,1)+12, so it is 18

(5,2) This position is the value of (2,1)+2 item and (5,1) is larger than the size, the result is (2,1)+12, so it is 22

So the maximum value of the final backpack is 22

package action;

public class demo {
	public static void main(String[] args) {
		int size = 5;
		int[] w = { 1, 2, 3 };
		int[] v = { 6, 10, 12 };
		System.out.println(backpack(w, v, size));
	}

	public static int backpack(int[] w, int[] v, int size) {
		if (w.length != v.length)
			return -1;

		int n = w.length;
		if (n == 0)
			return 0;
		int[][] memo1 = new int[n][size + 1];

		for (int i = 0; i <= size; i++) {
			memo1[0][i] = (i >= w[0] ? v[0] : 0);
		}
		for (int i = 1; i < n; i++) {
			for (int j = 0; j <= size; j++) {
				memo1[i][j] = memo1[i - 1][j];
				if (j >= w[i]) {
					memo1[i][j] = Math.max(memo1[i][j], v[i] + memo1[i - 1][j - w[i]]);
				}
			}
		}
		return memo1[n - 1][size];
	}
}

 

Guess you like

Origin blog.csdn.net/feng8403000/article/details/123901588