
The Myers diff algorithm: part 1 - arunc
https://blog.jcoglan.com/2017/02/12/the-myers-diff-algorithm-part-1/
======
zamalek
There's also patience diff[1], which does a better job in some situations[2]
(you might not normally see these inferior diffs because the diffs are
massaged after the LCS algorithm).

[1]:
[http://alfedenzo.livejournal.com/170301.html](http://alfedenzo.livejournal.com/170301.html)
[2]:
[http://bramcohen.livejournal.com/73318.html](http://bramcohen.livejournal.com/73318.html)

------
jasode
Fyi... a related wiki page about LCS (Longest Common Subsequence) with more
diagrams and pseudocode:
[https://en.wikipedia.org/wiki/Longest_common_subsequence_pro...](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem)

------
shaftway
I don't understand why there isn't a better code-aware diff tool available,
like an AST diff. When I split a method up, or add an if statement, can't we
detect that most of the code didn't change, it was just moved around?

This is the biggest frustration to me when doing and sending code reviews,
having to explain "I know it's a 5 thousand line change, but I really didn't
change any of the logic, I just moved the class into a subclass which caused
an indentation change."

~~~
mmun
I get your point, but for the indentation case in particular you can use `git
diff -w` or append `?w=1` to a github PR url.

~~~
shaftway
Except in Python where indentation changes are meaningful.

    
    
        def coalesce(*args):
          while args:
            next = args.pop()
            if next is not None:
              return next
          return None
    
        def coalesce(*args):
          while args:
            next = args.pop()
            if next is not None:
              return next
            return None

------
flippant
This is part of the Elixir standard library

[https://hexdocs.pm/elixir/List.html#myers_difference/2](https://hexdocs.pm/elixir/List.html#myers_difference/2)

[https://github.com/elixir-
lang/elixir/blob/master/lib/elixir...](https://github.com/elixir-
lang/elixir/blob/master/lib/elixir/lib/list.ex#L772)

------
teabee89
I strongly encourage people to read about the lesser-known Tichy diff
algorithm too: [https://bryanpendleton.blogspot.com/2010/04/more-study-of-
di...](https://bryanpendleton.blogspot.com/2010/04/more-study-of-diff-walter-
tichys-papers.html)

------
nachocab83
Does anyone know a way to modify this algorithm to list out all the possible
solutions? I asked something similar in stack overflow:
[http://softwareengineering.stackexchange.com/questions/34298...](http://softwareengineering.stackexchange.com/questions/342985/how-
can-i-modify-the-myers-diff-algorithm-to-choose-between-different-answers)

------
amelius
Any in-depth article on string diffs should mention alignment algorithms such
as Smith Waterman [1]

[1]
[https://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorit...](https://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorithm)

~~~
dekhn
All string diff algorithms in alignment derive from previous innovations on
raw strings. SW is a derivative of NW, which itself (although the paper does
not admit it) derives from the work done on edit distance in the mid 60s).
It's actually interesting that this algorithm was rediscovered independently
multiple times within a short time frame.

Also, the "Myers" in the article has worked a bit with sequence alignment. He
helped design the BLAST algorithm (with Altschul) as well as suffix arrays
(with Manber), the algorithms that reassembled the human genome for Celera,
and finally, dropped the ultimate bomb:
[https://academic.oup.com/bioinformatics/article/21/suppl_2/i...](https://academic.oup.com/bioinformatics/article/21/suppl_2/ii79/227189/The-
fragment-assembly-string-graph)

~~~
Animats
_All string diff algorithms in alignment derive from previous innovations on
raw strings._

For some applications, it's more useful to just take the hash of each line and
compute line diffs from an array of hashes. Diff algorithms which ignore
whitespace changes sometimes do this.

Few algorithms recognize text re-ordering. This is a problem with Wikipedia
diffs, where rearranging text is commonplace. Displaying the changes might
require big arrows, but there's no problem with that today.

~~~
WalterGR
_Few algorithms recognize text re-ordering._

WinMerge[1] does. Unfortunately it's no longer under development. (I've been
looking for an equally-capable tool for Macs but have found nothing...)

Do you know which algorithms recognize reordering?

[1] [http://winmerge.org/](http://winmerge.org/)

