1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graphp\Graph; |
4
|
|
|
|
5
|
|
|
use Graphp\Graph\Builder\GraphTypeBuilder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* class DefaultGraphType |
9
|
|
|
* |
10
|
|
|
* @package Graphp\Graph |
11
|
|
|
*/ |
12
|
|
|
class DefaultGraphType implements GraphTypeInterface |
13
|
|
|
{ |
14
|
|
|
private $directed; |
15
|
|
|
private $undirected; |
16
|
|
|
private $selfLoops; |
17
|
|
|
private $multipleEdges; |
18
|
|
|
private $weighted; |
19
|
|
|
private $allowsCycles; |
20
|
|
|
private $modifiable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Construct a default graph type |
24
|
|
|
*/ |
25
|
34 |
|
public function __construct( |
26
|
|
|
bool $directed, |
27
|
|
|
bool $undirected, |
28
|
|
|
bool $selfLoops, |
29
|
|
|
bool $multipleEdges, |
30
|
|
|
bool $weighted, |
31
|
|
|
bool $allowsCycles, |
32
|
|
|
bool $modifiable |
33
|
|
|
) { |
34
|
34 |
|
$this->directed = $directed; |
35
|
34 |
|
$this->undirected = $undirected; |
36
|
34 |
|
$this->selfLoops = $selfLoops; |
37
|
34 |
|
$this->multipleEdges = $multipleEdges; |
38
|
34 |
|
$this->weighted = $weighted; |
39
|
34 |
|
$this->allowsCycles = $allowsCycles; |
40
|
34 |
|
$this->modifiable = $modifiable; |
41
|
34 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check if all edges of the graph are directed |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
34 |
|
public function isDirected(): bool |
49
|
|
|
{ |
50
|
34 |
|
return $this->directed && !$this->undirected; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Check if all edges of the graph are undirected |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
4 |
|
public function isUndirected(): bool |
59
|
|
|
{ |
60
|
4 |
|
return $this->undirected && !$this->directed; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check if the graph contains both directed and undirected edges |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
1 |
|
public function isMixed(): bool |
69
|
|
|
{ |
70
|
1 |
|
return $this->undirected && $this->directed; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if multiple edges are allowed in the graph |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
23 |
|
public function isAllowingMultipleEdges(): bool |
79
|
|
|
{ |
80
|
23 |
|
return $this->multipleEdges; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check if cycles are allowed in the graph |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
1 |
|
public function isAllowingCycles(): bool |
89
|
|
|
{ |
90
|
1 |
|
return $this->allowsCycles; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check if self-loops are allowed in the graph |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
25 |
|
public function isAllowingSelfLoops(): bool |
99
|
|
|
{ |
100
|
25 |
|
return $this->selfLoops; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Check if weighted edges are allowed in the graph |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
34 |
|
public function isWeighted(): bool |
109
|
|
|
{ |
110
|
34 |
|
return $this->weighted; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Check if the graph is simple |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
1 |
|
public function isSimple(): bool |
119
|
|
|
{ |
120
|
1 |
|
return !$this->isAllowingMultipleEdges() && !$this->isAllowingSelfLoops(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Check if the graph is multigraph |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
1 |
|
public function isMultigraph(): bool |
129
|
|
|
{ |
130
|
1 |
|
return $this->isAllowingMultipleEdges() && !$this->isAllowingSelfLoops(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Check if the graph is modifiable |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
1 |
|
public function isModifiable(): bool |
139
|
|
|
{ |
140
|
1 |
|
return $this->modifiable; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Create a directed variant of the current graph type. |
145
|
|
|
* |
146
|
|
|
* @return GraphTypeInterface |
147
|
|
|
*/ |
148
|
1 |
|
public function asDirected(): GraphTypeInterface |
149
|
|
|
{ |
150
|
1 |
|
$builder = new GraphTypeBuilder($this); |
151
|
1 |
|
return $builder->directed()->build(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Create an undirected variant of the current graph type. |
156
|
|
|
* |
157
|
|
|
* @return GraphTypeInterface |
158
|
|
|
*/ |
159
|
1 |
|
public function asUndirected(): GraphTypeInterface |
160
|
|
|
{ |
161
|
1 |
|
$builder = new GraphTypeBuilder($this); |
162
|
1 |
|
return $builder->undirected()->build(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Create a mixed variant of the current graph type. |
167
|
|
|
* |
168
|
|
|
* @return GraphTypeInterface |
169
|
|
|
*/ |
170
|
1 |
|
public function asMixed(): GraphTypeInterface |
171
|
|
|
{ |
172
|
1 |
|
$builder = new GraphTypeBuilder($this); |
173
|
1 |
|
return $builder->mixed()->build(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Create a weighted variant of the current graph type. |
178
|
|
|
* |
179
|
|
|
* @return GraphTypeInterface |
180
|
|
|
*/ |
181
|
1 |
|
public function asWeighted(): GraphTypeInterface |
182
|
|
|
{ |
183
|
1 |
|
$builder = new GraphTypeBuilder($this); |
184
|
1 |
|
return $builder->weighted(true)->build(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Create an unweighted variant of the current graph type. |
189
|
|
|
* |
190
|
|
|
* @return GraphTypeInterface |
191
|
|
|
*/ |
192
|
1 |
|
public function asUnweighted(): GraphTypeInterface |
193
|
|
|
{ |
194
|
1 |
|
$builder = new GraphTypeBuilder($this); |
195
|
1 |
|
return $builder->weighted(false)->build(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Create a modifiable variant of the current graph type. |
200
|
|
|
* |
201
|
|
|
* @return GraphTypeInterface |
202
|
|
|
*/ |
203
|
1 |
|
public function asModifiable(): GraphTypeInterface |
204
|
|
|
{ |
205
|
1 |
|
$builder = new GraphTypeBuilder($this); |
206
|
1 |
|
return $builder->modifiable(true)->build(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Create an unmodifiable variant of the current graph type. |
211
|
|
|
* |
212
|
|
|
* @return GraphTypeInterface |
213
|
|
|
*/ |
214
|
1 |
|
public function asUnmodifiable(): GraphTypeInterface |
215
|
|
|
{ |
216
|
1 |
|
$builder = new GraphTypeBuilder($this); |
217
|
1 |
|
return $builder->modifiable(false)->build(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Create a simple graph. |
222
|
|
|
* |
223
|
|
|
* @return GraphTypeInterface |
224
|
|
|
*/ |
225
|
1 |
|
public static function simple(): GraphTypeInterface |
226
|
|
|
{ |
227
|
1 |
|
$builder = new GraphTypeBuilder(); |
228
|
1 |
|
return $builder->undirected() |
229
|
1 |
|
->allowSelfLoops(false) |
230
|
1 |
|
->allowMultipleEdges(false) |
231
|
1 |
|
->weighted(false) |
232
|
1 |
|
->build(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Create a multigraph. |
237
|
|
|
* |
238
|
|
|
* @return GraphTypeInterface |
239
|
|
|
*/ |
240
|
1 |
|
public static function multigraph(): GraphTypeInterface |
241
|
|
|
{ |
242
|
1 |
|
$builder = new GraphTypeBuilder(); |
243
|
1 |
|
return $builder->undirected() |
244
|
1 |
|
->allowSelfLoops(false) |
245
|
1 |
|
->allowMultipleEdges(true) |
246
|
1 |
|
->weighted(false) |
247
|
1 |
|
->build(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Create a pseudograph. |
252
|
|
|
* |
253
|
|
|
* @return GraphTypeInterface |
254
|
|
|
*/ |
255
|
1 |
|
public static function pseudograph(): GraphTypeInterface |
256
|
|
|
{ |
257
|
1 |
|
$builder = new GraphTypeBuilder(); |
258
|
1 |
|
return $builder->undirected() |
259
|
1 |
|
->allowSelfLoops(true) |
260
|
1 |
|
->allowMultipleEdges(true) |
261
|
1 |
|
->weighted(false) |
262
|
1 |
|
->build(); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Create a directed simple graph. |
267
|
|
|
* |
268
|
|
|
* @return GraphTypeInterface |
269
|
|
|
*/ |
270
|
1 |
|
public static function directedSimple(): GraphTypeInterface |
271
|
|
|
{ |
272
|
1 |
|
$builder = new GraphTypeBuilder(); |
273
|
1 |
|
return $builder->directed() |
274
|
1 |
|
->allowSelfLoops(false) |
275
|
1 |
|
->allowMultipleEdges(false) |
276
|
1 |
|
->weighted(false) |
277
|
1 |
|
->build(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Create a directed multigraph. |
282
|
|
|
* |
283
|
|
|
* @return GraphTypeInterface |
284
|
|
|
*/ |
285
|
1 |
|
public static function directedMultigraph(): GraphTypeInterface |
286
|
|
|
{ |
287
|
1 |
|
$builder = new GraphTypeBuilder(); |
288
|
1 |
|
return $builder->directed() |
289
|
1 |
|
->allowSelfLoops(false) |
290
|
1 |
|
->allowMultipleEdges(true) |
291
|
1 |
|
->weighted(false) |
292
|
1 |
|
->build(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Create a directed pseudograph. |
297
|
|
|
* |
298
|
|
|
* @return GraphTypeInterface |
299
|
|
|
*/ |
300
|
1 |
|
public static function directedPseudograph(): GraphTypeInterface |
301
|
|
|
{ |
302
|
1 |
|
$builder = new GraphTypeBuilder(); |
303
|
1 |
|
return $builder->directed() |
304
|
1 |
|
->allowSelfLoops(true) |
305
|
1 |
|
->allowMultipleEdges(true) |
306
|
1 |
|
->weighted(false) |
307
|
1 |
|
->build(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Create a mixed graph. |
312
|
|
|
* |
313
|
|
|
* @return GraphTypeInterface |
314
|
|
|
*/ |
315
|
1 |
|
public static function mixed(): GraphTypeInterface |
316
|
|
|
{ |
317
|
1 |
|
$builder = new GraphTypeBuilder(); |
318
|
1 |
|
return $builder->mixed() |
319
|
1 |
|
->allowSelfLoops(true) |
320
|
1 |
|
->allowMultipleEdges(true) |
321
|
1 |
|
->weighted(false) |
322
|
1 |
|
->build(); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Create a directed acyclic graph. |
327
|
|
|
* |
328
|
|
|
* @return GraphTypeInterface |
329
|
|
|
*/ |
330
|
1 |
|
public static function dag(): GraphTypeInterface |
331
|
|
|
{ |
332
|
1 |
|
$builder = new GraphTypeBuilder(); |
333
|
1 |
|
return $builder->directed() |
334
|
1 |
|
->allowSelfLoops(false) |
335
|
1 |
|
->allowMultipleEdges(true) |
336
|
1 |
|
->allowCycles(false) |
337
|
1 |
|
->weighted(false) |
338
|
1 |
|
->build(); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Get the graph type string representation |
343
|
|
|
* |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
1 |
|
public function __toString(): string |
347
|
|
|
{ |
348
|
1 |
|
return "DefaultGraphType [directed=" . $this->directed . ", undirected=" . $this->undirected |
349
|
1 |
|
. ", self-loops=" . $this->selfLoops . ", multiple-edges=" . $this->multipleEdges . ", weighted=" |
350
|
1 |
|
. $this->weighted . ", allows-cycles=" . $this->allowsCycles . ", modifiable=" . $this->modifiable . "]"; |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|