• Stars
    star
    170
  • Rank 222,693 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 11 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A graph library for PHP.

Gliph

Build Status Latest Stable Version Coverage Status

Gliph is a graph library for PHP. It provides graph building blocks and datastructures for use by other PHP applications. It is designed for use with in-memory graphs, not for interaction with a graph database like Cayley or Neo4J (though it could be used to facilitate such connection).

Gliph aims for both sane interfaces and as performant an implementation as userspace PHP allows.

This does require knowing enough about graphs to know what type is appropriate for your use case, but we are aiming to provide helpers that simplify those choices.

Quickstart

Working with gliph is easy: pick a graph implementation, then add edges and vertices as needed. ()Note that gliph currently supports only object vertices, though this limitation may be loosened in future releases)

<?php

use Gliph\Graph\DirectedAdjacencyList;

class Vertex {
    public $val;

    public function __construct($val) {
        $this->val = $val;
    }
}

$vertices = array(
    'a' => new Vertex('a'),
    'b' => new Vertex('b'),
    'c' => new Vertex('c'),
    'd' => new Vertex('d'),
    'e' => new Vertex('e'),
    'f' => new Vertex('f'),
);

$g = new DirectedAdjacencyList();

foreach ($vertices as $vertex) {
    $g->ensureVertex($vertex);
}

$g->ensureArc($vertices['a'], $vertices['b']);
$g->ensureArc($vertices['b'], $vertices['c']);
$g->ensureArc($vertices['a'], $vertices['c']);
$g->ensureArc($vertices['d'], $vertices['a']);
$g->ensureArc($vertices['d'], $vertices['e']);

This would create the following directed graph:

Base digraph

Once your graph is created, gliph provides a number of Traversable mechanisms for doing work on the graph. These mechanisms are important to understand and are explored in greater detail in the wiki, but for those familiar with graph theory, the method naming is intended to be self-explanatory:

For directed and undirected graphs:

  • Graph::vertices()
  • Graph::edges()
  • Graph::adjacentTo($vertex)
  • Graph::incidentTo($vertex)

And for directed graphs only:

  • Digraph::successorsOf($vertex)
  • Digraph::predecessorsOf($vertex)
  • Digraph::arcsFrom($vertex)
  • Digraph::arcsTo($vertex)

Core Concepts

Gliph has several conceptual components that work together to create a coherent library: graph implementations, algorithms, and visitors.

Gliph has several components that work together: graph classes, algorithms, and visitors. Generally speaking, Gliph is patterned after the C++ Boost Graph Library; reading their documentation can yield a lot of insight into how Gliph is intended to work.

Graphs

Gliph’s most important baseline export is its assorted graph interfaces. The other components (algorithms and visitors) rely strictly on the interfaces, never on concrete implementations. Consequently, users of gliph can craft case-specific implementations with the assurance that they will work with the algorithms. To assist towards that end, gliph provides phpunit testing traits that make it easy to ensure your custom graph implementations conform to both the letter and the spirit of gliph’s interfaces.

Gliph’s own implementations are designed to be as performant as possible for the general case. Current implementations include only adjacency lists, in both directed and undirected flavors.

Algorithms

Gliph provides various algorithms that can be run on graph objects. These algorithms interact with the graph by making calls to methods, primarily the iterators, defined in the assorted Graph interfaces. If a graph implements the interface type-hinted by a particular algorithm, then the algorithm can run on that graph. But the efficiency of the algorithm will be largely determined by how efficiently that graph implementation can meet the requirements of the interface. Adjacency lists, for example, are not terribly efficient at providing a list of all edges in a graph, but are very good at single-vertex-centric operations.

Gliph's algorithms are typically implemented quite sparsely (especially traversers) - they seek to implement the simplest, most generic version of an algorithm. They also may not return any output, as that work is left to Visitors.

Visitors

Most algorithms require a visitor object to be provided. The visitor conforms to an interface specified by the algorithm, and the algorithm will call the visitor at certain choice points during its execution. This allows the algorithms to stay highly generic, while visitors can be tailored to a more specific purpose.

For example, a DepthFirst visitor might be used to calculate vertex reach, or generate a topologically sorted list. Each of these are things that a depth-first graph traversal can do. But the work of doing so is left to the visitor so that only one traversal algorithm is needed, and that algorithm is as cheap (memory and cycles) as possible.

Acknowledgements

This library draws inspiration from the C++ Boost Graph Library, though has diverged in some significant design philosophies. Gliph generally follows the same patterns as gogl, though the concepts are more rigorously applied there.

License

MIT

More Repositories

1

gps

your dependencies have arrived
Go
270
star
2

gogl

A graph library in Go
Go
77
star
3

dear_github_can_i_go_home_now

FOR GREAT JUSTICE
Ruby
51
star
4

transducers-go

Transducers for Go
Go
47
star
5

drupal-git-scripts

A collection of scripts to facilitate easy interoperation between a local git workflow and drupal contrib CVS
38
star
6

constext

ctx = Cons(ctx1, ctx2)
Go
22
star
7

svnlib

a php wrapper for invoking the svn binary
PHP
19
star
8

drupalorg-git

the official scripts used to migrate drupal.org's CVS repository to git
PHP
16
star
9

gta

Gotta test 'em all
Go
16
star
10

ctools

cvs2git-based port of drupal ctools for the d7 port sprint
PHP
14
star
11

gitrd

A daemon for all your git serving needs
Go
12
star
12

panels

cvs2git-based port of drupal panels for the d7 port sprint
PHP
7
star
13

tbsp

run your table-based tests with Go 1.7 subtests
Go
5
star
14

frozone

Helpers to manage PHP object state
PHP
5
star
15

versioncontrol

mirror of drupal versioncontrol api
PHP
4
star
16

versioncontrol_git

mirror of drupal versioncontrol api, git backend
PHP
3
star
17

cuerious

but wait so like how does CUE work again?
Go
3
star
18

php_microbench

A small PHP microbenchmarking framework
PHP
3
star
19

cligit

a php wrapper for invoking git cli commands
PHP
2
star
20

sdboyer-test

initial test repo for sdboyer
PHP
2
star
21

netanalysis

preliminary work on drupal-based network analysis
2
star
22

mock_druplatform_api

Go
1
star
23

drupal-c2g

Drupal core migration using cvs2git
PHP
1
star
24

fatlazy

JavaScript
1
star
25

grpchc

Standard gRPC health checks from the CLI
1
star
26

jpgprog

because @ericduran tricked me into it
Go
1
star
27

sdboyer.io

HTML
1
star
28

collaboration_test

PHP
1
star
29

drupal

My working fork of D8
PHP
1
star
30

pharnish

PHP interface to Varnish management
1
star
31

testpusher

testing how github handles --mirror pushes
1
star
32

intmap

Go
1
star
33

testing_your_helptext

because ur cool, github
1
star
34

memoize

Go
1
star
35

drupal-git-everywhere-example

An example repository demonstrating how to build a Drupal site using Git everywhere. Git FTW
PHP
1
star
36

php-src

The PHP Interpreter
C
1
star
37

node

evented I/O for v8 javascript
JavaScript
1
star