View difference between Paste ID: 5KRZErc8 and Dvh7TFkH
SHOW: | | - or go back to the newest paste.
1
<?php
2-
    //Unbounded knapsack problem
2+
        //Unbounded knapsack problem
3-
    //still need to figure out how many of each size
3+
        //still need to figure out how many of each size
4
         
5-
    //<?php
5+
        //<?php
6-
    $w=array(1,2,5,7);
6+
        $w=array(1,2,3,4);
7-
    $v=array(15,23,50,65);
7+
        $v=array(1,2.2,3.3,4.4);
8-
    $itemCount=array();
8+
        $itemCount=array();
9-
    for($i=0;$i<count($v);$i++){
9+
        for($i=0;$i<count($v);$i++){
10-
    	$itemCount[]=0;
10+
            $itemCount[]=0;
11-
    };
11+
        };
12-
    $C=18;//max weight limit of knapsack
12+
        $C=16;//max weight limit of knapsack
13
         
14-
    echo 'Knapsack is: '.Knapsack($v,$w,$C,$itemCount);
14+
        echo 'Knapsack is: '.Knapsack($v,$w,$C,$itemCount);
15
         
16-
        //(N = # different items)
16+
            //(N = # different items)
17
         
18-
    function Knapsack($v,$w,$C,$itemCount){
18+
        function Knapsack($v,$w,$C,$itemCount){
19-
           $sol=array();
19+
               $sol=array();
20-
               $mySol=array();
20+
                   $mySol=array();
21-
               $myFinalSol=array();
21+
                   $myFinalSol=array();
22-
               $N=count($v);
22+
                   $N=count($v);
23-
           /* ***************************************
23+
               /* ***************************************
24-
              Base case
24+
                  Base case
25-
              *************************************** */
25+
                  *************************************** */
26-
                    if ($C == 0 )
26+
                        if ($C == 0 )
27-
            return 0;
27+
                return 0;
28
         
29
         
30-
           /* ==============================================
30+
               /* ==============================================
31-
              Divide and conquer procedure
31+
                  Divide and conquer procedure
32-
              ============================================== */
32+
                  ============================================== */
33
         
34-
           /* ---------------------------------------
34+
               /* ---------------------------------------
35-
              Solve the appropriate smaller problems
35+
                  Solve the appropriate smaller problems
36-
              --------------------------------------- */
36+
                  --------------------------------------- */
37-
                    for ($i=0;$i<$N;$i++){
37+
                        for ($i=0;$i<$N;$i++){
38-
                            if($C>=$w[$i]){
38+
							echo 'ORIGINAL Knapsack I: '.$i.
39-
                            $GLOBALS["itemCount"][$i]+=1;
39+
                                if($C>=$w[$i]){
40-
                            		echo 'itemCount['.$i.']='.$GLOBALS["itemCount"][$i].' i='.$i."\n";
40+
                                $GLOBALS["itemCount"][$i]+=1;
41-
                                //echo 'capacity('.$C.')>w[i]('.$w[$i].')<br />';
41+
                                            echo ' GLOBALS itemCount['.$i.']='.$GLOBALS["itemCount"][$i].' i='.$i."\n";
42-
                                    $sol[$i] = Knapsack($v,$w,$C-$w[$i],$GLOBALS["itemCount"]);// Knapsack capacity reduced by w[i]    
42+
                                    //echo 'capacity('.$C.')>w[i]('.$w[$i].')<br />';
43-
                            		echo ' for loop 1 '."\n".'i: '.$i.' $sol[$i]: '.$sol[$i]."\n\n";
43+
                                        $sol[$i] = Knapsack($v,$w,$C-$w[$i],$GLOBALS["itemCount"]);// Knapsack capacity reduced by w[i]    
44-
                            }                                                               // because it has item i packed in
44+
                                            echo ' for loop 1 '."\n".'i: '.$i.' $sol[$i]: '.$sol[$i]."\n\n";
45-
                                                                                            // it already  
45+
                                }                                                               // because it has item i packed in
46-
                            else{$sol[$i] = 0;}        // Not enough space to  pack item i
46+
                                                                                                // it already  
47-
                    }
47+
                                else{$sol[$i] = 0;}        // Not enough space to  pack item i
48-
48+
                        }
49-
    //var_dump($sol);
49+
50-
 
50+
        //var_dump($sol);
51-
           /* ---------------------------------------------
51+
52-
              Use the solutions to the smaller problems
52+
               /* ---------------------------------------------
53-
              to solve original problem
53+
                  Use the solutions to the smaller problems
54-
              --------------------------------------------- */
54+
                  to solve original problem
55-
                    for($i=0;$i<$N;$i++){
55+
                  --------------------------------------------- */
56-
                            if($C>=$w[$i]){
56+
                        for($i=0;$i<$N;$i++){
57-
                            		
57+
                                if($C>=$w[$i]){
58-
                                    $mySol[$i]=$sol[$i]+$v[$i]; // Value is increased by v[i]
58+
                                           
59-
                                    echo ' for loop 2 '."\n".'i: '.$i.' $mySol[$i]: '.$mySol[$i]."\n\n";
59+
                                        $mySol[$i]=$sol[$i]+$v[$i]; // Value is increased by v[i]
60-
                            }                                                               // because it has item i packed in
60+
									echo ' for loop 2 '."\n".'i: '.$i.' $mySol[$i]: '.$mySol[$i]."\n\n<br />";
61-
                                                                                            // it already
61+
									echo '<strong> TO ADD V[i]'.$v[$i].'</strong><br />';
62-
                            else{$mySol[$i]=0;}        // Not enough space to  pack item i
62+
                                }                                                               // because it has item i packed in
63-
           }
63+
                                                                                                // it already
64
                                else{$mySol[$i]=0;}        // Not enough space to  pack item i
65-
           /* *************************
65+
               }
66-
              Find the best (maximum)
66+
         
67-
              ************************* */
67+
               /* *************************
68-
                    $myFinalSol = $mySol[1];
68+
                  Find the best (maximum)
69-
                    for($i=1;$i<$N;$i++){
69+
                  ************************* */
70-
              if($mySol[$i]>$myFinalSol){
70+
                        $myFinalSol = $mySol[1];
71-
                            $myFinalSol = $mySol[$i];}
71+
                        for($i=1;$i<$N;$i++){
72-
                    }
72+
                  if($mySol[$i]>$myFinalSol){
73-
                    return $myFinalSol;
73+
                                $myFinalSol = $mySol[$i];}
74-
       };
74+
                        }
75-
    //?>
75+
                        return $myFinalSol;
76-
76+
           };
77-
77+
        //?>
78-
?>
78+
79
     
80
    ?>