https://github.com/phgraph/graph
modern mathematical graph/network library written in PHP
https://github.com/phgraph/graph
graph-algorithms graph-search-algorithms graphviz php shortest-path-algorithm
Last synced: about 1 month ago
JSON representation
modern mathematical graph/network library written in PHP
- Host: GitHub
- URL: https://github.com/phgraph/graph
- Owner: phgraph
- License: mit
- Created: 2019-03-17T06:49:38.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-19T20:08:22.000Z (almost 3 years ago)
- Last Synced: 2025-07-16T15:05:58.605Z (7 months ago)
- Topics: graph-algorithms, graph-search-algorithms, graphviz, php, shortest-path-algorithm
- Language: PHP
- Homepage:
- Size: 435 KB
- Stars: 13
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://travis-ci.org/phgraph/graph)
[](https://coveralls.io/github/phgraph/graph?branch=master)
[](https://github.styleci.io/repos/176066306)
[](https://codeclimate.com/github/phgraph/graph/maintainability)
# phgraph/graph
PHGraph is a modern mathematical graph/network library written in PHP.
## Installation
You can install the package via composer:
```bash
composer require phgraph/graph
```
## Usage
### Creation and Search
```php
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";
}
```
### Graph drawing
This library has support for visualizing graphs as images using
[GraphViz](http://www.graphviz.org/) "Graph Visualization Software". You will
need GraphViz installed on your system for this to work.
```php
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:

### Algorithms
A graph library is rather boring without the ability to use algorithms on it,
here is a list of the currently supported ones:
- [Search](https://en.wikipedia.org/wiki/Graph_traversal)
- [Depth first](https://en.wikipedia.org/wiki/Depth-first_search)
- [Breadth first](https://en.wikipedia.org/wiki/Breadth-first_search)
- [Shortest path](https://en.wikipedia.org/wiki/Shortest_path_problem)
- [Dijkstra](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)
- [Moore-Bellman-Ford](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm)
- [Least Hops](https://en.wikipedia.org/wiki/Best-first_search)
- [Minimum Spanning Tree](https://en.wikipedia.org/wiki/Minimum_spanning_tree)
- [Kruskal’s algorithm](https://en.wikipedia.org/wiki/Kruskal%27s_algorithm)
- [Prim’s algorithm](https://en.wikipedia.org/wiki/Prim%27s_algorithm)
- [Traveling Salesman Problem](https://en.wikipedia.org/wiki/Travelling_salesman_problem)
- [Nearest Neighbor](https://en.wikipedia.org/wiki/Nearest_neighbour_algorithm)
# Development
## Installing dependencies
You will need [Composer](https://getcomposer.org/) for the development
dependencies. Once you have that, run the following
```bash
$ composer install
```
## Running tests
You can run the current test suite with the following command
```bash
$ composer test
```
For static analysis of the code run the following
```bash
$ composer analyse
```
## Bug Reports
Bug reports for the current release version can be opened in this repository’s
[issue tracker](https://github.com/phgraph/graph/issues).
## Thanks
this was heavily inspired by [graphp/graph](https://github.com/graphp/graph).