Skip to content

lextoumbourou/bfs-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bfs-php

A very simple breadth-first search implementation written in PHP. Nothing fancy: does what it says on the tin :)

Requires PHP 5.4+

Docs

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.

Examples

<?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']

Tests

Firstly, download all the deps (just unittest)

> php composer.phar update

Run tests!

> vendor/bin/phpunit tests/unit.php

License

MIT

About

Breadth-First Search implementation in PHP

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages