Skip to content

Commit

Permalink
Create kruskal min tree
Browse files Browse the repository at this point in the history
  • Loading branch information
encryptau authored Oct 23, 2022
1 parent 01d08ae commit ef2baba
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Graphs/kruskal min tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// C++ program for the above approach

#include <bits/stdc++.h>
using namespace std;

// DSU data structure
// path compression + rank by union

class DSU {
int* parent;
int* rank;

public:
DSU(int n)
{
parent = new int[n];
rank = new int[n];

for (int i = 0; i < n; i++) {
parent[i] = -1;
rank[i] = 1;
}
}

// Find function
int find(int i)
{
if (parent[i] == -1)
return i;

return parent[i] = find(parent[i]);
}

// Union function
void unite(int x, int y)
{
int s1 = find(x);
int s2 = find(y);

if (s1 != s2) {
if (rank[s1] < rank[s2]) {
parent[s1] = s2;
rank[s2] += rank[s1];
}
else {
parent[s2] = s1;
rank[s1] += rank[s2];
}
}
}
};

class Graph {
vector<vector<int> > edgelist;
int V;

public:
Graph(int V) { this->V = V; }

void addEdge(int x, int y, int w)
{
edgelist.push_back({ w, x, y });
}

void kruskals_mst()
{
// 1. Sort all edges
sort(edgelist.begin(), edgelist.end());

// Initialize the DSU
DSU s(V);
int ans = 0;
cout << "Following are the edges in the "
"constructed MST"
<< endl;
for (auto edge : edgelist) {
int w = edge[0];
int x = edge[1];
int y = edge[2];

// Take this edge in MST if it does
// not forms a cycle
if (s.find(x) != s.find(y)) {
s.unite(x, y);
ans += w;
cout << x << " -- " << y << " == " << w
<< endl;
}
}

cout << "Minimum Cost Spanning Tree: " << ans;
}
};

0 comments on commit ef2baba

Please sign in to comment.