|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Letournel\PathFinder\Algorithms\ShortestDistance; |
|
4
|
|
|
|
|
5
|
|
|
use Letournel\PathFinder\AlgorithmShortestDistance; |
|
6
|
|
|
use Letournel\PathFinder\Core\Node; |
|
7
|
|
|
use Letournel\PathFinder\Core\NodeGraph; |
|
8
|
|
|
use Letournel\PathFinder\Core\NodeGrid; |
|
9
|
|
|
use Letournel\PathFinder\Core\NodePriorityQueueMin; |
|
10
|
|
|
use Letournel\PathFinder\Distance; |
|
11
|
|
|
|
|
12
|
|
|
class Dijkstra implements AlgorithmShortestDistance |
|
13
|
|
|
{ |
|
14
|
|
|
/* |
|
15
|
|
|
* For more info see |
|
16
|
|
|
* http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
private |
|
20
|
|
|
$distance, |
|
|
|
|
|
|
21
|
|
|
$distanceGraph, |
|
22
|
|
|
$grid; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(Distance $distance) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->distance = $distance; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function setGrid(NodeGrid $grid) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->grid = $grid; |
|
32
|
|
|
$this->distanceGraph = new NodeGraph($grid->buildWalkableNodesList()); |
|
33
|
|
|
$this->distanceGraph->setSymetricMode(false); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
View Code Duplication |
public function computeLength(Node $source, Node $target) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
if(! $this->grid instanceof NodeGrid) |
|
39
|
|
|
{ |
|
40
|
|
|
throw new \RuntimeException('Invalid Grid'); |
|
41
|
|
|
} |
|
42
|
|
|
if(! $this->distanceGraph instanceof NodeGraph) |
|
43
|
|
|
{ |
|
44
|
|
|
throw new \RuntimeException('Invalid Graph'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$edges = $this->distanceGraph->getEdgesFrom($source); |
|
48
|
|
|
if(empty($edges)) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->computeDistanceGraph(array($source)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->distanceGraph->getEdgeBetween($source, $target); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function computeDistanceGraph(array $nodesList) |
|
57
|
|
|
{ |
|
58
|
|
|
foreach($nodesList as $source) |
|
59
|
|
|
{ |
|
60
|
|
|
$priorityQueue = new NodePriorityQueueMin(); |
|
61
|
|
|
$priorityQueue->insert($source, 0); |
|
62
|
|
|
|
|
63
|
|
|
while(! $priorityQueue->isEmpty()) |
|
64
|
|
|
{ |
|
65
|
|
|
$node = $priorityQueue->extract(); |
|
66
|
|
|
|
|
67
|
|
|
$neighbors = $this->grid->getWalkableNeighbors($node); |
|
68
|
|
|
foreach($neighbors as $neighbor) |
|
69
|
|
|
{ |
|
70
|
|
|
$alternativeDistance = $this->distanceGraph->getEdgeBetween($source, $node) + $this->distance->compute($node, $neighbor); |
|
71
|
|
|
if(! $this->distanceGraph->existsEdgeBetween($source, $neighbor) || $alternativeDistance < $this->distanceGraph->getEdgeBetween($source, $neighbor)) |
|
72
|
|
|
{ |
|
73
|
|
|
$priorityQueue->insert($neighbor, $alternativeDistance); |
|
74
|
|
|
$this->distanceGraph->createEdgeBetween($source, $neighbor, $alternativeDistance); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.