10. Dynamic Planning (3) -- 0-1 backpack problem, 0-1 backpack

Source: Internet
Author: User

10. Dynamic Planning (3) -- 0-1 backpack problem, 0-1 backpack

In the previous article "9. Dynamic Planning (2) -- Subsets and problems", we talked about Subsets and problems and their implementations. Backpack problems are actually a subset and a type of problem, but the problem is not "determining the problem" but "optimal problem ". The problem with a backpack is actually divided into "0-1 backpack" and "full backpack". This article explains "0-1 backpack.

Q: There are n items. The weight of each item is weigh [I], and the value of each item is price [I]. Now there is a backpack, the weight of a backpack is W. What is the maximum total value of items that a backpack can hold?

Define s [I, j] to indicate the total value of the first I item, and j is the weight of the backpack. When j = W or W is the closest and less than W, this is the solution to the problem.

The key to "Dynamic Planning" is to find its recursive formula, which usually Splits a problem into two parts based on a certain value as the boundary. The solution to the knapsack problem is the optimal solution for the subset and problem. dynamic Planning (2) -- Analysis of the derivation project of the recursive formula in the subset and problem, and re-analysis and derivation here.

Analysis: s [I, j] indicates the first I items. If the value of the first I-1 item has reached the threshold of carrying weight j, therefore, the I-th item cannot be placed in (j-wi <0), which indicates s [I, j] = s [I-1, j]. However, if the I-1 item does not reach the limits of backpack weight j (j-wi> = 0 ), in this case, the value of the first I-1 item is s [I-1, j-wi]. the value of the I-th item can be expressed as s [I-1, j-wi] + pi.

In conclusion, the recursive formula is obtained:

Example: item weight set w = (2, 4, 1, 5, 3), item price set p = (4, 5, 19, 3, 2 ), the weight of the backpack is 6. Using the recursive formula above, we can use a matrix to represent this backpack problem. The maximum value of the 6th column is the maximum value when the weight of the backpack is 6.

Java

1 package com. algorithm. dynamicprogramming; 2 3 import java. util. arrays; 4 5/** 6*0-1 backpack problem 7 * | s [I-1, j] (j-wi <0) 8 * s [I, j] = | s [I-1, j] 9 * | Max | (j-wi> = 0) 10 * | s [I-1, j-wi] + pi11 * Created by yulinfeng on 7/3/17.12 */13 public class KnapsackProblem {14 public static void main (String [] args) {15 int [] weight = {2, 4, 1, 5, 2}; 16 int [] price = {4, 5, 19, 3, 2 }; 17 int knapsackWeight = 6; 18 int value = knapsackProblem (weight, price, knapsackWeight); 19 System. out. println (value ); 20} 21 22/** 23 * dynamic planning solution 0-1 backpack problem 24 * @ param weight item weight 25 * @ param price item value 26 * @ param knapsackWeight backpack weight 27 * @ return28 */29 private static int knapsackProblem (int [] weight, int [] price, int knapsackWeight) {30 int row = weight. length + 1; 31 int col = knapsackWeight + 1; 32 int [] [] solutionMatrix = new int [row] [col]; 33 int [] values = new int [row]; 34 values [0] = 0; 35 for (int I = 0; I <row; I ++) {36 solutionMatrix [I] [0] = 0; 37} 38 for (int j = 0; j <col; j ++) {39 solutionMatrix [0] [j] = 0; 40} 41 42 for (int I = 1; I <row; I ++) {43 for (int j = 1; j <col; j ++) {44 solutionMatrix [I] [j] = solutionMatrix [I-1] [j]; 45 if (j-weight [I-1]> = 0 & solutionMatrix [I-1] [j-weight [I-1] + price [I-1]> solutionMatrix [I] [j]) {46 solutionMatrix [I] [j] = solutionMatrix [I-1] [j-weight [I-1] + price [I-1]; 47} 48} 49 values [I] = solutionMatrix [I] [col-1]; 50} 51 Arrays. sort (values); 52 return values [values. length-1]; 53} 54}

Python3

1 def knapsack_problem (weight, price, knapsackWeight): 2'''3 0-1 backpack Problem 4 | s [I-1, j] (j-wi <0) 5 s [I, j] = | s [I-1, j] 6 | Max | (j-wi> = 0) 7 | s [I-1, j-wi] + pi 8 9 Created by yulinfeng on 7/3/17.10 ''' 11 row = len (weight) + 112 col = len (price) + 113 solutionMatrix = [[0 for c in range (col)] for r in range (row)] 14 values = [0] * row15 for I in range (row ): 16 solutionMatrix [0] [I] = 017 for j in range (col): 18 solutionMatrix [j] [0] = 019 for m in range (1, row ): 20 for n in range (1, col ): 21 solutionMatrix [m] [n] = solutionMatrix [m-1] [n] 22 if n-weight [m-1]> = 0 and solutionMatrix [m-1] [n -weight [m-1] + price [m-1]> solutionMatrix [m] [n]: 23 solutionMatrix [m] [n] = solutionMatrix [m-1] [n-weight [m-1] + price [m-1] 24 values [m] = solutionMatrix [m] [col-1] 25 26 values. sort () 27 return values [len (values)-1] 28 29 weight = (2, 4, 1, 5, 2) 30 price = (4, 5, 19, 3, 2) 31 knapsackWeight = 632 value = knapsack_problem (weight, price, knapsackWeight) 33 print (value)

 

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.