Challenge: Detect a Cycle in a Graph

Solve the challenge of detecting a cycle in a graph.

Problem statement

The concept of loops or cycles is very common in graph theory. A cycle exists when we traverse the graph and come upon a vertex that has already been visited.

Implement the DetectCycle function, which tells us whether or not a graph contains a cycle.

Input

A graph.

Output

Return true if a cycle exists and false if it doesn’t.

Sample input

Graph:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.