Deviopath › JavaScript

Detect a Cycle

Hard · Graphs

A directed graph has nodes numbered 0 to n - 1, and each edge [from, to] points from one node to another.

Write a function hasCycle that returns true if you can start at some node, follow edges, and come back to the same node. An edge from a node to itself is also a cycle.

Careful: two paths that meet at the same node (like a diamond shape) are not a cycle.

Next: Shortest Distances (Dijkstra)