Proofs: Myers diff algorithm
These are the human proofs of Myers diff algorithm, at full length.
Myers' greedy search finds a shortest edit script full
This proof shows: correctness.
We use the pseudocode of the page. The search of lines L4 to L7 can produce points with or , so we work in an extended graph . Its vertices are all points with integers . Each point has a right edge to and a down edge to , each of cost 1, and a diagonal edge to of cost 0 if , and . The edit graph of the page is the part with and . Along each edge, and do not decrease. The diagonal of is ; a right edge raises by 1, a down edge lowers it by 1, and a diagonal edge keeps it. The cost of a path is its number of right and down edges.
For a point , let be the end of the longest run of diagonal edges from : the point with largest such that the diagonal edges exist. This is what line L7 computes.
Let be the set of points that are the end of some path of cost exactly from , and for each let , or undefined if no such point exists.
Lemma 1 (diagonals). If , then and . For each , is defined and finite.
Proof. A path of cost with right and down edges ends on diagonal , which has the parity of and lies in . For a given , the path of right edges then down edges reaches diagonal , so is defined. It is finite, since a path of cost has at most right edges and at most diagonal edges, so .
Lemma 2 (later is better). Let and be on the same diagonal with . Then each point on this diagonal that can be reached from by diagonal edges only has first coordinate at most that of .
Proof. The diagonal edges from go through the points , If the run from stops before , then has first coordinate below the first coordinate of . Otherwise the run passes through , and from there it uses the same edges as the run from , so it ends at , and is on this run.
Lemma 3 (the recurrence, Myers' Lemma 2). is the first coordinate of . For and , let
Then is the first coordinate of .
Proof. For , the paths of cost 0 are runs of diagonal edges from , and the furthest one ends at .
Let . () The point of diagonal at is in ; a down edge takes it to on diagonal , at cost ; then the snake keeps cost . In the same way, the point of diagonal at and a right edge reach on diagonal . These points exist when and respectively, by Lemma 1, which are the cases of . So .
() Let be on diagonal , at the end of a path of cost . Cut the path after its last right or down edge, at the point . Then is reached from by diagonal edges only. The point before this last edge is in . If the edge is a down edge, that point is on diagonal with first coordinate at most , and has the same first coordinate. If it is a right edge, the point is on diagonal with first coordinate at most , and has one more. In both cases is on diagonal with first coordinate at most . By Lemma 2 with and , the first coordinate of is at most that of .
Lemma 4 (the loop computes ). After round of the loop of line L2, for each , and these values are saved as .
Proof. Induction on . In round 0, , so line L4 sets (line L1), and lines L6 to L8 store the snake from ; this is by Lemma 3. In round , the entries read in lines L4 and L5 are on diagonals of the parity of , which round does not write, so they hold by the induction hypothesis. If , line L4 takes . If , the test of line L4 fails and line L5 takes . Otherwise line L4 takes when , that is, when , and line L5 takes otherwise; in both cases . Line L7 follows the snake: its test is exactly the condition for a diagonal edge of . By Lemma 3, line L8 stores .
Lemma 5 (the stop). Let be the cost of a cheapest path from to , and call a point final if and . If a final point is in , then , and if the point is .
Proof. Let a path of cost end at a final point . Along the path the coordinates grow by at most 1 per edge, from , so there is a first point of the path with or ; then and . After the path has no diagonal edges, since these need and , so the rest of the path costs . The path from to , followed by right edges and down edges (one of the two numbers is 0), reaches at cost
So , with equality only if and .
Theorem. MYERS returns , and TRACEBACK outputs a shortest edit script from to .
Proof. The point is reached by a path of cost , so it is in on diagonal , and , with ; so round finds a final point. By Lemma 5, no earlier round does, since a final point of round is in by Lemma 4. So the loop stops at round (and , so line L2 runs far enough). In round , each final point that line L9 finds is , again by Lemma 5.
TRACEBACK starts at with . In step , it is at the point for the current . Line L12 repeats the choice of line L4 in round , with the same values , so is the point of round from which round started, is after one down or right edge, and is the end of the snake from it (Lemma 4). So the steps of the traceback walk back a path of : a snake, one right or down edge, then the same for , down to round 0, whose snake starts at . This path has cost and ends at . A path of that ends at stays in the edit graph, since coordinates do not decrease. Its right edges are deletions, its down edges are insertions, and its diagonal edges are kept items, so the output is an edit script of operations. By the theorem of the shortest edit script page, the edit scripts correspond to the paths of the edit graph, so no script is shorter.
Cost. Round visits diagonals with work outside line L7, so in all. For line L7, consider a diagonal and rounds and with . The point of round on diagonal , followed by a right edge, gives , so in round : the snake of round starts after the end of the snake of round . So each diagonal edge on diagonal is walked at most once, and diagonal has at most diagonal edges. With at most diagonals, line L7 takes steps, and the total is .