Resource · Guide or article
Detect Cycle in Graphs
Sets out the vocabulary behind cycle detection, namely closed walks, simple cycles and circuits, and how the definitions differ between directed and undirected graphs. Read it to get the terms straight before detect-a-cycle problems.
Reviewed
Open the original: Detect Cycle in Graphs (opens the original site in a new tab)Practice this topicOpens the practice interviewer with a question on Graphs.
Add to a roadmap
Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.
Source content: English
In graph theory, a cycle is a path of edges and vertices wherein a vertex is reachable from itself. There are several different types of cycles, principally a closed walk and a simple cycle. A closed walk consists of a sequence of vertices starting and ending at the same vertex, with each two consecutive vertices in the sequence adjacent to each other in the graph. In a directed graph, each edge must be traversed by the walk consistently with its direction: the edge must be oriented from the earlier of two consecutive vertices to the later of the two vertices in the sequence. The choice of starting vertex is not important: traversing the same cyclic sequence of edges from different starting vertices produces the same closed walk. A simple cycle may be defined either as a closed walk with no repetitions of vertices and edges allowed, other than the repetition of the starting and ending vertex, or as the set of edges in such a walk. The two definitions are equivalent in directed graphs, where simple cycles are also called directed cycles: the cyclic sequence of vertices and edges in a walk is completely determined by the set of edges that it uses. In undirected graphs the set of…
PrepKit overview
PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.
- Suited to
- The source does not state a level
- Format
- Reference
What it covers
- Closed walk definition, including the rule that a directed walk must follow edge direction
- Simple cycle versus circuit, and why each undirected cycle corresponds to two directed cycles
- A colored example graph separating the path H-A-B, a walk with a repeated vertex and the simple cycle H-D-G-H
- Separate reference lists for detecting cycles in undirected and in directed graphs
How to use it
PrepKit study advice, not a claim made by the source.
Solve both versions: parent-tracking DFS or union-find for undirected graphs, three-color DFS for directed ones. Explain why the undirected method fails on directed graphs.
Scope and limitations
Definitions only. Detection algorithms, complexity and code are not described in the README and appear only through GeeksForGeeks and YouTube links.
Access and format
README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository; English, free to read.
Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88
Evidence behind this overview
- src/algorithms/graph/detect-cycle/README.md
Pages that include this
Get the PrepKit newsletter
Optional. You don't need it to use PrepKit.