5
5
use Fhaculty \Graph \Algorithm \Tree \Base as Tree ;
6
6
use Fhaculty \Graph \Exception \UnderflowException ;
7
7
use Fhaculty \Graph \Exception \UnexpectedValueException ;
8
- use Fhaculty \Graph \Algorithm \Search \Base as Search ;
9
- use Fhaculty \Graph \Algorithm \Search \StrictDepthFirst ;
10
8
use Fhaculty \Graph \Vertex ;
11
9
12
10
/**
16
14
*/
17
15
abstract class BaseDirected extends Tree
18
16
{
19
- const DIRECTION_CHILDREN = -1 ;
20
-
21
17
/**
22
18
* get root vertex for this in-tree
23
19
*
@@ -29,12 +25,7 @@ public function getVertexRoot()
29
25
{
30
26
$ root = $ this ->getVertexPossibleRoot ();
31
27
32
- $ search = new StrictDepthFirst ($ root );
33
- $ search ->setDirection (static ::DIRECTION_CHILDREN );
34
-
35
- $ num = $ search ->getNumberOfVertices ();
36
-
37
- if ($ num !== $ this ->graph ->getNumberOfVertices ()) {
28
+ if (count ($ this ->getVerticesSubtree ($ root )) !== $ this ->graph ->getNumberOfVertices ()) {
38
29
throw new UnexpectedValueException ();
39
30
}
40
31
@@ -207,4 +198,51 @@ public function getHeightVertex(Vertex $vertex)
207
198
}
208
199
return $ max ;
209
200
}
201
+
202
+ /**
203
+ * get all vertices that are in the subtree of the given $vertex (which IS included)
204
+ *
205
+ * root vertex will return the whole tree, leaf vertices will only return themselves
206
+ *
207
+ * @param Vertex $vertex
208
+ * @throws UnexpectedValueException if there are invalid edges (check isTree()!)
209
+ * @return Vertex[]
210
+ * @uses self::getVerticesChildren()
211
+ * @uses self::getVerticesSubtree()
212
+ */
213
+ public function getVerticesSubtree (Vertex $ vertex )
214
+ {
215
+ $ vertices = array ($ vertex ->getId () => $ vertex );
216
+ foreach ($ this ->getVerticesChildren ($ vertex ) as $ vid => $ vertexChild ) {
217
+ if (isset ($ vertices [$ vid ])) {
218
+ throw new UnexpectedValueException ('Multiple links to child vertex found ' );
219
+ }
220
+ foreach ($ this ->getVerticesSubtree ($ vertexChild ) as $ vid => $ vertexSub ) {
221
+ if (isset ($ vertices [$ vid ])) {
222
+ throw new UnexpectedValueException ('Multiple links to vertex found ' );
223
+ }
224
+ $ vertices [$ vid ] = $ vertexSub ;
225
+ }
226
+ }
227
+
228
+ return $ vertices ;
229
+ }
230
+
231
+ /**
232
+ * get all vertices below the given $vertex (which is NOT included)
233
+ *
234
+ * think of this as the recursive version of getVerticesChildren()
235
+ *
236
+ * @param Vertex $vertex
237
+ * @return Vertex[]
238
+ * @throws UnexpectedValueException if there are invalid edges (check isTree()!)
239
+ * @uses self::getVerticesSubtree()
240
+ */
241
+ public function getVerticesDescendant (Vertex $ vertex )
242
+ {
243
+ $ vertices = $ this ->getVerticesSubtree ($ vertex );
244
+ unset($ vertices [$ vertex ->getId ()]);
245
+
246
+ return $ vertices ;
247
+ }
210
248
}
0 commit comments