Backtracking -- solving the 0-1 knapsack problem

Source: Internet
Author: User


I have studied a simple n queen problem before, and I have a vague understanding of the backtracking method. The general understanding is: I have always been doing something, when a condition is completed or a condition is violated, return to the nearest Restore Point.


When we use the Backtracking Method to Solve the 0-1 knapsack problem, we mainly encounter three relatively difficult problems: 1. What is a boundary function; 2. When to use it; 3, back to where.





What is a boundary function?



For example:

When we are standing in a search space tree and standing at a k point, we can use it to estimate the total value we will get when we continue to go down the road. Let us assume that we have a maximum solution. When I use the boundary function to calculate a value and compare it with my current maximum solution, if we can get more benefits, we choose to continue downward. If not, give up.


We can see from the pseudocode in that we use a greedy algorithm when calculating the maximum value of the second half. Although the split is not allowed, however, we can use this result for estimation.







Search space tree obtained by the Backtracking Method:









When Will boundary functions be used?


The mathematical expression is: when X [I] = 0.

 

To put it simply, when you enter the right node.



How to trace back?


Go back to the first node that is not 0 (and this node is not a vertex ).



Solution


For example, in the Search Tree, before creating a search tree, I sorted all the items by V/W (weight-to-value ratio) in ascending order, and then started from the first one, put it into the backpack (the size of the backpack is 110) and put it in the first place. At this time, it is found that 6 is too big and cannot be loaded. At this time, use the boundary function to judge. If you continue, the greatest value we can obtain is compared with the maximum value we have obtained in the previous searches, but we have not obtained any other solutions before, therefore, when we compare the boundary function with the initial value-1 of the maximum value, we always choose to continue. In this way, we obtain a solution 139. then we go back to the first place where x [I] is not equal to 0. Here we set X [5] to 0, at this time, if X [5] is set to 0, we first use the boundary function to determine the situations from X [6] to X [8] And get a 164.44 value, this is 139 larger than the first solution we obtained last time. It means that a solution larger than 139 will appear later, so we choose to continue ......


.........


However, when we go back to X [1], we set X [1] to 0. At this time, we use the boundary function to estimate the maximum value that an item may obtain from item 2 to item 8, the conclusion is 155.11, which is smaller than the actual maximum solution 159. Then, we give up and go back to the next step to find that this has come to an end and then stops.



In combination with the previous n queen problem, the n queen problem is the queen of my line. If the current line is placed at the last position, it will still generate attacks, at this time, we will adjust the position of the Queen of the previous line, and then return to the bank to start from the first line. Compared with the 0-1 backpack, this is to complete a solution process, and then continue to solve the problem.



Therefore, the backtracking method is to keep doing it, and then go back.





Summary:

It is critical to use the Backtracking Method to Solve the first three problems proposed by the 0-1 backpack. If a problem is large enough, the boundary function can be used to cut down many subnodes that are not suitable for the problem, this greatly improves the efficiency.












Backtracking -- solving the 0-1 knapsack problem

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.