List Every Path From Node 0 to the Last Node in a Directed Acyclic Graph

Quick Overview

Return every path from node 0 to node n - 1 in a directed acyclic graph given as an adjacency list, sorted in lexicographic order. It tests depth-first search with backtracking, copying the path at the right moment, and knowing why a DAG needs no visited set.

List Every Path From Node 0 to the Last Node in a Directed Acyclic Graph

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a directed acyclic graph (DAG) of `n` nodes labeled from `0` to `n - 1`, find all possible paths from node `0` to node `n - 1`. The graph is given as an adjacency list: `graph[i]` is a list of all nodes you can visit from node `i`, so there is a directed edge from node `i` to node `graph[i][j]`. ### Function Signature ```python def all_paths_source_target(graph: list[list[int]]) -> list[list[int]]: ``` ### Rules - A path is a list of node labels that starts with `0`, ends with `n - 1`, and follows every edge in its direction. - Return the paths sorted in ascending lexicographic order, comparing them as lists of integers. For example, `[0, 1, 2, 3, 4]` comes before `[0, 1, 4]`, which comes before `[0, 4]`. - If node `n - 1` cannot be reached from node `0`, return an empty list. ### Constraints - `n == len(graph)` - `2 <= n <= 15` - `0 <= graph[i][j] < n` - `graph[i][j] != i` (there are no self-loops) - All the elements of `graph[i]` are unique. - The input graph is guaranteed to be a DAG. ### Examples ```text Input: graph = [[1,2],[3],[3],[]] Output: [[0,1,3],[0,2,3]] ``` Node `0` reaches node `3` either through node `1` or through node `2`, so there are two paths: `0 -> 1 -> 3` and `0 -> 2 -> 3`. ```text Input: graph = [[4,3,1],[3,2,4],[3],[4],[]] Output: [[0,1,2,3,4],[0,1,3,4],[0,1,4],[0,3,4],[0,4]] ``` There are five paths. The three that start `0 -> 1` come first because `1` is the smallest second node, and among those the third node (`2`, then `3`, then `4`) decides the order. ```text Input: graph = [[],[0]] Output: [] ``` The only edge goes from node `1` to node `0`, so node `1` cannot be reached from node `0`.

Overview: Return every path from node 0 to node n - 1 in a directed acyclic graph given as an adjacency list, sorted in lexicographic order. It tests depth-first search with backtracking, copying the path at the right moment, and knowing why a DAG needs no visited set.

General logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1.

The graph is given as an adjacency list: graph[i] is a list of all nodes you can visit from node i, so there is a directed edge from node i to node graph[i][j].

Function Signature

def all_paths_source_target(graph: list[list[int]]) -> list[list[int]]:

Rules

  • A path is a list of node labels that starts with 0 , ends with n - 1 , and follows every edge in its direction.
  • Return the paths sorted in ascending lexicographic order, comparing them as lists of integers. For example, [0, 1, 2, 3, 4] comes before [0, 1, 4] , which comes before [0, 4] .
  • If node n - 1 cannot be reached from node 0 , return an empty list.

Constraints

  • n == len(graph)
  • 2 <= n <= 15
  • 0 <= graph[i][j] < n
  • graph[i][j] != i (there are no self-loops)
  • All the elements of graph[i] are unique.
  • The input graph is guaranteed to be a DAG.

Examples

Input: graph = [[1,2],[3],[3],[]]
Output: [[0,1,3],[0,2,3]]

Node 0 reaches node 3 either through node 1 or through node 2, so there are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.

Input: graph = [[4,3,1],[3,2,4],[3],[4],[]]
Output: [[0,1,2,3,4],[0,1,3,4],[0,1,4],[0,3,4],[0,4]]

There are five paths. The three that start 0 -> 1 come first because 1 is the smallest second node, and among those the third node (2, then 3, then 4) decides the order.

Input: graph = [[],[0]]
Output: []

The only edge goes from node 1 to node 0, so node 1 cannot be reached from node 0.