graph->getVertices()->getVector()) as $vertex) { $this->visit($vertex, $visited, $tsl); } return new Vertices(array_reverse($tsl, true)); } protected function visit(Vertex $vertex, array &$visited, array &$tsl) { $vid = $vertex->getId(); if (isset($visited[$vid])) { if ($visited[$vid] === false) { // temporary mark => not a DAG throw new UnexpectedValueException('Not a DAG'); } // otherwise already marked/visisted => no need to check again } else { // temporary mark $visited[$vid] = false; foreach (array_reverse($vertex->getVerticesEdgeTo()->getVector()) as $v) { $this->visit($v, $visited, $tsl); } // mark as visited and include in result $visited[$vid] = true; $tsl[$vid] = $vertex; } } }