Verify Topological Order

Given a directed graph and a proposed ordering of its vertices, return true if the ordering is a valid topological order. Pattern focus: Kahn Topological Order. This is a strong way to test whether the dependency relation is respected.

Input Format

n = number of vertices, edges = directed edge list, order = proposed vertex order

Output Format

true if the order is a valid topological ordering

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9

Examples

Example 1:

Input:

n = 4
edges = [[0,1],[0,2],[1,3],[2,3]]
order = [0,1,2,3]

Output:

true

Explanation:

Every edge points from an earlier vertex to a later vertex.

Example 2:

Input:

n = 4
edges = [[0,1],[0,2],[1,3],[2,3]]
order = [0,2,1,3]

Output:

true

Explanation:

This order also respects all dependencies.

Example 3:

Input:

n = 3
edges = [[0,1],[1,2]]
order = [1,0,2]

Output:

false

Explanation:

Vertex 0 must appear before vertex 1.

Loading...
Verify Topological Order - Topological Sort