A cycle in a directed graph is a circular path v0→v1→v2→⋯→vk→v0.
The graph below has
quite a few of them, for example, B→E→F→B.
A graph without cycles is
acyclic. It turns out we can test for acyclicity in linear time, with a single depth-first search.
Property A directed graph has a cycle if and only if its depth-first search reveals a back
edge.
Proof. One direction is quite easy: if (u,v) is a back edge, then there is a cycle consisting of
this edge together with the path from v to u in the search tree.
Conversely, if the graph has a cycle v0→v1→v2→⋯→vk→v0, look at the first node on this
cycle to be discovered (the node with the lowest pre number). Suppose it is vi. All the other
vj on the cycle are reachable from it and will therefore be its descendants in the search tree.
In particular, the edge vi−1→vi (or vk→v0 if i=0) leads from a node to its ancestor and is
thus by definition a back edge. ∎
Directed acyclic graphs (DAG), or dags for short, come up all the time. They are good for modeling
relations like causalities, hierarchies, and temporal dependencies.