Skip to content

Commit

Permalink
Dijkstra
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Feb 15, 2015
0 parents commit ddfbf31
Show file tree
Hide file tree
Showing 10 changed files with 2,507 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
vendor/
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: php

php:
- 5.6
- 5.5
- 5.4
- 5.3

install:
- composer install --dev --no-interaction

script:
- mkdir -p build/logs
- phpunit --coverage-clover build/logs/clover.xml

after_script:
- php vendor/bin/coveralls -v
- CODECLIMATE_REPO_TOKEN=4a5c02026c25f24ff6314ad888d40491ad64b2347c9ea3e985b541389404d438 ./vendor/bin/test-reporter
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CHANGE LOG
==========

## 1.0.0 (2015-02-15)
- Dijkstra.
677 changes: 677 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[![Build Status](https://travis-ci.org/fisharebest/algorithm.svg)](https://travis-ci.org/fisharebest/algorithm)
[![Coverage Status](https://coveralls.io/repos/fisharebest/algorithm/badge.svg?branch=master)](https://coveralls.io/r/fisharebest/algorithm?branch=master)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/4997a2c6-fb22-433e-92c5-ae7285f1a5a0/mini.png)](https://insight.sensiolabs.com/projects/4997a2c6-fb22-433e-92c5-ae7285f1a5a0)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/fisharebest/algorithm/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/fisharebest/algorithm/?branch=master)
[![Code Climate](https://codeclimate.com/github/fisharebest/algorithm/badges/gpa.svg)](https://codeclimate.com/github/fisharebest/algorithm)

# fisharebest/algorithm

General purpose algorithms in PHP

## Installation

Use [composer](https://getcomposer.org), and add `"fisharebest/algorithm": "*"` to the dependencies in your `composer.json`.


## Dijkstra

[Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) finds the
shortest path(s) between two nodes in a weighted, directed graph.

Graphs are specified as an array of edges, each with a cost. The example below is
an undirected graph (i.e. if D→E is 9, then E→D is also 9.), because it is easy to
understand and easy to draw. However, the algorithm works equally well for undirected
graphs, where links can be one-way only or have different costs in each direction.
```
D---9---E
/ \ \
14 2 6
/ \ \
A---9---B--11--C
\ / /
7 10 /
\ / /
F-----15 G
```

Sample code for the above graph.

``` php
use Fisharebest\Algorithm\Dijkstra;

$graph = array(
'A' => array('B' => 9, 'D' => 14, 'F' => 7),
'B' => array('A' => 9, 'C' => 11, 'D' => 2, 'F' => 10),
'C' => array('B' => 11, 'E' => 6, 'F' => 15),
'D' => array('A' => 14, 'B' => 2, 'E' => 9),
'E' => array('C' => 6, 'D' => 9),
'F' => array('A' => 7, 'B' => 10, 'C' => 15),
'G' => array(),
);

$dijkstra = new Dijkstra($graph);

// There can be zero, one or more shortest (i.e. same total cost) paths.

// No shortest path.
$path = $dijkstra->shortestPaths('A', 'G'); // array()

// Exactly one shortest path.
$path = $dijkstra->shortestPaths('A', 'E'); // array(array('A', 'B', 'D', 'E'))

// Multiple solutions with the same shortest path.
$path = $dijkstra->shortestPaths('E', 'F'); // array(array('E', 'D', 'B', 'F'), array('E', 'C', 'F'))

// To find next-shortest paths, exclude one or intermediate nodes from the shortest path.
$path = $dijkstra->shortestPaths('A', 'E'); // array(array('A', 'B', 'D', 'E'))
$path = $dijkstra->shortestPaths('A', 'E', array('B')); // array(array('A', 'B', 'D', 'E'))
$path = $dijkstra->shortestPaths('A', 'E', array('D')); // array(array('A', 'B', 'C', 'E'))
$path = $dijkstra->shortestPaths('A', 'E', array('B', 'D')); // array(array('A', 'F', 'C', 'E'))


```

38 changes: 38 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "fisharebest/algorithm",
"description": "Implementation of standard algorithms in PHP.",
"type": "library",
"keywords": [
"algorithm", "dijkstra"
],
"homepage": "https://github.com/fisharebest/algorithm",
"license": "GPL-3.0+",
"authors": [{
"name": "Greg Roach",
"email": "greg@subaqua.co.uk",
"role": "Developer"
}],
"support": {
"issues": "https://github.com/fisharebest/algorithm/issues"
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "*",
"satooshi/php-coveralls": "*"
},
"autoload": {
"psr-4": {
"Fisharebest\\Algorithm\\": "src/"
}
},
"scripts": {
"post-install-cmd": [
"composer dump-autoload --optimize"
],
"post-update-cmd": [
"composer dump-autoload --optimize"
]
}
}
Loading

0 comments on commit ddfbf31

Please sign in to comment.