Skip to content

Commit

Permalink
Merge pull request #80 from clue/directed
Browse files Browse the repository at this point in the history
Rename Algorithm\Directed::isDirected()
  • Loading branch information
clue committed Nov 26, 2013
2 parents 2769dc6 + bd92553 commit 52fc5d8
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ you spot any mistakes.

## 0.8.0 (2013-xx-xx)

* BC break: Rename `Algorithm\Directed::isDirected()` to remove its ambiguity
in regards to mixed and/or empty graphs
([#80](https://github.com/clue/graph/issues/80))

| Old name | New name |
|---|---|
| `Algorithm\Directed::isDirected()` | `Algorithm\Directed::hasDirected()` |

* Feature:: Add new `Algorithm\Directed::hasUndirected()` and
`Algorithm\Directed::isMixed()` in order to complement the renamed
`Algorithm\Directed::hasDirected()`
([#80](https://github.com/clue/graph/issues/80))

* Fix: Throwing an `UnexpectedValueException` if writing GraphViz Dot script
to a temporary file fails and remove its debugging output
([#77](https://github.com/clue/graph/issues/77) and [#78](https://github.com/clue/graph/issues/78) @Metabor)
Expand Down
39 changes: 37 additions & 2 deletions lib/Fhaculty/Graph/Algorithm/Directed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fhaculty\Graph\Algorithm\BaseGraph;
use Fhaculty\Graph\Edge\Directed as EdgeDirected;
use Fhaculty\Graph\Edge\Undirected as EdgeUndirected;

/**
* Basic algorithms for working with the undirected or directed Graphs (digraphs) / Walks.
Expand All @@ -14,11 +15,14 @@
class Directed extends BaseDual
{
/**
* checks whether the graph has any directed edges (aka digraph)
* checks whether the graph has any directed edges
*
* This method is intentionally not named "isDirected()" (aka digraph),
* because that might be misleading in regards to empty and/or mixed graphs.
*
* @return boolean
*/
public function isDirected()
public function hasDirected()
{
foreach ($this->set->getEdges() as $edge) {
if ($edge instanceof EdgeDirected) {
Expand All @@ -28,4 +32,35 @@ public function isDirected()

return false;
}

/**
* checks whether the graph has any undirected edges
*
* This method is intentionally not named "isUndirected()",
* because that might be misleading in regards to empty and/or mixed graphs.
*
* @return boolean
*/
public function hasUndirected()
{
foreach ($this->set->getEdges() as $edge) {
if ($edge instanceof EdgeUndirected) {
return true;
}
}

return false;
}

/**
* checks whether this is a mixed graph (contains both directed and undirected edges)
*
* @return boolean
* @uses self::hasDirected()
* @uses self::hasUndirected()
*/
public function isMixed()
{
return ($this->hasDirected() && $this->hasUndirected());
}
}
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Algorithm/MaximumMatching/Flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Flow extends Base
public function getEdges()
{
$alg = new Directed($this->graph);
if ($alg->isDirected()) {
if ($alg->hasDirected()) {
throw new UnexpectedValueException('Input graph contains directed edges');
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Fhaculty/Graph/GraphViz.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,14 @@ public function createImageFile()
* create graphviz script representing this graph
*
* @return string
* @uses Directed::isDirected()
* @uses Directed::hasDirected()
* @uses Graph::getVertices()
* @uses Graph::getEdges()
*/
public function createScript()
{
$alg = new Directed($this->graph);
$directed = $alg->isDirected();
$directed = $alg->hasDirected();

$script = ($directed ? 'di':'') . 'graph G {' . self::EOL;

Expand Down
16 changes: 12 additions & 4 deletions tests/Fhaculty/Graph/Algorithm/DirectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public function testGraphEmpty()

$alg = new AlgorithmDirected($graph);

$this->assertFalse($alg->isDirected());
$this->assertFalse($alg->hasDirected());
$this->assertFalse($alg->hasUndirected());
$this->assertFalse($alg->isMixed());
}

public function testGraphUndirected()
Expand All @@ -22,7 +24,9 @@ public function testGraphUndirected()

$alg = new AlgorithmDirected($graph);

$this->assertFalse($alg->isDirected());
$this->assertFalse($alg->hasDirected());
$this->assertTrue($alg->hasUndirected());
$this->assertFalse($alg->isMixed());
}

public function testGraphDirected()
Expand All @@ -33,7 +37,9 @@ public function testGraphDirected()

$alg = new AlgorithmDirected($graph);

$this->assertTrue($alg->isDirected());
$this->assertTrue($alg->hasDirected());
$this->assertFalse($alg->hasUndirected());
$this->assertFalse($alg->isMixed());
}

public function testGraphMixed()
Expand All @@ -45,6 +51,8 @@ public function testGraphMixed()

$alg = new AlgorithmDirected($graph);

$this->assertTrue($alg->isDirected());
$this->assertTrue($alg->hasDirected());
$this->assertTrue($alg->hasUndirected());
$this->assertTrue($alg->isMixed());
}
}
2 changes: 1 addition & 1 deletion tests/Fhaculty/Graph/Loader/CompleteGraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testDirected()
$this->assertEquals($n*($n-1), count($graph->getEdges())); // n*(n-1) for directed graphs

$alg = new Directed($graph);
$this->assertTrue($alg->isDirected());
$this->assertTrue($alg->hasDirected());

$alg = new Complete($graph);
$this->assertTrue($alg->isComplete());
Expand Down

0 comments on commit 52fc5d8

Please sign in to comment.