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;