Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem definition: Give the program that implement Kruskal’s algorithm. Input:

ID: 3848996 • Letter: P

Question

Problem definition:
Give the program that implement Kruskal’s algorithm.
Input:
First line is N, denotes the amount of test case, then there are Ns graph data with an option number (determine whether output the selected edges or not).
Each graph is undirected and connected, it is composed of V (the number of vertices, <= 1000), E (the number of edges, <=10000), then followed by Es edges which are denoted by pair of vertex and weight (e.g., 2 4 10 means an edge connecting vertices 2 and 4, and its weight is 10).
The first data of each measurement on behalf of the test vertex number, edge number and option number.
The option number. It could be 1 or 2, output the selected edge and the sum of all minimuspanning tree’s weight if it is 1, or only the sum if it is 2.
We restrict that selected node of Prim always start from 0, and there is no “tree edge” with same weight.
Output:
If option is 1:
The selected edges which forms the spanning tree. Order is important! The sum of all edges weight in minimum spanning tree. Note that the edge should put smaller node first, e.g., if the edge (4, 2) is selected, it should be output by 2 4, not 4 2
If option is 2:
The sum of all edges weight in minimum spanning tree.
Example:
Input:
1
5 7 1
0 2 1
2 1 6
4 2 7
1 4 2
1 3 5
3 0 3
3 2 4
Output: (Kruskal’s algorithm)
0 2
1 4
0 3
1 3
11
Example:
Input:
1
6 12 2
1 0 5
0 4 1
4 5 10
4 3 4
3 0 9
0 5 2
2 0 8
2 1 3
5 2 11
2 3 6
3 5 7
1 5 12
Output: (Kruskal’s algorithm)
15
Please using c++ programming language and follow my Example INPUT and OUTPUT formats!!!!! Thank you .

Explanation / Answer

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> iPair;
struct Graph
{
int V, E;
vector< pair<int, iPair> > edges;
Graph(int V, int E)
{
this->V = V;
this->E = E;
}
void addEdge(int u, int v, int w)
{
edges.push_back({w, {u, v}});
}
int kruskalMST();
};
struct DisjointSets
{
int *parent, *rnk;
int n;
DisjointSets(int n)
{
this->n = n;
parent = new int[n+1];
rnk = new int[n+1];
for (int i = 0; i <= n; i++)
{
rnk[i] = 0;
parent[i] = i;
}
}
int find(int u)
{
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
void merge(int x, int y)
{
x = find(x), y = find(y);
if (rnk[x] > rnk[y])
parent[y] = x;
else
parent[x] = y;

if (rnk[x] == rnk[y])
rnk[y]++;
}
};
int Graph::kruskalMST()
{
int mst_wt = 0;
sort(edges.begin(), edges.end());
DisjointSets ds(V);
vector< pair<int, iPair> >::iterator it;
for (it=edges.begin(); it!=edges.end(); it++)
{
int u = it->second.first;
int v = it->second.second;
int set_u = ds.find(u);
int set_v = ds.find(v);
if (set_u != set_v)
{
cout << u << " - " << v << endl;
mst_wt += it->first;
ds.merge(set_u, set_v);
}
}

return mst_wt;
}
int main()
{
int V = 10, E = 15;
Graph g(V, E);
g.addEdge(6, 12,2);
g.addEdge(0,4,1);
g.addEdge(4,5,10);
g.addEdge(4,3,4);
g.addEdge(3,0,9);
g.addEdge(0,5,2);
g.addEdge(2,0,8);
g.addEdge(2,1,3);
g.addEdge(5,2,11);
g.addEdge(2,3,6);
g.addEdge(3,5,7);
g.addEdge(1,5,12);
cout << "Edges of MST are ";
int mst_wt = g.kruskalMST();
cout << " Weight of MST is " << mst_wt;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote