Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.

Commit 6c8a577

Browse files
authored
Merge pull request #287 from Himanshu-77/master
2 parents baf137e + 489e673 commit 6c8a577

File tree

1 file changed

+62
-62
lines changed

1 file changed

+62
-62
lines changed

graphs/kruskal.cpp

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,81 @@
1+
//
2+
// The following is C++ implementation of Kruskal's algorithm
3+
// on a graph.
4+
5+
// Kruskal's Algorithm is used to find minimum spanning tree
6+
// of a graph .
7+
// Here 'Disjoint Sets' method is used for cycle detection.
8+
// Disjoint sets are sets whose intersection is empty set
9+
// if they don't have any common element
10+
111
// The All ▲lgorithms Project
212
//
313
// https://allalgorithms.com/graphs/
414
// https://github.com/allalgorithms/cpp
515
//
6-
// Contributed by: Leonardo Su
7-
// Github: @Leonardosu
16+
// Contributed by: Himanshu Airan
17+
// Github: @Himanshu-77
18+
//
819

9-
#include <bits/stdc++.h>
10-
#define f first
11-
#define s second
12-
#define mp make_pair
13-
#define pb push_back
1420

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

17-
const int N = 100010;
18-
typedef pair<int,int> ii;
19-
typedef pair<int,ii> iii;
20-
vector<iii> edges;
24+
const int MAX = 1e4 + 5;
25+
int id[MAX] ;
2126

22-
int p[N],lv[N];
23-
int total_cost = 0,n,m;
2427

25-
//Union-Find
26-
//-------
27-
int find(int x)
28+
int root(int x)
2829
{
29-
if(p[x] == x) return x;
30-
return p[x] = find(p[x]);
30+
while(id[x] != x)
31+
{
32+
id[x] = id[id[x]];
33+
x = id[x];
34+
}
35+
return x;
3136
}
3237

33-
void join(int x,int y)
38+
int main()
3439
{
35-
x = find(x);
36-
y = find(y);
37-
38-
if(x == y) return;
39-
if(lv[x] < lv[y]) p[x] = y;
40-
else if(lv[x] > lv[y]) p[y] = x;
41-
else
42-
{
43-
p[x] = y;
44-
lv[y]++;
45-
}
46-
}
47-
//-------
40+
int nodes, edges, x, y, weight;
41+
int cost, minimumCost=0 ;
42+
pair <int, pair<int, int> > Graph[MAX];
4843

44+
// initially all elements are in different sets
45+
for(int i = 0;i < MAX;++i)
46+
id[i] = i;
47+
48+
49+
// input number of nodes and edges in graph
50+
cin >> nodes >> edges;
51+
for(int i = 0;i < edges;++i)
52+
{
53+
cin >> x >> y >> weight;
54+
Graph[i] = make_pair(weight, make_pair(x, y));
55+
}
56+
57+
// Sort the edges in the ascending order of weights
58+
sort(Graph, Graph + edges);
59+
60+
// find weight of minimum spanning tree
61+
for(int i = 0;i < edges;++i)
62+
{
63+
// Selecting edges one by one in increasing order from the beginning
64+
x = Graph[i].second.first;
65+
y = Graph[i].second.second;
66+
cost = Graph[i].first;
67+
// Check if the selected edge is creating a cycle or not
68+
if(root(x) != root(y))
69+
{
70+
minimumCost += cost;
71+
72+
// join sets of both elements
73+
id[root(x)] = id[root(y)];
74+
}
75+
}
4976

50-
int main()
51-
{
5277

53-
cin>>n>>m; // "n" is the number of vertex and "m" = number of edges
54-
55-
for(int i=1;i<=n;++i)
56-
p[i] = i,lv[i] = 0;
57-
58-
int a,b,c;
59-
for(int i=1;i<=m;++i) // Read the input, there is an edge between "a" and "b" with cost c
60-
{
61-
cin>>a>>b>>c;
62-
edges.pb(mp(c,mp(a,b) ));
63-
}
64-
65-
// Kruskal
66-
// --------
67-
sort(edges.begin(),edges.end());
68-
69-
for(int i=0;i<edges.size();++i)
70-
{
71-
int a = edges[i].s.f;
72-
int b = edges[i].s.s;
73-
if(find(a) != find(b))
74-
{
75-
join(a,b);
76-
total_cost+=edges[i].f;
77-
}
78-
}
79-
// --------
80-
cout<<total_cost<<"\n";
78+
cout << "Cost of minimum spanning tree is : "
79+
<< minimumCost << endl;
80+
return 0;
8181
}

0 commit comments

Comments
 (0)