0% found this document useful (0 votes)
2 views7 pages

Ai 5

The document contains a C++ implementation of Kruskal's algorithm to find the Minimum Spanning Tree (MST) of a graph. It defines an Edge class and includes functions for finding the parent of a node with path compression and performing union operations by rank. The main function initializes a graph with vertices and edges, then calculates and outputs the weight of the MST.

Uploaded by

sheetanshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Ai 5

The document contains a C++ implementation of Kruskal's algorithm to find the Minimum Spanning Tree (MST) of a graph. It defines an Edge class and includes functions for finding the parent of a node with path compression and performing union operations by rank. The main function initializes a graph with vertices and edges, then calculates and outputs the weight of the MST.

Uploaded by

sheetanshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name : Rutu Mahesh Ghatge Roll no.

15
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

class Edge {

public:

int src, dest, weight;

Edge(int s, int d, int w) : src(s), dest(d), weight(w) {}

};

// Find function with path compression

int findParent(int u, vector<int>& parent) {

if (u != parent[u])

parent[u] = findParent(parent[u], parent);

return parent[u];

// Union function by rank

void unionSets(int u, int v, vector<int>& parent, vector<int>& rank) {

int rootU = findParent(u, parent);

int rootV = findParent(v, parent);

if (rank[rootU] > rank[rootV])

parent[rootV] = rootU;
else if (rank[rootU] < rank[rootV])

parent[rootU] = rootV;

else {

parent[rootV] = rootU;

rank[rootU]++;

// Kruskal's MST algorithm

int kruskalMST(vector<Edge>& edges, int V) {

sort(edges.begin(), edges.end(), [](Edge a, Edge b) {

return a.weight < b.weight;

});

vector<int> parent(V), rank(V, 0);

for (int i = 0; i < V; i++) parent[i] = i;

int mstWeight = 0;

for (Edge& edge : edges) {

int u = findParent(edge.src, parent);

int v = findParent(edge.dest, parent);

if (u != v) {

mstWeight += edge.weight;

unionSets(u, v, parent, rank);

}
}

return mstWeight;

int main() {

int V = 4; // Number of vertices

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>

using namespace std;

class Edge {

public:

int src, dest, weight;

Edge(int s, int d, int w) : src(s), dest(d), weight(w) {}


};

// Find function with path compression

int findParent(int u, vector<int>& parent) {

if (u != parent[u])

parent[u] = findParent(parent[u], parent);

return parent[u];

// Union function by rank

void unionSets(int u, int v, vector<int>& parent, vector<int>& rank) {

int rootU = findParent(u, parent);

int rootV = findParent(v, parent);

if (rank[rootU] > rank[rootV])

parent[rootV] = rootU;

else if (rank[rootU] < rank[rootV])

parent[rootU] = rootV;

else {

parent[rootV] = rootU;

rank[rootU]++;

// Kruskal's MST algorithm

int kruskalMST(vector<Edge>& edges, int V) {


sort(edges.begin(), edges.end(), [](Edge a, Edge b) {

return a.weight < b.weight;

});

vector<int> parent(V), rank(V, 0);

for (int i = 0; i < V; i++) parent[i] = i;

int mstWeight = 0;

for (Edge& edge : edges) {

int u = findParent(edge.src, parent);

int v = findParent(edge.dest, parent);

if (u != v) {

mstWeight += edge.weight;

unionSets(u, v, parent, rank);

return mstWeight;

int main() {

int V = 4; // Number of vertices

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 :

You might also like