← Quiz: Minimum Spanning Trees: all 10 questions
Quiz: Minimum Spanning Trees · Question 3 of 10
What does this print? (code)
What does this print?
edges = sorted([(3, "A", "B"), (1, "B", "C"), (2, "A", "C"), (4, "C", "D")])
parent = {x: x for x in "ABCD"}
def find(x):
while parent[x] != x:
x = parent[x]
return x
total = 0
for w, u, v in edges:
if find(u) != find(v):
parent[find(u)] = find(v)
total += w
print(total)
- A.10
- B.6
- C.7
- D.8
Answer & explanation
Correct answer: 7
Take B–C (1) and A–C (2); A–B (3) would make a cycle; take C–D (4). Total 1 + 2 + 4 = 7.
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 →