An example of greedy algorithm implemented by PHP

  • 2021-08-10 06:53:35
  • OfStack

This paper describes the greedy algorithm implemented by PHP. Share it for your reference, as follows:

Background introduction: Greedy algorithm and data structure knowledge base algorithm can be said to be the closest algorithm to our life. People are always greedy, so the design of this algorithm is very consistent with human nature. The reason for this is that people will use greedy algorithms to solve problems intentionally or unintentionally in their lives. The most common thing is change. Everyone hasn't learned how to change, but when there is enough money in all denominations, everyone will find the same combination to make up the money they need. In fact, greedy algorithm is at work.

Design ideas: Greedy design ideas can be understood from two aspects, namely intuitive and mathematical. To understand greedy algorithm intuitively is to solve the problem with the fastest method. Here "fast" is the main goal, such as the above change example, if you are looking for change for 6.6 yuan. First of all, you should take a 5 yuan one, because it can make your money grow the fastest. If RMB has 6 yuan denomination, you will definitely choose 6 yuan instead of taking two other pieces to make up 6 yuan; Mathematically, greedy algorithm takes the current optimal solution as the goal when making judgments, which is similar to the steepest descent method in optimization. The advantage of this method is that the problem solving speed is extremely fast, which can be completed basically once.

Algorithm defects: Just as people can't be too greedy, greedy algorithm itself has fatal defects, which makes its application background receive a lot of restrictions. Because the algorithm takes the local optimal solution, it does not consider the future problems. This is just like a selfish person. Although you can get some benefits in a short time, it is difficult to achieve great achievements in the long run. Of course, the society is very complicated, and some people may go on selfish and live well. This is reflected in the algorithm, that is, in some cases (specifically mentioned below), greedy algorithm can get the optimal solution, which is of course a good thing for algorithm design.


/*
*  Greedy algorithm 
* $arr   array   Processing arrays 
* $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;
}

For more readers interested in PHP related content, please check the topics on this site: "PHP Data Structure and Algorithm Tutorial", "PHP Basic Grammar Introduction Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php String (string) Usage Summary" and "php Programming Algorithm Summary"

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


Related articles: