Prim - MST - Hackerrank Problem Solution
Given an undirected weighted connected graph, find the MST in it. The MST is defined as a subgraph consisting of all the vertices in the graph and: • There is only one exclusive path from a vertex to every other vertex. • The subgraph is of minimum overall weight among all subgraphs. • No cycles are created. One specific vertex S is fixed as the starting point of finding the subgraph using Prim's Algorithm. Prim’s method has the following parameter(s):
v: an integer that represents the number of vertices in the graph e: a two-dimensional array where each element contains three integers, two nodes numbers that are connected and the weight of that edge.
Input Format
The first line has two integers n and m, the number of vertices and edges in the graph.
Each of the next ‘m’ lines contains three integers x, y and r, the end nodes of e[i], and the edge's weight.
Constraints
Output Format
Print a single integer denoting the total weight of the MST.
Sample Input 0
3 3
1 2 1
2 3 2
3 1 4
Sample Output 0
3
CODE TOGETHER..GROW TOGETHER.
Comments