
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Kruskal’s Minimum Spanning Tree using STL in C++
In this tutorial, we will be discussing a program to understand Kruskal’s minimum spanning tree using STL in C++.
For this, we will be provided with a connected, undirected and weighted graph. Our task is to calculate the Minimum spanning tree for the given graph.
Example
#include<bits/stdc++.h> using namespace std; typedef pair<int, int> iPair; //structure for graph 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 = 9, E = 14; Graph g(V, E); g.addEdge(0, 1, 4); g.addEdge(0, 7, 8); g.addEdge(1, 2, 8); g.addEdge(1, 7, 11); g.addEdge(2, 3, 7); g.addEdge(2, 8, 2); g.addEdge(2, 5, 4); g.addEdge(3, 4, 9); g.addEdge(3, 5, 14); g.addEdge(4, 5, 10); g.addEdge(5, 6, 2); g.addEdge(6, 7, 1); g.addEdge(6, 8, 6); g.addEdge(7, 8, 7); cout << "Edges of MST are \n"; int mst_wt = g.kruskalMST(); cout << "\nWeight of MST is " << mst_wt; return 0; }
Output
Edges of MST are 6 - 7 2 - 8 5 - 6 0 - 1 2 - 5 2 - 3 0 - 7 3 - 4 Weight of MST is 37
- Related Articles
- Kruskal’s Minimum Spanning Tree Algorithm
- Kruskal’s (Minimum Spanning Tree) MST Algorithm
- Minimum spanning tree (MST) in Javascript
- Minimum Spanning Tree in Data Structures
- Prim’s Minimum Spanning Tree Algorithm
- Prim’s (Minimum Spanning Tree) MST Algorithm
- Golang program to find minimum spanning tree using dijkstra Algorithm
- Kruskal's Minimum Spanning Tree Algorithm-Greedy algorithm in C++
- Spanning Tree Protocol
- Mininum spanning tree algorithms
- Binary Tree to Binary Search Tree Conversion using STL set C++?
- Types Of Spanning Tree Protocol (STP)
- Program to Find Out if an Edge is a Part of a Minimum Spanning Tree in Python
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph in C++
- What is Multiple Spanning Tree Protocol (MSTP)?

Advertisements