Thus, to determine, in linear time, if a digraph has a cycle it suffices to do a DFS and check for the presence of back edges. If you find a back edge, it is easy to find a cycle containing it (using the tree edges). If there is no back edge, then there is no cycle. |

Claim: A directed graph has a cycle if and only if the DFS yields at least one back edge.

(See DFSInDiGraphs .)

Is the claim true?

If there is a back edge, it seems clear there is a cycle (using the back edge (U,W) and the tree edges from W to U.

In the other direction, suppose the DFS yields no back edge. Then, considering the DFS tree of the graph, all edges go "left" or "down" in the DFS structure. Intuitively, this means there is no cycle. We tried to give a more precise argument as follows:

Suppose there is a cycle C. Let W be the first vertex in C encountered by the DFS. Let (U,W) be the edge into W on the cycle C.

Claim: (U,W) must be a back edge.

Why? Considering each case carefully, we can rule out that (U,W) is a tree edge, a cross edge, or a forward edge. Thus, it must be a back edge.

In this way we've convinced ourselves that if the graph has a cycle, the DFS must have a back edge.

Later we gave the following somewhat different argument.

Order the vertices v_{1},v_{2},...,v_{n} in the order in which the DFS finishes with them
(by what we call the DFS *post-order* numbering).

Every cross edge, tree edge, or forward edge must go backward in this ordering. (Verify: for each such edge (U,W), one can reason out that W must be finished before U.)

Thus, there can be no cycle using just the cross edges, tree edges, and forward edges. (Because along any path using these edges, the ranks (in the ordering) of the respective vertices along the path is always decreasing.)

Thus, to determine, in linear time, if a digraph has a cycle it suffices to do a DFS and check for the presence of back edges.

If you find a back edge, it is easy to find a cycle containing it (using the tree edges). If there is no back edge, then there is no cycle.