PHP Backtracking method solves 0-1 knapsack problem case analysis, 0-1 instance Analysis _php Tutorial

Source: Internet
Author: User

PHP Backtracking method solves 0-1 knapsack problem case analysis, 0-1 instance analysis


This paper describes the method of PHP backtracking to solve the 0-1 knapsack problem. Share to everyone for your reference. The specific analysis is as follows:

This code is written according to the pseudo code of the software designer tutorial;
The most troublesome is not to change the pseudo-code to PHP, but the array subscript starting from 0, and the corresponding subscript judgment problem;
With a debug output piece written on

<?php $v _arr = Array (11,21,31,33,43,53,55,65);  $w _arr = Array (1,11,21,23,33,43,45,55);  $n = count ($w _arr);  Test output Var_dump (BKNAP1);//var_dump (bound (139,89,7,110));    function bound ($v, $w, $k, $W _total) {global $v _arr, $w _arr, $n;    $b = $v;    $c = $w;//var_dump ($W _total); Var_dump ($n); Var_dump ($k); Var_dump ($v); Var_dump ($w);//die;      for ($i = $k +1; $i < $n; $i + +) {$c = $c + $w _arr[$i];      Var_dump ($W _total); Var_dump ($c);      if ($c < $W _total) $b + = $v _arr[$i];        Else{//var_dump (($c-$W _total)/$w _arr[$i]) * $v _arr[$i]);        $b = $b + (n ($c-$W _total)/$w _arr[$i]) * $v _arr[$i];       return $b;    }}/*var_dump ('------bound head ');    Var_dump ($K);    Var_dump ($b);   Var_dump ('------bound end '); */return $b;    } function Bknap1 ($W _total) {global $v _arr, $w _arr, $n;    $CW = $CP = 0;    $k = 0;    $fp =-1;        while (true) {while ($k < $n && $CW + $w _arr[$k]<= $W _total) {$CW + = $w _arr[$k];        $CP + = $v _arr[$k]; $Y _arr[$k] = 1;      $k +=1;      }//var_dump ($CW), Var_dump ($CP), Var_dump ($Y _arr), Var_dump ($k); Var_dump ($n);        if ($k = = $n) {$fp = $CP;        $FW = $CW;        $k = $n-1; $X _arr = $Y _arr;//bound ($CP, $CW, $k, $W _total),//var_dump (Bound ($CP, $CW, $k, $W _total), $fp, $k);d ie;//var_dump ($FP);      Var_dump ($FW); Var_dump ($Y _arr); Var_dump ($k); Var_dump ($n);      }else{$Y _arr[$k] = 0;      }//var_dump ($Y _arr); Var_dump ($k); Var_dump ($n)//die;//var_dump (bound ($CP, $CW, $k, $W _total), $fp);d ie;        while (Bound ($CP, $CW, $k, $W _total) <= $fp) {while ($k >=0 && $Y _arr[$k]!=1) {$k-= 1;        } if ($k <0) {return $X _arr;        } var_dump ($k);        $Y _arr[$k] = 0;        $CW-= $w _arr[$k];      $CP-= $v _arr[$k];    } $k + = 1; }}?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/972652.html www.bkjia.com true http://www.bkjia.com/PHPjc/972652.html techarticle The php backtracking method solves 0-1 knapsack problem case Analysis, 0-1 example analysis this article narrated the PHP backtracking method solves 0-1 knapsack question method. Share to everyone for your reference. The specific analysis is as follows: ...

  • 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.