← Quiz: Topological Sort: all 15 questions
Quiz: Topological Sort · Question 11 of 15
What does this print? (code)
What does this print?
from collections import deque
edges = [("A", "C"), ("B", "C"), ("C", "D")]
nodes = ["A", "B", "C", "D"]
indeg = {n: 0 for n in nodes}
for u, v in edges:
indeg[v] += 1
print(indeg, [n for n in nodes if indeg[n] == 0])
- A.{'A': 1, 'B': 1, 'C': 1, 'D': 0} ['D']
- B.{'A': 0, 'B': 0, 'C': 2, 'D': 1} ['A', 'B']
- C.{'A': 0, 'B': 0, 'C': 1, 'D': 1} ['A', 'B']
- D.{'A': 0, 'B': 0, 'C': 2, 'D': 1} ['A']
Answer & explanation
Correct answer: {'A': 0, 'B': 0, 'C': 2, 'D': 1} ['A', 'B']
C has two incoming edges (from A and B) and D has one. A and B have none, so they start in the queue.
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 →