01Chapter 1: Representing Graphs for Cycle DetectionEvery cycle detection algorithm in this tutorial reads a graph from one of two structures: an edge list or an adjacency list. Choosing the right structure, and storing the right details in it, decides whether later algorithms handle tricky inputs correctly.2 min read
02Chapter 2: What Counts as a CycleThis chapter assumes you know basic Python, the terms vertex and edge, and the difference between directed and undirected graphs. Before you can detect a cycle, you need an exact definition of one. Otherwise an algorithm can report cycles that are not there, or miss real ones. Most bugs in this area3 min read
03Chapter 3: Depth-First Search RefresherThis chapter assumes you can read Python functions and loops, know what an adjacency list is, and have seen depth-first search (DFS) at a conceptual level. The goal here is to pin down the parts of DFS that cycle detection relies on: the visited set, the outer loop over all vertices, and the idea of2 min read
04Chapter 4: Undirected Graphs: DFS with Parent TrackingThis chapter assumes you can read Python lists and loops, know what an adjacency list is, and understand depth-first search (DFS) at a conceptual level. The previous chapters define a cycle and review DFS. Here we put the two together for undirected graphs.2 min read
05Chapter 5: Edge Cases: Self-Loops, Parallel Edges, and Disconnected GraphsThis chapter assumes you have read the previous chapter on DFS with parent tracking, that you can read Python, and that you know what an adjacency list is. Here we look at three kinds of input that break simple cycle detectors, and at one implementation that handles all of them.3 min read
06Chapter 6: Undirected Graphs: Union-FindThis chapter assumes you can read Python functions and loops. It also assumes you know what an undirected cycle is and how the DFS parent-tracking detector from the previous chapter works. Union-find detects the same cycles in a different way. It never builds an adjacency list or traverses the graph3 min read
07Chapter 7: Classifying DFS EdgesThis chapter assumes you can read Python with recursion and nested functions, and that you know how depth-first search (DFS) visits vertices. It also assumes the directed and undirected cycle definitions from earlier chapters. Here you will learn the vocabulary that the next chapter, three-color DFS3 min read
08Chapter 8: Directed Graphs: Three-Color DFSThis chapter assumes you know recursive depth-first search (DFS), the DFS edge types from the previous chapter (tree, back, forward, and cross edges), and the parent-tracking method for undirected graphs.3 min read
09Chapter 9: Directed Graphs: Kahn's Algorithm and Topological OrderThis chapter assumes you know adjacency lists, the idea of in-degree, and breadth-first queues. It also assumes you have seen three-color DFS from the previous chapter, because Kahn's algorithm answers the same question from a different direction.2 min read
10Chapter 10: Recovering the Cycle ItselfThis chapter assumes you know three-color DFS for directed graphs and parent tracking for undirected graphs from the earlier chapters. It also assumes you can read recursive Python.2 min read
11Chapter 11: Iterative DFS for Large GraphsThis chapter assumes you know three-color DFS for directed graphs and how to rebuild a cycle from parent pointers. Both were covered in earlier chapters. Here the same algorithm is rewritten so it no longer depends on Python's call stack.2 min read
12Chapter 12: Functional Graphs and Floyd's Tortoise and HareThis chapter assumes you know three-color DFS from earlier chapters, can read Python, and are comfortable with Big-O notation. It covers a special kind of graph where you can detect a cycle using constant extra memory.3 min read
13Chapter 13: Strongly Connected Components as Cyclic RegionsThis chapter assumes you know three-color DFS for directed graphs, iterative DFS with an explicit stack, and the fact that a directed graph has a topological order exactly when it has no cycle. Earlier chapters answered a yes-or-no question: is there a cycle? This chapter answers a broader one: whic3 min read
14Chapter 14: Complexity and Choosing the Right MethodThis chapter assumes you have read the earlier chapters on parent-tracking DFS, union-find, three-color DFS, Kahn's algorithm, and Floyd's tortoise and hare. It also assumes you can read Big-O notation. Every method in this tutorial detects a cycle correctly. They differ in cost, in the input format3 min read
15Chapter 15: Where Cycle Detection Is UsedThis chapter assumes you already know three-color DFS and Kahn's algorithm from earlier chapters. It looks at real systems that rely on cycle detection, which algorithm each one usually picks, and why.3 min read