[Php] 0-1 backpack backtracking

Source: Internet
Author: User
<? Php
Header ("content-type: text/html; charset = UTF-8 ");
/**
* @ Description
* Search for a tree with a depth of n (n = number of items). The root node is the first item. If the weight of the root contact does not exceed the weight limit, the left and right subnodes of the root node are the second item. If the value exceeds the limit, the second item is used only as the left subnode.
* The main idea is that if a node meets the conditions, the left and right subtree will be searched, and the left subtree will be searched only for the nodes that do not match the conditions.
* Assume that w [] = (, 4) p [] = (, 6) and the weight limit is 7, the final search tree is
2
0/\ 1
3 3
0/\ 1 0/\ 1
4 4 4
0 | 1 0 | 1 0 | 1 0 |
(4) (3) (3) (3, 4) (2) (2, 4) (2, 3) --- The final possible solution is to determine the group with the highest value of an item.
* 0, 1 indicates whether the current node is superimposed/or \ the weight of the online node.
* If the preceding data limit is 4, the final search tree is
2
0/\ 1
3 3 (because 3 + 2> 4, only the left subtree is searched)
0/\ 1 0/
4 4 4
0 | 1 0 | 0 |
(4) (3) (2) --- The final possible solution to determine the group with the highest value
*/
Class bag01_bt {
Public $ weight = array (); // weight
Public $ price = array (); // price
Public $ limitw = 0; // weight limit
Public $ n = 0; // hierarchy of the tree
Public $ x = array (); // underlying node-> overlay node of the Root Node
Public $ cx = array (); // whether to overlay the current node
Public $ cp = 0; // the total price of the superimposed Node
Public $ cw = 0; // total weight of the superimposed Node
Public $ bp = 0; // optimal price
Public function bag01_bt ($ limitw, $ weight, $ price)
{
$ This-> n = count ($ weight );
$ This-> weight = $ weight;
$ This-> price = $ price;
$ This-> limitw = $ limitw;
}

Public function backstracking ($ n = 0)
{
If ($ n >=$ this-> n)
{
If ($ this-> cp> $ this-> bp)
{
$ This-> bp = $ this-> cp;
$ This-> x = $ this-> cx;
}
Return false;
}
// If the condition is met, record the current node as 1 and overlay the current item weight to continue searching for the right Child Count
If ($ this-> cw + $ this-> weight [$ n] <= $ this-> limitw)
{
$ This-> cw + = $ this-> weight [$ n];
$ This-> cp + = $ this-> price [$ n];
$ This-> cx [$ n] = 1;
$ This-> backstracking ($ n + 1 );
$ This-> cw-= $ this-> weight [$ n];
$ This-> cp-= $ this-> price [$ n];
}
// Do not record the current node, do not overlay the current item weight, search for the left subtree
$ This-> cx [$ n] = 0;
$ This-> backstracking ($ n + 1 );
}
}


$ Weight = array (2, 3, 44, 5, 15, 12 );
$ Price = array (5, 5, 8, 17, 3, 7 );
$ Limitw = 30;

$ Bag = new bag01_bt ($ limitw, $ weight, $ price );
$ Bag-> backstracking ();
Echo "<pre/> <br/> ";
Echo "Optimal Solution :";
Echo $ bag-> bp. "<br/> ";
Echo "item: <br/> ";
Foreach ($ bag-> x as $ k => $ v)
{
If ($ v = 1) $ items [] = $ bag-> weight [$ k]. "(". $ bag-> price [$ k]. ")";
}
Print_r ($ items );
?>

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.