|
| 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 | + |
1 | 11 | // The All ▲lgorithms Project |
2 | 12 | // |
3 | 13 | // https://allalgorithms.com/graphs/ |
4 | 14 | // https://github.com/allalgorithms/cpp |
5 | 15 | // |
6 | | -// Contributed by: Leonardo Su |
7 | | -// Github: @Leonardosu |
| 16 | +// Contributed by: Himanshu Airan |
| 17 | +// Github: @Himanshu-77 |
| 18 | +// |
8 | 19 |
|
9 | | -#include <bits/stdc++.h> |
10 | | -#define f first |
11 | | -#define s second |
12 | | -#define mp make_pair |
13 | | -#define pb push_back |
14 | 20 |
|
| 21 | +#include<bits/stdc++.h> |
15 | 22 | using namespace std; |
16 | 23 |
|
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] ; |
21 | 26 |
|
22 | | -int p[N],lv[N]; |
23 | | -int total_cost = 0,n,m; |
24 | 27 |
|
25 | | -//Union-Find |
26 | | -//------- |
27 | | -int find(int x) |
| 28 | +int root(int x) |
28 | 29 | { |
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; |
31 | 36 | } |
32 | 37 |
|
33 | | -void join(int x,int y) |
| 38 | +int main() |
34 | 39 | { |
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]; |
48 | 43 |
|
| 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 | + } |
49 | 76 |
|
50 | | -int main() |
51 | | -{ |
52 | 77 |
|
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; |
81 | 81 | } |
0 commit comments