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 TreeSingleSourcePaths |
14
|
|
|
* |
15
|
|
|
* @package Graphp\Alg\Shortestpath |
16
|
|
|
*/ |
17
|
|
|
class TreeSingleSourcePaths 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
|
|
|
* A map which keeps for each target vertex the predecessor edge and the total length of the |
35
|
|
|
* shortest path |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $map; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Construct a new instance |
43
|
|
|
* |
44
|
|
|
* @param GraphInterface $graph - the graph |
45
|
|
|
* @param VertexInterface $source - the source vertex |
46
|
|
|
* @param array $map - the map |
47
|
|
|
*/ |
48
|
6 |
|
public function __construct(GraphInterface $graph, VertexInterface $source, array $map) |
49
|
|
|
{ |
50
|
6 |
|
$this->graph = $graph; |
51
|
6 |
|
$this->source = $source; |
52
|
6 |
|
$this->map = $map; |
53
|
6 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the graph over which this set of paths is defined |
57
|
|
|
* |
58
|
|
|
* @return GraphInterface |
59
|
|
|
*/ |
60
|
1 |
|
public function getGraph(): GraphInterface |
61
|
|
|
{ |
62
|
1 |
|
return $this->graph; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the single source vertex |
67
|
|
|
* |
68
|
|
|
* @return GraphInterface |
69
|
|
|
*/ |
70
|
2 |
|
public function getSourceVertex(): VertexInterface |
71
|
|
|
{ |
72
|
2 |
|
return $this->source; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the weight of the path from the source vertex to the target vertex |
77
|
|
|
* |
78
|
|
|
* @param VertexInterface $targetVertex - the target vertex |
79
|
|
|
* |
80
|
|
|
* @return float |
81
|
|
|
*/ |
82
|
2 |
|
public function getWeight(VertexInterface $targetVertex): ?float |
83
|
|
|
{ |
84
|
2 |
|
$id = $targetVertex->getHash(); |
85
|
2 |
|
if (array_key_exists($id, $this->map)) { |
86
|
2 |
|
return $this->map[$id][0]; |
87
|
2 |
|
} elseif ($this->source->equals($targetVertex)) { |
88
|
1 |
|
return 0.0; |
89
|
|
|
} |
90
|
1 |
|
return INF; |
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
|
6 |
|
public function getPath(VertexInterface $targetVertex): ?GraphPathInterface |
101
|
|
|
{ |
102
|
6 |
|
if ($this->source->equals($targetVertex)) { |
103
|
2 |
|
return GraphWalk::singletonWalk($this->graph, $this->source, 0.0); |
104
|
|
|
} |
105
|
|
|
|
106
|
6 |
|
$edgeList = []; |
107
|
|
|
|
108
|
6 |
|
$cur = $targetVertex; |
109
|
|
|
|
110
|
6 |
|
$id = $cur->getHash(); |
111
|
6 |
|
if (!array_key_exists($id, $this->map) || $this->map[$id] == INF) { |
112
|
3 |
|
return null; |
113
|
|
|
} |
114
|
6 |
|
$p = $this->map[$id]; |
115
|
|
|
|
116
|
6 |
|
$weight = 0.0; |
117
|
6 |
|
while (!is_null($p) && !$cur->equals($this->source)) { |
118
|
6 |
|
$edge = $p[1]; |
119
|
6 |
|
if (is_null($edge)) { |
120
|
|
|
break; |
121
|
|
|
} |
122
|
6 |
|
array_unshift($edgeList, $edge); |
123
|
6 |
|
$weight += $this->graph->getEdgeWeight($edge); |
124
|
6 |
|
$cur = GraphUtils::getOppositeVertex($this->graph, $edge, $cur); |
125
|
|
|
|
126
|
6 |
|
$id = $cur->getHash(); |
127
|
6 |
|
if (!array_key_exists($id, $this->map)) { |
128
|
1 |
|
break; |
129
|
|
|
} |
130
|
6 |
|
$p = $this->map[$id]; |
131
|
|
|
} |
132
|
6 |
|
return new GraphWalk($this->graph, $this->source, $targetVertex, null, $edgeList, $weight); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|