Implemented cycle detection for a graph