/** * Convert a Graph to a phpDocumentor graph, usable for printing * * @param \Gliph\Graph\Digraph $graph The graph to print * * @return \phpDocumentor\GraphViz\Graph The copied graph */ protected function convertGraph(Digraph $graph) { $new = Graph::create("dump"); $nodes = new \SplObjectStorage(); $ctr = 0; foreach ($graph->vertices() as $vertex) { $nodes[$vertex] = Node::create('node_' . $ctr++, (string) $vertex); $new->setNode($nodes[$vertex]); } foreach ($graph->edges() as $edge) { $new->link(Edge::create($nodes[$edge[0]], $nodes[$edge[1]])); } return $new; }
/** * Finds source vertices in a Digraph, then enqueues them. * * @param Digraph $graph * @param DepthFirstVisitorInterface $visitor * * @return \SplQueue */ public static function find_sources(Digraph $graph, DepthFirstVisitorInterface $visitor) { $incomings = new \SplObjectStorage(); $queue = new \SplQueue(); foreach ($graph->edges() as $edge) { if (!isset($incomings[$edge[1]])) { $incomings[$edge[1]] = new \SplObjectStorage(); } $incomings[$edge[1]]->attach($edge[0]); } // Prime the queue with vertices that have no incoming edges. foreach ($graph->vertices() as $vertex) { if (!$incomings->contains($vertex)) { $queue->push($vertex); $visitor->onInitializeVertex($vertex, TRUE, $queue); } else { $visitor->onInitializeVertex($vertex, FALSE, $queue); } } return $queue; }