Notes algorithms - greedy algorithm

Greedy algorithm: to ensure that every step is optimal, the end result is certainly the best (to simplify a lot of problems, you do not need to have a bigger picture)

Scene: demand limit value set (up, shortest, cheapest, and so on and so on ...)

 

(1) Organization of the session

  Description: more meetings within a limited time, any time two conferences can not have a conflict. The figure is the meeting details:

   Ideas: The next meeting is to take the earliest end time (better shortest duration of the conference, which is equivalent to dynamically change the start time)

   Code:

. 1 <? PHP
 2  $ Meetings = [
 . 3      '. 1' => [. 3,. 6], '2' => [. 1,. 4], '. 3' => [. 5,. 7], '. 4' => [2 5], '5' => [5, 9],
 4      '6' => [3, 8], '7' => [8, 11], '8' => [6, 10], ' 9 '=> [8, 12],' 10 '=> [12, 14],
 5      ];
 . 6  
. 7  // The end of the time-ordered 
8  uasort ( $ Meetings , function ( $ P1 , $ P2 ) {
 9      IF ( P1 $ [. 1] == $ P2 [. 1]) return 0 ;
10     return $p1[1] > $p2[1] ? 1 : -1;
11 });
12 
13 //取不冲突
14 $last = null;
15 foreach ($meetings as $k => $meet) {
16     if (($last === null) || ($last !== null && $meet[0] >= $last))
17         list($res[], $last) = [$k, $meet[1]];   
18 }
View Code

 

(2) the shortest path

  Description: There are five scenic spot, seeking a first point to all other points of the shortest distance. Map as follows:

   Idea: in order to find the nearest point. Substantially as follows:

     Currently in 1: 2 that is from 1-2; 1-3 to 5 away. 2 and 3 below to (because of the two is 1 2 and 3)

     Then go to 2: 6 that is from 2-4; 2-3 run is 1; obtained from the above new binding: 4 from 1-3 (1-2-3, smaller than the upper 5 to herein refresh) 1-4 from 6 (the branch to go below 3 and 4)

       Then go to 3: 7 as that from 3-4; 3-5 run is 1; obtained from the above new binding: 1-4 or away from 4,1-5 5 (1-2-3-5) (this branch go below 4 and 5)

      And so on...

  Code:

 

. 1 <? PHP
 2  $ Map = [                                                                         // here represented by a matrix map 
. 3      [ null , 2,. 5, null , null ],
 . 4      [ null , null , 2,. 6, null ],
 . 5      [ null , null , null , . 7,. 1],
 . 6      [ null , null , 2, null ,. 4],
 . 7      [ null , null ,null , null , null ],
 . 8  ];
 . 9  $ position = $ Start = 0 ;
 10  $ been = [];      // visited points 
. 11  
12 is  function minPath ( $ position )
 13 is  {
 14      Global  $ Map , $ Start , been $ ;
 15      static  $ RES = [];                                                            // results 
16      $ been [] = $ position;                                                         // record through the point 
. 17      $ Next = [];
 18 is      the foreach ( $ Map [ $ position ] AS  $ K => $ V ) {
 . 19          IF ( $ V ! == null ) {
 20 is              IF ! ( Isset ( RES $ [ $ Start . $ K ]) || ( $ RES [ $ Start . $ K ]> $ RES [ $ Start . $ position] + $ V )) {
 21 is                  $ RES [ $ Start . $ K ] = $ RES [ $ Start . $ Position ] + $ V ;               // record the shortest distance 
22 is                  IF ( the in_array ( $ K , $ been )) $ Next [] = $ K ;                           // here will be explained later on 
23 is              }
 24              ! the in_array ( $ K , $ been ) && $ Next [] = $ K;                               // record go the next point 
25          }
 26 is      } 
 27      
28      the foreach ( $ Next  AS  $ V ) {                                                      // Next find the next point, which is also the end of the recursive determination (next blank) 
29          minPath ( $ V );
 30      }
 31 is      return  $ RES ;
 32  }
 33 is  $ RES = minPath ( $ position );
View Code

   Added: Code Line 22 Notes:

    At first I did not find the problem, perform the above case also lacks the problem. However, following the implementation of this case there is a problem (if there is no line 22). Reach the first two points when the distance is also determined as 8 1-2, 1-4 9 so calculated. And so we got to the third point, the distance to the nearest 1-2 3, but it was too late. The first two points have come once, not leavin '. That will not be updated from 1-4. Therefore, when it is detected when a more precise distance (1-3-2 to 1-2 than near), that the point (second point) from the recording medium through the array of point removed, to go back in time.

    Or you can manually traverse at the results, it will require precise accuracy, for example, when found to be nearly 1-3-2 than 1-2, (all out of the first two points) 1-2-4 is updated. But here to record the path (not only recorded 1-4 = 9, to record 1-2-4 = 9), performance should be a lot better than the last.

  

 

 

  

Guess you like

Origin www.cnblogs.com/wangjianheng/p/11697363.html