Skip to main content

Cycle Detection

Learn Cycle Detection (Graphs, Advanced DSA) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Detecting a Cycle in an UNDIRECTED Graph DFS with a visited set - but a subtlety: encountering an already-visited neighbor is only a REAL cycle if that neighbor isn't the PARENT you just came from (in an undirected graph, the edge back to your immediate parent is expected, not a cycle).

More lessons in Graphs