A very simple breadth-first search implementation written in PHP. Nothing fancy: does what it says on the tin :)
Requires PHP 5.4+
bool bfs(array $graph, mixed $start_node, mixed $end_node)
Return true
if there's a connected path from $start_node
to $end_node
. false
otherwise.
array bfs_path(array $graph, mixed $start_node, mixed $end_node)
Return a path as an array if there's a connected path from $start_node
to $end_node
. false
otherwise.
<?php
include 'bfs.php'
$graph = [
'A' => ['B', 'C'],
'B' => ['A', 'D'],
'D' => ['B'],
'C' => ['A',],
];
bfs($graph, 'A', 'D'); // true
bfs($graph, 'A', 'G'); // false
print_r(bfs_path($graph, 'A', 'D')); // ['A', 'B', 'D']
Firstly, download all the deps (just unittest)
> php composer.phar update
Run tests!
> vendor/bin/phpunit tests/unit.php
MIT