PHPackages                             rhodri-m/graph - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rhodri-m/graph

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rhodri-m/graph
==============

A very simple implementation of a Graph data structure.

1.6.2(6y ago)059MITPHPPHP &gt;=5.6.30CI failing

Since Feb 11Pushed 6y ago1 watchersCompare

[ Source](https://github.com/RhodriM/graph)[ Packagist](https://packagist.org/packages/rhodri-m/graph)[ RSS](/packages/rhodri-m-graph/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (4)Versions (20)Used By (0)

rhodri-m/graph
==============

[](#rhodri-mgraph)

A very simple implementation of a Graph data structure.

Installation
------------

[](#installation)

### Composer

[](#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
-----------

[](#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:

```
class Person extends \Graph\Node
{
    [...]
}
```

`GraphContainer` is the recommended way of storing, adding, removing nodes and edges to ensure consistency.

### Example

[](#example)

```
$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

[](#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:

```
$gmlOutput = new \Graph\Output\Gml();
$gmlOutput->writeToFile('testGraph.gml', $graphCon);
```

Exporting the very basic graph example above to Gephi gives:

[![alt text](https://camo.githubusercontent.com/15afcf923999972879fe443e6033384dcc691d1a2af2ae7c6fc0ddebf205dcf5/687474703a2f2f72686f6472696d6f727269732e636f2e756b2f62617369632d67726170682e706e67 "basic graph")](https://camo.githubusercontent.com/15afcf923999972879fe443e6033384dcc691d1a2af2ae7c6fc0ddebf205dcf5/687474703a2f2f72686f6472696d6f727269732e636f2e756b2f62617369632d67726170682e706e67)

It is also possible to assign labels and colours to nodes for use in external viewers:

```
$node1->name = "Node 1";
$node1->colour = 'FF8888';
```

[![alt text](https://camo.githubusercontent.com/a1a384a95a72c5462408869ab0bbed0fd7b81cd9389e1a769435f14ebb8a5ffa/687474703a2f2f72686f6472696d6f727269732e636f2e756b2f62617369632d6772617068322e706e67 "basic graph")](https://camo.githubusercontent.com/a1a384a95a72c5462408869ab0bbed0fd7b81cd9389e1a769435f14ebb8a5ffa/687474703a2f2f72686f6472696d6f727269732e636f2e756b2f62617369632d6772617068322e706e67)

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~58 days

Recently: every ~141 days

Total

14

Last Release

2254d ago

Major Versions

v0.6 → 1.0.02018-02-19

### Community

Maintainers

![](https://www.gravatar.com/avatar/2e76c1e9d10335aecb9983e36bf05952186ef019473c118207401ec2890e28c9?d=identicon)[RhodriM](/maintainers/RhodriM)

---

Top Contributors

[![RhodriM](https://avatars.githubusercontent.com/u/761821?v=4)](https://github.com/RhodriM "RhodriM (65 commits)")

---

Tags

edgegmlgraphgraph-theorygxlnetwork-analysisnetwork-graphnodephpnetworknodegraphedgegml

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/rhodri-m-graph/health.svg)

```
[![Health](https://phpackages.com/badges/rhodri-m-graph/health.svg)](https://phpackages.com/packages/rhodri-m-graph)
```

###  Alternatives

[graphp/graph

GraPHP is the mathematical graph/network library written in PHP.

711292.6k3](/packages/graphp-graph)[clue/graph

GraPHP is the mathematical graph/network library written in PHP.

7114.3M41](/packages/clue-graph)[novus/nvd3

A reusable charting library written in d3.js

7.2k207.7k2](/packages/novus-nvd3)[phonetworks/pho-lib-graph

A general purpose graph library in PHP

556.0k1](/packages/phonetworks-pho-lib-graph)[s1lentium/iptools

PHP Library for manipulating network addresses (IPv4 and IPv6)

2446.2M24](/packages/s1lentium-iptools)[fab2s/nodalflow

A PHP Nodal WorkFlow

16362.4k1](/packages/fab2s-nodalflow)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
