public function getVerticesChildren(Vertex $vertex) { $vertices = $vertex->getVerticesEdgeTo(); if ($vertices->hasDuplicates()) { throw new UnexpectedValueException(); } return $vertices; }
protected function getVerticesAdjacent(Vertex $vertex) { if ($this->direction === self::DIRECTION_FORWARD) { return $vertex->getVerticesEdgeTo(); } elseif ($this->direction === self::DIRECTION_REVERSE) { return $vertex->getVerticesEdgeFrom(); } elseif ($this->direction === self::DIRECTION_BOTH) { return $vertex->getVerticesEdge(); } else { throw new DomainException('Should not happen. Invalid direction setting'); } }
/** * * calculates the recursive algorithm * * fills $this->visitedVertices * * @param Vertex $vertex */ private function recursiveDepthFirstSearch(Vertex $vertex, array &$visitedVertices) { // If I didn't visited this vertex before if (!isset($visitedVertices[$vertex->getId()])) { // Add Vertex to already visited vertices $visitedVertices[$vertex->getId()] = $vertex; // Get next vertices $nextVertices = $vertex->getVerticesEdgeTo(); foreach ($nextVertices as $nextVertix) { // recursive call for next vertices $this->recursiveDepthFirstSearch($nextVertix, $visitedVertices); } } }
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; } }