Backend Engineer
Senior
programming
Design a function to detect whether a given directed graph contains a cycle. The graph will be given as an adjacency list representation.
Function Signature:
def has_cycle(graph: List[List[int]]) -> bool: Constraints: - The number of vertices in the graph will be between 1 and 10^5.
- The number of edges will be between 0 and 10^5.
Example 1:
Input:
graph = [[1], [2], []] Output:
False Example 2: Input:
graph = [[1], [2], [0]] Output:
True (Graph contains a cycle)
Suggested Answer