This repository has been archived by the owner on Sep 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
AdjacencyList.php
363 lines (311 loc) · 9.75 KB
/
AdjacencyList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/**
* Hoa
*
*
* @license
*
* New BSD License
*
* Copyright © 2007-2017, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Hoa nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace Hoa\Graph;
use Hoa\Iterator;
/**
* Class \Hoa\Graph\AdjacencyList.
*
* Graph implementation using an adjacency list.
*
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
class AdjacencyList extends Graph
{
/**
* Node value index.
*
* @const int
*/
const NODE_VALUE = 0;
/**
* Node child index.
*
* @const int
*/
const NODE_CHILDREN = 1;
/**
* Add a node.
*
* @param \Hoa\Graph\Node $node Node to add.
* @param array $parents Parents.
* @return \Hoa\Graph\Graph
* @throws \Hoa\Graph\Exception
*/
public function addNode(Node $node, array $parents = [])
{
$id = $node->getNodeId();
if (parent::DISALLOW_LOOP === $this->isLoopAllowed()) {
if (true === $this->nodeExists($id)) {
throw new Exception(
'Node %s already exists, cannot re-add it ' .
'(loop is not allowed).',
0,
$id
);
}
if (true === in_array($node, $parents)) {
throw new Exception(
'Reflexive node is not allowed, tried to add %s.',
1,
$id
);
}
}
if (!isset($this->_nodes[$id])) {
$this->_nodes[$id] = [
self::NODE_VALUE => null,
self::NODE_CHILDREN => []
];
}
$this->_nodes[$id][self::NODE_VALUE] = $node;
foreach ($parents as $parent) {
if (!($parent instanceof Node)) {
throw new Exception(
'Parent %s must be an instance of Hoa\Graph\Node.',
2,
$parent
);
}
$parentId = $parent->getNodeId();
if (false === $this->nodeExists($parentId)) {
throw new Exception(
'Cannot use %s as a parent node of %s ' .
'because it does exists.',
3,
[$parentId, $id]
);
}
$this->_nodes[$parentId][self::NODE_CHILDREN][] = $id;
}
return $this;
}
/**
* Check if a node does already exist or not.
*
* @param mixed $nodeId Node ID.
* @return bool
*/
public function nodeExists($nodeId)
{
if ($nodeId instanceof Node) {
$nodeId = $nodeId->getNodeId();
}
return isset($this->_nodes[$nodeId]);
}
/**
* Get a node.
*
* @param mixed $nodeId Node ID.
* @return \Hoa\Graph\Node
* @throws \Hoa\Graph\Exception
*/
public function getNode($nodeId)
{
if (false === $this->nodeExists($nodeId)) {
throw new Exception(
'Node %s does not exist, cannot get it.',
4,
$nodeId
);
}
return $this->_nodes[$nodeId][self::NODE_VALUE];
}
/**
* Get parents of a specific node.
*
* @param \Hoa\Graph\Node $node Node.
* @return array
* @throws \Hoa\Graph\Exception
*/
public function getParents(Node $node)
{
$id = $node->getNodeId();
if (false === $this->nodeExists($id)) {
throw new Exception(
'Node %s does not exist, cannot get its parents.',
5,
$id
);
}
$parents = [];
foreach ($this->getNodes() as $nodeId => $nodeBucket) {
if (true === in_array($id, $nodeBucket[self::NODE_CHILDREN])) {
$parents[$nodeId] = $this->getNode($nodeId);
}
}
return $parents;
}
/**
* Get children of a specific node.
*
* @param \Hoa\Graph\Node $node Node.
* @return array
* @throws \Hoa\Graph\Exception
*/
public function getChildren(Node $node)
{
$id = $node->getNodeId();
if (false === $this->nodeExists($id)) {
throw new Exception(
'Node %s does not exist, cannot get its children.',
6,
$id
);
}
$children = [];
foreach ($this->_nodes[$id][self::NODE_CHILDREN] as $childId) {
$children[$childId] = $this->getNode($childId);
}
return $children;
}
/**
* Delete a node.
*
* @param \Hoa\Graph\Node $node Node.
* @param bool $propagate Propagate the erasure.
* @return \Hoa\Graph\Graph
* @throws \Hoa\Graph\Exception
*/
public function deleteNode(Node $node, $propagate = self::DELETE_RESTRICT)
{
$id = $node->getNodeId();
if (false === $this->nodeExists($id)) {
return $this;
}
if (parent::DELETE_RESTRICT === $propagate) {
if (!empty($this->_nodes[$id][self::NODE_CHILDREN])) {
throw new Exception(
'Cannot delete %s node in restrict delete mode, because ' .
'it has one or more children.',
7,
$id
);
}
foreach ($this->getParents($node) as $parentId => $parent) {
unset(
$this->_nodes
[$parentId]
[self::NODE_CHILDREN]
[array_search($id, $this->_nodes[$parentId][self::NODE_CHILDREN])]
);
}
unset($this->_nodes[$id]);
return $this;
}
foreach ($this->getChildren($node) as $child) {
if ($node === $child) {
unset(
$this->_nodes
[$id]
[self::NODE_CHILDREN]
[array_search($id, $this->_nodes[$id][self::NODE_CHILDREN])]
);
}
$this->deleteNode($child, $propagate);
}
return $this->deleteNode($node, parent::DELETE_RESTRICT);
}
/**
* Whether node is a leaf, i.e. if it does not have any child.
*
* @param \Hoa\Graph\Node $node Node.
* @return bool
* @throws \Hoa\Graph\Exception
*/
public function isLeaf(Node $node)
{
$id = $node->getNodeId();
if (false === $this->nodeExists($id)) {
throw new Exception(
'Node %s does not exist, ' .
'cannot check if this is a leaf or not.',
8,
$id
);
}
return empty($this->_nodes[$id][self::NODE_CHILDREN]);
}
/**
* Whether node is a root, i.e. if it does not have any parent.
*
* @param \Hoa\Graph\Node $node Node.
* @return bool
* @throws \Hoa\Graph\Exception
*/
public function isRoot(Node $node)
{
$id = $node->getNodeId();
if (false === $this->nodeExists($id)) {
throw new Exception(
'Node %s does not exist, ' .
'cannot check if this is a root or not.',
9,
$id
);
}
return 0 === count($this->getParents($node));
}
/**
* Iterator over all nodes ordered by declarations.
*
* @return \Generator
*/
public function getIterator()
{
foreach ($this->getNodes() as $nodeId => $nodeBucket) {
yield $nodeId => $nodeBucket[self::NODE_VALUE];
}
}
/**
* Print the graph in the DOT language.
*
* @return string
*/
public function __toString()
{
$out = 'digraph {' . "\n";
foreach ($this->getNodes() as $nodeId => $_) {
$out .= ' ' . $nodeId . ';' . "\n";
}
foreach ($this->getNodes() as $nodeId => $node) {
foreach ($this->getChildren($node[self::NODE_VALUE]) as $childId => $child) {
$out .= ' ' . $nodeId . ' -> ' . $childId . ';' . "\n";
}
}
$out .= '}';
return $out;
}
}