All Paths from Source Lead to Destination Problem
Description
LeetCode Problem 1059.
Given the edges of a directed graph where edges[i] = [a_i, b_i] indicates there is an edge between nodes a_i and b_i, and two nodes source and destination of this graph, determine whether or not all paths starting from source eventually, end at destination, that is:
- At least one path exists from the
sourcenode to thedestinationnode - If a path exists from the
sourcenode to a node with no outgoing edges, then that node is equal todestination. - The number of possible paths from
sourcetodestinationis 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 <= 10^40 <= edges.length <= 10^4edges.length == 20 <= a_i, b_i <= n - 10 <= source <= n - 10 <= destination <= n - 1- The given graph may have self-loops and parallel edges.
Solution
Approach: DFS with Three Node States
All paths from source lead to destination exactly when, among the nodes reachable from source:
- every dead end (a node without outgoing edges) is
destination, and - there is no cycle, since a cycle gives infinitely many paths, or a path that never ends.
A first quick check: if destination itself has outgoing edges, a path can walk past it, so the answer is false.
Then run a DFS from source with three states per node: 0 not visited yet, 1 on the current DFS path, 2 already proved to lead only to destination. When the DFS reaches a node:
- in state
1, we have come back to a node on the current path, so there is a cycle:false; - in state
2, it was already checked:true; - with no outgoing edges, it is fine only if it is
destination; - otherwise mark it
1, check every neighbour, and mark it2once they all succeed.
State 2 is what keeps the search linear: a node reached through many different paths is only explored once. It also tells a cycle apart from two paths that merge again (like 0 -> 1 -> 3 and 0 -> 2 -> 3), which is not a problem.
Complexity: O(n + e) time and space, where e is the number of edges.
class Solution:
def leadsToDestination(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
graph = [[] for _ in range(n)]
for a, b in edges:
graph[a].append(b)
if graph[destination]:
return False # paths could walk on past the destination
# 0 = not visited, 1 = on the current DFS path, 2 = known to lead only to destination
state = [0] * n
def dfs(u: int) -> bool:
if state[u] != 0:
return state[u] == 2 # 1 means we came back around a cycle
if not graph[u]:
return u == destination # a dead end must be the destination
state[u] = 1
for v in graph[u]:
if not dfs(v):
return False
state[u] = 2
return True
return dfs(source)