In GNU diff, also used on FreeBSD, the --minimal flag triggers an algorithm variation by Paul Eggert that causes it "to limit the cost to O(N**1.5 log N) at the price of producing suboptimal output for large inputs with differences". More specifically, it causes it to not apply several heuristics that deal in finding merely close to optimal solutions and in throwing out "confusing" lines as extra differences.
In OpenBSD diff, which uses the older Unix diff algorithm from the 1970s, the algorithm employed is credited to Harold Stone, and the -d flagthe --minimal flag triggers a search that is (effectively un-) bounded by the maximum value of an unsigned integer instead of by the square root of the size of the range of lines being compared (or 256 if it is greater).
Further reading
- Eugene W. Myers (November 1986). "An O(ND) difference algorithm and its variations". Algorithmica. Volume 1. Issue 1–4. pp. 251–266. DOI 10.1007/BF01840446.
- J. W. Hunt and M. D. McIlroy (June 1976). "An Algorithm for Differential File Comparison". Report 41. Computing Science. Bell Laboratories.
- Richard Hartman (1988-01-13). Unix diff(1) algorithm. [email protected]. comp.unix.questions.
- https://github.com/openbsd/src/blob/d1e24f318523607c98dc6fbe5a06a5d9e5c87293/usr.bin/diff/diffreg.c#L93
- https://github.com/freebsd/freebsd/blob/40ec4fdc9a74bfdb83f13672acdb88af5c91ab46/contrib/diff/src/analyze.c#L23
- Comprehensive review of diff algorithms, their history and implementations