P11: search solution for knapsack problems

Source: Internet
Author: User
P11: search solution for knapsack problems

The nine lectures on knapsack problems are intended to be explained as one of the dynamic planning issues. However, as there are indeed some knapsack problems that can only be solved by searching, we will also give a brief introduction to the problem of using searching to solve the backpack. Most of them use the 01 backpack as an example. Other items should be accessible.

Simple Deep Search

For the 01 backpack problem, the complexity of simple deep search is O (2 ^ N ). It is to enumerate all the 2 ^ n solutions to put the item into the backpack, and then find the optimal solution. The basic framework is as follows:

 
Procedure searchpack (I, cur_v, cur_w)
 
If (I> N)
 
If (cur_w> Best)
 
Best = cur_w
 
Return
 
If (cur_v + V [I] <= V)
 
Searchpack (I + 1, cur_v + V [I], cur_w + W [I])
 
Searchpack (I + 1, cur_v, cur_w)

Cur_v and cur_w indicate the charges and weights of the current solution. MasterProgramYou can call searchpack (, 0.

Search pruning

The basic pruning method is feasible or optimized.

Feasibility pruning determines whether a feasible solution can be found based on the current search path. For example: if you still cannot fill your backpack with all the items left in it (you must fill your backpack with the question), then trim it.

Optimization pruning determines whether an optimal solution can be found based on the current search path. For example, if the weights of all remaining items are added, the solution cannot be better than the current optimal solution, then, pruning is performed.

Search Order

In the search, you can think that the items with the top order will be given priority. Therefore, the greedy idea is used to advance the order of items that may appear in the results, so that the greedy optimal solution can be obtained quickly, which is more conducive to optimum pruning. Therefore, you can consider sorting the search order by "cost effectiveness" (weight/cost.

On the other hand, if more expensive items are arranged in front, you can quickly fill the backpack, which is conducive to the feasibility of pruning.

The last option is to randomly disrupt the order of the items in the input file before you start searching. This avoids the trap deliberately set by the proposition.

The above three methods determine the search order. It is difficult to say which method is better. In fact, each method has applicable questions and data, and it is possible to mix them to some extent.

Subset and Problem

Subset and problem is an NP-complete problem, which is different from the aforementioned (weighted) 01 backpack problem. Given a set of integers s and an integer x, ask if a subset of S satisfies the sum of all elements in it.

This problem involves a more efficient search with a time complexity of O (2 ^ (n/2 ).AlgorithmWhere N is the size of the Set S.

The idea of the first step is binary. Divide the set S into two subsets S1 and S2. Their sizes are n/2. For S1 and S2, enumerate all their 2 ^ (n/2) Child sets and save them to a data structure that supports searching, such as hash set.

Then we need to merge the two results to find whether there is a subset of S for X. In fact, for a specific sum of S1 and a subset of X1, you only need to find out whether S2 has a subset of sum and X-X1.

Assuming that the hash set is ideal, it takes only O (1) Time for each search and insertion. The time complexity of the two steps is obviously O (2 ^ (n/2 )).

In practice, we can first sort the two subsets obtained in the first step and sort them separately, and then use the two pointer scans to find out whether there are subsets that meet the requirements and. The maximum number of such implementations that can be solved within an acceptable period of time is about n = 42.

Search or DP?

Should I use search or dynamic planning when I see a problem with a backpack?

First, we can obtain the clue of the proposition's intention from the data range. If a backpack problem can be solved by DP, V1. otherwise, the O (VN) algorithm cannot afford it. The general search solution is only related to N, it has nothing to do with v. Therefore, when V is very large (such as millions), the intention of the proposition person should be to evaluate the search. On the other hand, when n is large (for example, hundreds), the intention of the proposition person is likely to be to examine the dynamic planning.

In addition, you can only search for an appropriate dynamic planning algorithm. For example, if you see a restriction on items in a backpack that you have never seen before, and you cannot come up with the DP equation, you have to write a search to get a certain score.

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.