1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graphp\Alg\Shortestpath; |
4
|
|
|
|
5
|
|
|
use Graphp\GraphInterface; |
6
|
|
|
use Graphp\GraphPathInterface; |
7
|
|
|
use Graphp\GraphUtils; |
8
|
|
|
use Graphp\Vertex\VertexInterface; |
9
|
|
|
use Graphp\Path\GraphWalk; |
10
|
|
|
use Graphp\Alg\Interfaces\SingleSourcePathsInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ListSingleSourcePaths |
14
|
|
|
* |
15
|
|
|
* @package Graphp\Alg\Shortestpath |
16
|
|
|
*/ |
17
|
|
|
class ListSingleSourcePaths implements SingleSourcePathsInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The graph |
21
|
|
|
* |
22
|
|
|
* @var GraphInterface |
23
|
|
|
*/ |
24
|
|
|
protected $graph; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The source vertex |
28
|
|
|
* |
29
|
|
|
* @var VertexInterface |
30
|
|
|
*/ |
31
|
|
|
protected $source; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* One path per vertex |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $paths = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Construct a new instance |
42
|
|
|
* |
43
|
|
|
* @param GraphInterface $graph - the graph |
44
|
|
|
* @param VertexInterface $source - the source vertex |
45
|
|
|
* @param array $paths - one path per target vertex |
46
|
|
|
*/ |
47
|
|
|
public function __construct(GraphInterface $graph, VertexInterface $source, array $paths = []) |
48
|
|
|
{ |
49
|
|
|
$this->graph = $graph; |
50
|
|
|
$this->source = $source; |
51
|
|
|
$this->paths = $paths; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the graph over which this set of paths is defined |
56
|
|
|
* |
57
|
|
|
* @return GraphInterface |
58
|
|
|
*/ |
59
|
|
|
public function getGraph(): GraphInterface |
60
|
|
|
{ |
61
|
|
|
return $this->graph; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the single source vertex |
66
|
|
|
* |
67
|
|
|
* @return GraphInterface |
68
|
|
|
*/ |
69
|
|
|
public function getSourceVertex(): VertexInterface |
70
|
|
|
{ |
71
|
|
|
return $this->source; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the weight of the path from the source vertex to the target vertex |
76
|
|
|
* |
77
|
|
|
* @param VertexInterface $targetVertex - the target vertex |
78
|
|
|
* |
79
|
|
|
* @return float |
80
|
|
|
*/ |
81
|
|
|
public function getWeight(VertexInterface $targetVertex): float |
82
|
|
|
{ |
83
|
|
|
$id = $targetVertex->getHash(); |
84
|
|
|
if (!array_key_exists($id, $this->paths)) { |
85
|
|
|
if ($this->source->equals($targetVertex)) { |
86
|
|
|
return 0.0; |
87
|
|
|
} |
88
|
|
|
return INF; |
89
|
|
|
} |
90
|
|
|
return $this->paths[$id]->getWeight(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the path from the source vertex to the target vertex |
95
|
|
|
* |
96
|
|
|
* @param VertexInterface $targetVertex - the target vertex |
97
|
|
|
* |
98
|
|
|
* @return null|GraphPathInterface |
99
|
|
|
*/ |
100
|
|
|
public function getPath(VertexInterface $targetVertex): ?GraphPathInterface |
101
|
|
|
{ |
102
|
|
|
$id = $targetVertex->getHash(); |
103
|
|
|
if (!array_key_exists($id, $this->paths)) { |
104
|
|
|
if ($this->source->equals($targetVertex)) { |
105
|
|
|
return GraphWalk::singletonWalk($this->graph, $source, 0.0); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
return $this->paths[$id]; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|