PHPackages                             fisharebest/algorithm - 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. fisharebest/algorithm

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

fisharebest/algorithm
=====================

Implementation of standard algorithms in PHP.

1.6.0(4y ago)7192.7k↓34.5%22[3 issues](https://github.com/fisharebest/algorithm/issues)[1 PRs](https://github.com/fisharebest/algorithm/pulls)1GPL-3.0-or-laterPHPPHP &gt;=5.3.0CI failing

Since Feb 15Pushed 1y ago6 watchersCompare

[ Source](https://github.com/fisharebest/algorithm)[ Packagist](https://packagist.org/packages/fisharebest/algorithm)[ Docs](https://github.com/fisharebest/algorithm)[ RSS](/packages/fisharebest-algorithm/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (12)Used By (1)

[![Latest Stable Version](https://camo.githubusercontent.com/7c756d47c5b6de25c72afb563872bc479bdf7b7d0f15176201cf85346b7b5144/68747470733a2f2f706f7365722e707567782e6f72672f66697368617265626573742f616c676f726974686d2f762f737461626c652e737667)](https://packagist.org/packages/fisharebest/algorithm)[![Unit tests](https://github.com/fisharebest/algorithm/actions/workflows/phpunit.yaml/badge.svg)](https://github.com/fisharebest/algorithm/actions/workflows/phpunit.yaml)[![Coverage Status](https://camo.githubusercontent.com/219b2886e4141736e30e08507fff2447257d7acb92813d0da2b41d00251c5230/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f66697368617265626573742f616c676f726974686d2f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/r/fisharebest/algorithm?branch=main)[![SensioLabsInsight](https://camo.githubusercontent.com/214cffcc523f7926e6189682ebb733957c5185b34480aa265476c62cbed6fa18/68747470733a2f2f696e73696768742e73796d666f6e792e636f6d2f70726f6a656374732f34393937613263362d666232322d343333652d393263352d6165373238356631613561302f6d696e692e706e67)](https://insight.symfony.com/projects/4997a2c6-fb22-433e-92c5-ae7285f1a5a0)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b95decd0021f505fce7de6a9497178cd72c6e5aec7058fa831d919d4fad7dd7a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f66697368617265626573742f616c676f726974686d2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/fisharebest/algorithm/?branch=main)[![StyleCI](https://camo.githubusercontent.com/45713d1ad94ac393021f13567e6fad7c06e9190257a6cf623cfc21b44183a6f7/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f33303832333734372f736869656c64)](https://github.styleci.io/repos/30823747)[![Code Climate](https://camo.githubusercontent.com/95f13aab5251fbe8d25902e0ec3b7dc9fcfe1532d63dd347920c45cad26043c5/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f66697368617265626573742f616c676f726974686d2f6261646765732f6770612e737667)](https://codeclimate.com/github/fisharebest/algorithm)

fisharebest/algorithm
=====================

[](#fisharebestalgorithm)

General purpose algorithms in PHP

- Dijkstra
- Myers’ diff
- Connected/unconnected components of a graph

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

[](#installation)

Install using composer.

```
composer require fisharebest/algorithm

```

Dijkstra
--------

[](#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 directed 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.

```
$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(),
);

$algorithm = new \Fisharebest\Algorithm\Dijkstra($graph);

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

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

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

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

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

Myers’ diff
-----------

[](#myers-diff)

Find the difference between two sequences of tokens (characters, words, lines, etc.) using “[An O(ND) Difference Algorithm and Its Variations](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927)” by Eugene W. Myers.

The output can be interpreted as either:

- A series of instructions to transform the first sequence into the second sequence.
- A list of matches (tokens that appear in both sequences) and mismatches (tokens that appear in just one sequence).

```
$x = array('a', 'b', 'c', 'a', 'b', 'b', 'a');
$y = array('c', 'b', 'a', 'b', 'a', 'c');
$algorithm = new MyersDiff;
$diff = $algorithm->calculate($x, $y);
// array(
//  array('a', MyersDiff::DELETE),   i.e. 'a' occurs only in $x
//  array('b', MyersDiff::DELETE),   i.e. 'b' occurs only in $x
//  array('c', MyersDiff::KEEP),     i.e. 'c' occurs both $x and $y
//  array('b', MyersDiff::INSERT),   i.e. 'b' occurs only in $y
//  array('a', MyersDiff::KEEP),     i.e. 'a' occurs in both $x and $y
//  array('b', MyersDiff::KEEP),     i.e. 'b' occurs in both $x and $y
//  array('b', MyersDiff::DELETE),   i.e. 'b' occurs only in $x
//  array('a', MyersDiff::KEEP),     i.e. 'a' occurs in both $x and $y
//  array('c', MyersDiff::INSERT),   i.e. 'c' occurs only in $y
// );
```

Connected components
--------------------

[](#connected-components)

A depth-first search of a graph to find isolated groups of nodes.

```
    D    E
   / \    \
  /   \    \
 A-----B   C
  \   /
   \ /
    F

```

Sample code for the above graph

```
$graph = array(
	'A' => array('B' => 1, 'D' => 1, 'F' => 1),
	'B' => array('A' => 1, 'D' => 1, 'F' => 1),
	'C' => array('E' => 1),
	'D' => array('A' => 1, 'B' => 1),
	'E' => array('C' => 1),
	'F' => array('A' => 1, 'B' => 1),
);

$algorithm  = new \Fisharebest\Algorithm\ConnectedComponent($graph);
$components = $algorithm->findConnectedComponents());
// array(
//  1 => array('A', 'B', 'D', 'F'),
//  2 => array('C', 'E'),
// );
```

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 93.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~265 days

Recently: every ~433 days

Total

10

Last Release

1718d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2306088?v=4)[Greg Roach](/maintainers/fisharebest)[@fisharebest](https://github.com/fisharebest)

---

Top Contributors

[![fisharebest](https://avatars.githubusercontent.com/u/2306088?v=4)](https://github.com/fisharebest "fisharebest (27 commits)")[![donquixote](https://avatars.githubusercontent.com/u/150032?v=4)](https://github.com/donquixote "donquixote (1 commits)")[![grillonbleu](https://avatars.githubusercontent.com/u/3072856?v=4)](https://github.com/grillonbleu "grillonbleu (1 commits)")

---

Tags

diffAlgorithmdijkstramyers

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fisharebest-algorithm/health.svg)

```
[![Health](https://phpackages.com/badges/fisharebest-algorithm/health.svg)](https://phpackages.com/packages/fisharebest-algorithm)
```

###  Alternatives

[rubix/ml

A high-level machine learning and deep learning library for the PHP language.

2.2k1.4M28](/packages/rubix-ml)[jfcherng/php-diff

A comprehensive library for generating differences between two strings in multiple formats (unified, side by side HTML etc).

4705.1M51](/packages/jfcherng-php-diff)[caxy/php-htmldiff

A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.

21320.9M15](/packages/caxy-php-htmldiff)[localheinz/diff

Fork of sebastian/diff for use with ergebnis/composer-normalize

4737.0M5](/packages/localheinz-diff)[donatello-za/rake-php-plus

Yet another PHP implementation of the Rapid Automatic Keyword Extraction algorithm (RAKE).

271865.1k10](/packages/donatello-za-rake-php-plus)[graphp/algorithms

Common mathematical graph algorithms implemented in PHP

1402.9M14](/packages/graphp-algorithms)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
