Skip to content

Commit ab1bbdb

Browse files
committed
Add depth-first search for checking tree
1 parent a9dc7fd commit ab1bbdb

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

src/Tree/BaseDirected.php

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use Fhaculty\Graph\Algorithm\Tree\Base as Tree;
66
use Fhaculty\Graph\Exception\UnderflowException;
77
use Fhaculty\Graph\Exception\UnexpectedValueException;
8-
use Fhaculty\Graph\Algorithm\Search\Base as Search;
9-
use Fhaculty\Graph\Algorithm\Search\StrictDepthFirst;
108
use Fhaculty\Graph\Vertex;
119

1210
/**
@@ -16,8 +14,6 @@
1614
*/
1715
abstract class BaseDirected extends Tree
1816
{
19-
const DIRECTION_CHILDREN = -1;
20-
2117
/**
2218
* get root vertex for this in-tree
2319
*
@@ -29,12 +25,7 @@ public function getVertexRoot()
2925
{
3026
$root = $this->getVertexPossibleRoot();
3127

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()) {
3829
throw new UnexpectedValueException();
3930
}
4031

@@ -207,4 +198,51 @@ public function getHeightVertex(Vertex $vertex)
207198
}
208199
return $max;
209200
}
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+
}
210248
}

src/Tree/InTree.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Fhaculty\Graph\Algorithm\Tree\BaseDirected as DirectedTree;
66
use Fhaculty\Graph\Exception\UnexpectedValueException;
7-
use Fhaculty\Graph\Algorithm\Search\Base as Search;
87
use Fhaculty\Graph\Vertex;
98

109
/**
@@ -14,8 +13,6 @@
1413
*/
1514
class InTree extends DirectedTree
1615
{
17-
const DIRECTION_CHILDREN = Search::DIRECTION_REVERSE;
18-
1916
public function getVerticesChildren(Vertex $vertex)
2017
{
2118
return $vertex->getVerticesEdgeFrom();

src/Tree/OutTree.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Fhaculty\Graph\Algorithm\Tree\BaseDirected as DirectedTree;
66
use Fhaculty\Graph\Exception\UnexpectedValueException;
7-
use Fhaculty\Graph\Algorithm\Search\StrictDepthFirst;
87
use Fhaculty\Graph\Vertex;
98

109
/**
@@ -15,8 +14,6 @@
1514
*/
1615
class OutTree extends DirectedTree
1716
{
18-
const DIRECTION_CHILDREN = StrictDepthFirst::DIRECTION_FORWARD;
19-
2017
public function getVerticesChildren(Vertex $vertex)
2118
{
2219
return $vertex->getVerticesEdgeTo();

0 commit comments

Comments
 (0)