Skip to content

Commit 20f700a

Browse files
committed
A null graph is not a valid tree
1 parent 3f4cc6e commit 20f700a

File tree

5 files changed

+17
-18
lines changed

5 files changed

+17
-18
lines changed

src/Tree/Base.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Fhaculty\Graph\Exception\UnexpectedValueException;
1111
use Fhaculty\Graph\Algorithm\Search\StrictDepthFirst;
1212
use Fhaculty\Graph\Algorithm\Degree;
13+
use Fhaculty\Graph\Algorithm\ConnectedComponents;
1314

1415
/**
1516
* Abstract base class for tree algorithms
@@ -18,7 +19,9 @@
1819
* graphs that represent a tree.
1920
*
2021
* A tree is a connected Graph (single component) with no cycles. Every Tree is
21-
* a Graph, but not every Graph is a Tree.
22+
* a Graph, but not every Graph is a Tree. A null Graph (a Graph with no Vertices
23+
* and thus no Edges) is *NOT* considered a valid Tree, as it is not considered
24+
* connected (@see ConnectedComponents and @link)
2225
*
2326
* A
2427
* / \
@@ -32,6 +35,7 @@
3235
*
3336
* @link http://en.wikipedia.org/wiki/Tree_%28graph_theory%29
3437
* @link http://en.wikipedia.org/wiki/Tree_%28data_structure%29
38+
* @link http://mathoverflow.net/questions/120536/is-the-empty-graph-a-tree
3539
* @see Undirected for an implementation of these algorithms on (undirected) trees
3640
* @see BaseDirected for an abstract implementation of these algorithms on directed, rooted trees
3741
*/

src/Tree/BaseDirected.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,11 @@ public function getVertexRoot()
7373
* checks if this is a tree
7474
*
7575
* @return boolean
76-
* @uses Vertices::isEmpty() to skip empty Graphs (an empty Graph is a valid tree)
7776
* @uses self::getVertexRoot() to get root Vertex to start search from
7877
* @uses self::getVerticesSubtree() to count number of vertices connected to root
7978
*/
8079
public function isTree()
8180
{
82-
if ($this->graph->getVertices()->isEmpty()) {
83-
return true;
84-
}
85-
8681
try {
8782
$root = $this->getVertexRoot();
8883
}
@@ -138,7 +133,7 @@ abstract public function getVerticesChildren(Vertex $vertex);
138133
/**
139134
* internal helper to get all parents vertices
140135
*
141-
* a valid tree vertex only ever has a single parent, expect for the root,
136+
* a valid tree vertex only ever has a single parent, except for the root,
142137
* which has none.
143138
*
144139
* @param Vertex $vertex

src/Tree/Undirected.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ class Undirected extends Tree
4949
* checks if this is a tree
5050
*
5151
* @return boolean
52-
* @uses Vertices::isEmpty() to skip empty Graphs (an empty Graph is a valid tree)
52+
* @uses Vertices::isEmpty() to skip null Graphs (a Graph with no Vertices is *NOT* a valid tree)
5353
* @uses Graph::getVertexFirst() to get get get random "root" Vertex to start search from
5454
* @uses self::getVerticesSubtreeRecursive() to count number of vertices connected to root
5555
*/
5656
public function isTree()
5757
{
5858
if ($this->graph->getVertices()->isEmpty()) {
59-
return true;
59+
return false;
6060
}
6161

6262
// every vertex can represent a root vertex, so just pick one

tests/Tree/BaseDirectedTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ abstract protected function createGraphTree();
3030
*/
3131
abstract protected function createGraphParallelEdge();
3232

33-
public function testEmptyGraph()
33+
public function testNullGraph()
3434
{
3535
$graph = new Graph();
3636

3737
$tree = $this->createTreeAlg($graph);
38-
$this->assertTrue($tree->isTree());
38+
$this->assertFalse($tree->isTree());
3939
$this->assertTrue($tree->getVerticesLeaf()->isEmpty());
4040
$this->assertTrue($tree->getVerticesInternal()->isEmpty());
4141

@@ -44,7 +44,7 @@ public function testEmptyGraph()
4444

4545
/**
4646
* @param BaseDirected $tree
47-
* @depends testEmptyGraph
47+
* @depends testNullGraph
4848
* @expectedException UnderflowException
4949
*/
5050
public function testEmptyGraphDoesNotHaveRootVertex(BaseDirected $tree)
@@ -54,7 +54,7 @@ public function testEmptyGraphDoesNotHaveRootVertex(BaseDirected $tree)
5454

5555
/**
5656
* @param BaseDirected $tree
57-
* @depends testEmptyGraph
57+
* @depends testNullGraph
5858
* @expectedException UnderflowException
5959
*/
6060
public function testEmptyGraphDoesNotHaveDegree(BaseDirected $tree)
@@ -64,7 +64,7 @@ public function testEmptyGraphDoesNotHaveDegree(BaseDirected $tree)
6464

6565
/**
6666
* @param BaseDirected $tree
67-
* @depends testEmptyGraph
67+
* @depends testNullGraph
6868
* @expectedException UnderflowException
6969
*/
7070
public function testEmptyGraphDoesNotHaveHeight(BaseDirected $tree)

tests/Tree/UndirectedTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ protected function createTree(Graph $graph)
1111
return new Undirected($graph);
1212
}
1313

14-
public function testGraphEmpty()
14+
public function testNullGraph()
1515
{
1616
$graph = new Graph();
1717

1818
$tree = $this->createTree($graph);
1919

20-
$this->assertTrue($tree->isTree());
21-
$this->assertSame(array(), $tree->getVerticesInternal()->getVector());
22-
$this->assertSame(array(), $tree->getVerticesLeaf()->getVector());
20+
$this->assertFalse($tree->isTree());
21+
$this->assertTrue($tree->getVerticesInternal()->isEmpty());
22+
$this->assertTrue($tree->getVerticesLeaf()->isEmpty());
2323
}
2424

2525
public function testGraphTrivial()

0 commit comments

Comments
 (0)