Depth-first search

Depth-first search follows one path as far as it can before backing up to try another, by recursion or with a stack, and visits everything reachable in $O(V + E)$; an edge back to a vertex still on the path means a cycle.

From Data Structures and Algorithms.