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 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
๐Ÿ›ฃ๏ธ Highway Safety InspectorFinding safe entry points in a highway systemEntryARoadBEntryCRestAreaLOOP!Safe RouteโŒ UnsafeโŒ Unsafeโœ… Safeโœ… Terminal๐Ÿ” Inspection Process (Three-Color Algorithm):๐Ÿ Step 1: Start at Entry A, mark as "Under Inspection" (GRAY)๐Ÿ›ฃ๏ธ Step 2: Follow road to B, mark B as "Under Inspection" (GRAY)โš ๏ธ Step 3: B leads back to A (already GRAY) โ†’ Dangerous loop detected!โŒ Step 4: Mark A and B as permanently unsafe (stay RED)๐Ÿ Step 5: Start at Entry C, mark as "Under Inspection" (GRAY)๐Ÿ–๏ธ Step 6: C leads to Rest Area (terminal) โ†’ Safe route!โœ… Step 7: Mark C and Rest Area as permanently safe (GREEN)๐Ÿ“‹ Final Report: Safe entry points = [C, Rest Area]
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

n
2n
โœ“ Linear Growth
Space Complexity
O(V)

Color array and recursion stack in worst case

n
2n
โœ“ 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
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
38.2K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen