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