/Graph

The Hoa\Graph library.

Primary LanguagePHP

Hoa


Build status Code coverage Packagist 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

Help on IRC Help on Gitter Documentation Board

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.

Installation

With Composer, to include this library into your dependencies, you need to require hoa/graph:

$ composer require hoa/graph '~1.0'

For more installation procedures, please read the Source page.

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.

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 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):

$ dot -Tsvg -oresult.svg <( echo 'digraph { … }'; )

And the result should look like the following image:

result.svg

We can see that n1 is the parent of n2 and n3. n2 is the parent of n3. n3 is parent of n4 and also or iself. And finally, n4 is the parent of n2.

Our directed graph is created. Depending of the node, we can add more information on it. The SimpleNode class has been used. It extends the Hoa\Graph\Node interface.

Documentation

The hack book of Hoa\Graph contains detailed information about how to use this library and how it works.

To generate the documentation locally, execute the following commands:

$ composer require --dev hoa/devtools
$ vendor/bin/hoa devtools:documentation --open

More documentation can be found on the project's website: hoa-project.net.

Getting help

There are mainly two ways to get help:

Contribution

Do you want to contribute? Thanks! A detailed contributor guide explains everything you need to know.

License

Hoa is under the New BSD License (BSD-3-Clause). Please, see LICENSE for details.