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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Data;
/**
* Graph data structure
*/
class Graph
{
/**#@+
* Search modes
*/
const DIRECTIONAL = 1;
const INVERSE = 2;
const NON_DIRECTIONAL = 3;
/**#@-*/
/**#@-*/
protected $_nodes = [];
/**
* Declared relations directed "from" "to"
*
* @var array
*/
protected $_from = [];
/**
* Inverse relations "to" "from"
*
* @var array
*/
protected $_to = [];
/**
* Validate consistency of the declared structure and assign it to the object state
*
* @param array $nodes plain array with node identifiers
* @param array $relations array of 2-item plain arrays, which represent relations of nodes "from" "to"
*/
public function __construct(array $nodes, array $relations)
{
foreach ($nodes as $node) {
$this->_assertNode($node, false);
$this->_nodes[$node] = $node;
}
foreach ($relations as $pair) {
list($fromNode, $toNode) = $pair;
$this->addRelation($fromNode, $toNode);
}
}
/**
* Set a relation between nodes
*
* @param string|int $fromNode
* @param string|int $toNode
* @return $this
* @throws \InvalidArgumentException
*/
public function addRelation($fromNode, $toNode)
{
if ($fromNode == $toNode) {
throw new \InvalidArgumentException("Graph node '{$fromNode}' is linked to itself.");
}
$this->_assertNode($fromNode, true);
$this->_assertNode($toNode, true);
$this->_from[$fromNode][$toNode] = $toNode;
$this->_to[$toNode][$fromNode] = $fromNode;
return $this;
}
/**
* Export relations between nodes. Can return inverse relations
*
* @param int $mode
* @return array
* @throws \InvalidArgumentException
*/
public function getRelations($mode = self::DIRECTIONAL)
{
switch ($mode) {
case self::DIRECTIONAL:
return $this->_from;
case self::INVERSE:
return $this->_to;
case self::NON_DIRECTIONAL:
$graph = $this->_from;
foreach ($this->_to as $idTo => $relations) {
foreach ($relations as $idFrom) {
$graph[$idTo][$idFrom] = $idFrom;
}
}
return $graph;
default:
throw new \InvalidArgumentException("Unknown search mode: '{$mode}'");
}
}
/**
* Find a cycle in the graph
*
* Returns first/all found cycle
* Optionally may specify a node to return a cycle if it is in any
*
* @param string|int $node
* @param boolean $firstOnly found only first cycle
* @return array
*/
public function findCycle($node = null, $firstOnly = true)
{
$nodes = null === $node ? $this->_nodes : [$node];
$results = [];
foreach ($nodes as $node) {
$result = $this->dfs($node, $node);
if ($result) {
if ($firstOnly) {
return $result;
} else {
$results[] = $result;
}
}
}
return $results;
}
/**
* Find paths to reachable nodes from root node
*
* Returns array of paths, key is destination node and value is path (an array) from rootNode to destination node
* Eg. dest => [root, A, dest] means root -> A -> dest
*
* @param string|int $rootNode
* @param int $mode
* @return array
*/
public function findPathsToReachableNodes($rootNode, $mode = self::DIRECTIONAL)
{
$graph = $this->getRelations($mode);
$paths = [];
$queue = [$rootNode];
$visited = [$rootNode => $rootNode];
$paths[$rootNode] = [$rootNode];
while (!empty($queue)) {
$node = array_shift($queue);
if (!empty($graph[$node])) {
foreach ($graph[$node] as $child) {
if (!isset($visited[$child])) {
$paths[$child] = $paths[$node];
$paths[$child][] = $child;
$visited[$child] = $child;
$queue[] = $child;
}
}
}
}
return $paths;
}
/**
* "Depth-first search" of a path between nodes
*
* Returns path as array of nodes or empty array if path does not exist.
* Only first found path is returned. It will be not necessary the shortest or optimal in any way.
*
* @param string|int $fromNode
* @param string|int $toNode
* @param int $mode
* @return array
*/
public function dfs($fromNode, $toNode, $mode = self::DIRECTIONAL)
{
$this->_assertNode($fromNode, true);
$this->_assertNode($toNode, true);
return $this->_dfs($fromNode, $toNode, $this->getRelations($mode));
}
/**
* Recursive sub-routine of dfs()
*
* @param string|int $fromNode
* @param string|int $toNode
* @param array $graph
* @param array &$visited
* @param array $stack
* @return array
* @link http://en.wikipedia.org/wiki/Depth-first_search
*/
protected function _dfs($fromNode, $toNode, $graph, &$visited = [], $stack = [])
{
$stack[] = $fromNode;
$visited[$fromNode] = $fromNode;
if (isset($graph[$fromNode][$toNode])) {
$stack[] = $toNode;
return $stack;
}
if (isset($graph[$fromNode])) {
foreach ($graph[$fromNode] as $node) {
if (!isset($visited[$node])) {
$result = $this->_dfs($node, $toNode, $graph, $visited, $stack);
if ($result) {
return $result;
}
}
}
}
return [];
}
/**
* Verify existence or non-existence of a node
*
* @param string|int $node
* @param bool $mustExist
* @return void
* @throws \InvalidArgumentException according to assertion rules
*/
protected function _assertNode($node, $mustExist)
{
if (isset($this->_nodes[$node])) {
if (!$mustExist) {
throw new \InvalidArgumentException("Graph node '{$node}' already exists'.");
}
} else {
if ($mustExist) {
throw new \InvalidArgumentException("Graph node '{$node}' does not exist.");
}
}
}
}