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
Visualization
Time & Space Complexity
Single pass through all edges to count in-degrees
HashSet to store vertices with incoming edges
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