Skip to content

Commit

Permalink
Myers diff algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Oct 20, 2015
1 parent d2272de commit 01ed447
Show file tree
Hide file tree
Showing 7 changed files with 352 additions and 1,430 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
CHANGE LOG
==========

## 1.1.0 (2015-10-20)
- Myers’ diff

## 1.0.1 (2015-05-15)
- Exclude test scripts in export.

Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

General purpose algorithms in PHP

* Dijkstra
* Myers’ diff

## Installation

Use [composer](https://getcomposer.org), and add `"fisharebest/algorithm": "*"` to the dependencies in your `composer.json`.
Expand Down Expand Up @@ -67,7 +70,34 @@ $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'))
```

## 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).

``` php
$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
// );
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Implementation of standard algorithms in PHP.",
"type": "library",
"keywords": [
"algorithm", "dijkstra"
"algorithm", "dijkstra", "myers", "diff"
],
"homepage": "https://github.com/fisharebest/algorithm",
"license": "GPL-3.0+",
Expand Down
Loading

0 comments on commit 01ed447

Please sign in to comment.