Given a directed graph, determine whether a topological ordering exists. Pattern focus: Cycle Detection. A topological ordering exists if and only if the graph is a DAG.
n = number of vertices, edges = directed edge list
true if a topological ordering exists
Example 1:
Input:
n = 4 edges = [[0,1],[1,2],[2,3]]
Output:
true
Explanation:
The graph is a DAG, so a topological ordering exists.
Example 2:
Input:
n = 4 edges = [[0,1],[1,2],[2,0]]
Output:
false
Explanation:
A directed cycle prevents any topological order.
Example 3:
Input:
n = 2 edges = [[0,1]]
Output:
true