What goes in the blank?
# directed cycle check: GRAY = still on the DFS path
def visit(u):
color[u] = GRAY
for v in graph[u]:
if color[v] == ____:
return True # back edge
if color[v] == WHITE and visit(v):
return True
color[u] = BLACK
return False
Answer: GRAY
Reaching a vertex that is still on the current path (GRAY) is a back edge, which means a cycle; BLACK vertices are finished and safe.
Play Problem Solving ▸