Challenge 3: Detect Cycle in Graph
This lesson provides another coding challenge on graphs. You'll implement a cool function that detects loops!
We'll cover the following
Problem statement
The concept of loops or cycles is very common in graph theory. A cycle exists when you traverse the graph and come upon a vertex that has already been visited.
Note: Your solution should work for both connected and unconnected graphs.
You have to implement the detectCycle
function, which tells you whether or not a graph contains a cycle.
Input
The input is a graph in the form of an adjacency list.
Note: You can start the traversal from any vertex.
Output
true
if a cycle exists but it is false
if it doesn’t.
Sample input
graph = {
0 -> 1
1 -> 2
2 -> 0
}
Sample output
true
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.