Detect cycle in a directed graph
Medium Accuracy: 30.19% Submissions: 90494 Points: 4

Given a Directed Graph with V vertices and E edges, check whether it contains any cycle or not.

Example 1:


Output: 1
Explanation: 3 -> 3 is a cycle

Example 2:


Output: 0
Explanation: no cycle in the graph

Your task:
You don’t need to read input or print anything. Your task is to complete the function isCyclic() which takes the integer V denoting the number of vertices and adjacency list as input parameters and returns a boolean value denoting if the given directed graph contains a cycle or not.

Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V)

1 ≤ V, E ≤ 105

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Detect cycle in a directed graph

Output Window