All Paths from Source Lead to Destination - Problem
Journey Through a Directed Network
Imagine you're navigating through a complex road network represented as a directed graph. You have a collection of one-way roads defined by
Your mission is to determine whether every possible journey starting from a
• At least one path exists from source to destination
• No dead ends allowed: If any path leads to a city with no outgoing roads, that city must be the destination
• No infinite loops: All paths must have finite length
Return
Imagine you're navigating through a complex road network represented as a directed graph. You have a collection of one-way roads defined by
edges[i] = [ai, bi], indicating a direct path from city ai to city bi.Your mission is to determine whether every possible journey starting from a
source city will eventually lead to a specific destination city. This means:• At least one path exists from source to destination
• No dead ends allowed: If any path leads to a city with no outgoing roads, that city must be the destination
• No infinite loops: All paths must have finite length
Return
true if and only if all roads from source inevitably lead to the destination. Input & Output
example_1.py — Valid Path Network
$
Input:
n = 4, edges = [[0,1],[0,2],[1,3],[2,3]], source = 0, destination = 3
›
Output:
true
💡 Note:
There are two paths from source 0 to destination 3: 0→1→3 and 0→2→3. Both paths end at the destination with no cycles or invalid dead ends.
example_2.py — Invalid Dead End
$
Input:
n = 4, edges = [[0,1],[0,3],[1,2],[2,1]], source = 0, destination = 3
›
Output:
false
💡 Note:
Path 0→1→2→1 creates a cycle between nodes 1 and 2, making it impossible to reach the destination. The infinite loop violates our requirement.
example_3.py — Wrong Terminal Node
$
Input:
n = 3, edges = [[0,1],[1,1],[1,2]], source = 0, destination = 2
›
Output:
false
💡 Note:
From source 0, we can go to node 1, but node 1 has a self-loop (1→1), creating an infinite cycle that prevents reaching destination 2.
Visualization
Tap to expand
Understanding the Visualization
1
Build Road Map
Create adjacency list representing all one-way roads in the network
2
Start Journey
Begin DFS from source (warehouse) marking it as currently traveling (GRAY)
3
Detect Traffic Circles
If we encounter a GRAY node, we found a cycle - invalid route
4
Check Dead Ends
Roads with no exits must lead to destination only
5
Mark Verified Routes
Mark completed paths as BLACK to avoid rechecking (memoization)
Key Takeaway
🎯 Key Insight: By using three-color DFS (WHITE/GRAY/BLACK), we can detect cycles during traversal while simultaneously validating paths and memoizing results to avoid redundant work.
Time & Space Complexity
Time Complexity
O(2^N)
In worst case, we might explore exponentially many paths before detecting cycles
✓ Linear Growth
Space Complexity
O(N)
Recursion stack depth and path tracking can go up to N nodes
✓ Linear Space
Constraints
- 1 ≤ n ≤ 104
- 0 ≤ edges.length ≤ 104
- edges[i].length == 2
- 0 ≤ ai, bi ≤ n - 1
- 0 ≤ source ≤ n - 1
- 0 ≤ destination ≤ n - 1
- The given graph may have self-loops and parallel edges
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code