Example #1
2
 public function testBFS()
 {
     $Graph = ['A' => ['B', 'C'], 'B' => ['A', 'D'], 'D' => ['B'], 'C' => ['A']];
     $this->assertTrue(bfs($Graph, 'A', 'D'));
     $this->assertFalse(bfs($Graph, 'A', 'F'));
 }
Example #2
0
$root->left = new Node(4);
$root->right = new Node(3);
$root->left->left = new Node(6);
$root->left->right = new Node(7);
$root->right->left = new Node(8);
$root->right->right = new Node(9);
function bfs(Node $node)
{
    $queue = array($node);
    $node->hd = 0;
    $output = array();
    while (count($queue) > 0) {
        $current_node = array_shift($queue);
        $output[$current_node->hd][] = $current_node->info;
        if ($current_node->left != null) {
            $current_node->left->hd = $current_node->hd + 1;
            array_push($queue, $current_node->left);
        }
        if ($current_node->right != NULL) {
            $current_node->right->hd = $current_node->hd - 1;
            array_push($queue, $current_node->right);
        }
    }
    return $output;
}
$result = bfs($root);
$keys = array_keys($result);
arsort($keys);
foreach ($keys as $key) {
    echo implode(' ', $result[$key]), "\n";
}
Example #3
0
$dag = array(array(0, 1, 1, 0, 0, 0, 0), array(0, 0, 0, 1, 0, 0, 0), array(0, 0, 0, 0, 1, 0, 0), array(0, 0, 0, 0, 1, 0, 0), array(0, 0, 0, 0, 0, 0, 1), array(0, 0, 0, 0, 0, 0, 1), array(0, 0, 0, 0, 0, 0, 0));
function bfs(&$graph, $start, $visited)
{
    // create an empty queue
    $q = array();
    // initially enqueue only the starting vertex
    array_push($q, $start);
    $visited[$start] = 1;
    echo $start . "\n";
    while (count($q)) {
        $t = array_shift($q);
        foreach ($graph[$t] as $key => $vertex) {
            if (!$visited[$key] && $vertex == 1) {
                $visited[$key] = 1;
                array_push($q, $key);
                echo $key . "\t";
            }
        }
        echo "\n";
    }
}
function init(&$visited, &$graph)
{
    foreach ($graph as $key => $vertex) {
        $visited[$key] = 0;
    }
}
$visited = array();
init($visited, $dag);
bfs($dag, 4, $visited);
    $queue = array();
    array_push($queue, $start_vertex);
    while (count($queue) > 0) {
        $current_vertext = array_shift($queue);
        $neighbor = $current_vertext->getConnections();
        foreach ($neighbor as $nbr_vertext) {
            if ($nbr_vertext['neighbor']->getColor() == 'white') {
                $nbr_vertext['neighbor']->setColor('gray');
                $nbr_vertext['neighbor']->setDistance($current_vertext->getDistance() + 1);
                $nbr_vertext['neighbor']->setPredecessor($current_vertext);
                array_push($queue, $nbr_vertext['neighbor']);
            }
        }
        $current_vertext->setColor('black');
    }
}
/**
 * 목표 단어부터 전임자를 탐색하여 시작 단어까지
 */
function traverse($end_vertex)
{
    $vertex = $end_vertex;
    while ($vertex->getPredecessor()) {
        echo $vertex->getId() . ' <- ';
        $vertex = $vertex->getPredecessor();
    }
    echo $vertex->getId();
}
$word_radder = buildWordLadderGraph('word_source.txt');
bfs($word_radder->getVertex('fool'));
traverse($word_radder->getVertex('sale'));