1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fisharebest\Algorithm; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Greg Roach <[email protected]> |
7
|
|
|
* @copyright (c) 2021 Greg Roach <[email protected]> |
8
|
|
|
* @license GPL-3.0+ |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU General Public License as published by |
12
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
13
|
|
|
* (at your option) any later version. |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Dijkstra - Use Dijkstra's algorithm to calculate the shortest path |
24
|
|
|
* through a weighted, directed graph. |
25
|
|
|
*/ |
26
|
|
|
class Dijkstra |
27
|
|
|
{ |
28
|
|
|
/** @var integer[][] The graph, where $graph[node1][node2]=cost */ |
29
|
|
|
protected $graph; |
30
|
|
|
|
31
|
|
|
/** @var int[] Distances from the source node to each other node */ |
32
|
|
|
protected $distance; |
33
|
|
|
|
34
|
|
|
/** @var string[][] The previous node(s) in the path to the current node */ |
35
|
|
|
protected $previous; |
36
|
|
|
|
37
|
|
|
/** @var int[] Nodes which have yet to be processed */ |
38
|
|
|
protected $queue; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param integer[][] $graph |
42
|
|
|
*/ |
43
|
|
|
public function __construct($graph) |
44
|
|
|
{ |
45
|
|
|
$this->graph = $graph; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Process the next (i.e. closest) entry in the queue. |
50
|
|
|
* |
51
|
|
|
* @param string[] $exclude A list of nodes to exclude - for calculating next-shortest paths. |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
protected function processNextNodeInQueue(array $exclude) |
56
|
|
|
{ |
57
|
|
|
// Process the closest vertex |
58
|
|
|
$closest = array_search(min($this->queue), $this->queue); |
59
|
|
|
if (!empty($this->graph[$closest]) && !in_array($closest, $exclude)) { |
60
|
|
|
foreach ($this->graph[$closest] as $neighbor => $cost) { |
61
|
|
|
if (isset($this->distance[$neighbor])) { |
62
|
|
|
if ($this->distance[$closest] + $cost < $this->distance[$neighbor]) { |
63
|
|
|
// A shorter path was found |
64
|
|
|
$this->distance[$neighbor] = $this->distance[$closest] + $cost; |
65
|
|
|
$this->previous[$neighbor] = array($closest); |
66
|
|
|
$this->queue[$neighbor] = $this->distance[$neighbor]; |
67
|
|
|
} elseif ($this->distance[$closest] + $cost === $this->distance[$neighbor]) { |
68
|
|
|
// An equally short path was found |
69
|
|
|
$this->previous[$neighbor][] = $closest; |
70
|
|
|
$this->queue[$neighbor] = $this->distance[$neighbor]; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
unset($this->queue[$closest]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Extract all the paths from $source to $target as arrays of nodes. |
80
|
|
|
* |
81
|
|
|
* @param string $target The starting node (working backwards) |
82
|
|
|
* |
83
|
|
|
* @return string[][] One or more shortest paths, each represented by a list of nodes |
84
|
|
|
*/ |
85
|
|
|
protected function extractPaths($target) |
86
|
|
|
{ |
87
|
|
|
$paths = array(array($target)); |
88
|
|
|
|
89
|
|
|
for ($key = 0; isset($paths[$key]); ++$key) { |
90
|
|
|
$path = $paths[$key]; |
91
|
|
|
|
92
|
|
|
if (!empty($this->previous[$path[0]])) { |
93
|
|
|
foreach ($this->previous[$path[0]] as $previous) { |
94
|
|
|
$copy = $path; |
95
|
|
|
array_unshift($copy, $previous); |
96
|
|
|
$paths[] = $copy; |
97
|
|
|
} |
98
|
|
|
unset($paths[$key]); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return array_values($paths); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Calculate the shortest path through a a graph, from $source to $target. |
107
|
|
|
* |
108
|
|
|
* @param string $source The starting node |
109
|
|
|
* @param string $target The ending node |
110
|
|
|
* @param string[] $exclude A list of nodes to exclude - for calculating next-shortest paths. |
111
|
|
|
* |
112
|
|
|
* @return string[][] Zero or more shortest paths, each represented by a list of nodes |
113
|
|
|
*/ |
114
|
|
|
public function shortestPaths($source, $target, array $exclude = array()) |
115
|
|
|
{ |
116
|
|
|
// The shortest distance to all nodes starts with infinity... |
117
|
|
|
$this->distance = array_fill_keys(array_keys($this->graph), INF); |
118
|
|
|
// ...except the start node |
119
|
|
|
$this->distance[$source] = 0; |
120
|
|
|
|
121
|
|
|
// The previously visited nodes |
122
|
|
|
$this->previous = array_fill_keys(array_keys($this->graph), array()); |
123
|
|
|
|
124
|
|
|
// Process all nodes in order |
125
|
|
|
$this->queue = array($source => 0); |
126
|
|
|
while (!empty($this->queue)) { |
127
|
|
|
$this->processNextNodeInQueue($exclude); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($source === $target) { |
131
|
|
|
// A null path |
132
|
|
|
return array(array($source)); |
133
|
|
|
} elseif (empty($this->previous[$target])) { |
134
|
|
|
// No path between $source and $target |
135
|
|
|
return array(); |
136
|
|
|
} else { |
137
|
|
|
// One or more paths were found between $source and $target |
138
|
|
|
return $this->extractPaths($target); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|