Challenge: Detect a Cycle in a Graph
Here's another coding challenge on graphs. You'll implement a cool function which 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 which has already been visited.
You have to implement the detect_cycle
function which tells you whether or not a graph contains a cycle.
Input
A graph
Output
True
if a cycle exists and False
, if it doesn’t
Sample input
Graph:
Vertex | Edges |
---|---|
0 | 1 |
1 | 2 |
2 | 0 |
Sample output
result = True
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.