Added DFS for cycle detection. Cleans up the outgoing edges for visited safe nodes so that work is not repeated.