What is the time complexity of finding a cycle in a graph?
What is the time complexity of finding a cycle in a graph?
Approach: Analyze the cost of DFS or Union-Find.
O(V + E). It can be done using DFS (detecting a back edge) or Union-Find (detecting an edge where both nodes are already in the same set).
Follow-up: How do you find a cycle in a directed graph?
Key concepts: O(V+E), back edge, recursion.