You are to write a function that will calculate the minimum to cost to connect a group of cities. The cities will be represented by vertices and the roads will be edges. You must implement Kruskals algorithm to solve this problem now. Use the union find functions you wrote in the previous problem. You will not get credit for using your submission from the previous version. You may use the algorithm library for the sort function. I recommend you create your own struct for the edges, it makes the problem much simpler. Do not use the following libraries: cmath
Input
NO INPUT TO READ IN. The graphs will be weighted undirected adjacency matrices.
Example
{{0, 1, 2, 3, 4}
{1, 0, 5, 0, 7},
{2, 5, 0, 6, 0},
{3, 0, 6, 0, 0},
{4, 7, 0, 0, 0}};
City 0 has edges to city 1 with a cost of 1, city 2 with a cost of 2, city 3 with a cost of 3, and city 4 with a cost of 4. A 0(zero) in the graph means there is no road between those two cities.
Your function should return a value of 10 for this particular graph.
Output
NO OUTPUT
my code that i currently have
i got the union and find function but idk how to implement kruskals using the function given it has to be that exact function. any help?
/* YOU DON'T HAVE TO USE THIS, IT MAY HELP IF YOU WANT TO SORT THE EDGES
struct Edge{
int source;
int destination;
int cost;
Edge( int s, int d, int c ){
source = s;
destination = d;
cost = c;
}
};
Creates an adjacency list of edges
void createEdges( std::vector<std::vector<int>> & graph, std::vector<Edge> & customGraph ){
for( int i = 0; i < graph.size(); i++ ){
for( int x = 0; x < graph[0].size(); x++ ){
if( graph[i][x] != 0 ){
customGraph.emplace_back(Edge( i, x, graph[i][x] ));
}
}
}
}
*/
int find(std::vector<int> &subsets, int index) {
while (subsets[index] != index) {
index = subsets[index];
}
return index;
}
void Union(std::vector<int> &subsets, int x, int y){
int Xroot = find(subsets, x);
int Yroot = find(subsets, y);
subsets[Xroot] = Yroot;
}
int kruskal( std::vector<std::vector<int>> & graph ){
// Your code goes here
}