Minimum Spanning Tree
Given a weighted undirected graph. Find the sum of weights of edges of a Minimum Spanning Tree.
Input:
Given 2 integers N and M. N represents the number of vertices in the graph. M represents the number of edges between any 2 vertices.
Then M lines follow, each line has 3 space-separated integers a, b, w. Where, a and b represents an edge from a vertex a to a vertex b and w represents the weight of that edge.
Output:
Print the summation of edges weights in the MST.
Examples:
INPUT:
N = 4, M = 5
1 2 7
1 4 6
4 2 9
4 3 8
2 3 6
OUTPUT:
19, is the sum of edges of MST.
The problem can be solved with the help of Kruskal and Prim's Algorithm of the minimum spanning tree.
Kruskal Algorithm: Kruskal's algorithm follows the greedy approach in each iteration it adds the weight of the minimum edge to a growing spanning tree.
Following steps are used in Kruskal algo,
C++ Implementation:
Output: