Backpack Question 1

Source: Internet
Author: User

First, let's look at a problem prototype based on a backpack:

Two cargo ships need to ship N boxes. The first cargo ship carries C1, the second is C2, and Wi is the weight of I, and W1 + W2 + W3 +... wn <= C1 + C2: You want to determine whether there is a method to ship all N boxes. If so, find this method.

Analysis:

As a knapsack problem, the following methods are usually used: exhaustive (you need to know the number of bins), dynamic planning, and branch boundary search algorithms. This chapter discusses a exhaustive Algorithm Based on the subset Tree features.

The question must be W1 + W2 + W3 +... wn <= C1 + C2, that is, requires that the cargo ship C1 be filled up as much as possible on the premise that the total cargo volume does not exceed C1 + C2, so the problem can be changed: make sure W1 + W2 + W3 +... on the premise of Wn <= C1 + C2, how many methods can be used to fill up C1.

The feature of the subset tree is that only two branches of a node are selected and not selected. Generally, the left branch is selected, and the count is 1. Otherwise, the right branch is not selected, the count is 0. When a binary tree is formed for all nodes according to the above rules, the current selection can be obtained by traversing each tree root to the leaf node.

Figure 1 binary subset tree

 

Back to the problem, if the branch boundary search algorithm is used, it means that a subset tree of two forks is constructed indirectly, and the selection of parts is not possible through condition judgment in advance, when N is known, a N-weight loop is generated to solve the problem one by one.

For Figure 1, if all the situations are selected or not, the corresponding selection number is recorded. The result is as follows:

000,001,010,011,100,101,110,111

Convert to the corresponding decimal number:

0, 1, 2, 3, 4, 5, 6, 7

Based on the above results, it is not difficult to find that if there are N points to construct a binary subset tree, there will be 2 ∧ N options, the required results are also obtained in the range of [distinct N-1]

Improve the regular exhaustive algorithm:

Through the analysis, we can know that the results of the problem are obtained in the range of [Gb/s N-1], then we can design the framework prototype of the algorithm.

Count = 2 <num-1; // count is the total number of containers selected

While (count> = 0)

{

J = count;

For (I = num; I> 0; I --) // N goods are compared only 2 Gb/s N-1 times, each shift only needs to move N to the right
{

K + = (j & 0x01) * (* B); // extract the last binary of the selected number and multiply it with the current cargo in the array.

If (k = c1) // The current weight reaches the weight of the first cargo ship
{
Print (output result );
Break;
}
Else if (k> c1) // The current weight exceeds the weight of the first cargo ship
{
K-= * B; // remove the current weight
Break;
}
Else // The current weight does not reach the weight of the first cargo ship
{
J> = 1; // The current value shifts one digit to the right.
B ++; // point to the address of the array pointer to the next element
Continue;
}

}

If (k <c1 & (weight-k) <= c2) // the current selection does not fill the first cargo ship
{
Print (output result );
}
Count --; the number currently selected minus 1
}

Algorithm analysis:

Compared with the conventional exhaustive algorithm, this algorithm uses a dual loop, and the outer loop is the total number of choices, the inner layer is the number of shift operations selected for each number (only shift N places to the right for N boxes)

The time complexity depends on the number of bin N, that is, the time complexity is O (2 kg/n) * N. When the number of N approaches infinity, the algorithm still cannot be solved (depending on the number of digits that the computer can represent in binary format)

Appendix: complete C code (WIN 8.1 64-bit passed in VC6.0)

#include<stdio.h><stdlib.h><time.h> n 4 search(   *,,, main(,&,&(i=;i<n;i++,i+,& search(   *a, c1, c2, count,i,j,k,flag,weight,*=,weight==(i=,b=( *)a,count=;i<num;i++,b+++=*(weight>c1+=(flag!==<<num-(count>====( *(i=n;i>;i--+=(j&)*(*(k==,weight- (k>-=*>>=++(k<c1&&(weight-k)<=,weight---=,()(end-start)/

 

Related Article

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.