PHP greedy algorithm solves 0-1 knapsack problem case Analysis, 0-1 example Analysis _php tutorial

Source: Internet
Author: User

PHP greedy algorithm solves 0-1 knapsack problem case analysis, 0-1 instance analysis


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

Greedy algorithm solves 0-1 knapsack problem, the global optimal solution is obtained by local optimal solution! More flexible than dynamic planning to solve knapsack problems!

0-1 knapsack greedy algorithm problem class tanxin{public $weight;  Public $price;    Public function __construct ($weight =0, $price =0) {$this->weight= $weight;  $this->price= $price;  }}//Generate Data $n=10;for ($i =1; $i <= $n; $i + +) {$weight =rand (1,20);  $price =rand (1,10); $x [$i]=new tanxin ($weight, $price);}  Output function display ($x) {$len =count ($x);    foreach ($x as $val) {echo $val->weight, ', $val->price; Echo '
'; }}//sort function Tsort (& $x) {$len =count ($x) by price and weight ratio; for ($i =1; $i <= $len; $i + +) {for ($j =1; $j <= $len-$i; $j + +) {$temp = $x [$j]; $res = $x [$j +1]->price/$x [$j +1]->weight; $temres = $temp->price/$temp->weight; if ($res > $temres) {$x [$j]= $x [$j +1]; $x [$j +1]= $temp; }}}}//greedy algorithm function tanxin ($x, $totalweight =50) {$len =count ($x); $allprice = 0; for ($i =1; $i <= $len; $i + +) {if ($x [$i]->weight> $totalweight) break; else{$allprice + = $x [$i]->price; $totalweight = $totalweight-$x [$i]->weight; }} if ($i < $len) $allprice + = $x [$i]->price* ($totalweight/$x [$i]->weight); return $allprice;} Tsort ($x);//Sort display ($x) in a non-ascending order;//display echo ' 0-1 knapsack optimal solution: '; echo tanxin ($x);

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

http://www.bkjia.com/PHPjc/972651.html www.bkjia.com true http://www.bkjia.com/PHPjc/972651.html techarticle The PHP greedy algorithm solves 0-1 knapsack problem case Analysis, 0-1 example analysis this article narrated the PHP greedy algorithm solves 0-1 knapsack problem the method. Share to everyone for your reference. A specific analysis such as ...

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