|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace graphp\graph\builder; |
|
4
|
|
|
|
|
5
|
|
|
use graphp\GraphInterface; |
|
6
|
|
|
use graphp\GraphUtils; |
|
7
|
|
|
use graphp\edge\EdgeInterface; |
|
8
|
|
|
use graphp\vertex\VertexInterface; |
|
9
|
|
|
use graphp\vertex\VertexSet; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class GraphBuilder |
|
13
|
|
|
* |
|
14
|
|
|
* @package graphp\graph |
|
15
|
|
|
*/ |
|
16
|
|
|
class GraphBuilder |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The graph |
|
20
|
|
|
* |
|
21
|
|
|
* @var GraphInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $graph; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Construct the graph |
|
27
|
|
|
* |
|
28
|
|
|
* @param GraphInterface $graph - the graph |
|
29
|
|
|
*/ |
|
30
|
6 |
|
public function __construct(GraphInterface $graph) |
|
31
|
|
|
{ |
|
32
|
6 |
|
$this->graph = $graph; |
|
33
|
6 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Add a vertex to the graph being built |
|
37
|
|
|
* |
|
38
|
|
|
* @param VertexInterface $vertex - the vertex |
|
39
|
|
|
* |
|
40
|
|
|
* @return self |
|
41
|
|
|
*/ |
|
42
|
3 |
|
public function addVertex(VertexInterface $vertex): self |
|
43
|
|
|
{ |
|
44
|
3 |
|
$this->graph->addVertex($vertex); |
|
45
|
3 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add vertices to the graph being built |
|
50
|
|
|
* |
|
51
|
|
|
* @param VertexSet $vertices - the vertices |
|
52
|
|
|
* |
|
53
|
|
|
* @return self |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function addVertices(VertexSet $vertices): self |
|
56
|
|
|
{ |
|
57
|
1 |
|
foreach ($vertices as $vertex) { |
|
58
|
1 |
|
$this->addVertex($vertex); |
|
59
|
|
|
} |
|
60
|
1 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Add an edge to the graph being built |
|
65
|
|
|
* |
|
66
|
|
|
* @param VertexInterface $vertex - the source vertex |
|
67
|
|
|
* @param VertexInterface $targetVertex - the target vertex |
|
68
|
|
|
* @param EdgeInterface $edge - the edge to be added |
|
69
|
|
|
* |
|
70
|
|
|
* @return self |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function addEdge( |
|
73
|
|
|
VertexInterface $sourceVertex, |
|
74
|
|
|
VertexInterface $targetVertex, |
|
75
|
|
|
?EdgeInterface $edge = null, |
|
76
|
|
|
?float $weight = null |
|
77
|
|
|
): self { |
|
78
|
2 |
|
if (is_null($edge)) { |
|
79
|
2 |
|
GraphUtils::addEdgeWithVertices($this->graph, $sourceVertex, $targetVertex, $weight); |
|
80
|
|
|
} else { |
|
81
|
1 |
|
$this->addVertex($sourceVertex); |
|
82
|
1 |
|
$this->addVertex($targetVertex); |
|
83
|
1 |
|
$this->graph->addEdge($sourceVertex, $targetVertex, $edge); |
|
84
|
|
|
} |
|
85
|
2 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Add an chain of edges to the graph being built |
|
90
|
|
|
* |
|
91
|
|
|
* @param VertexInterface $first - the first vertex |
|
92
|
|
|
* @param VertexInterface $second - the second vertex |
|
93
|
|
|
* @param mixed $vertices - the remaining vertices |
|
94
|
|
|
* |
|
95
|
|
|
* @return self |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function addEdgeChain(VertexInterface $first, VertexInterface $second, ...$vertices): self |
|
98
|
|
|
{ |
|
99
|
1 |
|
$this->addEdge($first, $second); |
|
100
|
1 |
|
$last = $second; |
|
101
|
1 |
|
foreach ($vertices as $vertex) { |
|
102
|
1 |
|
$this->addEdge($last, $vertex); |
|
103
|
1 |
|
$last = $vertex; |
|
104
|
|
|
} |
|
105
|
1 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Add all the vertices and all the edges of the specified graph to the graph being built |
|
110
|
|
|
* |
|
111
|
|
|
* @param GraphInterface $sourceGraph - the graph |
|
112
|
|
|
* |
|
113
|
|
|
* @return self |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function addGraph(GraphInterface $sourceGraph): self |
|
116
|
|
|
{ |
|
117
|
1 |
|
GraphUtils::addGraph($this->graph, $sourceGraph); |
|
118
|
1 |
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Remove the specified vertex from the graph being built |
|
123
|
|
|
* |
|
124
|
|
|
* @param VertexInterface $vertex - the vertex to remove |
|
125
|
|
|
* |
|
126
|
|
|
* @return self |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function removeVertex(VertexInterface $vertex): self |
|
129
|
|
|
{ |
|
130
|
1 |
|
$this->graph->removeVertex($vertex); |
|
131
|
1 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Remove vertices from the graph being built |
|
136
|
|
|
* |
|
137
|
|
|
* @param VertexSet $vertices - the vertices to remove |
|
138
|
|
|
* |
|
139
|
|
|
* @return self |
|
140
|
|
|
*/ |
|
141
|
1 |
|
public function removeVertices(VertexSet $vertices): self |
|
142
|
|
|
{ |
|
143
|
1 |
|
foreach ($vertices as $vertex) { |
|
144
|
1 |
|
$this->removeVertex($vertex); |
|
145
|
|
|
} |
|
146
|
1 |
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Remove the edge from the graph being built |
|
151
|
|
|
* |
|
152
|
|
|
* @param VertexInterface $sourceVertex - the edge source vertex |
|
153
|
|
|
* @param VertexInterface $tagretVertex - the edge target vertex |
|
154
|
|
|
* @param EdgeInterface $edge - the edge |
|
155
|
|
|
* |
|
156
|
|
|
* @return self |
|
157
|
|
|
*/ |
|
158
|
1 |
|
public function removeEdge( |
|
159
|
|
|
?VertexInterface $sourceVertex = null, |
|
160
|
|
|
?VertexInterface $targetVertex = null, |
|
161
|
|
|
?EdgeInterface $edge = null |
|
162
|
|
|
): self { |
|
163
|
1 |
|
$this->graph->removeEdge($sourceVertex, $targetVertex, $edge); |
|
164
|
1 |
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get the graph being built |
|
169
|
|
|
* |
|
170
|
|
|
* @return GraphInterface |
|
171
|
|
|
*/ |
|
172
|
1 |
|
public function build(): GraphInterface |
|
173
|
|
|
{ |
|
174
|
1 |
|
return $this->graph; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|