Case study of PHP greedy algorithm for solving the 0-1 knapsack problem

Source: Internet
Author: User
This article mainly introduces the PHP greedy algorithm to solve the 0-1 knapsack problem. The example analyzes the principles of the greedy algorithm and the implementation skills of the knapsack problem. For more information, see

This article mainly introduces the PHP greedy algorithm to solve the 0-1 knapsack problem. The example analyzes the principles of the greedy algorithm and the implementation skills of the knapsack problem. For more information, see

This article describes how to solve the 0-1 knapsack problem by using the PHP greedy algorithm. Share it with you for your reference. The specific analysis is as follows:

The greedy algorithm solves the problem of 0-1 backpacks. The global optimal solution is obtained through the local optimal solution! This solution is more flexible than dynamic planning!

// 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 (); $ price = rand ); $ x [$ I] = new tanxin ($ weight, $ price);} // output result function display ($ x) {$ len = count ($ x ); foreach ($ x as $ val) {echo $ val-> weight, '', $ val-> price; echo'
';}} // Sort the function tsort (& $ x) {$ len = count ($ x); 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 ); // display ($ x) by non-incrementing order; // The echo '0-1 backpack's optimal solution is: '; echo tanxin ($ x );

I hope this article will help you with php programming.

,

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.