Question: Data structures: need help writing the method sumOfMst(). Write a method that is part of a class for a graph implemented as an adjacency matrix.
Data structures: need help writing the method sumOfMst().
Write a method that is part of a class for a graph implemented as an adjacency matrix. The method returns the sum of weights for a minimum spanning tree using Kruskal's algorithm. The graph is an undirected graph.
First line of input is the number of preceding lines or edges (n). And next n lines represents from_edge, to_edge and weight.
The output from the method should be the sum of weights of the minimum spanning tree.
Sample Input:
6 1 2 10 1 3 20 1 4 15 2 4 40 2 3 50 3 4 5
Sample Output:
30
#include
class My_Graph { const static int MAXNUMVERTICES = 100; int theGraph[MAXNUMVERTICES][MAXNUMVERTICES]; public:
void insertEdge(int to, int from, int weight) { theGraph[to][from] = weight; theGraph[from][to] = weight; }
int sumOfMST() { //code here } };
int main() { My_Graph *theGraph = new My_Graph(); int numEdges, inVert, outVert, wt; std::cin >> numEdges; for (int i=0; i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
