<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Naimur Rahman</title>
    <description>The latest articles on Forem by Naimur Rahman (@naimur_rahman_lam).</description>
    <link>https://forem.com/naimur_rahman_lam</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3946692%2F40d7055a-d31f-422c-99e8-b42b9fbc9176.jpg</url>
      <title>Forem: Naimur Rahman</title>
      <link>https://forem.com/naimur_rahman_lam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/naimur_rahman_lam"/>
    <language>en</language>
    <item>
      <title>DSA Application in Real Life: How Git Diff Works: LCS Intuition, Myers Algorithm, and Real Code Changes</title>
      <dc:creator>Naimur Rahman</dc:creator>
      <pubDate>Sat, 23 May 2026 04:42:14 +0000</pubDate>
      <link>https://forem.com/naimur_rahman_lam/dsa-application-in-real-life-how-git-diff-works-lcs-intuition-myers-algorithm-and-real-code-4ma2</link>
      <guid>https://forem.com/naimur_rahman_lam/dsa-application-in-real-life-how-git-diff-works-lcs-intuition-myers-algorithm-and-real-code-4ma2</guid>
      <description>&lt;h1&gt;
  
  
  The Algorithm Hiding Behind &lt;code&gt;git diff&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;You've run &lt;code&gt;git diff&lt;/code&gt; hundreds of times.&lt;/p&gt;

&lt;p&gt;Red lines. Green lines. Done.&lt;/p&gt;

&lt;p&gt;But have you ever stopped and asked — &lt;em&gt;what algorithm is actually doing that?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It turns out, the idea is closely related to one of the most classic problems in computer science: &lt;strong&gt;Longest Common Subsequence&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore how Git-style diffing works, why LCS is the right mental model, how the actual algorithm Git uses — &lt;strong&gt;Myers diff&lt;/strong&gt; — connects to it, and what tradeoffs real tools make when choosing a diff algorithm.&lt;/p&gt;

&lt;p&gt;This is the first article in my series &lt;strong&gt;"DSA Application in Real Life"&lt;/strong&gt; — where I explore how common data structures and algorithms power the tools developers use every day.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem Git Is Solving
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj7cc3ernrkipj6fl3u0c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj7cc3ernrkipj6fl3u0c.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine we have an old version of a file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we update it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we run &lt;code&gt;git diff&lt;/code&gt;, Git shows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-function add(a, b) {
&lt;/span&gt;&lt;span class="gi"&gt;+function addNumbers(a, b) {
&lt;/span&gt;   return a + b;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks obvious to us as humans. Only the function name changed.&lt;/p&gt;

&lt;p&gt;But Git does not "understand" JavaScript the way we do. At the diffing level, Git treats the file as a &lt;strong&gt;sequence of lines&lt;/strong&gt;. Its job is to compare two sequences and decide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which lines stayed the same?&lt;/li&gt;
&lt;li&gt;Which lines were deleted?&lt;/li&gt;
&lt;li&gt;Which lines were added?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a sequence comparison problem — and that's exactly where LCS comes in.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Simple Line-by-Line Comparison Is Not Enough
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8l58vzip9dko7jnkbtu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8l58vzip9dko7jnkbtu.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A beginner might think Git just compares files line by line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Old line 1 vs New line 1
Old line 2 vs New line 2
Old line 3 vs New line 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works only when changes happen at the same position. But real code changes are rarely that simple.&lt;/p&gt;

&lt;p&gt;Consider this old file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we insert one new line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;checkPermission&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A naive line-by-line comparison would produce:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Old: login()      New: login()            same
Old: validate()   New: checkPermission()  different
Old: save()       New: validate()         different
Old: logout()     New: save()             different
Old: (nothing)    New: logout()           added
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That makes it look like almost the entire file changed — which is completely wrong. Only one line was added.&lt;/p&gt;

&lt;p&gt;A smarter approach does not compare by position only. It first finds what stayed common between the two files.&lt;/p&gt;

&lt;p&gt;That is the LCS idea.&lt;/p&gt;




&lt;h2&gt;
  
  
  LCS: The Mental Model Behind Diffing
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexq9gjm8ddkxb5belj0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexq9gjm8ddkxb5belj0h.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LCS&lt;/strong&gt; stands for &lt;strong&gt;Longest Common Subsequence&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A subsequence means you can pick elements from a sequence while keeping their relative order — but they do not need to be adjacent.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Old = [A, B, C, D]
New = [A, C, E, D]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The longest common subsequence is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[A, C, D]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;C&lt;/code&gt;, and &lt;code&gt;D&lt;/code&gt; appear in both sequences in the same order.&lt;/p&gt;

&lt;p&gt;Applied to file diffing, the lines of each file become the sequences:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Old = [login(), validate(), save(), logout()]
New = [login(), checkPermission(), validate(), save(), logout()]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The LCS is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[login(), validate(), save(), logout()]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Git-style diffing can reason:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These lines are &lt;strong&gt;common&lt;/strong&gt; → unchanged&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;checkPermission()&lt;/code&gt; is only in the new file → added&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; login()
&lt;span class="gi"&gt;+checkPermission()
&lt;/span&gt; validate()
 save()
 logout()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the core idea.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Actual LCS Algorithm with Code
&lt;/h2&gt;

&lt;p&gt;Here's the classic dynamic programming solution you've likely seen in competitive programming:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lcs_length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# dp[i][j] = LCS length of A[:i] and B[:j]
&lt;/span&gt;    &lt;span class="n"&gt;dp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For sequences:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A = [A, B, C, D]
B = [A, C, E, D]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The DP table looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     ""  A   C   E   D
""  [ 0   0   0   0   0 ]
A   [ 0   1   1   1   1 ]
B   [ 0   1   1   1   1 ]
C   [ 0   1   2   2   2 ]
D   [ 0   1   2   2   3 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The answer is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dp[4][4] = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the LCS length is &lt;code&gt;3&lt;/code&gt;, and the LCS is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[A, C, D]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time complexity:&lt;/strong&gt; &lt;code&gt;O(m × n)&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Space complexity:&lt;/strong&gt; &lt;code&gt;O(m × n)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For large files, this gets expensive — which is why Git does not use textbook LCS directly.&lt;/p&gt;


&lt;h2&gt;
  
  
  Reconstructing the LCS from the DP Table
&lt;/h2&gt;

&lt;p&gt;The DP table gives us the length of the LCS.&lt;/p&gt;

&lt;p&gt;But to build an actual diff, we also need the common lines themselves.&lt;/p&gt;

&lt;p&gt;We can get them by backtracking from the bottom-right corner of the table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;build_lcs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;dp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

    &lt;span class="c1"&gt;# Build DP table
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

    &lt;span class="c1"&gt;# Backtrack to reconstruct the actual LCS
&lt;/span&gt;    &lt;span class="n"&gt;lcs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;lcs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lcs&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


&lt;span class="n"&gt;old_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;A&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;B&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;C&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;D&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;new_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;A&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;C&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;E&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;D&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;build_lcs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_file&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# Output: ['A', 'C', 'D']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we do not only know the LCS length. We also know the actual common lines.&lt;/p&gt;

&lt;p&gt;That is what lets us decide which lines stayed unchanged, which lines were deleted, and which lines were added.&lt;/p&gt;




&lt;h2&gt;
  
  
  How LCS Builds the Diff
&lt;/h2&gt;

&lt;p&gt;Once you know the LCS, building the diff becomes straightforward:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lines in the LCS → unchanged&lt;/li&gt;
&lt;li&gt;Lines in old but not in the LCS → deleted&lt;/li&gt;
&lt;li&gt;Lines in new but not in the LCS → added&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Old = [A, B, C, D]
New = [A, C, E, D]
LCS = [A, C, D]

B is only in Old  → deleted
E is only in New  → added
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Diff output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; A
&lt;span class="gd"&gt;-B
&lt;/span&gt; C
&lt;span class="gi"&gt;+E
&lt;/span&gt; D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the basic shape of what Git, GitHub pull requests, VS Code comparison, and merge tools show: unchanged lines, deleted lines, and added lines.&lt;/p&gt;




&lt;h2&gt;
  
  
  Does Git Actually Use Textbook LCS?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjiv9l0cjufcdvhv2ea9p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjiv9l0cjufcdvhv2ea9p.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not directly.&lt;/p&gt;

&lt;p&gt;Git's default algorithm is &lt;strong&gt;Myers diff&lt;/strong&gt; — and it solves a slightly different but deeply related problem called the &lt;strong&gt;Shortest Edit Script&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Shortest Edit Script asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is the smallest number of insertions and deletions needed to transform the old file into the new file?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;LCS and Shortest Edit Script are closely connected.&lt;/p&gt;

&lt;p&gt;LCS asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is the longest structure that stayed the same?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Shortest Edit Script asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is the smallest set of changes needed to transform one sequence into another?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When only insertions and deletions are allowed, minimizing the edit script is mathematically related to maximizing the LCS length.&lt;/p&gt;

&lt;p&gt;For two sequences with lengths &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;edit distance = m + n - 2 × LCS length
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So yes, they are two sides of the same coin — but they approach the problem from different directions.&lt;/p&gt;

&lt;p&gt;When we say "Git uses LCS-based diffing," the accurate meaning is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Git's diffing is based on sequence-comparison ideas rooted in LCS, but its default implementation uses Myers' shortest edit script algorithm, which is faster in practice.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  How Myers Diff Actually Works
&lt;/h2&gt;

&lt;p&gt;Myers models the diff problem as a graph search.&lt;/p&gt;

&lt;p&gt;Imagine a grid where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The X-axis represents lines of the &lt;strong&gt;old&lt;/strong&gt; file&lt;/li&gt;
&lt;li&gt;The Y-axis represents lines of the &lt;strong&gt;new&lt;/strong&gt; file&lt;/li&gt;
&lt;li&gt;Moving right means deleting a line from the old file&lt;/li&gt;
&lt;li&gt;Moving down means inserting a line from the new file&lt;/li&gt;
&lt;li&gt;Moving diagonally means the lines match, so no edit is needed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Old = [A, B, C, D]
New = [A, C, E, D]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The matching positions are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A matches A
C matches C
D matches D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A simplified grid looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          Old
        A    B    C    D
      ┌────┬────┬────┬────┐
New A │╲   │    │    │    │
      ├────┼────┼────┼────┤
    C │    │    │╲   │    │
      ├────┼────┼────┼────┤
    E │    │    │    │    │
      ├────┼────┼────┼────┤
    D │    │    │    │╲   │
      └────┴────┴────┴────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The diagonal marks show where the two sequences have the same line.&lt;/p&gt;

&lt;p&gt;But the important part is not only the matching cells.&lt;/p&gt;

&lt;p&gt;The important part is the &lt;strong&gt;path&lt;/strong&gt; from the top-left corner to the bottom-right corner.&lt;/p&gt;

&lt;p&gt;For this example, one shortest path is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(0,0)
  │
  ├─ diagonal: A matches A
  │
(1,1)
  │
  ├─ right: delete B
  │
(2,1)
  │
  ├─ diagonal: C matches C
  │
(3,2)
  │
  ├─ down: insert E
  │
(3,3)
  │
  ├─ diagonal: D matches D
  │
(4,4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the path is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;diagonal → right → diagonal → down → diagonal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Keep A
Delete B
Keep C
Insert E
Keep D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's walk through that path step by step.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Match &lt;code&gt;A&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Both files start with &lt;code&gt;A&lt;/code&gt;, so Myers can move diagonally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Old: A B C D
New: A C E D

Match: A
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No edit is needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Delete &lt;code&gt;B&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;After &lt;code&gt;A&lt;/code&gt;, the old file has &lt;code&gt;B&lt;/code&gt;, but the new file has &lt;code&gt;C&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;They do not match, so one shortest path deletes &lt;code&gt;B&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-B
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Match &lt;code&gt;C&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now both sides line up at &lt;code&gt;C&lt;/code&gt;, so Myers moves diagonally again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Match: C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Insert &lt;code&gt;E&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;After &lt;code&gt;C&lt;/code&gt;, the new file has &lt;code&gt;E&lt;/code&gt;, but the old file moves toward &lt;code&gt;D&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So Myers inserts &lt;code&gt;E&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gi"&gt;+E
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Match &lt;code&gt;D&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Finally, both files match again at &lt;code&gt;D&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The final shortest edit script is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; A
&lt;span class="gd"&gt;-B
&lt;/span&gt; C
&lt;span class="gi"&gt;+E
&lt;/span&gt; D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the shortest edit script has only two edits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Delete B
Insert E
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So here, &lt;code&gt;D = 2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That is the key idea behind Myers.&lt;/p&gt;

&lt;p&gt;It is not randomly comparing lines.&lt;/p&gt;

&lt;p&gt;It is searching the edit graph for the shortest path that converts one sequence into another.&lt;/p&gt;

&lt;p&gt;The path with the fewest right and down moves gives the shortest edit script.&lt;/p&gt;

&lt;p&gt;Diagonal moves are free because they represent lines that already match.&lt;/p&gt;

&lt;p&gt;The algorithm is commonly described as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Time complexity: O(ND)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;N&lt;/code&gt; is the total number of lines across both files&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;D&lt;/code&gt; is the size of the shortest edit script&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In simple words, &lt;code&gt;D&lt;/code&gt; means how many insertions and deletions are needed to transform the old file into the new file.&lt;/p&gt;

&lt;p&gt;For space complexity, it depends on the implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Common Myers implementation: O(N)
Linear-space Myers variant: O(D)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why Myers performs very well when two files are mostly similar, which is the common case in real codebases.&lt;/p&gt;

&lt;p&gt;Instead of comparing every possible pair of lines like textbook LCS DP, Myers focuses on finding a short path of edits between the two versions.&lt;/p&gt;




&lt;h2&gt;
  
  
  Diff as an Edit Script
&lt;/h2&gt;

&lt;p&gt;Let's walk through a concrete edit script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Old file → [A, B, C, D]
New file → [A, C, E, D]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 1: Delete &lt;code&gt;B&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A, C, D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Insert &lt;code&gt;E&lt;/code&gt; after &lt;code&gt;C&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A, C, E, D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Delete B, Insert E
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is just two operations.&lt;/p&gt;

&lt;p&gt;Git-style diff output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; A
&lt;span class="gd"&gt;-B
&lt;/span&gt; C
&lt;span class="gi"&gt;+E
&lt;/span&gt; D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clean, minimal, and easy to understand.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Matters in Real Development
&lt;/h2&gt;

&lt;p&gt;When we review code, we're not just looking at text changes — we're trying to understand intent.&lt;/p&gt;

&lt;p&gt;A good diff makes that easy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; function calculateTotal(items) {
&lt;span class="gd"&gt;-  return items.length;
&lt;/span&gt;&lt;span class="gi"&gt;+  return items.reduce((sum, item) =&amp;gt; sum + item.price, 0);
&lt;/span&gt; }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any reviewer immediately understands: the old code counted items, and the new code sums their prices.&lt;/p&gt;

&lt;p&gt;A bad diff creates noise and confusion.&lt;/p&gt;

&lt;p&gt;That's why diff algorithms matter. They are not only about correctness. They are also about readability.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Tradeoff: Shortest Diff vs Most Readable Diff
&lt;/h2&gt;

&lt;p&gt;The smallest diff is not always the most readable one — especially in code with repeated patterns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When many lines look similar, a diff algorithm can match the wrong lines.&lt;/p&gt;

&lt;p&gt;The result may be technically correct, but hard to read.&lt;/p&gt;

&lt;p&gt;That's why Git ships multiple diff algorithms.&lt;/p&gt;




&lt;h2&gt;
  
  
  Git's Four Diff Algorithms
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1v3irxxy6cl6zk6jdzdz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1v3irxxy6cl6zk6jdzdz.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff &lt;span class="nt"&gt;--diff-algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;myers      &lt;span class="c"&gt;# default&lt;/span&gt;
git diff &lt;span class="nt"&gt;--diff-algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;minimal
git diff &lt;span class="nt"&gt;--diff-algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;patience
git diff &lt;span class="nt"&gt;--diff-algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;histogram
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what each one does and when to use it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Myers
&lt;/h3&gt;

&lt;p&gt;Fast and generally good.&lt;/p&gt;

&lt;p&gt;This is what runs when you just type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Best for everyday use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Minimal
&lt;/h3&gt;

&lt;p&gt;Tries harder to find the smallest possible diff.&lt;/p&gt;

&lt;p&gt;It can be slower, but useful when patch size matters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Patience
&lt;/h3&gt;

&lt;p&gt;Prioritizes human readability.&lt;/p&gt;

&lt;p&gt;It matches unique lines first, which helps avoid false alignments on repeated code.&lt;/p&gt;

&lt;p&gt;Best for reviewing refactors or moved code blocks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Histogram
&lt;/h3&gt;

&lt;p&gt;An evolution of Patience that also handles low-frequency lines well.&lt;/p&gt;

&lt;p&gt;It often produces readable output for real codebases. Some developers prefer setting it as their global default because it can make source code diffs easier to review.&lt;/p&gt;

&lt;p&gt;To set Histogram as your global default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; diff.algorithm histogram
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Myers vs Patience Diff
&lt;/h2&gt;

&lt;p&gt;Myers is very good at finding a short edit script.&lt;/p&gt;

&lt;p&gt;But sometimes the shortest diff is not the most readable diff.&lt;/p&gt;

&lt;p&gt;This usually happens when a file has repeated or similar-looking lines. In that case, the algorithm may choose matches that are technically valid but not ideal for human review.&lt;/p&gt;

&lt;p&gt;Consider this example.&lt;/p&gt;

&lt;p&gt;Old version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;validate_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;validate_admin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;validate_admin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;validate_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;validate_admin&lt;/code&gt; moved from the bottom to the top.&lt;/p&gt;

&lt;p&gt;Because the functions contain repeated lines like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;a shortest-diff algorithm can sometimes align the repeated lines in a confusing way.&lt;/p&gt;

&lt;p&gt;A Myers-style diff may produce a technically correct result like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gi"&gt;+def validate_admin(admin):
+    if not admin.email:
+        return False
+    return True
+
&lt;/span&gt; def validate_user(user):
     if not user.email:
         return False
     return True
&lt;span class="err"&gt;
&lt;/span&gt; def save_user(user):
     database.save(user)
&lt;span class="gd"&gt;-
-def validate_admin(admin):
-    if not admin.email:
-        return False
-    return True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This output is correct: it shows that &lt;code&gt;validate_admin&lt;/code&gt; was added at the top and removed from the bottom.&lt;/p&gt;

&lt;p&gt;But for a reviewer, the important idea is simpler:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One function moved position.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Patience diff tries to make this kind of refactor easier to read by first looking for unique lines as anchors, such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;validate_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;validate_admin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These unique lines help the algorithm avoid matching only repeated lines like &lt;code&gt;return False&lt;/code&gt; and &lt;code&gt;return True&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For this small example, Patience may still produce an output that looks very similar to Myers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gi"&gt;+def validate_admin(admin):
+    if not admin.email:
+        return False
+    return True
+
&lt;/span&gt; def validate_user(user):
     if not user.email:
         return False
     return True
&lt;span class="err"&gt;
&lt;/span&gt; def save_user(user):
     database.save(user)
&lt;span class="gd"&gt;-
-def validate_admin(admin):
-    if not admin.email:
-        return False
-    return True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the important point is not that Patience magically shows a special "move" operation.&lt;/p&gt;

&lt;p&gt;Git diffs are still usually represented as additions and deletions.&lt;/p&gt;

&lt;p&gt;The real benefit of Patience appears more clearly in larger refactors, especially when a file has many repeated lines such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;break&lt;/span&gt;
&lt;span class="k"&gt;continue&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In those cases, Myers may match repeated lines too aggressively, while Patience prefers stronger unique anchors. That often makes the final diff easier for humans to review.&lt;/p&gt;

&lt;p&gt;The exact output can vary depending on file context and Git version, but the idea is the same:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Myers focuses on finding a short edit script.&lt;/li&gt;
&lt;li&gt;Patience focuses more on stable, unique anchors.&lt;/li&gt;
&lt;li&gt;The shortest diff is not always the clearest diff.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Algorithm Complexity Summary
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Algorithm&lt;/th&gt;
&lt;th&gt;Rough Idea&lt;/th&gt;
&lt;th&gt;Best For&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Textbook LCS DP&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;O(m × n)&lt;/code&gt; time and space&lt;/td&gt;
&lt;td&gt;Learning the concept&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Myers diff&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;O(ND)&lt;/code&gt; in the common description&lt;/td&gt;
&lt;td&gt;Default everyday diffs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Minimal&lt;/td&gt;
&lt;td&gt;Spends extra work to reduce diff size&lt;/td&gt;
&lt;td&gt;Smaller patches&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Patience&lt;/td&gt;
&lt;td&gt;Uses unique lines as anchors&lt;/td&gt;
&lt;td&gt;Refactors / moved blocks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Histogram&lt;/td&gt;
&lt;td&gt;Extends Patience using low-frequency lines&lt;/td&gt;
&lt;td&gt;Often readable code diffs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;m&lt;/code&gt; = number of lines in the old file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt; = number of lines in the new file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;N&lt;/code&gt; = total number of lines across both files&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;D&lt;/code&gt; = size of the shortest edit script&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Where the DSA Is Hiding
&lt;/h2&gt;

&lt;p&gt;In competitive programming, LCS is a textbook DP problem.&lt;/p&gt;

&lt;p&gt;In the real world, the same idea appears in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Git diff
GitHub pull request review
VS Code file comparison
Merge conflict resolution
Google Docs version history
Code review platforms
Patch generation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The input changes — lines of code, words in a document, DOM nodes in a UI, events in a timeline — but the core question is always the same:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What stayed the same, and what changed?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  A Real-World Developer Example
&lt;/h2&gt;

&lt;p&gt;Old code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="nf"&gt;validateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A well-tuned diff shows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-function createUser(name, email) {
&lt;/span&gt;&lt;span class="gi"&gt;+function createUser(name, email, role) {
&lt;/span&gt;&lt;span class="gd"&gt;-  const user = { name, email };
&lt;/span&gt;&lt;span class="gi"&gt;+  const user = { name, email, role };
+  validateUser(user);
&lt;/span&gt;   saveUser(user);
   return user;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any reviewer immediately understands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;code&gt;role&lt;/code&gt; parameter was added&lt;/li&gt;
&lt;li&gt;the role is stored on the user object&lt;/li&gt;
&lt;li&gt;validation was introduced before saving&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's the value of a good diff algorithm.&lt;/p&gt;

&lt;p&gt;It is not just computing differences. It is helping humans understand change.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Git Usually Works at the Line Level
&lt;/h2&gt;

&lt;p&gt;By default, Git usually presents diffs at the line level because source code is naturally organized line by line.&lt;/p&gt;

&lt;p&gt;For example, if we change this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;-const total = price * quantity;
&lt;/span&gt;&lt;span class="gi"&gt;+const total = price * quantity * tax;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A character-level diff could say that only &lt;code&gt;* tax&lt;/code&gt; was appended.&lt;/p&gt;

&lt;p&gt;That is more precise, but precision is not always the same as readability.&lt;/p&gt;

&lt;p&gt;In real code reviews, developers usually care about which lines changed and how those changes affect the surrounding code.&lt;/p&gt;

&lt;p&gt;Character-level diffs can become noisy very quickly, especially when formatting, indentation, or multiple small edits happen in the same line.&lt;/p&gt;

&lt;p&gt;That is why line-level diffing is a good default for most developer workflows.&lt;/p&gt;

&lt;p&gt;But Git still gives you more detailed options when you need them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff &lt;span class="nt"&gt;--word-diff&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This shows word-level changes inside modified lines.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The best algorithm is not always the most precise one. It is the one that gives the most useful output for the context.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  LCS vs Myers: The Mental Model
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LCS:    Find the longest part that stayed the same.
Myers:  Find the shortest set of changes to get from old to new.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;LCS gives you the intuition.&lt;/p&gt;

&lt;p&gt;Myers gives Git an efficient practical algorithm.&lt;/p&gt;

&lt;p&gt;When only insertions and deletions are allowed, these two views are mathematically connected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;edit distance = old length + new length - 2 × LCS length
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the LCS is long → fewer edits are needed&lt;/li&gt;
&lt;li&gt;If the LCS is short → more edits are needed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They measure the same underlying change from different directions.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Is a Great Example of DSA in Real Life
&lt;/h2&gt;

&lt;p&gt;Many beginners ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Where do we actually use DSA in real projects?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;git diff&lt;/code&gt; is one of the best answers — because every developer runs it daily without thinking about it.&lt;/p&gt;

&lt;p&gt;When you run &lt;code&gt;git diff&lt;/code&gt;, you're using an algorithm.&lt;/p&gt;

&lt;p&gt;When you review a pull request on GitHub, you're using an algorithm.&lt;/p&gt;

&lt;p&gt;When you resolve merge conflicts, you're relying on algorithms that compare versions of files.&lt;/p&gt;

&lt;p&gt;The algorithm is invisible behind a clean developer experience.&lt;/p&gt;

&lt;p&gt;That's what good engineering looks like: the user sees red and green lines, and behind it is a carefully designed algorithmic solution built on decades of computer science research.&lt;/p&gt;

&lt;p&gt;That's the beauty of DSA.&lt;/p&gt;

&lt;p&gt;Not just for interviews.&lt;/p&gt;

&lt;p&gt;Inside the tools you use every day.&lt;/p&gt;




&lt;h2&gt;
  
  
  Practical Commands to Try
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Try different algorithms on any repo&lt;/span&gt;
git diff &lt;span class="nt"&gt;--diff-algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;myers
git diff &lt;span class="nt"&gt;--diff-algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;patience
git diff &lt;span class="nt"&gt;--diff-algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;histogram
git diff &lt;span class="nt"&gt;--diff-algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;minimal

&lt;span class="c"&gt;# Word-level diff, great for prose or config files&lt;/span&gt;
git diff &lt;span class="nt"&gt;--word-diff&lt;/span&gt;

&lt;span class="c"&gt;# Set histogram as your permanent default&lt;/span&gt;
git config &lt;span class="nt"&gt;--global&lt;/span&gt; diff.algorithm histogram
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;When you first learn LCS, it may look like just another dynamic programming problem.&lt;/p&gt;

&lt;p&gt;But the core idea is powerful:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Find what stayed the same so we can understand what changed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That simple idea appears everywhere.&lt;/p&gt;

&lt;p&gt;Git uses related sequence-comparison ideas to show file changes. Code review tools use similar techniques to help developers understand pull requests. Merge tools use them to combine work from different branches. Document editors use them to show version history.&lt;/p&gt;

&lt;p&gt;So the next time you run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;remember that you are not just seeing red and green lines.&lt;/p&gt;

&lt;p&gt;You are seeing dynamic programming intuition, graph search, and decades of algorithmic research — all compressed into one everyday developer command.&lt;/p&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/docs/git-diff" rel="noopener noreferrer"&gt;Git Documentation: &lt;code&gt;--diff-algorithm={patience|minimal|histogram|myers}&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://link.springer.com/article/10.1007/s10664-019-09772-z" rel="noopener noreferrer"&gt;"How Different Are Different Diff Algorithms in Git?" — Yusuf Sulistyo Nugroho, Hideaki Hata, Kenichi Matsumoto&lt;/a&gt; &lt;em&gt;(Empirical Software Engineering, 2020)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.xmailserver.org/diff2.pdf" rel="noopener noreferrer"&gt;Myers, E.W. "An O(ND) Difference Algorithm and Its Variations" (1986)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>datastructures</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
