← Quiz: Routing Algorithms: all 10 questions
Quiz: Routing Algorithms · Question 10 of 10
What does this print? (code)
What does this print?
import heapq
graph = {"S": {"T": 7, "U": 2}, "U": {"T": 3}, "T": {}}
dist = {v: float("inf") for v in graph}
dist["S"] = 0
pq = [(0, "S")]
while pq:
d, u = heapq.heappop(pq)
if d > dist[u]:
continue
for v, w in graph[u].items():
if d + w < dist[v]:
dist[v] = d + w
heapq.heappush(pq, (dist[v], v))
print(dist["T"])
- A.7
- B.3
- C.10
- D.5
Answer & explanation
Correct answer: 5
S–U–T costs 2 + 3 = 5, cheaper than the direct S–T link of 7.
Can you get the rest right without peeking?
Take the timed Quiz: Routing Algorithms quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →