Skip to content
Open
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
43 changes: 43 additions & 0 deletions src/Property/GraphProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,47 @@ public function isTrivial()
{
return ($this->graph->getEdges()->isEmpty() && \count($this->graph->getVertices()) === 1);
}

/**
* checks whether this graph is acyclic (directed graph with no cycles)
* using the Kahn algorithm
*
* @return boolean
* @link https://en.wikipedia.org/wiki/Directed_acyclic_graph
*/
public function isAcyclic()
{
$vertices = $this->graph->getVertices();
$nVertices = count($vertices);
$visited = 0;
$inDegree = array();
$stack = array();

foreach($vertices as $vert){
$deg=count($vert->getEdgesIn());
$inDegree[$vert->getId()]=$deg;
if($deg==0){
\array_push($stack,$vert);
}
}

while(!(empty($stack))){
$n = array_pop($stack);
$visited++;
foreach($n->getEdgesOut() as $e){
$m = $e->getVertexEnd();
$inDegree[$m->getId()]--;
if($inDegree[$m->getId()]==0){
\array_push($stack,$m);
}
}
}

if($visited==$nVertices){
return true;
}
else{
return false;
}
}
}
46 changes: 46 additions & 0 deletions tests/Property/PropertyGraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function testEmptyIsEdgeless()
$this->assertTrue($alg->isNull());
$this->assertTrue($alg->isEdgeless());
$this->assertFalse($alg->isTrivial());
$this->assertTrue($alg->isAcyclic());
}

public function testSingleVertexIsTrivial()
Expand All @@ -26,5 +27,50 @@ public function testSingleVertexIsTrivial()
$this->assertFalse($alg->isNull());
$this->assertTrue($alg->isEdgeless());
$this->assertTrue($alg->isTrivial());
$this->assertTrue($alg->isAcyclic());
}

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

$alg = new GraphProperty($graph);

$this->assertFalse($alg->isNull());
$this->assertFalse($alg->isEdgeless());
$this->assertFalse($alg->isTrivial());
$this->assertFalse($alg->isAcyclic());
}

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

$alg = new GraphProperty($graph);

$this->assertFalse($alg->isNull());
$this->assertFalse($alg->isEdgeless());
$this->assertFalse($alg->isTrivial());
$this->assertTrue($alg->isAcyclic());
}

public function testGraphWithCycleIsAcyclic()
{
$graph = new Graph();
$vertexOne = $graph->createVertex(1);
$vertexTwo = $graph->createVertex(2);
$vertexThree = $graph->createVertex(3);
$vertexOne->createEdgeTo($vertexTwo);
$vertexTwo->createEdgeTo($vertexThree);
$vertexThree->createEdgeTo($vertexOne);

$alg = new GraphProperty($graph);

$this->assertFalse($alg->isNull());
$this->assertFalse($alg->isEdgeless());
$this->assertFalse($alg->isTrivial());
$this->assertFalse($alg->isAcyclic());
}
}