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\Distance; |
10
|
|
|
|
11
|
|
|
class FloydWarshall implements AlgorithmShortestDistance |
12
|
|
|
{ |
13
|
|
|
/* |
14
|
|
|
* For more info see |
15
|
|
|
* http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
private |
19
|
|
|
$distance, |
|
|
|
|
20
|
|
|
$distanceGraph, |
21
|
|
|
$grid; |
22
|
|
|
|
23
|
|
|
public function __construct(Distance $distance) |
24
|
|
|
{ |
25
|
|
|
$this->distance = $distance; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setGrid(NodeGrid $grid) |
29
|
|
|
{ |
30
|
|
|
$this->grid = $grid; |
31
|
|
|
$this->distanceGraph = new NodeGraph($grid->buildWalkableNodesList()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function computeLength(Node $source, Node $target) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
if(! $this->grid instanceof NodeGrid) |
37
|
|
|
{ |
38
|
|
|
throw new \RuntimeException('Invalid Grid'); |
39
|
|
|
} |
40
|
|
|
if(! $this->distanceGraph instanceof NodeGraph) |
41
|
|
|
{ |
42
|
|
|
throw new \RuntimeException('Invalid Graph'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$edges = $this->distanceGraph->getEdgesFrom($source); |
46
|
|
|
if(empty($edges)) |
47
|
|
|
{ |
48
|
|
|
$this->computeDistanceGraph(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->distanceGraph->getEdgeBetween($source, $target); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function computeDistanceGraph() |
55
|
|
|
{ |
56
|
|
|
$nbNodes = $this->grid->getNodesNb(); |
57
|
|
View Code Duplication |
for($i = 0; $i < $nbNodes; $i++) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$iNode = $this->grid->getNodeNumber($i); |
60
|
|
|
$neighbors = $this->grid->getWalkableNeighbors($iNode); |
61
|
|
|
foreach($neighbors as $neighbor) |
62
|
|
|
{ |
63
|
|
|
$alternativeDistance = $this->distance->compute($iNode, $neighbor); |
64
|
|
|
if($alternativeDistance < $this->distanceGraph->getEdgeBetween($iNode, $neighbor)) |
65
|
|
|
{ |
66
|
|
|
$this->distanceGraph->createEdgeBetween($iNode, $neighbor, $alternativeDistance); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
for($k = 0; $k < $nbNodes; $k++) |
72
|
|
|
{ |
73
|
|
|
$kNode = $this->grid->getNodeNumber($k); |
74
|
|
|
if(! $kNode->isWalkable()) |
75
|
|
|
{ |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
for($i = 0; $i < $nbNodes; $i++) |
79
|
|
|
{ |
80
|
|
|
$iNode = $this->grid->getNodeNumber($i); |
81
|
|
|
if(! $iNode->isWalkable()) |
82
|
|
|
{ |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
View Code Duplication |
for($j = 0; $j < $nbNodes; $j++) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$jNode = $this->grid->getNodeNumber($j); |
88
|
|
|
if(! $jNode->isWalkable()) |
89
|
|
|
{ |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
$alternativeDistance = $this->distanceGraph->getEdgeBetween($iNode, $kNode) + $this->distanceGraph->getEdgeBetween($kNode, $jNode); |
93
|
|
|
if($alternativeDistance < $this->distanceGraph->getEdgeBetween($iNode, $jNode)) |
94
|
|
|
{ |
95
|
|
|
$this->distanceGraph->createEdgeBetween($iNode, $jNode, $alternativeDistance); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.