Graph Cycle Detection Algorithm