Exemplo n.º 1
0
 /**
  * Finds source vertices in a DirectedGraph, then enqueues them.
  *
  * @param DirectedGraph $graph
  * @param DepthFirstVisitorInterface $visitor
  *
  * @return \SplQueue
  */
 public static function find_sources(DirectedGraph $graph, DepthFirstVisitorInterface $visitor)
 {
     $incomings = new \SplObjectStorage();
     $queue = new \SplQueue();
     $graph->eachEdge(function ($edge) use(&$incomings) {
         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.
     $graph->eachVertex(function ($vertex) use($queue, $incomings, $visitor) {
         if (!$incomings->contains($vertex)) {
             $queue->push($vertex);
             // TRUE second param indicates source vertex
             $visitor->onInitializeVertex($vertex, TRUE, $queue);
         } else {
             $visitor->onInitializeVertex($vertex, FALSE, $queue);
         }
     });
     return $queue;
 }
Exemplo n.º 2
0
 /**
  * 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;
 }