forked from graphp/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.php
More file actions
252 lines (221 loc) · 7.27 KB
/
Graph.php
File metadata and controls
252 lines (221 loc) · 7.27 KB
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
<?php
namespace Graphp\Graph;
use Graphp\Graph\Exception\InvalidArgumentException;
use Graphp\Graph\Set\DualAggregate;
use Graphp\Graph\Set\Edges;
use Graphp\Graph\Set\Vertices;
class Graph extends Entity implements DualAggregate
{
protected $vertices = array();
protected $edges = array();
/**
* @param array $attributes
*/
public function __construct(array $attributes = array())
{
$this->attributes = $attributes;
}
/**
* return set of Vertices added to this graph
*
* @return Vertices
*/
public function getVertices()
{
return new Vertices($this->vertices);
}
/**
* return set of ALL Edges added to this graph
*
* @return Edges
*/
public function getEdges()
{
return new Edges($this->edges);
}
/**
* create a new Vertex in the Graph
*
* @param array $attributes
* @return Vertex
*/
public function createVertex(array $attributes = array())
{
return new Vertex($this, $attributes);
}
/**
* Creates a new undirected (bidirectional) edge between the given two vertices.
*
* @param Vertex $a
* @param Vertex $b
* @param array $attributes
* @return EdgeUndirected
* @throws InvalidArgumentException
*/
public function createEdgeUndirected(Vertex $a, Vertex $b, array $attributes = array())
{
if ($a->getGraph() !== $this) {
throw new InvalidArgumentException('Vertices have to be within this graph');
}
return new EdgeUndirected($a, $b, $attributes);
}
/**
* Creates a new directed edge from the given start vertex to given target vertex
*
* @param Vertex $source source vertex
* @param Vertex $target target vertex
* @param array $attributes
* @return EdgeDirected
* @throws InvalidArgumentException
*/
public function createEdgeDirected(Vertex $source, Vertex $target, array $attributes = array())
{
if ($source->getGraph() !== $this) {
throw new InvalidArgumentException('Vertices have to be within this graph');
}
return new EdgeDirected($source, $target, $attributes);
}
/**
* Returns a copy of this graph without the given vertex
*
* If this vertex was not found in this graph, the returned graph will be
* identical.
*
* @param Vertex $vertex
* @return self
*/
public function withoutVertex(Vertex $vertex)
{
return $this->withoutVertices(new Vertices(array($vertex)));
}
/**
* Returns a copy of this graph without the given vertices
*
* If any of the given vertices can not be found in this graph, they will
* silently be ignored. If neither of the vertices can be found in this graph,
* the returned graph will be identical.
*
* @param Vertices $vertices
* @return self
*/
public function withoutVertices(Vertices $vertices)
{
// keep copy of original vertices and edges and temporarily remove all $vertices and their adjacent edges
$originalEdges = $this->edges;
$originalVertices = $this->vertices;
foreach ($vertices as $vertex) {
if (($key = \array_search($vertex, $this->vertices, true)) !== false) {
unset($this->vertices[$key]);
foreach ($vertex->getEdges() as $edge) {
if (($key = \array_search($edge, $this->edges, true)) !== false) {
unset($this->edges[$key]);
}
}
}
}
// no vertices matched => return graph as-is
if (\count($this->vertices) === \count($originalVertices)) {
return $this;
}
// clone graph with vertices/edges temporarily removed, then restore
$clone = clone $this;
$this->edges= $originalEdges;
$this->vertices = $originalVertices;
return $clone;
}
/**
* Returns a copy of this graph without the given edge
*
* If this edge was not found in this graph, the returned graph will be
* identical.
*
* @param Edge $edge
* @return self
*/
public function withoutEdge(Edge $edge)
{
return $this->withoutEdges(new Edges(array($edge)));
}
/**
* Returns a copy of this graph without the given edges
*
* If any of the given edges can not be found in this graph, they will
* silently be ignored. If neither of the edges can be found in this graph,
* the returned graph will be identical.
*
* @param Edges $edges
* @return self
*/
public function withoutEdges(Edges $edges)
{
// keep copy of original edges and temporarily remove all $edges
$original = $this->edges;
foreach ($edges as $edge) {
if (($key = \array_search($edge, $this->edges, true)) !== false) {
unset($this->edges[$key]);
}
}
// no edges matched => return graph as-is
if (\count($this->edges) === \count($original)) {
return $this;
}
// clone graph with edges temporarily removed, then restore
$clone = clone $this;
$this->edges = $original;
return $clone;
}
/**
* adds a new Vertex to the Graph (MUST NOT be called manually!)
*
* @param Vertex $vertex instance of the new Vertex
* @return void
* @internal
* @see self::createVertex() instead!
*/
public function addVertex(Vertex $vertex)
{
$this->vertices[] = $vertex;
}
/**
* adds a new Edge to the Graph (MUST NOT be called manually!)
*
* @param Edge $edge instance of the new Edge
* @return void
* @internal
* @see Graph::createEdgeUndirected() instead!
*/
public function addEdge(Edge $edge)
{
$this->edges []= $edge;
}
/**
* create new clone/copy of this graph - copy all attributes, vertices and edges
*/
public function __clone()
{
$vertices = $this->vertices;
$this->vertices = array();
$edges = $this->edges;
$this->edges = array();
$map = array();
foreach ($vertices as $originalVertex) {
\assert($originalVertex instanceof Vertex);
$vertex = new Vertex($this, $originalVertex->getAttributes());
// create map with old vertex hash to new vertex object
$map[\spl_object_hash($originalVertex)] = $vertex;
}
foreach ($edges as $originalEdge) {
\assert($originalEdge instanceof Edge);
// use map to match old vertex hashes to new vertex objects
$vertices = $originalEdge->getVertices()->getVector();
$v1 = $map[\spl_object_hash($vertices[0])];
$v2 = $map[\spl_object_hash($vertices[1])];
// recreate edge and assign attributes
if ($originalEdge instanceof EdgeUndirected) {
$this->createEdgeUndirected($v1, $v2, $originalEdge->getAttributes());
} else {
$this->createEdgeDirected($v1, $v2, $originalEdge->getAttributes());
}
}
}
}