/** * @param Digraph $graph * @param int $vertex */ private function depthSearch(Digraph $graph, int $vertex) { $this->onStack[$vertex] = true; $this->marked[$vertex] = true; foreach ($graph->adjacent($vertex) as $item) { if ($this->hasCircle()) { return; } else { if (!isset($this->marked[$item])) { $this->edgeTo[$item] = $vertex; $this->depthSearch($graph, $item); } else { if ($this->onStack[$item]) { $this->circle = new Stack(); for ($x = $vertex; $x !== $item; $x = $this->edgeTo[$x]) { $this->circle->add($x); } $this->circle->add($vertex); $this->circle->add($item); } } } } $this->onStack[$vertex] = false; }