Skip to content

Commit 7cd1e3c

Browse files
committed
Fix checking null graph
1 parent 099d7d4 commit 7cd1e3c

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

lib/Fhaculty/Graph/Algorithm/ConnectedComponents.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
use Fhaculty\Graph\Algorithm\BaseGraph;
66
use Fhaculty\Graph\Algorithm\Search\BreadthFirst as SearchBreadthFirst;
7-
8-
use Fhaculty\Graph\Exception\InvalidArgumentException;
9-
107
use Fhaculty\Graph\Graph;
118
use Fhaculty\Graph\Vertex;
9+
use Fhaculty\Graph\Exception\InvalidArgumentException;
10+
use Fhaculty\Graph\Exception\UnderflowException;
1211

1312
class ConnectedComponents extends BaseGraph
1413
{
@@ -50,7 +49,14 @@ private function createSearch(Vertex $vertex)
5049
*/
5150
public function isSingle()
5251
{
53-
$alg = $this->createSearch($this->graph->getVertexFirst());
52+
try {
53+
$vertex = $this->graph->getVertexFirst();
54+
}
55+
catch (UnderflowException $e) {
56+
// no first vertex => empty graph => has zero components
57+
return false;
58+
}
59+
$alg = $this->createSearch($vertex);
5460

5561
return ($this->graph->getNumberOfVertices() === $alg->getNumberOfVertices());
5662
}

0 commit comments

Comments
 (0)