Cycle detection through depth first search