Example #1
0
 /**
  * Breitensuche
  *
  * Algorithmus aus C. Cormen / E. Leiserson / R. Rivest / C. Stein: Algorithmen - Eine Einführung
  * @param Graph $g der Graph sollte nur aus GraphBFSVertice bestehen (das ist selber zu überprüfen aus Performancegründen)
  * @param GraphBFSVertice $s der Startknoten
  */
 public static function BFS(Graph $g, BFSVertice $s)
 {
     if (!$g->has($s)) {
         throw new Exception('Startknoten ' . $s . ' ist nicht im Graphen g vorhanden');
     }
     foreach ($g->V() as $vertice) {
         $vertice->color = 'weiss';
         $vertice->d = PHP_INT_MAX;
         $vertice->parent = NULL;
     }
     $s->color = 'grau';
     $s->parent = NULL;
     $s->d = 0;
     $queue = array($s);
     while (count($queue) > 0) {
         $u = array_shift($queue);
         foreach ($g->N($u) as $v) {
             if ($v->color == 'weiss') {
                 $v->color = 'grau';
                 $v->d = $u->d + 1;
                 $v->parent = $u;
                 $u->childs[] = $v;
                 array_push($queue, $v);
             }
         }
         $u->color = 'schwarz';
     }
 }