Advertisement
cahyadsn

Djikstra Algorithm PHP

May 16th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.93 KB | None | 0 0
  1. <?PHP
  2. class Dijkstra {
  3.  
  4.     var $visited = array();
  5.     var $distance = array();
  6.     var $previousNode = array();
  7.     var $startnode =null;
  8.     var $map = array();
  9.     var $infiniteDistance = 0;
  10.     var $numberOfNodes = 0;
  11.     var $bestPath = 0;
  12.     var $matrixWidth = 0;
  13.  
  14.     function Dijkstra(&$ourMap, $infiniteDistance) {
  15.         $this -> infiniteDistance = $infiniteDistance;
  16.         $this -> map = &$ourMap;
  17.         $this -> numberOfNodes = count($ourMap);
  18.         $this -> bestPath = 0;
  19.     }
  20.  
  21.     function findShortestPath($start,$to = null) {
  22.         $this -> startnode = $start;
  23.         for ($i=0;$i<$this -> numberOfNodes;$i++) {
  24.             if ($i == $this -> startnode) {
  25.                 $this -> visited[$i] = true;
  26.                 $this -> distance[$i] = 0;
  27.             } else {
  28.                 $this -> visited[$i] = false;
  29.                 $this -> distance[$i] = isset($this -> map[$this -> startnode][$i])
  30.                     ? $this -> map[$this -> startnode][$i]
  31.                     : $this -> infiniteDistance;
  32.             }
  33.             $this -> previousNode[$i] = $this -> startnode;
  34.         }
  35.  
  36.         $maxTries = $this -> numberOfNodes;
  37.         $tries = 0;
  38.         while (in_array(false,$this -> visited,true) && $tries <= $maxTries) {         
  39.             $this -> bestPath = $this->findBestPath($this->distance,array_keys($this -> visited,false,true));
  40.             if($to !== null && $this -> bestPath === $to) {
  41.                 break;
  42.             }
  43.             $this -> updateDistanceAndPrevious($this -> bestPath);         
  44.             $this -> visited[$this -> bestPath] = true;
  45.             $tries++;
  46.         }
  47.     }
  48.  
  49.     function findBestPath($ourDistance, $ourNodesLeft) {
  50.         $bestPath = $this -> infiniteDistance;
  51.         $bestNode = 0;
  52.         for ($i = 0,$m=count($ourNodesLeft); $i < $m; $i++) {
  53.             if($ourDistance[$ourNodesLeft[$i]] < $bestPath) {
  54.                 $bestPath = $ourDistance[$ourNodesLeft[$i]];
  55.                 $bestNode = $ourNodesLeft[$i];
  56.             }
  57.         }
  58.         return $bestNode;
  59.     }
  60.  
  61.     function updateDistanceAndPrevious($obp) {     
  62.         for ($i=0;$i<$this -> numberOfNodes;$i++) {
  63.             if(     (isset($this->map[$obp][$i]))
  64.                 &&  (!($this->map[$obp][$i] == $this->infiniteDistance) || ($this->map[$obp][$i] == 0 ))   
  65.                 &&  (($this->distance[$obp] + $this->map[$obp][$i]) < $this -> distance[$i])
  66.             )  
  67.             {
  68.                     $this -> distance[$i] = $this -> distance[$obp] + $this -> map[$obp][$i];
  69.                     $this -> previousNode[$i] = $obp;
  70.             }
  71.         }
  72.     }
  73.  
  74.     function printMap(&$map) {
  75.         $placeholder = ' %' . strlen($this -> infiniteDistance) .'d';
  76.         $foo = '';
  77.         for($i=0,$im=count($map);$i<$im;$i++) {
  78.             for ($k=0,$m=$im;$k<$m;$k++) {
  79.                 $foo.= sprintf($placeholder, isset($map[$i][$k]) ? $map[$i][$k] : $this -> infiniteDistance);
  80.             }
  81.             $foo.= "\n";
  82.         }
  83.         return $foo;
  84.     }
  85.  
  86.     function getResults($to = null) {
  87.         $ourShortestPath = array();
  88.         $foo = '';
  89.         for ($i = 0; $i < $this -> numberOfNodes; $i++) {
  90.             if($to !== null && $to !== $i) {
  91.                 continue;
  92.             }
  93.             $ourShortestPath[$i] = array();
  94.             $endNode = null;
  95.             $currNode = $i;
  96.             $ourShortestPath[$i][] = $i;
  97.             while ($endNode === null || $endNode != $this -> startnode) {
  98.                 $ourShortestPath[$i][] = $this -> previousNode[$currNode];
  99.                 $endNode = $this -> previousNode[$currNode];
  100.                 $currNode = $this -> previousNode[$currNode];
  101.             }
  102.             $ourShortestPath[$i] = array_reverse($ourShortestPath[$i]);
  103.             if ($to === null || $to === $i) {
  104.             if($this -> distance[$i] >= $this -> infiniteDistance) {
  105.                 $foo .= sprintf("no route from %d to %d. \n",$this -> startnode,$i);
  106.             } else {
  107.                 $foo .= sprintf('%d => %d = %d [%d]: (%s).'."\n" ,
  108.                         $this -> startnode,$i,$this -> distance[$i],
  109.                         count($ourShortestPath[$i]),
  110.                         implode('-',$ourShortestPath[$i]));
  111.             }
  112.             $foo .= str_repeat('-',20) . "\n";
  113.                 if ($to === $i) {
  114.                     break;
  115.                 }
  116.             }
  117.         }
  118.         return $foo;
  119.     }
  120. } // end class
  121. ?>
  122.  
  123. <?php
  124.  
  125. // I is the infinite distance.
  126. define('I',1000);
  127.  
  128. // Size of the matrix
  129. $matrixWidth = 20;
  130.  
  131. // $points is an array in the following format: (router1,router2,distance-between-them)
  132. $points = array(
  133.     array(0,1,4),
  134.     array(0,2,I),
  135.     array(1,2,5),
  136.     array(1,3,5),
  137.     array(2,3,5),
  138.     array(3,4,5),
  139.     array(4,5,5),
  140.     array(4,5,5),
  141.     array(2,10,30),
  142.     array(2,11,40),
  143.     array(5,19,20),
  144.     array(10,11,20),
  145.     array(12,13,20),
  146. );
  147.  
  148. $ourMap = array();
  149.  
  150.  
  151. // Read in the points and push them into the map
  152.  
  153. for ($i=0,$m=count($points); $i<$m; $i++) {
  154.     $x = $points[$i][0];
  155.     $y = $points[$i][1];
  156.     $c = $points[$i][2];
  157.     $ourMap[$x][$y] = $c;
  158.     $ourMap[$y][$x] = $c;
  159. }
  160.  
  161. // ensure that the distance from a node to itself is always zero
  162. // Purists may want to edit this bit out.
  163.  
  164. for ($i=0; $i < $matrixWidth; $i++) {
  165.     for ($k=0; $k < $matrixWidth; $k++) {
  166.         if ($i == $k) $ourMap[$i][$k] = 0;
  167.     }
  168. }
  169.  
  170.  
  171. // initialize the algorithm class
  172. $dijkstra = new Dijkstra($ourMap, I,$matrixWidth);
  173.  
  174. // $dijkstra->findShortestPath(0,13); to find only path from field 0 to field 13...
  175. $dijkstra->findShortestPath(0);
  176.  
  177. // Display the results
  178.  
  179. echo '<pre>';
  180. echo "the map looks like:\n\n";
  181. echo $dijkstra -> printMap($ourMap);
  182. echo "\n\nthe shortest paths from point 0:\n";
  183. echo $dijkstra -> getResults();
  184. echo '</pre>';
  185.  
  186. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement