Advanced graphs and trees · 12 / 15 Exercise Hard

Minimum spanning tree

Objective

A minimum spanning tree connects all the nodes at the lowest possible total cost and without cycles. Kruskal’s algorithm sorts the edges by weight and adds each one if it does not close a cycle, which is checked with union-find.

Instructions

  • The first line has n m; then m lines u v w (undirected edge with weight).
  • Show Minimum cost: x with the sum of weights of the tree, or Graph not connected if the graph is not connected.
Input
4 4
0 1 1
1 2 2
0 2 3
2 3 4
Output
Minimum cost: 7

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise