Examples of greedy algorithms implemented by PHP and php Algorithms

Source: Internet
Author: User

Examples of greedy algorithms implemented by PHP and php Algorithms

This article describes the greedy algorithm implemented by PHP. We will share this with you for your reference. The details are as follows:

Background:Greedy algorithms and data structure knowledge base algorithms can be said to be the closest algorithms to our lives. People are always greedy, so the design of these algorithms is very human. This is because people intentionally or unintentionally use greedy algorithms to solve problems in their lives. The most common thing is to find the change. Everyone has never learned how to find the change, but when all the denominations have enough money, everyone will find the same combination to make up the required money. In fact, greedy algorithms are at work.

Design Concept:The design idea of greedy method can be understood from two aspects: intuitive and mathematical. Intuitively, greedy algorithms are the fastest way to solve problems. In this case, "fast" is the main goal. For example, if you look for a change, assume that the change you are looking for is 6.6 yuan. First, you need to get a 5 yuan, because it can make your money grow fastest. If the RMB has a denomination of 6 yuan, you will certainly choose 6 yuan instead of 6 yuan with two others; in mathematics, greedy algorithms aim at the current optimal solution when making judgments, similar to the shortest descent method in optimization. The advantage of this method is that the problem solving speed is extremely fast, and it can be basically completed through a calendar.

Algorithm defects:Just as you can't be greedy, greedy algorithms have fatal defects, which puts a lot of restrictions on their application background. Because the algorithm is the local optimal solution, it does not consider future issues. This is like a selfish person. Although some benefits can be gained in a short time, it is difficult to achieve great success in the long term. Of course, the society is very complex. Maybe someone will keep selfish and live a good life. This is reflected in the algorithm in some cases (as mentioned below). Greedy algorithms can obtain the optimal solution, which is of course a good thing for algorithm design.

/** Greedy algorithm * $ arr array Processing array * $ volume int box capacity */function greedy ($ arr, $ volume) {$ box = array (); $ boxNum = 0; $ num = count ($ arr); for ($ I = 0; $ I <$ num; $ I ++) {$ boxCode = true; for ($ j = 0; $ j <$ boxNum; $ j ++) {if ($ arr [$ I] + $ box [$ j] ['V'] <= $ volume) {$ box [$ j] ['V'] + = $ arr [$ I]; $ box [$ j] ['K'] [] = $ I; $ boxCode = false; break; }}if ($ boxCode) {$ box [$ boxNum] ['V'] = $ arr [$ I]; $ box [$ boxNum] ['K'] [] = $ I; $ boxNum ++;} return $ box ;}

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.