Question DSA-GRAPH-0016
HardWhat is the output of this code?
g = {1: [2], 2: [3], 3: [1]}
state = {}
def has_cycle(u):
state[u] = 'grey'
for v in g[u]:
if state.get(v) == 'grey' or (v not in state and has_cycle(v)):
return True
state[u] = 'black'
return False
print(has_cycle(1))
- A.
False
- B.
Error
- C.
Sign up free to see the answer and a step-by-step explanation.