Ai 5
Ai 5
15
#include <iostream>
#include <vector>
#include <algorithm>
class Edge {
public:
};
if (u != parent[u])
return parent[u];
parent[rootV] = rootU;
else if (rank[rootU] < rank[rootV])
parent[rootU] = rootV;
else {
parent[rootV] = rootU;
rank[rootU]++;
});
int mstWeight = 0;
if (u != v) {
mstWeight += edge.weight;
}
}
return mstWeight;
int main() {
vector<Edge> edges = {
Edge(0, 1, 10),
Edge(0, 2, 6),
Edge(0, 3, 5),
Edge(1, 3, 15),
Edge(2, 3, 4)
};
cout << "Weight of the Minimum Spanning Tree: " << kruskalMST(edges, V) << endl;
return 0;
}#include <iostream>
#include <vector>
#include <algorithm>
class Edge {
public:
if (u != parent[u])
return parent[u];
parent[rootV] = rootU;
parent[rootU] = rootV;
else {
parent[rootV] = rootU;
rank[rootU]++;
});
int mstWeight = 0;
if (u != v) {
mstWeight += edge.weight;
return mstWeight;
int main() {
vector<Edge> edges = {
Edge(0, 1, 10),
Edge(0, 2, 6),
Edge(0, 3, 5),
Edge(1, 3, 15),
Edge(2, 3, 4)
};
cout << "Weight of the Minimum Spanning Tree: " << kruskalMST(edges, V) << endl;
return 0;
}
Output :