https://github.com/rhodrim/graph
A very simple extendable implementation of a Graph data structure which can be outputted to graph data formats eg gml.
https://github.com/rhodrim/graph
edge gml graph graph-theory gxl network-analysis network-graph node php
Last synced: about 1 month ago
JSON representation
A very simple extendable implementation of a Graph data structure which can be outputted to graph data formats eg gml.
- Host: GitHub
- URL: https://github.com/rhodrim/graph
- Owner: RhodriM
- License: mit
- Created: 2018-02-19T14:58:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-03-12T21:28:46.000Z (over 6 years ago)
- Last Synced: 2025-08-12T11:38:30.724Z (10 months ago)
- Topics: edge, gml, graph, graph-theory, gxl, network-analysis, network-graph, node, php
- Language: PHP
- Homepage:
- Size: 88.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# rhodri-m/graph
A very simple implementation of a Graph data structure.
## Installation
### Composer
It is highly recommended that you use composer to install this library.
[Get Composer](https://getcomposer.org/doc/00-intro.md)
Simply run ```composer require rhodri-m/graph``` and use composer's supplied autoloader
(usually by adding
```require __DIR__ . '/vendor/autoload.php';```
at the top of your script).
## Basic Usage
The ```Node``` and ```Edge``` classes are designed to be extended by your own if required, so you can use your own entities as nodes or edges within a graph structure. eg:
```php
class Person extends \Graph\Node
{
[...]
}
```
```GraphContainer``` is the recommended way of storing, adding, removing nodes and edges to ensure consistency.
### Example
```php
$maintainAdjacencyMatrix = true;
$directed = true;
$weighted = false;
$graphCon = new \Graph\GraphContainer(
$maintainAdjacencyMatrix,
$directed,
$weighted
);
$node1 = new \Graph\Node();
$graphCon->addNode($node1);
$node2 = new \Graph\Node();
$graphCon->addNode($node2);
$node3 = new \Graph\Node();
$graphCon->addNode($node3);
// add Edges by Node references
$graphCon->addEdge($node2, $node3);
$graphCon->addEdge($node3, $node1);
$graphCon->addEdge($node2, $node1);
// add Edge from node1 to node2 by (zero-indexed) ids
$graphCon->addEdgeByIds(0, 1);
echo "\nNumber of Nodes: " . count($graphCon->getNodes());
echo "\nNumber of Edges: " . count($graphCon->getEdges());
echo "\nAdjacency Matrix:\n";
print_r($adjacencyMatrix = $graphCon->getAdjacencyMatrix());
```
will output:
```
Number of Nodes: 3
Number of Edges: 4
Adjacency Matrix:
Array
(
[0] => Array
(
[0] =>
[1] => 1
[2] =>
)
[1] => Array
(
[0] => 1
[1] =>
[2] => 1
)
[2] => Array
(
[0] => 1
[1] =>
[2] =>
)
)
```
### Outputting to graph file formats for use by other applications
We can export our graphs to data formats such as GML for use by other applications, such as Gephi:
```php
$gmlOutput = new \Graph\Output\Gml();
$gmlOutput->writeToFile('testGraph.gml', $graphCon);
```
Exporting the very basic graph example above to Gephi gives:

It is also possible to assign labels and colours to nodes for use in external viewers:
```php
$node1->name = "Node 1";
$node1->colour = 'FF8888';
```
