Sub topic 14
Complexity and Choosing the Right Method
This 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 format they expect, and in what they give you besides a yes-or-no answer.
The Costs Side by Side
In the table, V is the number of vertices and E is the number of edges.
| Method | Graph type | Time | Extra memory | Gives you more than yes/no |
|---|---|---|---|---|
| DFS with parent-edge tracking | Undirected | O(V + E) | O(V + E) for the adjacency list and stack | Cycle via parent pointers |
| Union-find (path halving only) | Undirected | O(E log V) worst case | O(V) | The first edge that closes a cycle |
| Union-find (halving plus union by rank) | Undirected | O(E α(V)), close to linear | O(V) | The first edge that closes a cycle |
| Three-color DFS | Directed | O(V + E) | O(V) colors plus the stack | Cycle via parent pointers |
| Kahn's algorithm | Directed | O(V + E) | O(V) in-degrees plus the queue | A topological order when no cycle exists |
| Floyd's tortoise and hare | Functional (out-degree 1) | O(μ + λ) | O(1) | Start index μ and length λ of the cycle |
The union-find code from the earlier chapter uses path halving without union by rank. That is fast in practice, but its worst-case bound is logarithmic per operation. Track each root's rank and attach the shorter tree under the taller one to reach the inverse-Ackermann bound α, which stays below 5 for any graph you will meet.
Questions That Decide the Method
Big-O alone rarely settles the choice, because most methods are linear. These questions matter more:
- Is the graph directed? Parent tracking and union-find are wrong on directed graphs. In the directed graph 0→1, 0→2, 1→2, union-find sees 0, 1, and 2 already joined and reports a cycle, even though none exists.
- Do edges arrive over time? Union-find answers "does this new edge close a cycle?" in near-constant time without rebuilding anything. DFS would have to rerun over the whole graph after each edge.
- What input do you already have? Union-find works directly on an edge list. DFS and Kahn's algorithm need an adjacency list, which costs O(V + E) to build.
- Do you need the cycle itself or an ordering? DFS gives the cycle through parent pointers. Kahn's algorithm gives a valid processing order when the graph is acyclic. When a cycle exists, Kahn's algorithm only tells you which vertices were never removed, and that set can include vertices that sit downstream of the cycle without being on it.
- How deep can the graph get? Recursive DFS on a path of 100,000 vertices exceeds Python's default recursion limit of 1000. Kahn's algorithm and iterative DFS avoid that problem.
- Is every vertex's successor fixed by a function? If so, Floyd's algorithm needs no adjacency list and only constant memory.
Combining Methods
Production tools often run two methods in sequence. A build system runs Kahn's algorithm to get a build order. If some targets are never removed, it runs three-color DFS on just those leftover vertices to find one concrete cycle to show in the error message. Every cycle lies inside the leftover set, so the search is guaranteed to find one there.
Exercise
A network monitor receives undirected links one at a time and must raise an alert on the first link that creates a loop. Which method fits best, and what is its total cost for E links?
Show answer
Union-find. Each link is one find on each endpoint followed by a union, so it needs no adjacency list and no repeated traversal. With path halving and union by rank, the total cost is O(E α(V)), effectively linear. Rerunning DFS after every link would cost O(E(V + E)).
Exercise
Your Kahn's algorithm removes 7 of 10 vertices. Can you conclude that the 3 remaining vertices form a cycle?
Show answer
No. The remaining vertices contain at least one cycle, but some of them may only be reachable from a cycle without lying on it. Run three-color DFS on the remaining vertices to find an actual cycle.