Backpack problems-Total number of solutions for "01 backpack" and "full backpack" full backpack: Analysis and Implementation

Source: Internet
Author: User

----- Edit by zhusenlin HDU

I talked about "01 backpack" in my blog "backpack problem-" 01 backpack "Optimal Solution Total Number Analysis and Implementation" and "backpack problem-" full backpack "Optimal Solution Total Number Analysis and Implementation ". "and" full backpack "to achieve the maximum value of the total number of solutions, here we will discuss the total number of solutions for these two backpacks to be filled with items.

Major companies on the Internet often have questions: Suppose there are a lot of paper money for 1 yuan, 2 yuan, and 5 yuan. Now we need 20 yuan. How much money can you find, this can be considered as a complete backpack problem, that is, the size of the backpack is 20, and the size of the items is 1, 2, 5, respectively.

There is also a company's question: given a number m, how many solutions are there to split m into different forms of natural numbers? This is a typical 01 backpack problem. The size of the backpack is m, the number of items is k. Here, k is an implicit condition, which can be obtained, because m is up to 1 + 2 +... + K to obtain the maximum number of items according to m.

 

Now let's start with the question. Let's talk about the total number of solutions for "01 backpack" to just fill the backpack."Full backpack" is very similar to "01 backpack", with only a few code changes.

 

01 the problem of packing a backpack is Abstract:

Set the backpack capacity to V, a total of N items, each item volume to C [I], the value of each item to W [I], and the total number of solutions to fill the backpack.

1) Definition of sub-problem: F [I] [j] indicates the total number of solutions for filling a backpack with j space in the first I items selected.

2) make decisions based on the volume of items I and the size of the Left backpack

(1-1)

Note that the initialization condition is F [0] [0] = 1, that is, no items are placed in a backpack with a capacity of 0. The solution is 1.

The pseudocode is as follows:

F[0][0] ← 1  for i ← 1 to N      do for j ← 0 to V               if (j < C[i])                         then F[i][j] ← F[i-1][j]               else              F[i][j] ← F[i-1][j]+F[i-1][j-C[i]]  return F[N][V]

The space complexity of the above Code is O (NV). From the state equation, we can see that F [I] [] is only related to the state of F [I-1, therefore, we can use a one-dimensional array instead of a two-dimensional array to reduce the space complexity to O (V ).

The pseudocode for reducing the space complexity to O (V) is as follows:

F[0] ← 1  for i ← 1 to N      do for j ← V to C[i]             if (j >= C[i])              then F[j] ← F[j]+F[j-C[i]]  return F[V]

Note that the traversal of V Changes to backward order. For the reason, please refer to my blog "backpack problem-" 01 backpack "for details and implementation (including solving specific items in the backpack)".

Next, let's take a look at the changes in "full backpack. I have read about "backpack 9 lecture" or my blog article "backpack problem-" full backpack "for details and implementation (including solutions for specific backpack items) readers should be able to quickly think of the deformation of the state equation as follows:

(1-2)

Yes, the state equation is like this. F [I-1] [j] indicates a solution to fill the backpack with no I items in the backpack, F [I] [j-C [I] indicates the total number of solutions for filling a backpack with at least one Type I item. Therefore, when j <C [I] F [I] [j] = F [I-1] [j]; when j> = C [I, F [I] [j] = F [I] [j-C [I] + F [I-1] [j], why is the sum of the two, because F [I] [j-C [I] and F [I-1] [j] are both in [I] [j] State when the backpack is full, and the two are mutually exclusive.

The pseudocode is as follows:

F[0][0] ← 1  for i ← 1 to N          do for j ← 0 to V          if (j < C[i])                         then F[i][j] ← F[i-1][j]          else                    F[i][j] ← F[i-1][j]+F[i][j-C[i]]  return F[N][V]

Similarly, the space complexity of the above pseudo code isO (NV)We can also reduce the space complexity by using a one-dimensional arrayO (V).

The pseudocode is as follows:

F[0] ← 1for i ← 1 to N          do for j ← C[i] to V          if (j >= C[i])                  then F[j] ← F[j]+F[j-C[i]]  return F[V]

Note: The traversal of V is positive. Why? For more information, see my blog "backpack problem-" full backpack ".

 

The following describes the implementation code of these two backpacks.

01 total number of solutions for packing a backpack:

# Include <iostream> # include <cstring> # include "createarray. H" // This header file dynamically creates and destroys two-dimensional arrays. You can implement using namespace STD by yourself;

// Time complexity O (NV) space complexity O (NV)

int Package01_FullOfPackage(int Weight[], int nLen, int nCapacity){int** MethodTable = NULL;CreateTwoDimArray(MethodTable,nLen+1,nCapacity+1);MethodTable[0][0] = 1;for(int i = 1; i <= nLen; i++){for(int j = 0; j <= nCapacity; j++){if(j < Weight[i-1])MethodTable[i][j] = MethodTable[i-1][j];elseMethodTable[i][j] = MethodTable[i-1][j] + MethodTable[i-1][j-Weight[i-1]];}}cout << "MethodTable:" << endl;PrintTowDimArray(MethodTable,nLen+1,nCapacity+1);int nRet = MethodTable[nLen][nCapacity];DestroyTwoDimArray(MethodTable,nLen+1);return nRet;}

// Time complexity O (NV) space complexity O (V)

int Package01_FullOfPackage_Compress(int Weight[], int nLen, int nCapacity){int * MethodTable = new int [nCapacity+1];memset(MethodTable,0,(nCapacity+1)*sizeof(int));//initiallize all MethodTable[0] with 1MethodTable[0] = 1;for(int i = 0; i < nLen; i++){for(int j = nCapacity; j >=Weight[i]; j--){if(j >= Weight[i])MethodTable[j] += MethodTable[j-Weight[i]];}}int nRet = MethodTable[nCapacity];delete [] MethodTable;return nRet;}

// Test code 

int main(){//int Weight[] = {1,1,1,1,1,1};int Weight[] = {1,2,3,4,5,6,7,8,9,10};int nCapacity = 20;cout << "AllCount:" << Package01_FullOfPackage(Weight,sizeof(Weight)/sizeof(int),nCapacity) << endl;cout << "AllCount:" << Package01_FullOfPackage_Compress(Weight,sizeof(Weight)/sizeof(int),nCapacity) << endl;return 0;}

 

Total number of solutions for full backpacks:

// Time complexity O (NV) space complexity O (NV)

int Package02_FullOfPackage(int Weight[], int nLen, int nCapacity){int** MethodTable = NULL;CreateTwoDimArray(MethodTable,nLen+1,nCapacity+1);MethodTable[0][0] = 1;for(int i = 1; i <= nLen; i++){for(int j = 0; j <= nCapacity; j++){if(j < Weight[i-1])MethodTable[i][j] = MethodTable[i-1][j];elseMethodTable[i][j] = MethodTable[i-1][j]+MethodTable[i][j-Weight[i-1]];}}cout << "MethodTable:" << endl;PrintTowDimArray(MethodTable,nLen+1,nCapacity+1);int nRet = MethodTable[nLen][nCapacity];DestroyTwoDimArray(MethodTable,nLen+1);return nRet;}

// Time complexity O (NV) space complexity O (V)

int Package02_FullOfPackage_Compress(int Weight[], int nLen, int nCapacity){int * MethodTable = new int [nCapacity+1];memset(MethodTable,0,(nCapacity+1)*sizeof(int));//initiallize all MethodTable[0] with 1MethodTable[0] = 1;for(int i = 0; i < nLen; i++){for(int j = Weight[i]; j <= nCapacity; j++){if(j >= Weight[i])MethodTable[j] += MethodTable[j-Weight[i]];}}int nRet = MethodTable[nCapacity];delete [] MethodTable;return nRet;}

// Test code 

int main(){int Weight[] = {1,2,5};//int Weight[] = {2,2,2};int nCapacity = 20;cout << "AllCount:" << Package02_FullOfPackage(Weight,sizeof(Weight)/sizeof(int),nCapacity) << endl;cout << "AllCount:" << Package02_FullOfPackage_Compress(Weight,sizeof(Weight)/sizeof(int),nCapacity) << endl;return 0;}

For more information about this article, see section 9 about backpacks.

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.