Permalink
Browse files

fixed bug in pDijkstra where neighbours with same distance apart will…

… cause problem to the path finding.

potential calculation code also improved from two iterations into one iteration.

Signed-off-by: Sam-Mauris Yong <mauris@hotmail.sg>
  • Loading branch information...
1 parent 6715baa commit 090f8000c16367851622245c5b72addac28a0a13 @mauris mauris committed Jun 12, 2012
Showing with 12 additions and 17 deletions.
  1. +12 −17 pDijkstra.php
View
@@ -95,7 +95,7 @@ public function find($start = null, $end = null){
$this->end($end);
}
- $this->calculatePotentials($this->startVertex, $this->endVertex);
+ $this->calculatePotentials($this->startVertex);
$aPath = array();
$vertex = $this->endVertex;
@@ -118,18 +118,23 @@ public function find($start = null, $end = null){
/**
* Calculate the potentials to every other vertex from the start vertex
* @param IVertex $vertex The vertex to calculate potentials
- * @param IVertex $end The destination vertex
* @since 1.0-sofia
*/
- private function calculatePotentials($vertex, $end){
+ private function calculatePotentials($vertex){
$connections = $vertex->connections();
- $sorted = array_flip($connections->toArray());
- krsort($sorted);
- foreach($connections as $id => $cost){
+ $sorted = $connections->toArray();
+ arsort($sorted);
+
+ // mark the vector as you have already passed by
+ $vertex->mark();
+
+ foreach($sorted as $id => $cost){
/* @var $connection IVertex */
$connection = $this->graph->get($id);
$connection->setPotential($vertex->potential() + $cost, $vertex);
-
+ if(!$connection->passed()){
+ $this->calculatePotentials($connection);
+ }
foreach($this->paths as $path){
/* @var $path pList */
$last = $path->last();
@@ -140,16 +145,6 @@ private function calculatePotentials($vertex, $end){
}
}
}
-
- // mark the vector as you have already passed by
- $vertex->mark();
-
- foreach($sorted as $id){
- $nextVertex = $this->graph->get($id);
- if(!$nextVertex->passed()){
- $this->calculatePotentials($nextVertex, $end);
- }
- }
}
}

0 comments on commit 090f800

Please sign in to comment.