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
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();
}
Step by Step Solution
3.41 Rating (157 Votes )
There are 3 Steps involved in it
Step: 1
include include class MyGraph const static int MAXNUMVERTICES 100 int theGraphMAXN...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started