PHPackages                             hoa/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. hoa/graph

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

hoa/graph
=========

The Hoa\\Graph library.

1.17.01.13(9y ago)1517.7k↓43.3%5[1 PRs](https://github.com/hoaproject/Graph/pulls)1BSD-3-ClausePHP

Since Sep 16Pushed 5y ago10 watchersCompare

[ Source](https://github.com/hoaproject/Graph)[ Packagist](https://packagist.org/packages/hoa/graph)[ Docs](https://hoa-project.net/)[ RSS](/packages/hoa-graph/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (16)Used By (1)

 [![Hoa](https://camo.githubusercontent.com/2b5c32c5d4bc5e9298821b22d364a522e2dbc0295c1c011b1f9f86a4d07df07e/68747470733a2f2f7374617469632e686f612d70726f6a6563742e6e65742f496d6167652f486f612e737667)](https://camo.githubusercontent.com/2b5c32c5d4bc5e9298821b22d364a522e2dbc0295c1c011b1f9f86a4d07df07e/68747470733a2f2f7374617469632e686f612d70726f6a6563742e6e65742f496d6167652f486f612e737667)

---

 [![Build status](https://camo.githubusercontent.com/ea2b7e6b51f5b2eaf07ffeb2e0e7ecd488a1f4b1e579327976c2a19fc9f2eea5/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f686f6170726f6a6563742f47726170682f6d61737465722e737667)](https://travis-ci.org/hoaproject/Graph) [![Code coverage](https://camo.githubusercontent.com/031f970f32573e6fa17921f567e97b0a45fb13ea9c95baf75bafa16596906f2d/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f686f6170726f6a6563742f47726170682f6d61737465722e737667)](https://coveralls.io/github/hoaproject/Graph?branch=master) [![Packagist](https://camo.githubusercontent.com/93a96183df9c22f8abbada74743015e86d232a187b079e481170f9c9b328f99a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f612f67726170682e737667)](https://packagist.org/packages/hoa/graph) [![License](https://camo.githubusercontent.com/2599bfc94bdd7af31f1d90319711debc1ba907fb1d5ea72eaebe6641e066d7c0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f686f612f67726170682e737667)](https://hoa-project.net/LICENSE)

 Hoa is a **modular**, **extensible** and **structured** set of PHP libraries.
 Moreover, Hoa aims at being a bridge between industrial and research worlds.

Hoa\\Graph
==========

[](#hoagraph)

[![Help on IRC](https://camo.githubusercontent.com/4dbc9c9d28c30cf1ab591f4bb8212fe4dbddc734145df532a9bb86b09878d4c6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f68656c702d253233686f6170726f6a6563742d6666303036362e737667)](https://webchat.freenode.net/?channels=#hoaproject)[![Help on Gitter](https://camo.githubusercontent.com/8c4c85951788ff606b1268cb3dd946be05e3054795455d0a7b9250711bc2ac05/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f68656c702d6769747465722d6666303036362e737667)](https://gitter.im/hoaproject/central)[![Documentation](https://camo.githubusercontent.com/7059ad5f1a363f9098686c59d432f01d7330aed9d4b6c8111d985fd64cfc6c60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f63756d656e746174696f6e2d6861636b5f626f6f6b2d6666303036362e737667)](https://central.hoa-project.net/Documentation/Library/Graph)[![Board](https://camo.githubusercontent.com/fd81654ba14b3aca3a713e1b471bc3fc3ba7b5bb3761ccffd6eea2e2ed1fa5ca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6f7267616e69736174696f6e2d626f6172642d6666303036362e737667)](https://waffle.io/hoaproject/graph)

This library allows to create and manipulate directed graphs, a common data structure. A directed graph is basically a set of vertices (aka nodes) and directed edges between vertices.

[Learn more](https://central.hoa-project.net/Documentation/Library/Graph).

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

[](#installation)

With [Composer](https://getcomposer.org/), to include this library into your dependencies, you need to require [`hoa/graph`](https://packagist.org/packages/hoa/graph):

```
$ composer require hoa/graph '~1.0'
```

For more installation procedures, please read [the Source page](https://hoa-project.net/Source.html).

Testing
-------

[](#testing)

Before running the test suites, the development dependencies must be installed:

```
$ composer install
```

Then, to run all the test suites:

```
$ vendor/bin/hoa test:run
```

For more information, please read the [contributor guide](https://hoa-project.net/Literature/Contributor/Guide.html).

Quick usage
-----------

[](#quick-usage)

As a quick overview, we propose to see how to create a simple directed graph in memory and dump the result as [a DOT script](http://graphviz.org/content/dot-language) in order to visualize it in SVG. The graph implementation will use the adjacency list structure. Thus:

```
// Create the graph instance.
// By default, loops are not allowed and we would like loops for this example,
// so we enable them.
$graph = new Hoa\Graph\AdjacencyList(Hoa\Graph::ALLOW_LOOP);

// Create 4 vertices (aka nodes).
$n1 = new Hoa\Graph\SimpleNode('n1');
$n2 = new Hoa\Graph\SimpleNode('n2');
$n3 = new Hoa\Graph\SimpleNode('n3');
$n4 = new Hoa\Graph\SimpleNode('n4');

// Create edges (aka links) between them.
$graph->addNode($n1);
$graph->addNode($n2, [$n1]); // n2 has parent n1.
$graph->addNode($n3, [$n1, $n2, $n3]); // n3 has parents n1, n2 and n3.
$graph->addNode($n4, [$n3]); // n4 has parent n3.
$graph->addNode($n2, [$n4]); // Add parent n4 to n2.
```

The directed graph is created in memory. Now, let's dump into the DOT language:

```
echo $graph;

/**
 * Will output:
 *     digraph {
 *         n1;
 *         n2;
 *         n3;
 *         n4;
 *         n1 -> n2;
 *         n1 -> n3;
 *         n2 -> n3;
 *         n3 -> n3;
 *         n3 -> n4;
 *         n4 -> n2;
 *     }
 */
```

Then, to compile this DOT script into an SVG document, we will use [`dot(1)`](http://graphviz.org/pdf/dot.1.pdf):

```
$ dot -Tsvg -oresult.svg
