<?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>DEV Community: Naimur Rahman</title>
    <description>The latest articles on DEV Community by Naimur Rahman (@naimur_rahman_lam).</description>
    <link>https://dev.to/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>DEV Community: Naimur Rahman</title>
      <link>https://dev.to/naimur_rahman_lam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/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://dev.to/naimur_rahman_lam/dsa-application-in-real-life-how-git-diff-works-lcs-intuition-myers-algorithm-and-real-code-4ma2</link>
      <guid>https://dev.to/naimur_rahman_lam/dsa-application-in-real-life-how-git-diff-works-lcs-intuition-myers-algorithm-and-real-code-4ma2</guid>
      <description>&lt;h2&gt;
  
  
  The Algorithm Hiding Behind &lt;code&gt;git diff&lt;/code&gt;
&lt;/h2&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, it's one of the most classic problems in computer science: &lt;strong&gt;Longest Common Subsequence&lt;/strong&gt;. And it's been hiding inside your terminal every single day.&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 (Myers diff) 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 doesn't compare by position. It finds what's &lt;strong&gt;common&lt;/strong&gt; between the two files first. That's 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 Longest Common Subsequence.&lt;/p&gt;

&lt;p&gt;A subsequence means you can pick elements from a sequence while keeping their relative order — but they don't 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;code&gt;[A, C, D]&lt;/code&gt; — because A, C, and D 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;LCS = &lt;code&gt;[login(), validate(), save(), logout()]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now Git 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 → &lt;strong&gt;added&lt;/strong&gt;
&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;code&gt;A = [A, B, C, D]&lt;/code&gt; and &lt;code&gt;B = [A, C, E, D]&lt;/code&gt;, the DP table looks like:&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;code&gt;dp[4][4] = 3&lt;/code&gt; → LCS length is 3 → &lt;code&gt;[A, C, D]&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;For large files, this gets expensive — which is why Git doesn't use this directly.&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 is straightforward:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lines in the LCS → &lt;strong&gt;unchanged&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Lines in old but not in LCS → &lt;strong&gt;deleted&lt;/strong&gt; (prefix with &lt;code&gt;-&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Lines in new but not in LCS → &lt;strong&gt;added&lt;/strong&gt; (prefix with &lt;code&gt;+&lt;/code&gt;)&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. 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;blockquote&gt;
&lt;p&gt;What is the minimum number of insertions and deletions needed to transform the old file into the new file?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The connection to LCS is direct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LCS finds what is common.
Shortest Edit Script finds what changed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If LCS is long → fewer edits needed.&lt;br&gt;
If LCS is short → more edits needed.&lt;/p&gt;

&lt;p&gt;They are two sides of the same coin.&lt;/p&gt;

&lt;p&gt;So 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 (Simplified)
&lt;/h2&gt;

&lt;p&gt;Myers models the diff problem as a &lt;strong&gt;graph search&lt;/strong&gt;.&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 &lt;strong&gt;right&lt;/strong&gt; = delete a line from the old file&lt;/li&gt;
&lt;li&gt;Moving &lt;strong&gt;down&lt;/strong&gt; = insert a line from the new file&lt;/li&gt;
&lt;li&gt;Moving &lt;strong&gt;diagonally&lt;/strong&gt; = lines match (no edit needed)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For &lt;code&gt;Old = [A, B, C, D]&lt;/code&gt; and &lt;code&gt;New = [A, C, E, D]&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    B    C    D
   ┌────┬────┬────┬────┐
A  │╲   │    │    │    │
   ├────┼────┼────┼────┤
C  │    │    │╲   │    │
   ├────┼────┼────┼────┤
E  │    │    │    │    │
   ├────┼────┼────┼────┤
D  │    │    │    │╲   │
   └────┴────┴────┴────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Myers finds the &lt;strong&gt;path from (0,0) to (N,M) that uses the most diagonal moves&lt;/strong&gt; — because diagonals are free (matched lines). That path is the shortest edit script.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time complexity: O(n × d)&lt;/strong&gt; where d = number of differences&lt;br&gt;
&lt;strong&gt;Space complexity: O(n)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is much faster than O(m × n) LCS for files that are mostly similar — which is the common case in real codebases.&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;/p&gt;

&lt;p&gt;Old file → &lt;code&gt;[A, B, C, D]&lt;/code&gt;&lt;br&gt;
New file → &lt;code&gt;[A, C, E, D]&lt;/code&gt;&lt;/p&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;strong&gt;Delete B, Insert E&lt;/strong&gt; — that's just 2 operations.&lt;/p&gt;

&lt;p&gt;Git's 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, exactly right.&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 &lt;strong&gt;intent&lt;/strong&gt;.&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, the new code sums their prices.&lt;/p&gt;

&lt;p&gt;A bad diff creates noise and confusion. That's why diff algorithms matter — they're not just about correctness, they're about &lt;strong&gt;readability&lt;/strong&gt;.&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. The result is technically correct but hard to read. That's why Git ships multiple algorithms.&lt;/p&gt;




&lt;h2&gt;
  
  
  Git's Four Diff Algorithms — With a Real Example
&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 (default)
&lt;/h3&gt;

&lt;p&gt;Fast, generally good results. This is what runs when you just type &lt;code&gt;git diff&lt;/code&gt;. Best for everyday use.&lt;/p&gt;

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

&lt;p&gt;Tries harder to find the absolute smallest diff. Slower, but useful when patch size matters (e.g., generating patches to send via email).&lt;/p&gt;

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

&lt;p&gt;Prioritizes human readability. It only matches &lt;strong&gt;unique lines&lt;/strong&gt; first, avoiding false alignments on repeated code. 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. Often produces the most readable output for real codebases. Many developers set this as their global default.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side-by-side example — Myers vs Patience:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given this change (a function was refactored):&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="c1"&gt;# Old
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&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;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&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;item&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&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;result&lt;/span&gt;

&lt;span class="c1"&gt;# New
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Myers output&lt;/strong&gt; might show:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt; def process(data):
&lt;span class="gd"&gt;-    result = []
-    for item in data:
-        result.append(item * 2)
-    return result
&lt;/span&gt;&lt;span class="gi"&gt;+    return [item * 2 for item in data]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Patience output&lt;/strong&gt; groups the change more cleanly around unique anchors, making it immediately obvious that the body was replaced with a list comprehension — less noise, same information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To set histogram globally (recommended for most developers):&lt;/strong&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 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;
  
  
  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;Efficient when files are mostly similar&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;code&gt;m&lt;/code&gt; = number of lines in the old file and &lt;code&gt;n&lt;/code&gt; = number of lines in the new file.&lt;/p&gt;




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

&lt;p&gt;In competitive programming, LCS is a textbook DP problem. In the real world, the same idea powers:&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 (Gerrit, Phabricator)
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;&lt;strong&gt;What stayed the same, and what changed?&lt;/strong&gt;&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: a &lt;code&gt;role&lt;/code&gt; parameter was added, it's stored on the user object, and validation was introduced before saving. Three changes, instantly clear.&lt;/p&gt;

&lt;p&gt;That's the value of a good diff algorithm — it's not just computing differences. It's helping humans understand change.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Git Works at the Line Level (Not Character Level)
&lt;/h2&gt;

&lt;p&gt;Git diffs at the line level because source code is naturally line-based. A character-level diff would be more precise:&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;vs character diff: &lt;code&gt;* tax&lt;/code&gt; was appended.&lt;/p&gt;

&lt;p&gt;But character-level diffs get noisy fast for code review. Line-level is the right abstraction for most developer workflows. (You can get word-level diffs with &lt;code&gt;git diff --word-diff&lt;/code&gt; when you need them.)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The best algorithm isn't always the most precise one. It's the one that gives the most &lt;em&gt;useful&lt;/em&gt; 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 &lt;strong&gt;intuition&lt;/strong&gt;.&lt;br&gt;
Myers gives Git an &lt;strong&gt;efficient practical algorithm&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If the LCS is long → few changes needed.&lt;br&gt;
If the LCS is short → many changes needed.&lt;/p&gt;

&lt;p&gt;They measure the same thing 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;em&gt;"Where do we actually use DSA in real projects?"&lt;/em&gt;&lt;/p&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;br&gt;
When you review a pull request on GitHub, you're using an algorithm.&lt;br&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. 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. Not just for interviews. 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;LCS may look like just another DP problem when you first learn it. But the 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;Git uses it to show file changes. Code review tools use it to help developers understand pull requests. Merge tools use it to combine work from different branches. Document editors use it to show version history.&lt;/p&gt;

&lt;p&gt;So the next time you run &lt;code&gt;git diff&lt;/code&gt;, remember: you're not just seeing red and green lines. You're seeing dynamic programming, graph search, and decades of algorithmic research — all compressed into a two-word 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>
