Backend Engineer
Senior
programming
Write a function that returns all possible topological sorts of a directed acyclic graph (DAG). The graph will be given as an adjacency list.
Function Signature:
def all_topological_sorts(graph: List[List[int]]) -> List[List[int]]: Constraints: - The number of vertices will be between 1 and 10.
- The number of edges will be between 0 and 10.
Example 1:
Input:
graph = [[1], [2], []] Output:
[[0, 1, 2], [0, 2, 1]] Example 2: Input:
graph = [[1, 2], [2], []] Output:
[[0, 1, 2], [0, 2, 1]]
Suggested Answer