Count Visited Nodes in a Directed Graph - Problem

There is a directed graph consisting of n nodes numbered from 0 to n - 1 and n directed edges.

You are given a 0-indexed array edges where edges[i] indicates that there is an edge from node i to node edges[i].

Consider the following process on the graph:

  • You start from a node x and keep visiting other nodes through edges until you reach a node that you have already visited before on this same process.

Return an array answer where answer[i] is the number of different nodes that you will visit if you perform the process starting from node i.

Input & Output

Example 1 — Basic Cycle
$ Input: edges = [2,0,3,2]
Output: [3,4,2,2]
💡 Note: Starting from node 0: 0→2→3→2 (visits 3 nodes). Starting from node 1: 1→0→2→3→2 (visits 4 nodes). Starting from node 2: 2→3→2 (visits 2 nodes). Starting from node 3: 3→2→3 (visits 2 nodes).
Example 2 — Self Loop
$ Input: edges = [1,2,2,4,4]
Output: [3,2,1,2,1]
💡 Note: Node 0: 0→1→2→2 (visits 3 nodes, stops at self-loop). Node 1: 1→2→2 (visits 2 nodes). Node 2: 2→2 (visits 1 node, immediate self-loop). Node 3: 3→4→4 (visits 2 nodes). Node 4: 4→4 (visits 1 node).
Example 3 — Linear Chain
$ Input: edges = [1,2,0]
Output: [3,3,3]
💡 Note: All nodes eventually cycle through 0→1→2→0, so each starting point visits all 3 nodes before repeating.

Constraints

  • n == edges.length
  • 2 ≤ n ≤ 105
  • 0 ≤ edges[i] ≤ n - 1
  • edges[i] != i

Visualization

Tap to expand
Count Visited Nodes in a Directed Graph INPUT Directed Graph (n=4 nodes) 0 1 2 3 2 --> 0 --> 3 --> 2 --> edges array: 2 [0] 0 [1] 3 [2] 2 [3] edges[i] = destination of node i ALGORITHM STEPS 1 Detect Cycles Find all cycles using DFS Cycle found: 2 --> 3 --> 2 2 Mark Cycle Nodes Nodes 2,3 are in cycle Cycle length = 2 3 Process Cycle Nodes answer[2] = answer[3] = 2 (cycle length) 4 Process Non-cycle Node 0: 0-->2 (2 nodes) Node 1: 1-->0-->2 (3 nodes) Path Tracking: 1 --> 0 --> 2 --> 3 Green = cycle nodes (already computed) answer[1] = 1 + answer[0] answer[0] = 1 + answer[2] = 2 answer[1] = 1 + 2 = 3 FINAL RESULT Nodes visited from each start: From 0: 0 --> 2 (cycle) = 2 nodes From 1: 1 --> 0 --> 2 (cycle) = 3 nodes From 2: 2 --> 3 --> 2 (cycle) = 2 nodes From 3: 3 --> 2 --> 3 (cycle) = 2 nodes Output Array: 2 [0] 3 [1] 2 [2] 3 [3] OK - [2,3,2,3] Key Insight: Each node has exactly one outgoing edge, forming a functional graph. Every path eventually reaches a cycle. First, detect and process cycles (nodes in cycle visit cycle-length nodes). Then, for non-cycle nodes, answer[i] = 1 + answer[edges[i]]. Process in reverse topological order for O(n) complexity. TutorialsPoint - Count Visited Nodes in a Directed Graph | Cycle Detection with Path Tracking
Asked in
Google 28 Microsoft 15 Amazon 12 Meta 8
23.5K Views
Medium Frequency
~35 min Avg. Time
892 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