Write a method that is part of a class for a graph implemented as an adjacency matrix.
Question:
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 <iostream>
#include <queue>
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<numEdges; i++)
{
std::cin >> inVert;
std::cin >> outVert;
std::cin >> wt;
theGraph->insertEdge(inVert, outVert, wt);
}
std::cout<<theGraph->sumOfMST();
}
Data Structures and Algorithm Analysis in Java
ISBN: 978-0132576277
3rd edition
Authors: Mark A. Weiss