All problems
Kruskal Minimum Spanning Tree
Problem
Given n nodes and undirected weighted edges as [u, v, w] triples, return the total weight of a minimum spanning tree, or -1 if the graph is disconnected.
Sort edges by weight and add each edge whose endpoints lie in different union-find components, stopping after n - 1 edges. A single node has an empty spanning tree of weight 0.
Hint
A minimum spanning tree on n nodes uses exactly n - 1 edges.
Test cases
case 1
in: [4,[[0,1,1],[0,2,4],[1,2,2],[1,3,5],[2,3,3]]]
expected: 6
case 2
in: [1,[]]
expected: 0
case 3
in: [3,[[0,1,1]]]
expected: -1
case 4
in: [4,[[0,1,1],[1,2,2],[2,3,3],[0,3,10],[1,3,5]]]
expected: 6
case 5
in: [2,[[0,1,7]]]
expected: 7
Starter codePython
def kruskal_mst(n, edges):
# Your code here
pass5 test cases · run your code against them in the editor