Recall algorithm, non-algorithmic master do not enter

Source: Internet
Author: User
Backtracking algorithm, non-algorithmic master do not enter!
This post was last edited by xuzuning on 2011-06-10 14:40:16

Given the items n pieces, their weight is w[0],w[1],......w[n-1], the value of the goods is v[0],v[1],......v[n-1], and another backpack, it can hold the total weight of W. Design an item selection scheme that requires that the total weight of the items selected from these n items not exceed the capacity of the backpack w, so that the value of the selected item is the largest.

This is a very common backpack backtracking algorithm, who can write in PHP!


Note: The algorithm-independent reply will be ruthlessly deleted! Moderator

Share to:


------Solution--------------------
This question will take at least one hours, I say train of thought, let others do it.

1. Sort the array of W to select items less than W weight (assignment array p),
2. The Cartesian product array is computed for the P array, and the sum of the items in all subsets is selected and less than the W weight (Assignment array R).
3. Convert the entries in each subset of the r array to the corresponding V-values and sum them separately (array WV),
4. Sort the WV array and derive the key to the large value as the result.
------Solution--------------------

$m = 15;
$arr = Array (Array (2,1), Array (4,2), Array (3,6), Array (5,9), Array (9,8));//The first value is price; the second value is weight
function combination ($arr, $size = 1) {
$len = count ($arr);
$max = POW (2, $len)-POW (2, $len-$size);
$min = POW (2, $size)-1;
$r _arr = Array ();
for ($i = $min; $i <= $max; $i + +) {
$t _arr = Array ();
for ($j = 0, $k = 0; $j < $len; $j + +) {
$a = POW (2, $j);
$t = $i & $a;
if ($t = = $a) {
$t _arr [] = $arr [$j];
}
}
if (count ($t _arr) = = $size) {
$r _arr [] = $t _arr;
}
}
return $r _arr;
}
$num = count ($arr);
for ($i = 1; $i <= $num; $i + +) {
$_tt =combination ($arr, $i);
$num _tt = count ($_TT);
for ($j = 0; $j < $num _tt; $j + +) {
$_t[] = $_tt[$j];
}
}//find out the possible circumstances.
function Check_m ($arr, $m, $jk =1) {//$arr the $m of the array to be judged is the weight $jk to determine the weight or the price
$num _t = count ($arr);
for ($i = 0; $i < $num _t; $i + +) {
$num _ti = count ($arr [$i]);
$as = 0;
for ($j =0; $j < $num _ti; $j + +) {
$as + = $arr [$i] [$j] [$JK];
}
if ($as <= $m) {
$_r[] = $arr [$i];
}
}
Return $_r;
}


function Check_max ($arr) {
$ms = 0;
$num _t = count ($arr);
for ($i = 0; $i < $num _t; $i + +) {
$num _ti = count ($arr [$i]);
$as = 0;
for ($j =0; $j < $num _ti; $j + +) {
$as + = $arr [$i] [$j][0];
}
if ($as >= $ms) {
$_r = $arr [$i];
}
$ms = $as;
}
Return $_r;
}
$_RR = Check_m ($_t, $m, 1);
$_r=check_max ($_RR);
echo "
";
Print_r ($_r);
echo "
";
?>

------Solution--------------------
This post was last edited by xuzuning on 2011-06-10 14:01:34

class backtracking {
Private $c = 0; Backpack capacity
Private $n = 0; Number of items
Private $w = Array (); Item weight Array
Private $p = Array (); Object value Array
Private $CW = 0; Current weight
Private $CP = 0; Current value
Private $BESTP = 0; Current optimal value
private $d; Unit weight Value
Private $st = Array ();

function __construct ($w, $p, $c) {
$this->w = $w;
$this->p = $p;
$this->c = $c;
$this->n = count ($w);

$this->d = Array_map (Array ($this, ' calculation '), $this->p, $this->w);
Array_multisort ($this->d, Sort_desc, $this->w, $this->p);
}

Private function calculation ($p, $w) {
if ($w = = 0) return $p;
  • 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.