PHGraph is a modern mathematical graph/network library written in PHP.
You can install the package via composer:
composer require phgraph/graph
use PHGraph\Graph;
use PHGraph\Search\BreadthFirst;
…
$graph = new Graph;
$columbus = $graph->newVertex([
'name' => 'Columbus',
]);
$cleveland = $graph->newVertex([
'name' => 'Cleveland',
]);
$cincinnati = $graph->newVertex([
'name' => 'Cincinnati',
]);
$columbus->createEdge($cleveland);
$columbus->createEdge($cincinnati);
$search = new BreadthFirst($cincinnati);
if ($search->hasVertex($cleveland)) {
echo "We can get from Cincinnati to Cleveland\n";
} else {
echo "We can’t get from Cincinnati to Cleveland\n";
}
This library has support for visualizing graphs as images using GraphViz "Graph Visualization Software". You will need GraphViz installed on your system for this to work.
use PHGraph\Graph;
use PHGraph\GraphViz\GraphViz;
…
$graph = new Graph;
$columbus = $graph->newVertex([
'name' => 'Columbus',
]);
$cleveland = $graph->newVertex([
'name' => 'Cleveland',
]);
$cincinnati = $graph->newVertex([
'name' => 'Cincinnati',
]);
$columbus->createEdge($cleveland);
$columbus->createEdge($cincinnati);
$graphviz = new GraphViz($graph);
// open the image on your system
$graphviz->display();
output:
A graph library is rather boring without the ability to use algorithms on it, here is a list of the currently supported ones:
You will need Composer for the development dependencies. Once you have that, run the following
$ composer install
You can run the current test suite with the following command
$ composer test
For static analysis of the code run the following
$ composer analyse
Bug reports for the current release version can be opened in this repository’s issue tracker.
this was heavily inspired by graphp/graph.