Full Reachability in a Directed Graph¶
Edited by Charles
Medium · Python · Graph
Problem
You are given a directed graph with n nodes labeled from 1 to n.
The graph is represented by a list of directed edges, where:
represents a directed edge from node s to node t.
Starting from node 1, determine whether every node in the graph can be reached by following the directed edges.
Return:
1if every node can be reached from node1.-1otherwise.
Example 1¶
Input:

Sample Output¶
Explanation¶
As shown in the figure, starting from node 1, we can reach nodes 2, 3, and 4. Therefore, all nodes are reachable, and the answer is 1.
The graph contains the following directed edges:
Starting from node 1:
Therefore, nodes 1, 2, 3, and 4 are all reachable from node 1.
Return:
Example 2¶
Input:
Output:
Explanation:
Starting from node 1, we can reach:
However, nodes 4 and 5 cannot be reached from node 1.
Therefore, not every node is reachable.
Return:
Example 3¶
Input:
Output:
Explanation:
Starting from node 1:
All nodes are reachable from node 1.
Return:
Constraints¶
Each edge [s, t] represents a directed edge from node s to node t.
Explanation, examples & other approaches
Core idea
This problem is a basic directed graph traversal problem.
Original study notes follow, including intermediate snippets and earlier attempts.
Python¶
Idea¶
This problem is a basic directed graph traversal problem.
We are given n nodes and a list of directed edges:
which means there is a directed edge:
We need to determine whether every node can be reached from node 1.
The main idea is:
Build an adjacency list, start DFS from node
1, and mark every reachable node as visited.
After DFS finishes, if every node from 1 to n has been visited, return 1. Otherwise, return -1.
Step 1: Build the Adjacency List¶
The input gives us a list of edges:
We convert it into an adjacency list:
We can initialize the graph with:
Since the nodes are numbered from 1 to n, using size n + 1 makes indexing easier.
Then:
Important: This Is a Directed Graph¶
For an edge:
we only add:
We should not add:
unless the reverse edge actually exists in edges.
Step 2: Use a Visited Array¶
Create:
where:
means node i can be reached from node 1.
Initially:
Step 3: DFS¶
Start DFS from node 1:
When visiting a node:
Then recursively visit all of its outgoing neighbors:
The DFS logic is:
Why Do We Need visit?¶
The graph may contain cycles.
For example:
Without a visited array, DFS could repeatedly traverse:
and never stop.
Therefore:
prevents us from processing the same node multiple times.
Step 4: Check Whether All Nodes Were Visited¶
After:
we check every node:
If any node was not visited, that node cannot be reached from node 1.
Otherwise:
Hand Simulation¶
Consider:
The adjacency list is:
Start:
Visit node 1:
From node 1, visit node 2:
Now:
Node 2 has neighbors:
Node 1 has already been visited:
So we skip it.
Then visit node 4:
Now:
Return to node 1 and visit node 3:
Finally:
Every node is reachable, so:
Unreachable Example¶
Suppose:
The graph looks like:
Starting from node 1, DFS can only visit:
Therefore:
When checking all nodes:
So the answer is:
General Graph Traversal Pattern¶
This problem follows a very common graph traversal pattern:
This pattern can be reused for many graph reachability problems.
Complexity Analysis¶
Let:
Time Complexity¶
Each node is visited at most once, and each directed edge is examined at most once.
Space Complexity¶
The adjacency list requires:
The visit array requires:
The DFS recursion stack can require up to:
in the worst case.
Summary¶
1. Build an adjacency list from the directed edges.
2. Create a visited array.
3. Start DFS from node 1.
4. Mark every reachable node as visited.
5. Use visited to prevent cycles and repeated traversal.
6. Check nodes 1 through n.
7. If any node is unvisited, return -1.
8. Otherwise, return 1.
The key idea is:
To determine whether all nodes are reachable from a source, traverse the graph from that source and check whether every node has been visited.
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | August 20, 2026 |
Python solution¶
from typing import List
class Solution:
def canReachAllNodes(self, n: int, edges: List[List[int]]) -> int:
graph = [[] for _ in range(n + 1)]
for s, t in edges:
graph[s].append(t)
visit = [False] * (n + 1)
def dfs(i):
if visit[i]:
return
visit[i] = True
for neighbor in graph[i]:
dfs(neighbor)
dfs(1)
for i in range(1, n + 1):
if not visit[i]:
return -1
return 1