Question

I am implementing my own diff in c++ using the LCS algo. When looking at the LCS algo paper and trying to find the SES ( shortest edit script ) there are sometimes when you get a change for a line. In the following graph example it shows only inserts and deletes. Therefore traversing the graph backwards you have horizontal lines are deletes and vertical lines are inserts. What is the case where you have a 'c' change case? I am assuming it would be a horizontal line followed by a vertical ie: delete then insert would be considered change. I haven't seen any code in C++ that is creating the SES directly. Maybe there is a better implementation that can get the O(ND) or better time complexity, although I am not sure how to create the SES without creating and reversing the graph. I can implement the LCS build the graph and traverse it back I am just trying to know ahead of time what's the exact rules for creating the SES as it seems in this case you might need to maintain 2 states a delete and insert to be considered line change(s)? See the following files where you get a change 3,4c4,6 (see below). If anyone has any sample code that is doing this such that it is maximizing run time versus space in c++ that would be helpful as a reference especially in regards to creating the diff output in the current diff binary form shown below. I was using this version because I am maximizing on runtime and sacrificing space for this.

See page 3 of the document with the graph of their example where they show the SES reverse of the graph

http://www.xmailserver.org/diff2.pdf

My change diff example using regular diff on linux

fileA.txt
a
b
c
d
e
f
g

fileB.txt
w
a
b
x
z
y
e

diff fileA.txt fileB.txt 
0a1
> w
3,4c4,6
< c
< d
---
> x
> z
> y
6,7d7
< f
< g
Was it helpful?

Solution

Basically the 'c' or change functionality is not needed. You can just traverse back from LCS tree which gives the SES and you can reconstruct both with just insert and deletes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top