All Ancestors of a Node in a Directed Acyclic Graph - Problem

Imagine you have a family tree or a company hierarchy represented as a directed graph where edges show direct relationships. Your task is to find all the ancestors (predecessors) for each person or position in the hierarchy.

You are given a positive integer n representing the number of nodes in a Directed Acyclic Graph (DAG). The nodes are numbered from 0 to n - 1.

You are also given a 2D integer array edges, where edges[i] = [from_i, to_i] denotes that there is a unidirectional edge from from_i to to_i in the graph.

Goal: Return a list answer, where answer[i] is the list of ancestors of the i-th node, sorted in ascending order.

A node u is an ancestor of another node v if u can reach v via a set of edges (there exists a path from u to v).

Input & Output

example_1.py โ€” Basic DAG
$ Input: n = 8, edges = [[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]]
โ€บ Output: [[],[],[],[0,1],[0,2],[0,1,3],[0,1,2,3,4],[0,1,2,3]]
๐Ÿ’ก Note: Node 0 has no ancestors. Node 3 can be reached from nodes 0 and 1, so ancestors of 3 are [0,1]. Node 7 can be reached from 0โ†’3โ†’7, 1โ†’3โ†’7, and 2โ†’7, so ancestors are [0,1,2,3].
example_2.py โ€” Linear Chain
$ Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]]
โ€บ Output: [[],[0],[0,1],[0,1,2],[0,1,2,3]]
๐Ÿ’ก Note: This forms a linear chain 0โ†’1โ†’2โ†’3โ†’4. Each node's ancestors are all nodes that appear before it in the chain.
example_3.py โ€” No Edges
$ Input: n = 4, edges = []
โ€บ Output: [[],[],[],[]]
๐Ÿ’ก Note: With no edges, no node can reach any other node, so every node has an empty ancestor list.

Visualization

Tap to expand
Finding All Ancestors in Corporate HierarchyCEO (0)VP (1)VP (2)Dir (3)Dir (4)DFS Results (Who Each Position Influences)CEO (0) โ†’ Can influence: [1, 2, 3, 4]VP (1) โ†’ Can influence: [3]VP (2) โ†’ Can influence: [4]Dir (3) โ†’ Can influence: []Dir (4) โ†’ Can influence: []Reversed: Ancestors of Each PositionCEO (0): []VP (1): [0]VP (2): [0]Dir (3): [0, 1] โ† All managers aboveDir (4): [0, 2] โ† All managers above
Understanding the Visualization
1
Build the Hierarchy
Create the reporting structure from the given relationships
2
Trace from Top
Starting from each position, find everyone they can influence (descendants)
3
Reverse the Relationship
If A can influence B, then A is a manager/ancestor of B
4
Compile Results
Sort the manager lists for each employee position
Key Takeaway
๐ŸŽฏ Key Insight: Instead of searching upward from each node to find ancestors, we search downward from each node to find descendants, then reverse the relationship. This reduces redundant path exploration and gives us O(n ร— (V + E)) complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * (V + E))

We perform DFS from each of the n nodes, each DFS takes O(V + E) time

n
2n
โœ“ Linear Growth
Space Complexity
O(n + E)

Space for adjacency list, visited set, and result storage

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • 0 โ‰ค edges.length โ‰ค min(2000, n ร— (n - 1) / 2)
  • edges[i].length == 2
  • 0 โ‰ค fromi, toi โ‰ค n - 1
  • fromi โ‰  toi
  • There are no duplicate edges
  • The graph is acyclic
Asked in
Amazon 25 Google 18 Microsoft 12 Meta 8
23.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen