Skip to content

Commit f707d2c

Browse files
authored
Implemented Kruskals Algorithm.
Implemented Kruskals Using Pair, union and rank find in cpp for implicits Graphs.
1 parent e85628c commit f707d2c

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

kruskal.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long int
4+
#define pb push_back
5+
#define mp make_pair
6+
#define f first
7+
#define s second
8+
#define mod 1000000007
9+
#define iAmInevitable ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
10+
//map<ll,ll> freq;
11+
//map<ll,ll> :: iterator itr;
12+
//for(itr=freq.begin();itr!=freq.end();itr++)
13+
//itr->f itr->s
14+
const ll MAX = 1e6 + 5;
15+
typedef pair<ll, ll> PII;
16+
ll id[MAX],rank1[MAX] ,nodes, edges;
17+
pair <ll, PII > p[MAX];
18+
// Time Complexity O(ElogV)
19+
20+
void initialize()
21+
{
22+
for(ll i = 0;i < MAX;++i)
23+
{
24+
id[i] = i;
25+
rank1[i]=0; // or memset(rank,0,sizeof(rank))
26+
}
27+
}
28+
29+
// A utility function to find set of an element i
30+
// (uses path compression technique)
31+
ll findRoot(ll x)
32+
{
33+
// find root and make root as parent of x and evrything else in the path
34+
// path compression
35+
if(id[x] != x)
36+
{
37+
id[x] = findRoot(id[x]);
38+
}
39+
return id[x];
40+
}
41+
// A function that does union of two sets of x and y
42+
// (uses union by rank)
43+
void union1(ll x, ll y)
44+
{
45+
ll xroot = findRoot(x);
46+
ll yroot = findRoot(y);
47+
48+
// Attach smaller rank tree under root of high
49+
// rank tree (Union by Rank)
50+
if (rank1[xroot] < rank1[yroot])
51+
id[xroot]= yroot;
52+
else if (rank1[xroot] > rank1[yroot])
53+
id[yroot] = xroot ;
54+
55+
// If ranks are same, then make one as root and
56+
// increment its rank by one
57+
else
58+
{
59+
id[yroot] = xroot ;
60+
rank1[xroot] ++;
61+
}
62+
}
63+
64+
65+
long long kruskal(pair<ll, PII > p[])
66+
{
67+
ll x, y;
68+
long long cost, minimumCost = 0;
69+
for(ll i = 0;i < edges;++i)
70+
{
71+
// Selecting edges one by one in increasing order from the beginning
72+
x = p[i].second.first;
73+
y = p[i].second.second;
74+
cost = p[i].first;
75+
// Check if the selected edge is creating a cycle or not
76+
if(findRoot(x) != findRoot(y))
77+
{ //if not add the edges to the minimum spanning trees.
78+
minimumCost += cost;
79+
union1(x, y);
80+
}
81+
}
82+
return minimumCost;
83+
}
84+
85+
int main()
86+
{
87+
iAmInevitable;
88+
ll x, y;
89+
long long weight, cost, minimumCost;
90+
initialize();
91+
cin >> nodes >> edges;
92+
for(ll i = 0;i < edges;++i)
93+
{
94+
cin >> x >> y >> weight;
95+
p[i] = mp(weight, mp(x, y));
96+
}
97+
// Sort the edges in the ascending order
98+
sort(p, p + edges);
99+
minimumCost = kruskal(p);
100+
cout << minimumCost << endl;
101+
return 0;
102+
}
103+
104+
//This Code is Contributed by Amar Shankar

0 commit comments

Comments
 (0)