← Quiz: Topological Sort: all 15 questions
Quiz: Topological Sort · Question 9 of 15
What does this print? (code)
What does this print?
edges = {"a": ["b"], "b": ["c"], "c": ["a"], "d": []}
color = {k: 0 for k in edges}
def has_cycle(u):
color[u] = 1
for v in edges[u]:
if color[v] == 1 or (color[v] == 0 and has_cycle(v)):
return True
color[u] = 2
return False
print(any(color[n] == 0 and has_cycle(n) for n in edges))
- A.True
- B.False
- C.None
- D.Error
Answer & explanation
Correct answer: True
a → b → c → a reaches the gray node a again, so a cycle is reported.
Can you get the rest right without peeking?
Take the timed Quiz: Topological Sort quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →