🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Minimum Spanning Trees: all 10 questions

Quiz: Minimum Spanning Trees · Question 6 of 10

What does this print? (code)

What does this print?

import heapq
adj = {"A": [("B", 5), ("C", 1)], "B": [("A", 5), ("C", 2)], "C": [("A", 1), ("B", 2)]}
seen, total, pq = set(), 0, [(0, "A")]
while pq:
    w, u = heapq.heappop(pq)
    if u in seen:
        continue
    seen.add(u)
    total += w
    for v, wt in adj[u]:
        heapq.heappush(pq, (wt, v))
print(total)
  1. A.6
  2. B.3
  3. C.8
  4. D.7
Answer & explanation

Correct answer: 3

From A take C (1), then B through C (2). The heavy edge A–B (5) is popped later and skipped.

Can you get the rest right without peeking?

Take the timed Quiz: Minimum Spanning Trees quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →