Skip to content

Commit e8e12cb

Browse files
committed
Move Graph::isSymmetric() to new Algorithm\Symmetric
1 parent 57790ae commit e8e12cb

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/Symmetric.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Fhaculty\Graph\Algorithm;
4+
5+
use Fhaculty\Graph\Edge\Directed as EdgeDirected;
6+
use Fhaculty\Graph\Graph;
7+
use Fhaculty\Graph\Vertex;
8+
9+
class Symmetric extends Base
10+
{
11+
/**
12+
*
13+
* @var Graph
14+
*/
15+
private $graph;
16+
17+
public function __construct(Graph $graph)
18+
{
19+
$this->graph = $graph;
20+
}
21+
22+
/**
23+
* checks whether this graph is symmetric (for every edge a->b there's also an edge b->a)
24+
*
25+
* @return boolean
26+
* @uses Graph::getEdges()
27+
* @uses EdgeDirected::getVertexStart()
28+
* @uses EdgeDirected::getVertedEnd()
29+
* @uses Vertex::hasEdgeTo()
30+
*/
31+
public function isSymmetric()
32+
{
33+
// check all edges
34+
foreach ($this->graph->getEdges() as $edge) {
35+
// only check directed edges (undirected ones are symmetric by definition)
36+
if ($edge instanceof EdgeDirected) {
37+
// check if end also has an edge to start
38+
if (!$edge->getVertexEnd()->hasEdgeTo($edge->getVertexStart())) {
39+
return false;
40+
}
41+
}
42+
}
43+
44+
return true;
45+
}
46+
}

0 commit comments

Comments
 (0)