|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Component\Meta; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @since Class available since Release 2.1.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
class Dijkstra |
|
9
|
|
|
{ |
|
10
|
|
|
private $map; |
|
11
|
|
|
|
|
12
|
|
|
private $distance; |
|
13
|
|
|
|
|
14
|
|
|
private $prev; |
|
15
|
|
|
|
|
16
|
|
|
private $visited; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct($map) |
|
19
|
|
|
{ |
|
20
|
|
|
foreach ($map as $nodeName => $metaData) { |
|
21
|
|
|
foreach ($metaData['relations'] as $itemKey => $itemValue) { |
|
22
|
|
|
$this->map[$nodeName][$itemValue] = 1; |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
private function processQueue(array $excluded) |
|
28
|
|
|
{ |
|
29
|
|
|
$node = array_search(min($this->visited), $this->visited); |
|
30
|
|
|
|
|
31
|
|
|
if (!empty($this->map[$node]) && !in_array($node, $excluded)) { |
|
32
|
|
|
foreach ($this->map[$node] as $neighbor => $cost) { |
|
33
|
|
|
if (isset($this->distance[$neighbor])) { |
|
34
|
|
|
if ($this->distance[$node] + $cost < $this->distance[$neighbor]) { |
|
35
|
|
|
$this->distance[$neighbor] = $this->distance[$node] + $cost; |
|
36
|
|
|
$this->prev[$neighbor] = [$node]; |
|
37
|
|
|
$this->visited[$neighbor] = $this->distance[$neighbor]; |
|
38
|
|
|
} elseif ($this->distance[$node] + $cost === $this->distance[$neighbor]) { |
|
39
|
|
|
$this->prev[$neighbor][] = $node; |
|
40
|
|
|
$this->visited[$neighbor] = $this->distance[$neighbor]; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
unset($this->visited[$node]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function extractPaths($target) |
|
50
|
|
|
{ |
|
51
|
|
|
$paths = [[$target]]; |
|
52
|
|
|
while (current($paths) !== false) { |
|
53
|
|
|
$key = key($paths); |
|
54
|
|
|
$path = current($paths); |
|
55
|
|
|
next($paths); |
|
56
|
|
|
if (!empty($this->prev[$path[0]])) { |
|
57
|
|
|
foreach ($this->prev[$path[0]] as $prev) { |
|
58
|
|
|
$copy = $path; |
|
59
|
|
|
array_unshift($copy, $prev); |
|
60
|
|
|
$paths[] = $copy; |
|
61
|
|
|
} |
|
62
|
|
|
unset($paths[$key]); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
return array_values($paths); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function shortestPaths($source, $target, array $excluded = array()) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->distance = array_fill_keys(array_keys($this->map), INF); |
|
71
|
|
|
$this->distance[$source] = 0; |
|
72
|
|
|
$this->prev = array_fill_keys(array_keys($this->map), []); |
|
73
|
|
|
$this->visited = [$source => 0]; |
|
74
|
|
|
|
|
75
|
|
|
while (!empty($this->visited)) { |
|
76
|
|
|
$this->processQueue($excluded); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($source === $target) { |
|
80
|
|
|
return [[$source]]; |
|
81
|
|
|
} elseif (empty($this->prev[$target])) { |
|
82
|
|
|
return []; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this->extractPaths($target); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|