Develop a program to model a graph using an adjacency list and employ Kruskal's algorithm to find the minimum spanning tree
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Structure to represent an edge in the graph
struct Edge {
int src, dest, weight;
};
// Structure to represent a subset for union-find
struct Subset {
int parent, rank;
};
// Class to represent a graph and perform Kruskal's algorithm
class Graph {
int V; // Number of vertices
vector<Edge> edges; // Vector to store graph edges
public:
// Constructor
Graph(int V) {
this->V = V;
}
// Function to add an edge to the graph
void addEdge(int src, int dest, int weight) {
Edge edge = {src, dest, weight};
edges.push_back(edge);
}
// Function to find the subset of an element 'i'
int find(vector<Subset>& subsets, int i) {
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
// Function to perform union of two subsets
void Union(vector<Subset>& subsets, int x, int y) {
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
// Function to find the minimum spanning tree using Kruskal's algorithm
void KruskalMST() {
vector<Edge> result; // Vector to store the result MST
// Sort all the edges in non-decreasing order of their weight
sort(edges.begin(), edges.end(), [](Edge a, Edge b) {
return a.weight < b.weight;
});
// Allocate memory for creating V subsets
vector<Subset> subsets(V);
for (int v = 0; v < V; v++) {
subsets[v].parent = v;
subsets[v].rank = 0;
}
int i = 0; // Index used to pick the next edge
// Number of edges to be taken is equal to V-1
while (result.size() < V - 1) {
Edge next_edge = edges[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
// If including this edge doesn't cause cycle, include it in the result and increment the index for next edge
if (x != y) {
result.push_back(next_edge);
Union(subsets, x, y);
}
}
// Print the result MST
cout << "Edges of the Minimum Spanning Tree:" << endl;
for (auto edge : result)
cout << edge.src << " - " << edge.dest << " : " << edge.weight << endl;
}
};
// Driver code
int main() {
int V = 4; // Number of vertices in the graph
Graph g(V);
// Add edges to the graph
g.addEdge(0, 1, 10);
g.addEdge(0, 2, 6);
g.addEdge(0, 3, 5);
g.addEdge(1, 3, 15);
g.addEdge(2, 3, 4);
// Find minimum spanning tree using Kruskal's algorithm
g.KruskalMST();
return 0;
}
- Edge Struct: Represents an edge in the graph with source, destination, and weight.
- Subset Struct: Represents a subset for the union-find operation, used to check for cycles.
- Graph Class: Represents a graph and contains methods for adding edges and finding the minimum spanning tree using Kruskal's algorithm.
- addEdge(): Adds an edge to the graph.
- find() and Union(): Helper functions for the union-find algorithm to detect cycles.
- KruskalMST(): Implements Kruskal's algorithm to find the minimum spanning tree. It sorts the edges by weight, then iterates through them, adding them to the MST if they don't form a cycle.
- main(): Creates a graph and adds edges to it, then calls KruskalMST() to find the minimum spanning tree and prints the result.
Define structures: Define structures to represent edges (
struct Edge
) and subsets for the union-find operation (struct Subset
).Graph Class: Define a
Graph
class with private member variablesV
(number of vertices) and a vectoredges
to store the graph's edges.addEdge(): Define a method
addEdge(int src, int dest, int weight)
to add an edge to the graph. Create anEdge
structure with source, destination, and weight, and add it to theedges
vector.Union-Find Operations: Define helper functions
find()
andUnion()
to perform the union-find operations. These are used to check for cycles in the graph.Kruskal's Algorithm: Define a method
KruskalMST()
to find the minimum spanning tree using Kruskal's algorithm.- Sort the edges in non-decreasing order of their weight.
- Create subsets for each vertex.
- Iterate through sorted edges.
- For each edge, check if including it forms a cycle. If not, add it to the result and union the subsets of its source and destination vertices.
- Continue until the number of edges in the result equals V - 1 (where V is the number of vertices).
Main Function:
- Create a graph object and add edges to it.
- Call
KruskalMST()
to find the minimum spanning tree. - Print the edges of the minimum spanning tree.