Question DSA-GRAPH-0064
HardWhat is the output of this code?
def kruskal(n, edges):
p = list(range(n))
def f(x):
while p[x] != x: p[x] = p[p[x]]; x = p[x]
return x
tot, used = 0, []
for w, u, v in sorted(edges):
if f(u) != f(v):
p[f(u)] = f(v); tot += w; used.append((u, v))
return tot, len(used)
print(kruskal(5, [(2, 0, 1), (3, 0, 3), (6, 1, 2), (8, 1, 3), (5, 1, 4), (7, 2, 4), (9, , )]))
- A.
(15, 4)
- B.
(16, 4)
- C.
Sign up free to see the answer and a step-by-step explanation.