<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ayush Mishra on Medium]]></title>
        <description><![CDATA[Stories by Ayush Mishra on Medium]]></description>
        <link>https://medium.com/@ayush250173?source=rss-1f5648476c40------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gHlzrahIjyKZItjIeMa95g@2x.jpeg</url>
            <title>Stories by Ayush Mishra on Medium</title>
            <link>https://medium.com/@ayush250173?source=rss-1f5648476c40------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 13:11:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ayush250173/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Finding All Ancestors in a DAG: From DFS to Topological Dynamic Programming.]]></title>
            <link>https://medium.com/@ayush250173/finding-all-ancestors-in-a-dag-from-dfs-to-topological-dynamic-programming-95e8d9902d25?source=rss-1f5648476c40------2</link>
            <guid isPermaLink="false">https://medium.com/p/95e8d9902d25</guid>
            <dc:creator><![CDATA[Ayush Mishra]]></dc:creator>
            <pubDate>Sun, 03 May 2026 22:35:50 GMT</pubDate>
            <atom:updated>2026-05-03T23:18:14.564Z</atom:updated>
            <content:encoded><![CDATA[<p>The problem is simple: you have to find the ancestors of all the nodes in a Directed Acyclic Graph (DAG), which consists of V nodes and E edges.</p><p>How do you solve it? Think for at least 2 minutes before looking at the solutions.</p><p>Approach 1:</p><p>This is basically a brute force approach. You start from every node and perform DFS. Since we perform DFS from every node, a lot of work gets repeated.</p><p>Total time taken: O(V × (V + E))</p><p>Space: Normal recursion stack space</p><p>But, the implementation of this approach is a little tricky. For an edge u → v, we need to find ancestors of v, which lies in the opposite direction of traversal.</p><p>Approach 2:</p><p>When it comes to directed graphs, graph reversal is one of the standard tricks. Kosaraju’s algorithm also uses this idea.</p><p>After reversing, the implementation feels more natural. Again, we perform DFS from every node.</p><p>Total time taken: O(V × (V + E))</p><p>Space: Extra space is required to build the reversed graph</p><p>All these solutions will get accepted on LeetCode ✅</p><p>But on Codeforces and AtCoder, they will likely give Time Limit Exceeded 🫠</p><p>So, I want you to go the distance 🏃🏻</p><p>Approach 3: Dynamic Programming on Graphs 🍁</p><p>This approach is interesting.</p><p>Consider nodes u → v, where there is an edge from u to v.</p><p>We can write:</p><p>Ancestor(v) = Ancestor(v) ∪ {u} ∪ Ancestor(u)</p><p>This is where Dynamic Programming comes into play. Instead of recomputing ancestors of u again and again (as in DFS), we store and reuse Ancestor(u). This reuse of previously computed results avoids repeated work and makes the approach efficient.</p><p>Now, for calculating ancestors of v, we need to calculate ancestors of u first. This imposes a strict ordering.</p><p>Also, for a particular node v, there might be several such nodes u, so we apply this relation for all of them.</p><p>This equation is recursive, and the base case is all the nodes with in-degree zero (they have no ancestors). So, we start from nodes which don’t have any ancestors.</p><p>This is a perfect setup for topological sort, and we can implement it using Kahn’s Algorithm.</p><p>Graph invariant of Kahn’s Algorithm:</p><p>Every node we push into the queue must have in-degree = 0.</p><p>What improved in this approach?</p><p>We avoided re-computations which DFS was doing, and the time complexity boils down to:</p><p>O(V + E + some merging operations)</p><p>Earlier, it was V times this.</p><p>Such a massive improvement 🔥</p><p>Follow-up problem:</p><p>Longest path in a DAG (AtCoder)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/161/1*1APDxEBkJhoH6d-bMl_5Sw@2x.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95e8d9902d25" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>