Skip to content

Commit 5338a8b

Browse files
committed
Move Graph::isComplete() to Algorithm\Complete::isComplete()
1 parent abf4ca6 commit 5338a8b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/Complete.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Fhaculty\Graph\Algorithm;
4+
5+
use Fhaculty\Graph\Graph;
6+
7+
class Complete extends Base
8+
{
9+
/**
10+
*
11+
* @var Graph
12+
*/
13+
private $graph;
14+
15+
public function __construct(Graph $graph)
16+
{
17+
$this->graph = $graph;
18+
}
19+
20+
/**
21+
* checks whether this graph is complete (every vertex has an edge to any other vertex)
22+
*
23+
* @return boolean
24+
* @uses Graph::getVertices()
25+
* @uses Vertex::hasEdgeTo()
26+
*/
27+
public function isComplete()
28+
{
29+
// copy of array (separate iterator but same vertices)
30+
$c = $vertices = $this->graph->getVertices();
31+
// from each vertex
32+
foreach ($vertices as $vertex) {
33+
// to each vertex
34+
foreach ($c as $other) {
35+
// missing edge => fail
36+
if ($other !== $vertex && !$vertex->hasEdgeTo($other)) {
37+
return false;
38+
}
39+
}
40+
}
41+
42+
return true;
43+
}
44+
}

0 commit comments

Comments
 (0)