All Ancestors of a Node in a Directed Acyclic Graph

Problem

Intuition

It can be modelled as topological sort problem. Basically we need to find the ancestors of each node. And when traversing from one node to another

1 -> 3 -> 5

All the ancestors of 3 and 1 are also ancestors of 5. It is a basic traversal using Kahns algorithm, where we keep track of processed node

Code

Last updated