Type the DFS edge-colour test that detects a cycle
Type the DFS edge-colour test that detects a cycle
Answer
if color[v] == WHITE: visit(v) # tree edge elif color[v] == GRAY: has_back_edge = True # back edge -> cycle
White neighbour = tree edge (recurse). Gray neighbour = back edge, meaning v is an ancestor of u, so the graph has a cycle.