Minimum Number of Vertices to Reach All Nodes - Problem

Imagine you're a network administrator managing a directed acyclic graph (DAG) representing a data flow network. You have n vertices numbered from 0 to n-1, and an array edges where edges[i] = [fromi, toi] represents a directed edge from node fromi to node toi.

Your task is to find the smallest set of starting vertices from which all nodes in the graph can be reached. Think of it as finding the minimum number of "entry points" needed to access every node in the network.

Since the graph is acyclic, there's guaranteed to be a unique solution. You can return the vertices in any order.

Example: If you have vertices [0,1,2,3] with edges [[0,1],[0,2],[2,3]], you only need to start from vertex 0 to reach all other vertices (0→1, 0→2→3).

Input & Output

example_1.py — Basic DAG
$ Input: n = 6, edges = [[0,1],[0,2],[2,3],[3,4],[3,5]]
Output: [0]
💡 Note: From vertex 0, we can reach all other vertices: 0→1, 0→2→3→4, 0→2→3→5. Only vertex 0 has no incoming edges.
example_2.py — Multiple Sources
$ Input: n = 6, edges = [[0,1],[0,2],[3,1],[4,0],[4,2]]
Output: [3,4]
💡 Note: We need both vertex 3 and 4 as starting points. From 3 we can reach 1, from 4 we can reach 0,2. Together they cover all vertices.
example_3.py — Single Nodes
$ Input: n = 3, edges = []
Output: [0,1,2]
💡 Note: With no edges, each vertex is isolated and has in-degree 0. We need all vertices as starting points.

Visualization

Tap to expand
Company Hierarchy: Finding Minimum Entry PointsCEONode 0No Boss ✓ManagerNode 1ManagerNode 2EmployeeNode 3EmployeeNode 4EmployeeNode 5Solution AnalysisExecutives with no boss:CEO (0)Minimum Set: {0}Can reach all 6 employeesAlgorithm Steps1. Mark all employeeswith a boss: {1,2,3,4,5}2. Find unmarked people:Those are the top executives3. Return executives: {0}They can reach everyone!
Understanding the Visualization
1
Identify Reporting Structure
Map out who reports to whom in the organization
2
Find Top Executives
Locate all people who don't report to anyone (zero in-degree)
3
Verify Coverage
These executives can reach all employees through their reporting chains
Key Takeaway
🎯 Key Insight: In any DAG, nodes with zero in-degree are exactly the minimum set needed to reach all other nodes. No smaller set exists because these nodes cannot be reached from anywhere else!

Time & Space Complexity

Time Complexity
⏱️
O(E)

Single pass through all edges to count in-degrees

n
2n
Linear Growth
Space Complexity
O(V)

HashSet to store vertices with incoming edges

n
2n
Linear Space

Constraints

  • 2 ≤ n ≤ 105
  • 1 ≤ edges.length ≤ min(105, n × (n - 1) / 2)
  • edges[i].length == 2
  • 0 ≤ fromi, toi < n
  • All pairs (fromi, toi) are distinct
  • The graph is acyclic
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
65.0K Views
Medium Frequency
~15 min Avg. Time
2.9K 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