The Longest Common Subsequence
At the heart of every diff algorithm is the Longest Common Subsequence (LCS) problem. Given two sequences, LCS finds the longest ordered set of elements present in both. For diff, the LCS represents lines that are unchanged — everything outside the LCS is either added or removed. The classic dynamic programming solution runs in O(mn) time where m and n are the line counts. For a 10,000-line file compared to a 10,200-line file, that is roughly 102 million operations — completed in milliseconds on modern hardware.