Skip to content

Commit

Permalink
Merge pull request #72 from clue/improve-tree-null
Browse files Browse the repository at this point in the history
A null graph is not a valid tree
  • Loading branch information
clue committed Sep 11, 2013
2 parents fcbdc1c + f2b6159 commit 4d413c7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ you spot any mistakes.
`OutOfBoundsException` for unreachable vertices
([#62](https://github.com/clue/graph/issues/62))

* BC break: A null Graph (a Graph with no Vertices and thus no Edges) is not a
valid tree (because it is not connected), adjust `Algorithm\Tree\Base::isTree()`
accordingly.
([#72](https://github.com/clue/graph/issues/72))

* Feature: Add `Algorithm\ShortestPath::hasVertex(Vertex $vertex)` to check whether
a path to the given Vertex exists ([#62](https://github.com/clue/graph/issues/62)).

Expand Down
6 changes: 5 additions & 1 deletion lib/Fhaculty/Graph/Algorithm/Tree/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Fhaculty\Graph\Exception\UnexpectedValueException;
use Fhaculty\Graph\Algorithm\Search\StrictDepthFirst;
use Fhaculty\Graph\Algorithm\Degree;
use Fhaculty\Graph\Algorithm\ConnectedComponents;

/**
* Abstract base class for tree algorithms
Expand All @@ -18,7 +19,9 @@
* graphs that represent a tree.
*
* A tree is a connected Graph (single component) with no cycles. Every Tree is
* a Graph, but not every Graph is a Tree.
* a Graph, but not every Graph is a Tree. A null Graph (a Graph with no Vertices
* and thus no Edges) is *NOT* considered a valid Tree, as it is not considered
* connected (@see ConnectedComponents and @link)
*
* A
* / \
Expand All @@ -32,6 +35,7 @@
*
* @link http://en.wikipedia.org/wiki/Tree_%28graph_theory%29
* @link http://en.wikipedia.org/wiki/Tree_%28data_structure%29
* @link http://mathoverflow.net/questions/120536/is-the-empty-graph-a-tree
* @see Undirected for an implementation of these algorithms on (undirected) trees
* @see BaseDirected for an abstract implementation of these algorithms on directed, rooted trees
*/
Expand Down
7 changes: 1 addition & 6 deletions lib/Fhaculty/Graph/Algorithm/Tree/BaseDirected.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,11 @@ public function getVertexRoot()
* checks if this is a tree
*
* @return boolean
* @uses Vertices::isEmpty() to skip empty Graphs (an empty Graph is a valid tree)
* @uses self::getVertexRoot() to get root Vertex to start search from
* @uses self::getVerticesSubtree() to count number of vertices connected to root
*/
public function isTree()
{
if ($this->graph->getVertices()->isEmpty()) {
return true;
}

try {
$root = $this->getVertexRoot();
}
Expand Down Expand Up @@ -138,7 +133,7 @@ abstract public function getVerticesChildren(Vertex $vertex);
/**
* internal helper to get all parents vertices
*
* a valid tree vertex only ever has a single parent, expect for the root,
* a valid tree vertex only ever has a single parent, except for the root,
* which has none.
*
* @param Vertex $vertex
Expand Down
4 changes: 2 additions & 2 deletions lib/Fhaculty/Graph/Algorithm/Tree/Undirected.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class Undirected extends Tree
* checks if this is a tree
*
* @return boolean
* @uses Vertices::isEmpty() to skip empty Graphs (an empty Graph is a valid tree)
* @uses Vertices::isEmpty() to skip null Graphs (a Graph with no Vertices is *NOT* a valid tree)
* @uses Graph::getVertexFirst() to get get get random "root" Vertex to start search from
* @uses self::getVerticesSubtreeRecursive() to count number of vertices connected to root
*/
public function isTree()
{
if ($this->graph->getVertices()->isEmpty()) {
return true;
return false;
}

// every vertex can represent a root vertex, so just pick one
Expand Down
10 changes: 5 additions & 5 deletions tests/Fhaculty/Graph/Algorithm/Tree/BaseDirectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ abstract protected function createGraphTree();
*/
abstract protected function createGraphParallelEdge();

public function testEmptyGraph()
public function testNullGraph()
{
$graph = new Graph();

$tree = $this->createTreeAlg($graph);
$this->assertTrue($tree->isTree());
$this->assertFalse($tree->isTree());
$this->assertTrue($tree->getVerticesLeaf()->isEmpty());
$this->assertTrue($tree->getVerticesInternal()->isEmpty());

Expand All @@ -44,7 +44,7 @@ public function testEmptyGraph()

/**
* @param BaseDirected $tree
* @depends testEmptyGraph
* @depends testNullGraph
* @expectedException UnderflowException
*/
public function testEmptyGraphDoesNotHaveRootVertex(BaseDirected $tree)
Expand All @@ -54,7 +54,7 @@ public function testEmptyGraphDoesNotHaveRootVertex(BaseDirected $tree)

/**
* @param BaseDirected $tree
* @depends testEmptyGraph
* @depends testNullGraph
* @expectedException UnderflowException
*/
public function testEmptyGraphDoesNotHaveDegree(BaseDirected $tree)
Expand All @@ -64,7 +64,7 @@ public function testEmptyGraphDoesNotHaveDegree(BaseDirected $tree)

/**
* @param BaseDirected $tree
* @depends testEmptyGraph
* @depends testNullGraph
* @expectedException UnderflowException
*/
public function testEmptyGraphDoesNotHaveHeight(BaseDirected $tree)
Expand Down
8 changes: 4 additions & 4 deletions tests/Fhaculty/Graph/Algorithm/Tree/UndirectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ protected function createTree(Graph $graph)
return new Undirected($graph);
}

public function testGraphEmpty()
public function testNullGraph()
{
$graph = new Graph();

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

$this->assertTrue($tree->isTree());
$this->assertSame(array(), $tree->getVerticesInternal()->getVector());
$this->assertSame(array(), $tree->getVerticesLeaf()->getVector());
$this->assertFalse($tree->isTree());
$this->assertTrue($tree->getVerticesInternal()->isEmpty());
$this->assertTrue($tree->getVerticesLeaf()->isEmpty());
}

public function testGraphTrivial()
Expand Down

0 comments on commit 4d413c7

Please sign in to comment.