Cycle detection for undirected graphs