-
-
Notifications
You must be signed in to change notification settings - Fork 74
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 #34 from clue/move-algo-directed
Move Set::isDirected() helper to new Algorithm\Directed
- Loading branch information
Showing
7 changed files
with
116 additions
and
21 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,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; | ||
} | ||
} |
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
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,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()); | ||
} | ||
} |
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