-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from clue/move-algo-symmetric
Move Graph::isSymmetric() helper to new Algorithm\Symmetric
- Loading branch information
Showing
4 changed files
with
122 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Fhaculty\Graph\Algorithm; | ||
|
||
use Fhaculty\Graph\Edge\Directed as EdgeDirected; | ||
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; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
use Fhaculty\Graph\Algorithm\Symmetric as AlgorithmSymmetric; | ||
use Fhaculty\Graph\Graph; | ||
|
||
class SymmetricTest extends TestCase | ||
{ | ||
public function testGraphEmpty() | ||
{ | ||
$graph = new Graph(); | ||
|
||
$alg = new AlgorithmSymmetric($graph); | ||
|
||
$this->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()); | ||
} | ||
} |