Kruskal's MST Problem

Kruskal's MST Problem — ExecCode Medium DSA Practice

Solve the Kruskal's MST problem on ExecCode. Free online medium DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given an undirected weighted connected graph, compute the total weight of a Minimum Spanning Tree using Kruskal with Union-Find (path compression + union by rank).

Examples

Input n = 6, edges = [[0, 1, 2], [0, 2, 4], [1, 2, 1], [1, 3, 7], [2, 3, 3], [2, 5, 8], [3, 4, 5], [3, 5, 9], [4, 5, 6]]; Output 17. Input n = 4, edges = [[0, 1, 1], [0, 2, 4], [1, 2, 2], [1, 3, 6], [2, 3, 3]]; Output 6

Constraints

1 ≤ n ≤ 10^4 n-1 ≤ edges.length ≤ n*(n-1)/2 Weights > 0 Graph is connected

Practice Kruskal's MST free on ExecCode. Browse DSA problems, topic map, and placement guides.