AlgoFlow
advancedGraphs

Detect a cycle in a directed graph

Given a directed graph, return true if it contains a cycle — a path that follows directed edges back to a node it already visited.

Examples

input 0→1, 1→2, 2→3, 3→1

output true

1 → 2 → 3 → 1 closes the loop.

input 0→1, 0→2, 1→3

output false

Every edge moves away from 0.

Hints

Try the problem first. Hints are revealed one at a time so you keep thinking.

Solution

The written approach, complexity analysis and follow-ups stay hidden until you ask for them.