Find Eventual Safe States - Problem
Find Eventual Safe States is a fascinating graph problem that challenges you to identify which nodes in a directed graph are safe.
You're given a directed graph with
A node is considered safe if every possible path starting from that node eventually leads to a terminal node (a node with no outgoing edges). Think of it like finding all the starting points in a maze where no matter which path you take, you'll eventually reach an exit and won't get stuck in an infinite loop.
Goal: Return all safe nodes in ascending order.
Example: If node 0 points to node 1, and node 1 points back to node 0, then both nodes are unsafe because you can get stuck in an infinite cycle. However, if a node eventually leads to a dead-end (terminal node), it's safe.
You're given a directed graph with
n nodes labeled from 0 to n-1, represented as an adjacency list graph where graph[i] contains all nodes that node i points to.A node is considered safe if every possible path starting from that node eventually leads to a terminal node (a node with no outgoing edges). Think of it like finding all the starting points in a maze where no matter which path you take, you'll eventually reach an exit and won't get stuck in an infinite loop.
Goal: Return all safe nodes in ascending order.
Example: If node 0 points to node 1, and node 1 points back to node 0, then both nodes are unsafe because you can get stuck in an infinite cycle. However, if a node eventually leads to a dead-end (terminal node), it's safe.
Input & Output
example_1.py โ Basic Graph with Cycle
$
Input:
graph = [[1,2],[2,3],[5],[0],[5],[],[]]
โบ
Output:
[2,4,5,6]
๐ก Note:
Node 2 leads to node 5 (terminal), node 4 leads to node 5 (terminal), nodes 5 and 6 are terminal nodes. Nodes 0, 1, 3 are part of cycles or lead to cycles.
example_2.py โ Simple Cycle
$
Input:
graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
โบ
Output:
[4]
๐ก Note:
Only node 4 is terminal (safe). All other nodes either form cycles or eventually lead back to cycles.
example_3.py โ All Terminal Nodes
$
Input:
graph = [[],[],[],[]]
โบ
Output:
[0,1,2,3]
๐ก Note:
All nodes are terminal nodes (no outgoing edges), so all nodes are safe.
Visualization
Tap to expand
Understanding the Visualization
1
Mark Roads Under Inspection
When exploring a route, mark it as 'currently inspecting' (GRAY)
2
Detect Dangerous Loops
If we encounter a road already under inspection, we found a dangerous loop
3
Certify Safe Routes
Routes that lead to rest areas without loops are marked safe (BLACK)
4
Report Safe Entry Points
Return all highway entrances that guarantee safe journeys
Key Takeaway
๐ฏ Key Insight: The three-color DFS technique efficiently detects cycles by marking nodes currently under exploration, allowing us to identify dangerous loops immediately when we encounter a node we're already exploring.
Time & Space Complexity
Time Complexity
O(V + E)
Each node and edge is visited at most once during DFS traversal
โ Linear Growth
Space Complexity
O(V)
Color array and recursion stack in worst case
โ Linear Space
Constraints
- 1 โค graph.length โค 104
- 0 โค graph[i].length โค graph.length
- 0 โค graph[i][j] โค graph.length - 1
- graph[i] is sorted in a strictly increasing order
- The graph may contain self-loops and parallel edges
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code