LeetCode 1059, Medium. Topics: Graph, Topological Sort, Depth-First Search, Kosaraju. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:
Problem
Given the edges of a directed graph where edges[i] = [ai, bi] indicates there is an edge between nodes ai and bi, and two nodes source and destination of this graph, determine whether or not all paths starting from source eventually, end at destination, that is:
- At least one path exists from the
source node to the destination node
- If a path exists from the
source node to a node with no outgoing edges, then that node is equal to destination.
- The number of possible paths from
source to destination is a finite number.
Return true if and only if all roads from source lead to destination.
Examples
Constraints
- 1 <= n <= 10^4
- 0 <= edges.length <= 10^4
- edges[i].length == 2
- 0 <= ai, bi <= n - 1
- 0 <= source <= n - 1
- 0 <= destination <= n - 1
- The given graph may have self-loops and parallel edges.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026