public function isCyclic(Graph $graph)
    {
        // prepare stack
        $recursionStack = [];
        foreach ($graph->all() as $node) {
            $recursionStack[$node->getKey()] = false;
        }

        // start analysis
        $isCyclic = false;
        foreach ($graph->getEdges() as $edge) {
            if ($r = $this->detectCycle($edge->getFrom(), $recursionStack)) {
                $edge->cyclic = true;
                $isCyclic = true;
            }

            $recursionStack[$node->getKey()] = false;
        }

        $graph->resetVisits();

        return $isCyclic;
    }
public function isCyclic(Graph $graph)
    {
        // prepare stack
        $recursionStack = [];
        foreach ($graph->all() as $node) {
            $recursionStack[$node->getKey()] = false;
        }

        // start analysis
        $isCyclic = false;
        foreach ($graph->getEdges() as $edge) {
            if ($r = $this->detectCycle($edge->getFrom(), $recursionStack)) {
                $edge->cyclic = true;
                $isCyclic = true;
            }

            $recursionStack[$node->getKey()] = false;
        }

        $graph->resetVisits();

        return $isCyclic;
    }
public function isCyclic ( Graph $ graph ) { // prepare stack $ recursionStack = [ ] ; foreach ( $ graph -> all ( ) as $ node ) { $ recursionStack [ $ node -> getKey ( ) ] = false ; } // start analysis $ isCyclic = false ; foreach ( $ graph -> getEdges ( ) as $ edge ) { if ( $ r = $ this -> detectCycle ( $ edge -> getFrom ( ) , $ recursionStack ) ) { $ edge -> cyclic = true ; $ isCyclic = true ; } $ recursionStack [ $ node -> getKey ( ) ] = false ; } $ graph -> resetVisits ( ) ; return $ isCyclic ; }
Check if graph contains cycle

Each node in cycle is flagged with the "cyclic" attribute

@param Graph $graph
@return bool
Check if graph contains cycle