-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
Copy pathDijkstras.php
43 lines (40 loc) · 1.63 KB
/
Dijkstras.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/**
* The Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph.
* (https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm).
*
* @author Michał Żarnecki https://github.com/rzarno
* @param array $verticesNames An array of vertices names
* @param GraphEdge[] $edges An array of edges
* @param string $start The starting vertex
* @return array An array of shortest paths from $start to all other vertices
*/
function dijkstras(array $verticesNames, array $edges, string $start): array
{
$vertices = array_combine($verticesNames, array_fill(0, count($verticesNames), PHP_INT_MAX));
$visitedNodes = [];
$nextVertex = $start;
$vertices[$start] = 0;
while (count($visitedNodes) < count($verticesNames)) { //continue until all nodes are visited
foreach ($edges as $edge) {
if ($edge->start == $nextVertex) { //consider only nodes connected to current one
$vertices[$edge->end] = min($vertices[$edge->end], $vertices[$nextVertex] + $edge->weight);
}
}
// find vertex with current lowest value to be starting point in next iteration
$minVertexName = null;
$minVertexWeight = PHP_INT_MAX;
foreach ($vertices as $name => $weight) {
if (in_array($name, $visitedNodes) || $name == $nextVertex) {
continue;
}
if ($weight <= $minVertexWeight) {
$minVertexName = $name;
$minVertexWeight = $weight;
}
}
$visitedNodes[] = $nextVertex;
$nextVertex = $minVertexName;
}
return $vertices;
}