Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -12,61 +12,61 @@
|
|||
|
||||
function knapSolveFast2($w, $v, $i, $aW, &$m) {
|
||||
|
||||
global $numcalls;
|
||||
$numcalls ++;
|
||||
// echo "Called with i=$i, aW=$aW<br>";
|
||||
global $numcalls;
|
||||
$numcalls ++;
|
||||
// echo "Called with i=$i, aW=$aW<br>";
|
||||
|
||||
// Return memo if we have one
|
||||
if (isset($m[$i][$aW])) {
|
||||
return array( $m[$i][$aW], $m['picked'][$i][$aW] );
|
||||
} else {
|
||||
// Return memo if we have one
|
||||
if (isset($m[$i][$aW])) {
|
||||
return array( $m[$i][$aW], $m['picked'][$i][$aW] );
|
||||
} else {
|
||||
|
||||
// At end of decision branch
|
||||
if ($i == 0) {
|
||||
if ($w[$i] <= $aW) { // Will this item fit?
|
||||
$m[$i][$aW] = $v[$i]; // Memo this item
|
||||
$m['picked'][$i][$aW] = array($i); // and the picked item
|
||||
return array($v[$i],array($i)); // Return the value of this item and add it to the picked list
|
||||
// At end of decision branch
|
||||
if ($i == 0) {
|
||||
if ($w[$i] <= $aW) { // Will this item fit?
|
||||
$m[$i][$aW] = $v[$i]; // Memo this item
|
||||
$m['picked'][$i][$aW] = array($i); // and the picked item
|
||||
return array($v[$i],array($i)); // Return the value of this item and add it to the picked list
|
||||
|
||||
} else {
|
||||
// Won't fit
|
||||
$m[$i][$aW] = 0; // Memo zero
|
||||
$m['picked'][$i][$aW] = array(); // and a blank array entry...
|
||||
return array(0,array()); // Return nothing
|
||||
}
|
||||
}
|
||||
|
||||
// Not at end of decision branch..
|
||||
// Get the result of the next branch (without this one)
|
||||
list ($without_i, $without_PI) = knapSolveFast2($w, $v, $i-1, $aW, $m);
|
||||
} else {
|
||||
// Won't fit
|
||||
$m[$i][$aW] = 0; // Memo zero
|
||||
$m['picked'][$i][$aW] = array(); // and a blank array entry...
|
||||
return array(0,array()); // Return nothing
|
||||
}
|
||||
}
|
||||
|
||||
if ($w[$i] > $aW) { // Does it return too many?
|
||||
|
||||
$m[$i][$aW] = $without_i; // Memo without including this one
|
||||
$m['picked'][$i][$aW] = $without_PI; // and a blank array entry...
|
||||
return array($without_i, $without_PI); // and return it
|
||||
// Not at end of decision branch..
|
||||
// Get the result of the next branch (without this one)
|
||||
list ($without_i, $without_PI) = knapSolveFast2($w, $v, $i-1, $aW, $m);
|
||||
|
||||
} else {
|
||||
|
||||
// Get the result of the next branch (WITH this one picked, so available weight is reduced)
|
||||
list ($with_i,$with_PI) = knapSolveFast2($w, $v, ($i-1), ($aW - $w[$i]), $m);
|
||||
$with_i += $v[$i]; // ..and add the value of this one..
|
||||
|
||||
// Get the greater of WITH or WITHOUT
|
||||
if ($with_i > $without_i) {
|
||||
$res = $with_i;
|
||||
$picked = $with_PI;
|
||||
array_push($picked,$i);
|
||||
} else {
|
||||
$res = $without_i;
|
||||
$picked = $without_PI;
|
||||
}
|
||||
|
||||
$m[$i][$aW] = $res; // Store it in the memo
|
||||
$m['picked'][$i][$aW] = $picked; // and store the picked item
|
||||
return array ($res,$picked); // and then return it
|
||||
}
|
||||
}
|
||||
if ($w[$i] > $aW) { // Does it return too many?
|
||||
|
||||
$m[$i][$aW] = $without_i; // Memo without including this one
|
||||
$m['picked'][$i][$aW] = $without_PI; // and a blank array entry...
|
||||
return array($without_i, $without_PI); // and return it
|
||||
|
||||
} else {
|
||||
|
||||
// Get the result of the next branch (WITH this one picked, so available weight is reduced)
|
||||
list ($with_i,$with_PI) = knapSolveFast2($w, $v, ($i-1), ($aW - $w[$i]), $m);
|
||||
$with_i += $v[$i]; // ..and add the value of this one..
|
||||
|
||||
// Get the greater of WITH or WITHOUT
|
||||
if ($with_i > $without_i) {
|
||||
$res = $with_i;
|
||||
$picked = $with_PI;
|
||||
array_push($picked,$i);
|
||||
} else {
|
||||
$res = $without_i;
|
||||
$picked = $without_PI;
|
||||
}
|
||||
|
||||
$m[$i][$aW] = $res; // Store it in the memo
|
||||
$m['picked'][$i][$aW] = $picked; // and store the picked item
|
||||
return array ($res,$picked); // and then return it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -92,9 +92,9 @@ echo "<table border cellspacing=0>";
|
|||
echo "<tr><td>Item</td><td>Value</td><td>Weight</td></tr>";
|
||||
$totalVal = $totalWt = 0;
|
||||
foreach($pickedItems as $key) {
|
||||
$totalVal += $v4[$key];
|
||||
$totalWt += $w4[$key];
|
||||
echo "<tr><td>".$items4[$key]."</td><td>".$v4[$key]."</td><td>".$w4[$key]."</td></tr>";
|
||||
$totalVal += $v4[$key];
|
||||
$totalWt += $w4[$key];
|
||||
echo "<tr><td>".$items4[$key]."</td><td>".$v4[$key]."</td><td>".$w4[$key]."</td></tr>";
|
||||
}
|
||||
echo "<tr><td align=right><b>Totals</b></td><td>$totalVal</td><td>$totalWt</td></tr>";
|
||||
echo "</table><hr>";
|
||||
|
|
|
|||
|
|
@ -9,25 +9,25 @@
|
|||
|
||||
function knapSolve($w,$v,$i,$aW) {
|
||||
|
||||
global $numcalls;
|
||||
$numcalls ++;
|
||||
// echo "Called with i=$i, aW=$aW<br>";
|
||||
global $numcalls;
|
||||
$numcalls ++;
|
||||
// echo "Called with i=$i, aW=$aW<br>";
|
||||
|
||||
if ($i == 0) {
|
||||
if ($w[$i] <= $aW) {
|
||||
return $v[$i];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
$without_i = knapSolve($w, $v, $i-1, $aW);
|
||||
if ($w[$i] > $aW) {
|
||||
return $without_i;
|
||||
} else {
|
||||
$with_i = $v[$i] + knapSolve($w, $v, ($i-1), ($aW - $w[$i]));
|
||||
return max($with_i, $without_i);
|
||||
}
|
||||
if ($i == 0) {
|
||||
if ($w[$i] <= $aW) {
|
||||
return $v[$i];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
$without_i = knapSolve($w, $v, $i-1, $aW);
|
||||
if ($w[$i] > $aW) {
|
||||
return $without_i;
|
||||
} else {
|
||||
$with_i = $v[$i] + knapSolve($w, $v, ($i-1), ($aW - $w[$i]));
|
||||
return max($with_i, $without_i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -44,36 +44,36 @@ function knapSolve($w,$v,$i,$aW) {
|
|||
|
||||
function knapSolveFast($w,$v,$i,$aW,&$m) { // Note: We use &$m because the function writes to the $m array
|
||||
|
||||
global $numcalls;
|
||||
$numcalls ++;
|
||||
// echo "Called with i=$i, aW=$aW<br>";
|
||||
global $numcalls;
|
||||
$numcalls ++;
|
||||
// echo "Called with i=$i, aW=$aW<br>";
|
||||
|
||||
// Return memo if we have one
|
||||
if (isset($m[$i][$aW])) {
|
||||
return $m[$i][$aW];
|
||||
} else {
|
||||
// Return memo if we have one
|
||||
if (isset($m[$i][$aW])) {
|
||||
return $m[$i][$aW];
|
||||
} else {
|
||||
|
||||
if ($i == 0) {
|
||||
if ($w[$i] <= $aW) {
|
||||
$m[$i][$aW] = $v[$i]; // save memo
|
||||
return $v[$i];
|
||||
} else {
|
||||
$m[$i][$aW] = 0; // save memo
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
$without_i = knapSolveFast($w, $v, $i-1, $aW,$m);
|
||||
if ($w[$i] > $aW) {
|
||||
$m[$i][$aW] = $without_i; // save memo
|
||||
return $without_i;
|
||||
} else {
|
||||
$with_i = $v[$i] + knapSolveFast($w, $v, ($i-1), ($aW - $w[$i]),$m);
|
||||
$res = max($with_i, $without_i);
|
||||
$m[$i][$aW] = $res; // save memo
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
if ($i == 0) {
|
||||
if ($w[$i] <= $aW) {
|
||||
$m[$i][$aW] = $v[$i]; // save memo
|
||||
return $v[$i];
|
||||
} else {
|
||||
$m[$i][$aW] = 0; // save memo
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
$without_i = knapSolveFast($w, $v, $i-1, $aW,$m);
|
||||
if ($w[$i] > $aW) {
|
||||
$m[$i][$aW] = $without_i; // save memo
|
||||
return $without_i;
|
||||
} else {
|
||||
$with_i = $v[$i] + knapSolveFast($w, $v, ($i-1), ($aW - $w[$i]),$m);
|
||||
$res = max($with_i, $without_i);
|
||||
$m[$i][$aW] = $res; // save memo
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue