> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# All Paths From Source to Target

> Tested Python solution for LeetCode 797 with 34 pytest cases. Generate a practice environment with lcpy.

LeetCode 797, [Medium](/catalog/medium). Topics: [Backtracking](/catalog/topics/backtracking), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Graph Theory](/catalog/topics/graph-theory), Directed Acyclic Graph. [View on LeetCode](https://leetcode.com/problems/all-paths-source-target/description/).

Generate this problem as a practice environment: tested reference solution, 34 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 797   # by problem number
lcpy gen -s all_paths_source_target   # by problem name
```

## Problem

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` and return them in **any order**.

The graph is given as follows: `graph[i]` is a list of all nodes you can visit from node `i` (i.e., there is a directed edge from node `i` to node `graph[i][j]`).

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/09/28/all_1.jpg)

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

**Explanation:** There are two paths: `0 -> 1 -> 3` and `0 -> 2 -> 3`.

![Example 2](https://assets.leetcode.com/uploads/2020/09/28/all_2.jpg)

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/all_paths_source_target/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/all_paths_source_target/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + e) per path, O(2^n) total in the worst case
    # Space: O(n) recursion depth excluding the output
    def all_paths_source_target(self, graph: list[list[int]]) -> list[list[int]]:
        target = len(graph) - 1
        paths: list[list[int]] = []
        path = [0]

        def dfs(node: int) -> None:
            if node == target:
                paths.append(path[:])
                return
            for nxt in graph[node]:
                path.append(nxt)
                dfs(nxt)
                path.pop()

        dfs(0)
        return paths
```

## Complexity

| Time | Space |
| - | - |
| O(n + e) per path, O(2^n) total in the worst case | O(n) recursion depth excluding the output |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.