From 87f3950fb5a9c4be9ba4f3d9e521f9ce2aee5b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 7 Jun 2013 09:49:38 +0200 Subject: [PATCH 1/4] Move Graph::isSymmetric() to new Algorithm\Symmetric --- lib/Fhaculty/Graph/Algorithm/Symmetric.php | 46 ++++++++++++++++++++++ lib/Fhaculty/Graph/Graph.php | 24 ----------- 2 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 lib/Fhaculty/Graph/Algorithm/Symmetric.php diff --git a/lib/Fhaculty/Graph/Algorithm/Symmetric.php b/lib/Fhaculty/Graph/Algorithm/Symmetric.php new file mode 100644 index 00000000..1fafddf0 --- /dev/null +++ b/lib/Fhaculty/Graph/Algorithm/Symmetric.php @@ -0,0 +1,46 @@ +graph = $graph; + } + + /** + * checks whether this graph is symmetric (for every edge a->b there's also an edge b->a) + * + * @return boolean + * @uses Graph::getEdges() + * @uses EdgeDirected::getVertexStart() + * @uses EdgeDirected::getVertedEnd() + * @uses Vertex::hasEdgeTo() + */ + public function isSymmetric() + { + // check all edges + foreach ($this->graph->getEdges() as $edge) { + // only check directed edges (undirected ones are symmetric by definition) + if ($edge instanceof EdgeDirected) { + // check if end also has an edge to start + if (!$edge->getVertexEnd()->hasEdgeTo($edge->getVertexStart())) { + return false; + } + } + } + + return true; + } +} diff --git a/lib/Fhaculty/Graph/Graph.php b/lib/Fhaculty/Graph/Graph.php index 5c40877c..b2ff8ff3 100644 --- a/lib/Fhaculty/Graph/Graph.php +++ b/lib/Fhaculty/Graph/Graph.php @@ -328,30 +328,6 @@ public function isTrivial() return (!$this->edges && count($this->vertices) === 1); } - /** - * checks whether this graph is symmetric (for every edge a->b there's also an edge b->a) - * - * @return boolean - * @uses EdgeDirected::getVertexStart() - * @uses EdgeDirected::getVertedEnd() - * @uses Vertex::hasEdgeTo() - */ - public function isSymmetric() - { - // check all edges - foreach ($this->edges as $edge) { - // only check directed edges (undirected ones are symmetric by definition) - if ($edge instanceof EdgeDirected) { - // check if end also has an edge to start - if (!$edge->getVertexEnd()->hasEdgeTo($edge->getVertexStart())) { - return false; - } - } - } - - return true; - } - /** * checks whether this graph has any parallel edges (aka multigraph) * From c3adfdc3aeb4ab2623a83c1e1b7ae1a437ce30ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 7 Jun 2013 09:56:49 +0200 Subject: [PATCH 2/4] Add tests with 100% coverage for Algorithm\Symmetric --- .../Graph/Algorithm/SymmetricTest.php | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/Fhaculty/Graph/Algorithm/SymmetricTest.php diff --git a/tests/Fhaculty/Graph/Algorithm/SymmetricTest.php b/tests/Fhaculty/Graph/Algorithm/SymmetricTest.php new file mode 100644 index 00000000..cd689aaf --- /dev/null +++ b/tests/Fhaculty/Graph/Algorithm/SymmetricTest.php @@ -0,0 +1,61 @@ +assertTrue($alg->isSymmetric()); + } + + public function testGraphIsolated() + { + $graph = new Graph(); + $graph->createVertex(1); + $graph->createVertex(2); + + $alg = new AlgorithmSymmetric($graph); + + $this->assertTrue($alg->isSymmetric()); + } + + public function testGraphSingleArcIsNotSymmetricr() + { + // 1 -> 2 + $graph = new Graph(); + $graph->createVertex(1)->createEdgeTo($graph->createVertex(2)); + + $alg = new AlgorithmSymmetric($graph); + + $this->assertFalse($alg->isSymmetric()); + } + + public function testGraphAntiparallelIsSymmetricr() + { + // 1 -> 2 -> 1 + $graph = new Graph(); + $graph->createVertex(1)->createEdgeTo($graph->createVertex(2)); + $graph->getVertex(2)->createEdgeTo($graph->getVertex(1)); + + $alg = new AlgorithmSymmetric($graph); + + $this->assertTrue($alg->isSymmetric()); + } + + public function testGraphSingleUndirectedIsSymmetricr() + { + // 1 -- 2 + $graph = new Graph(); + $graph->createVertex(1)->createEdge($graph->createVertex(2)); + + $alg = new AlgorithmSymmetric($graph); + + $this->assertTrue($alg->isSymmetric()); + } +} From 5af3262b596fb4ee8886daee4963a78d3bb80577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 7 Jun 2013 10:05:57 +0200 Subject: [PATCH 3/4] Document new Algorithm\Symmetric --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b82d19f..7f0eb5dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ you spot any mistakes. * 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) +* BC break: Move `Graph::isSymmetric()` to new `Algorithm\Symmetric` (#41) * BC break: Remove unneeded algorithm alias definitions to reduce complexity, improve testability and avoid tight coupling (#31) * `Graph::isConnected()` (=> `Algorithm\ConnectedComponents::isSingle()`) * `Graph::hasEulerianCycle()` (=> `Algorithm\Eulerian::hasCycle()`) From 640468e2a279acd32a149f5dcc39c6bdf0917cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 7 Jun 2013 10:10:40 +0200 Subject: [PATCH 4/4] Add documentation --- lib/Fhaculty/Graph/Algorithm/Symmetric.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/Fhaculty/Graph/Algorithm/Symmetric.php b/lib/Fhaculty/Graph/Algorithm/Symmetric.php index 1fafddf0..bd16993f 100644 --- a/lib/Fhaculty/Graph/Algorithm/Symmetric.php +++ b/lib/Fhaculty/Graph/Algorithm/Symmetric.php @@ -6,14 +6,28 @@ use Fhaculty\Graph\Graph; use Fhaculty\Graph\Vertex; +/** + * Basic algorithms for working with symmetric digraphs + * + * A directed graph is called symmetric if, for every arc that belongs to it, + * the corresponding reversed arc (antiparallel directed edge) also belongs to it. + * + * @link http://en.wikipedia.org/wiki/Directed_graph#Classes_of_digraphs + */ class Symmetric extends Base { /** - * + * Graph to operate on + * * @var Graph */ private $graph; + /** + * instantiate symmetric algorithm + * + * @param Graph $graph Graph + */ public function __construct(Graph $graph) { $this->graph = $graph;