Skip to content

Commit 6c8eb92

Browse files
committed
Initial project skeleton
1 parent b9f43a1 commit 6c8eb92

File tree

8 files changed

+203
-0
lines changed

8 files changed

+203
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/composer.lock

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
php:
3+
- 5.6
4+
- 5.5
5+
- 5.4
6+
- 5.3
7+
- hhvm
8+
install:
9+
- composer install --prefer-source --no-interaction
10+
script:
11+
- php vendor/bin/phpunit --coverage-text

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
## 0.8.0 (2015-xx-xx)
4+
5+
* First tagged release, split off from [clue/graph](https://github.com/clue/graph) v0.8.0
6+
([#1](https://github.com/graphp/algorithms/issues/1))

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Christian Lück
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is furnished
10+
to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# graphp/algorithms [![Build Status](https://travis-ci.org/graphp/algorithms.svg?branch=master)](https://travis-ci.org/graphp/algorithms)
2+
3+
Common mathematical graph algorithms implemented in PHP
4+
5+
> Note: This project is in beta stage! Feel free to report any issues you encounter.
6+
7+
## Install
8+
9+
The recommended way to install this library is [through composer](http://getcomposer.org). [New to composer?](http://getcomposer.org/doc/00-intro.md)
10+
11+
```JSON
12+
{
13+
"require": {
14+
"graphp/algorithms": "dev-master"
15+
}
16+
}
17+
```
18+
19+
## License
20+
21+
Released under the terms of the permissive [MIT license](http://opensource.org/licenses/MIT).

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "graphp/algorithms",
3+
"description": "Common mathematical graph algorithms",
4+
"keywords": ["Graph algorithms", "shortest path", "dijkstra", "moore-bellman-ford", "minimum spanning tree", "kruskal", "prim"],
5+
"homepage": "https://github.com/graphp/algorithms",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Christian Lück",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.3",
15+
"clue/graph": "~0.8.0"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit": "~4.0"
19+
},
20+
"autoload": {
21+
"psr-4": {"Graphp\\Algorithms\\": "src/"}
22+
}
23+
}

phpunit.xml.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap="tests/bootstrap.php"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
>
9+
<testsuites>
10+
<testsuite name="Algorithm Test Suite">
11+
<directory>./tests/</directory>
12+
</testsuite>
13+
</testsuites>
14+
<filter>
15+
<whitelist>
16+
<directory>./src/</directory>
17+
</whitelist>
18+
</filter>
19+
</phpunit>

tests/bootstrap.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
use Fhaculty\Graph\Edge\Directed;
4+
use Fhaculty\Graph\Edge\Base as Edge;
5+
use Fhaculty\Graph\Graph;
6+
use Fhaculty\Graph\Vertex;
7+
use Fhaculty\Graph\Set\Vertices;
8+
9+
(include_once __DIR__ . '/../vendor/autoload.php') OR die(PHP_EOL . 'ERROR: composer autoloader not found, run "composer install" or see README for instructions' . PHP_EOL);
10+
11+
class TestCase extends PHPUnit_Framework_TestCase
12+
{
13+
protected function assertGraphEquals(Graph $expected, Graph $actual)
14+
{
15+
$f = function(Graph $graph){
16+
$ret = get_class($graph);
17+
$ret .= PHP_EOL . 'vertices: ' . count($graph->getVertices());
18+
$ret .= PHP_EOL . 'edges: ' . count($graph->getEdges());
19+
20+
return $ret;
21+
};
22+
23+
// assert graph base parameters are equal
24+
$this->assertEquals($f($expected), $f($actual));
25+
26+
// next, assert that all vertices in both graphs are the same
27+
// each vertex has a unique ID, therefor it's easy to search a matching partner
28+
// do not use assertVertexEquals() in order to not increase assertion counter
29+
30+
foreach ($expected->getVertices()->getMap() as $vid => $vertex) {
31+
try {
32+
$other = $actual->getVertex($vid);
33+
} catch (Exception $e) {
34+
$this->fail();
35+
}
36+
if ($this->getVertexDump($vertex) !== $this->getVertexDump($vertex)) {
37+
$this->fail();
38+
}
39+
}
40+
41+
// next, assert that all edges in both graphs are the same
42+
// assertEdgeEquals() does not work, as the order of the edges is unknown
43+
// therefor, build an array of edge dump and make sure each entry has a match
44+
45+
$edgesExpected = array();
46+
foreach ($expected->getEdges() as $edge) {
47+
$edgesExpected []= $this->getEdgeDump($edge);
48+
}
49+
50+
foreach ($actual->getEdges() as $edge) {
51+
$dump = $this->getEdgeDump($edge);
52+
53+
$pos = array_search($dump, $edgesExpected, true);
54+
if ($pos === false) {
55+
$this->fail('given edge ' . $dump . ' not found');
56+
} else {
57+
unset($edgesExpected[$pos]);
58+
}
59+
}
60+
}
61+
62+
protected function assertVertexEquals(Vertex $expected, Vertex $actual)
63+
{
64+
$this->assertEquals($this->getVertexDump($expected), $this->getVertexDump($actual));
65+
}
66+
67+
protected function assertEdgeEquals(Edge $expected, Edge $actual)
68+
{
69+
$this->assertEquals($this->getEdgeDump($expected), $this->getEdgeDump($actual));
70+
}
71+
72+
private function getVertexDump(Vertex $vertex)
73+
{
74+
$ret = get_class($vertex);
75+
76+
$ret .= PHP_EOL . 'id: ' . $vertex->getId();
77+
$ret .= PHP_EOL . 'attributes: ' . json_encode($vertex->getAttributeBag()->getAttributes());
78+
$ret .= PHP_EOL . 'balance: ' . $vertex->getBalance();
79+
$ret .= PHP_EOL . 'group: ' . $vertex->getGroup();
80+
81+
return $ret;
82+
}
83+
84+
private function getEdgeDump(Edge $edge)
85+
{
86+
$ret = get_class($edge) . ' ';
87+
if ($edge instanceof Directed) {
88+
$ret .= $edge->getVertexStart()->getId() . ' -> ' . $edge->getVertexEnd()->getId();
89+
} else {
90+
$vertices = $edge->getVertices()->getIds();
91+
$ret .= $vertices[0] . ' -- ' . $vertices[1];
92+
}
93+
$ret .= PHP_EOL . 'flow: ' . $edge->getFlow();
94+
$ret .= PHP_EOL . 'capacity: ' . $edge->getCapacity();
95+
$ret .= PHP_EOL . 'weight: ' . $edge->getWeight();
96+
$ret .= PHP_EOL . 'attributes: ' . json_encode($edge->getAttributeBag()->getAttributes());
97+
98+
return $ret;
99+
}
100+
}

0 commit comments

Comments
 (0)