Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Set::isDirected() helper to new Algorithm\Directed #34

Merged
merged 4 commits into from
Jun 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ you spot any mistakes.
* BC break: Move `Set::getWeight()`, `Set::getWeightFlow()`, `Set::getWeightMin()` and `Set::isWeighted()` to new `Algorithm\Weight` (#33)
* BC break: Move `Graph::getDegree()`, `Graph::getDegreeMin()`, `Graph::getDegreeMax()`, `Graph::isRegular()` and `Graph::isBalanced()` to new `Algorithm\Degree` (#29)
* BC break: Move `Graph::getBalance()` and `Graph::isBalancedFlow()` to new `Algorithm\Balance` (#30)
* BC break: Move `Set::isDirected()` to new `Algorithm\Directed` (#34)
* Feature: `Graph::createVertices()` now also accepts an array of vertex IDs (#19)
* Fix: Various issues with `Vertex`/`Edge` layout attributes (#32)

Expand Down
51 changes: 51 additions & 0 deletions lib/Fhaculty/Graph/Algorithm/Directed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Fhaculty\Graph\Algorithm;

use Fhaculty\Graph\Set;
use Fhaculty\Graph\Algorithm\Base;
use Fhaculty\Graph\Edge\Directed as EdgeDirected;
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Walk;

/**
* Basic algorithms for working with the undirected or directed Graphs (digraphs) / Walks.
*
* @link http://en.wikipedia.org/wiki/Glossary_of_graph_theory#Direction
* @link http://en.wikipedia.org/wiki/Digraph_%28mathematics%29
*/
class Directed extends Base
{
/**
* Graph/Walk to operate on
*
* @var Set
*/
private $set;

/**
* instanciate new directed algorithm
*
* @param Set|Graph|Walk $graphOrWalk either the Graph or Walk to operate on (or the common base class Set)
*/
public function __construct(Set $graphOrWalk)
{
$this->set = $graphOrWalk;
}

/**
* checks whether the graph has any directed edges (aka digraph)
*
* @return boolean
*/
public function isDirected()
{
foreach ($this->set->getEdges() as $edge) {
if ($edge instanceof EdgeDirected) {
return true;
}
}

return false;
}
}
5 changes: 4 additions & 1 deletion lib/Fhaculty/Graph/Algorithm/MaximumMatching/Flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Fhaculty\Graph\Algorithm\MaximumMatching;

use Fhaculty\Graph\Algorithm\Directed;

use Fhaculty\Graph\Exception\LogicException;

use Fhaculty\Graph\Exception\UnexpectedValueException;
Expand All @@ -14,7 +16,8 @@ class Flow extends Base
{
public function getEdges()
{
if ($this->graph->isDirected()) {
$alg = new Directed($this->graph);
if ($alg->isDirected()) {
throw new UnexpectedValueException('Input graph contains directed edges');
}

Expand Down
6 changes: 4 additions & 2 deletions lib/Fhaculty/Graph/GraphViz.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fhaculty\Graph;

use Fhaculty\Graph\Algorithm\Directed;
use Fhaculty\Graph\Algorithm\Groups;
use Fhaculty\Graph\Exception\UnexpectedValueException;
use Fhaculty\Graph\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -269,13 +270,14 @@ public function createImageFile()
* create graphviz script representing this graph
*
* @return string
* @uses Graph::isDirected()
* @uses Directed::isDirected()
* @uses Graph::getVertices()
* @uses Graph::getEdges()
*/
public function createScript()
{
$directed = $this->graph->isDirected();
$alg = new Directed($this->graph);
$directed = $alg->isDirected();

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

Expand Down
17 changes: 0 additions & 17 deletions lib/Fhaculty/Graph/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Fhaculty\Graph;

use Fhaculty\Graph\Edge\Base as Edge;
use Fhaculty\Graph\Edge\Directed as EdgeDirected;

/**
*
Expand Down Expand Up @@ -65,22 +64,6 @@ public function getNumberOfEdges()
return count($this->edges);
}

/**
* checks whether the graph has any directed edges (aka digraph)
*
* @return boolean
*/
public function isDirected()
{
foreach ($this->edges as $edge) {
if ($edge instanceof EdgeDirected) {
return true;
}
}

return false;
}

/**
* check if this graph has any flow set (any edge has a non-NULL flow)
*
Expand Down
50 changes: 50 additions & 0 deletions tests/Fhaculty/Graph/Algorithm/DirectedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use Fhaculty\Graph\Algorithm\Directed as AlgorithmDirected;
use Fhaculty\Graph\Graph;

class DirectedTest extends TestCase
{
public function testGraphEmpty()
{
$graph = new Graph();

$alg = new AlgorithmDirected($graph);

$this->assertFalse($alg->isDirected());
}

public function testGraphUndirected()
{
// 1 -- 2
$graph = new Graph();
$graph->createVertex(1)->createEdge($graph->createVertex(2));

$alg = new AlgorithmDirected($graph);

$this->assertFalse($alg->isDirected());
}

public function testGraphDirected()
{
// 1 -> 2
$graph = new Graph();
$graph->createVertex(1)->createEdgeTo($graph->createVertex(2));

$alg = new AlgorithmDirected($graph);

$this->assertTrue($alg->isDirected());
}

public function testGraphMixed()
{
// 1 -- 2 -> 3
$graph = new Graph();
$graph->createVertex(1)->createEdge($graph->createVertex(2));
$graph->getVertex(2)->createEdgeTo($graph->createVertex(3));

$alg = new AlgorithmDirected($graph);

$this->assertTrue($alg->isDirected());
}
}
7 changes: 6 additions & 1 deletion tests/Fhaculty/Graph/Loader/CompleteGraphTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Fhaculty\Graph\Algorithm\Directed;

use Fhaculty\Graph\Graph;

use Fhaculty\Graph\Loader\CompleteGraph;
Expand Down Expand Up @@ -38,7 +40,10 @@ public function testDirected()

$this->assertEquals($n, $graph->getNumberOfVertices());
$this->assertEquals($n*($n-1), $graph->getNumberOfEdges()); // n*(n-1) for directed graphs
$this->assertTrue($graph->isDirected());

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

$this->assertTrue($graph->isComplete());
}
}