PHPackages                             actived/graphphp - 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. actived/graphphp

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

actived/graphphp
================

A PHP graph theory package.

0.2.2(2y ago)6587MITPHPPHP &gt;=8.0

Since Sep 29Pushed 2y ago1 watchersCompare

[ Source](https://github.com/ActiveDevOrga/graphPHP)[ Packagist](https://packagist.org/packages/actived/graphphp)[ RSS](/packages/actived-graphphp/feed)WikiDiscussions main Synced 1mo ago

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

GraphPHP
========

[](#graphphp)

A PHP graph theory package that provides structures and algorithms for working with graphs.

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

[](#installation)

You can install the package via Composer:

```
composer require actived/graphphp
```

Features
========

[](#features)

Graph
-----

[](#graph)

The `Graph` class provides the foundation for working with undirected graphs in PHP. It includes methods for manipulating nodes, edges, and retrieving various properties of the graph.

### Creating a Graph

[](#creating-a-graph)

```
use GraphPHP\Graph\Graph;
use GraphPHP\Node\Node;
use GraphPHP\Edge\Edge;

$graph = new Graph();
```

### Adding Nodes

[](#adding-nodes)

```
$nodeA = new Node('A');
$graph->addNode($nodeA);
```

### Adding Edges

[](#adding-edges)

```
$nodeB = new Node('B');
$edge = new Edge($nodeA, $nodeB);
$graph->addEdge($edge);
```

### Removing Nodes and Edges

[](#removing-nodes-and-edges)

Nodes and edges can be removed from the graph:

```
$graph->removeNode($nodeA);
$graph->removeEdge($edge);
```

### Neighbors

[](#neighbors)

Retrieve the neighbors of a given node:

```
$neighbors = $graph->getNeighbors($nodeB);
```

### Adjacency Matrix

[](#adjacency-matrix)

Get the adjacency matrix of the graph:

```
$matrix = $graph->getAdjacencyMatrix();
```

### Checking for Cycles

[](#checking-for-cycles)

Determine if the graph contains a cycle:

```
if ($graph->hasCycle()) {
    echo "The graph has a cycle.";
} else {
    echo "The graph does not have a cycle.";
}
```

### Transitive Closure

[](#transitive-closure)

Compute the transitive closure of the graph using the Floyd-Warshall algorithm:

```
$closure = $graph->transitiveClosure();
```

### Shortest Path

[](#shortest-path)

Compute the shortest path between two nodes using Dijkstra's algorithm:

```
$pathInfo = $graph->shortestPathDijkstra($nodeA, $nodeC);
echo "Shortest path: " . implode(' -> ', $pathInfo['path']);
echo "Cost: " . $pathInfo['cost'];
```

### String Representation

[](#string-representation)

To get a string representation of the graph:

```
echo $graph;
```

Note: The Graph class assumes an undirected graph. For directed graphs, refer to the DiGraph class documentation.

DiGraph (Directed Graph)
------------------------

[](#digraph-directed-graph)

The `DiGraph` class extends the base `Graph` class and represents a directed graph. This means all edges in this graph have a direction, going from a source node to a target node.

### Creating a Directed Graph

[](#creating-a-directed-graph)

```
use GraphPHP\Graph\DiGraph;
use GraphPHP\Node\Node;
use GraphPHP\Edge\DirectedEdge;

$diGraph = new DiGraph();
```

### Adding Directed Edges

[](#adding-directed-edges)

Only directed edges can be added to a directed graph:

```
$nodeA = new Node('A');
$nodeB = new Node('B');
$directedEdge = new DirectedEdge($nodeA, $nodeB);
$diGraph->addEdge($directedEdge);
```

### Outgoing Neighbors

[](#outgoing-neighbors)

Retrieve the outgoing neighbors of a given node:

```
$outgoingNeighbors = $diGraph->getNeighbors($nodeA);
```

### Predecessors

[](#predecessors)

Retrieve the predecessors (nodes with directed edges pointing to the given node) of a node:

```
$predecessors = $diGraph->getPredecessors($nodeB);
```

### Bellman-Ford Shortest Path

[](#bellman-ford-shortest-path)

Compute the shortest path between two nodes using the Bellman-Ford algorithm:

```
$pathInfo = $diGraph->shortestPathBellmanFord($nodeA, $nodeC);
echo "Shortest path: " . implode(' -> ', $pathInfo['path']);
echo "Cost: " . $pathInfo['cost'];
```

### Checking for Cycles in Directed Graphs

[](#checking-for-cycles-in-directed-graphs)

Determine if the directed graph contains a cycle:

```
if ($diGraph->hasCycle()) {
    echo "The directed graph has a cycle.";
} else {
    echo "The directed graph does not have a cycle.";
}
```

### Adjacency Matrix for Directed Graphs

[](#adjacency-matrix-for-directed-graphs)

Get the adjacency matrix of the directed graph:

```
$matrix = $diGraph->getAdjacencyMatrix();
```

Note: The DiGraph class is specific to directed graphs. If you need an undirected graph, refer to the base Graph class documentation.

Directed Acyclic Graphs (DAG)
-----------------------------

[](#directed-acyclic-graphs-dag)

Create and manipulate directed acyclic graphs.

```
use GraphPHP\Graph\DAG;
use GraphPHP\Node\Node;
use GraphPHP\Edge\DirectedEdge;

$graph = new DAG();
$nodeA = new Node('A');
$nodeB = new Node('B');
$nodeC = new Node('C');

$graph->addNode($nodeA)
    ->addNode($nodeB)
    ->addNode($nodeC)
    ->addEdge(new DirectedEdge($nodeA, $nodeB, 4))
    ->addEdge(new DirectedEdge($nodeB, $nodeC, -6))
    ->addEdge(new DirectedEdge($nodeA, $nodeC, 2));
```

### Transitive Reduction

[](#transitive-reduction)

Perform transitive reduction on a DAG.

```
$graph->transitiveReduction();
echo $graph; // Visual representation of the graph
```

### Topological Sort

[](#topological-sort)

Get a topological ordering of the nodes in a DAG.

```
$order = $graph->topologicalSort();
print_r($order);
```

Roadmap
=======

[](#roadmap)

- Testing: Implement comprehensive tests for the current functionalities.
- Trees: Introduce tree graph structures.
- Directed Trees: Extend the tree structures to support directed trees.
- Binary Trees: Implement binary tree structures and related algorithms.

Contributing
============

[](#contributing)

If you have suggestions or improvements, feel free to submit a pull request or open an issue on the GitHub repository.

License
=======

[](#license)

This package is open-sourced software licensed under the MIT license.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 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 ~1 days

Total

3

Last Release

961d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/34bad47ff5d5e75b3045f5e79f97d2f12e22e9a90b6c408d58478f3ae1b2d70d?d=identicon)[actived](/maintainers/actived)

![](https://www.gravatar.com/avatar/3dde5d93053abd2f030d5f6244363e6cd7db365f302cb9b61a4ef2810b3c4e50?d=identicon)[Houssem-Guemer](/maintainers/Houssem-Guemer)

---

Top Contributors

[![Houssem-Guemer](https://avatars.githubusercontent.com/u/39636747?v=4)](https://github.com/Houssem-Guemer "Houssem-Guemer (14 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/actived-graphphp/health.svg)

```
[![Health](https://phpackages.com/badges/actived-graphphp/health.svg)](https://phpackages.com/packages/actived-graphphp)
```

###  Alternatives

[magepal/magento2-googletagmanager

Google Tag Manager (GTM) for Magento 2 with Advance Data Layer

2671.5M4](/packages/magepal-magento2-googletagmanager)[beechit/default-upload-folder

Make it possible to configure the default upload folder for a certain TCA column

13127.6k](/packages/beechit-default-upload-folder)

PHPackages © 2026

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