1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TemplesOfCode\NodesAndEdges; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class UndirectedGraph |
9
|
|
|
* @package TemplesOfCode\NodesAndEdges |
10
|
|
|
*/ |
11
|
|
|
class UndirectedGraph extends Graph |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Add edge v-w to this graph |
15
|
|
|
* |
16
|
|
|
* @param int $v |
17
|
|
|
* @param int $w |
18
|
|
|
*/ |
19
|
|
|
public function addEdge(int $v, int $w) |
20
|
|
|
{ |
21
|
|
|
// validate the vertex |
22
|
|
|
Graph::validateVertex($v, $this->vertices); |
23
|
|
|
// validate the vertex |
24
|
|
|
Graph::validateVertex($v, $this->vertices); |
25
|
|
|
// bump |
26
|
|
|
$this->edges++; |
27
|
|
|
// w is adjacent to v |
28
|
|
|
array_unshift($this->adjacencyList[$v], $w); |
29
|
|
|
// v is adjacent to w |
30
|
|
|
array_unshift($this->adjacencyList[$w], $v); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Initializes a graph from the specified input stream. |
35
|
|
|
* |
36
|
|
|
* @param string $file |
37
|
|
|
* @return UndirectedGraph |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
*/ |
40
|
|
|
public static function fromFile(string $file) |
41
|
|
|
{ |
42
|
|
|
// open the stream for reading |
43
|
|
|
if (!$handle = fopen($file, 'r')) { |
44
|
|
|
throw new InvalidArgumentException('could not open stream'); |
45
|
|
|
} |
46
|
|
|
// fetch from stream |
47
|
|
|
$first = fgets($handle); |
48
|
|
|
// fetch from stream |
49
|
|
|
$second = fgets($handle); |
50
|
|
|
// parse V and E |
51
|
|
|
list ( |
52
|
|
|
$vertices, |
53
|
|
|
$edges |
54
|
|
|
) = self::parseGraphVEFromString($first, $second); |
55
|
|
|
// instantiate a new graph |
56
|
|
|
$graph = new UndirectedGraph($vertices); |
57
|
|
|
// read in the edges |
58
|
|
|
for ($i = 0; $i < $edges; $i++) { |
59
|
|
|
// fet from source |
60
|
|
|
$raw = fgets($handle); |
61
|
|
|
// parse data |
62
|
|
|
list ( |
63
|
|
|
$v, |
64
|
|
|
$w |
65
|
|
|
) = self::parseEdge($raw, $vertices); |
66
|
|
|
// add to the graph |
67
|
|
|
$graph->addEdge($v, $w); |
68
|
|
|
} |
69
|
|
|
// close the stream |
70
|
|
|
fclose($handle); |
71
|
|
|
// return the built graph |
72
|
|
|
return $graph; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Initializes a new graph that is a deep copy of $g |
77
|
|
|
* |
78
|
|
|
* @param UndirectedGraph $g |
79
|
|
|
* @return UndirectedGraph |
80
|
|
|
*/ |
81
|
|
|
public static function fromGraph(UndirectedGraph $g) |
82
|
|
|
{ |
83
|
|
|
// get the number of vertices |
84
|
|
|
$vertices = $g->getVertices(); |
85
|
|
|
// init |
86
|
|
|
$adjacencyList = []; |
87
|
|
|
// iterate over the vertices |
88
|
|
|
for ($vertex = 0; $vertex < $vertices; $vertex++) { |
89
|
|
|
// get the adjacent vertices |
90
|
|
|
$adjacencyList[$vertex] = $g->adjacent($vertex); |
91
|
|
|
} |
92
|
|
|
// return the new graph |
93
|
|
|
return new UndirectedGraph($vertices, $adjacencyList); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $graph |
98
|
|
|
* @return UndirectedGraph |
99
|
|
|
*/ |
100
|
|
|
public static function fromString(string $graph) |
101
|
|
|
{ |
102
|
|
|
// parse the lines |
103
|
|
|
$lines = explode("\n", $graph); |
104
|
|
|
// parse V and E |
105
|
|
|
list ( |
106
|
|
|
$vertices, |
107
|
|
|
$edges |
108
|
|
|
) = self::parseGraphVEFromString($lines[0], $lines[1]); |
109
|
|
|
// instantiate a new graph |
110
|
|
|
$graph = new UndirectedGraph($vertices); |
111
|
|
|
// read in the edges |
112
|
|
|
for ($i = 0; $i < $edges; $i++) { |
113
|
|
|
// fet from source |
114
|
|
|
$raw = $lines[$i+2]; |
115
|
|
|
// parse data |
116
|
|
|
list ( |
117
|
|
|
$v, |
118
|
|
|
$w |
119
|
|
|
) = self::parseEdge($raw, $vertices); |
120
|
|
|
// add to the graph |
121
|
|
|
$graph->addEdge($v, $w); |
122
|
|
|
} |
123
|
|
|
// return the built graph |
124
|
|
|
return $graph; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param resource $handle |
129
|
|
|
* @return UndirectedGraph |
130
|
|
|
*/ |
131
|
|
|
protected static function fromStream($handle) |
132
|
|
|
{ |
133
|
|
|
|
134
|
|
|
// fetch from stream |
135
|
|
|
$first = fgets($handle); |
136
|
|
|
// fetch from stream |
137
|
|
|
$second = fgets($handle); |
138
|
|
|
// parse V and E |
139
|
|
|
list ( |
140
|
|
|
$vertices, |
141
|
|
|
$edges |
142
|
|
|
) = self::parseGraphVEFromString($first, $second); |
143
|
|
|
// instantiate a new graph |
144
|
|
|
$graph = new UndirectedGraph($vertices); |
145
|
|
|
// read in the edges |
146
|
|
|
for ($i = 0; $i < $edges; $i++) { |
147
|
|
|
// fet from source |
148
|
|
|
$raw = fgets($handle); |
149
|
|
|
// parse data |
150
|
|
|
list ( |
151
|
|
|
$v, |
152
|
|
|
$w |
153
|
|
|
) = self::parseEdge($raw, $vertices); |
154
|
|
|
// add to the graph |
155
|
|
|
$graph->addEdge($v, $w); |
156
|
|
|
} |
157
|
|
|
// close the stream |
158
|
|
|
fclose($handle); |
159
|
|
|
// return the built graph |
160
|
|
|
return $graph; |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|