Given the edges of a directed graph where edges[i] = [ai, bi] indicates there is an edge between nodes ai and bi, and two nodes source and destination of this graph, determine whether or not all paths starting from source eventually, end at destination, that is:
source node to the destination nodesource node to a node with no outgoing edges, then that node is equal to destination.source to destination is a finite number.Return true if and only if all roads from source lead to destination.
Example 1:
Input: n = 3, edges = [[0,1],[0,2]], source = 0, destination = 2 Output: false Explanation: It is possible to reach and get stuck on both node 1 and node 2.
Example 2:
Input: n = 4, edges = [[0,1],[0,3],[1,2],[2,1]], source = 0, destination = 3 Output: false Explanation: We have two possibilities: to end at node 3, or to loop over node 1 and node 2 indefinitely.
Example 3:
Input: n = 4, edges = [[0,1],[0,2],[1,3],[2,3]], source = 0, destination = 3 Output: true
Constraints:
1 <= n <= 1040 <= edges.length <= 104edges.length == 20 <= ai, bi <= n - 10 <= source <= n - 10 <= destination <= n - 1Problem Overview: Given a directed graph, a source node, and a destination node, verify that every possible path starting from the source eventually ends at the destination. No path should end at another node, and cycles that allow infinite traversal must not exist.
Approach 1: Brute Force DFS Path Exploration (Exponential time)
Start from the source and recursively explore every outgoing edge using depth‑first search. Track the current path and stop when you reach a node with no outgoing edges. If that terminal node is not the destination, the condition fails. This method effectively enumerates paths and checks each terminal state. Time complexity can grow exponentially in dense graphs because the same subgraphs are explored repeatedly. Space complexity is O(V) for the recursion stack.
Approach 2: DFS with Graph Coloring / Cycle Detection (O(V + E))
Build an adjacency list and run DFS from the source while marking nodes with three states: unvisited, visiting, and safe. When DFS reaches a node with no outgoing edges, it must equal the destination; otherwise return false. If DFS encounters a node marked visiting, a cycle exists, which allows infinite paths and invalidates the requirement. Once all children of a node lead to the destination, mark it safe so future DFS calls reuse the result. This pruning avoids repeated exploration. Time complexity is O(V + E) with O(V) space for recursion and state tracking.
Approach 3: Reverse Graph + Topological Validation (O(V + E))
Another way to reason about the constraint is that every reachable node must eventually flow into the destination without cycles. Build the graph and analyze nodes using ideas from topological sort. Nodes that have no outgoing edges must be the destination; otherwise the condition fails immediately. You can propagate validity backward from the destination through incoming edges. If any reachable node cannot be proven to terminate at the destination, the graph violates the rule. Time complexity remains O(V + E) with O(V + E) space for adjacency structures.
The key insight is that the graph must behave like a directed acyclic funnel into the destination. Cycles break the guarantee because a path could loop forever, and dead-end nodes break it because a path stops before the destination.
Recommended for interviews: DFS with coloring is the expected approach. It shows you understand graph traversal, cycle detection, and memoization. A brute force DFS demonstrates the basic idea but wastes work by revisiting subgraphs. The optimized DFS proves correctness while keeping the complexity linear in the number of vertices and edges.
We use a state array state to record the status of each node, where:
First, we build the graph as an adjacency list, then perform a depth-first search (DFS) starting from the source node. During the DFS process:
false directly;true directly;true; otherwise, return false;true; otherwise, return false.The answer is the result of dfs(source).
The time complexity is O(n + m), where n and m are the number of nodes and edges, respectively. The space complexity is O(n + m), used to store the graph's adjacency list and state array.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force DFS Path Exploration | Exponential | O(V) | Useful for understanding the problem or very small graphs |
| DFS with Graph Coloring | O(V + E) | O(V) | Best general solution with cycle detection and memoization |
| Reverse Graph / Topological Validation | O(V + E) | O(V + E) | When reasoning about termination using topological ordering |
LeetCode 1059. All Paths from Source Lead to Destination Explanation and Solution • happygirlzt • 4,223 views views
Watch 7 more video solutions →Practice All Paths from Source Lead to Destination with our built-in code editor and test cases.
Practice on FleetCode