forked from Bharanidharan7708/Hacktober2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kruskal_algo.cpp
104 lines (89 loc) · 1.98 KB
/
kruskal_algo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <bits/stdc++.h>
using namespace std;
struct node
{
int u;
int v;
int wt;
node(int first, int second, int weight)
{
u = first;
v = second;
wt = weight;
}
};
bool comp(node a, node b)
{
return a.wt < b.wt; // sort according to weight
}
int findParent(int u, vector<int> &parent)
{
if (u == parent[u])
{
return u;
}
return parent[u] = findParent(parent[u], parent);
}
void unionn(int u, int v, vector<int> &parent, vector<int> &rankArr)
{
u = findParent(u, parent);
v = findParent(v, parent);
if (rankArr[u] < rankArr[v])
{
parent[u] = v;
rankArr[v] += rankArr[u];
}
else
{
parent[v] = u;
rankArr[u] += rankArr[v];
}
}
int main()
{
int n, m;
cout << "Enter the number of nodes and edges: ";
cin >> n >> m;
vector<node> edges;
// take edges as input
cout << "Enter the edges along with weight of the graph\n";
for (int i = 0; i < m; i++)
{
int u, v, wt;
cin >> u >> v >> wt;
edges.push_back(node(u, v, wt));
}
sort(edges.begin(), edges.end(), comp);
// if graph start from node 0
// vector<int> parent(n);
// for (int i = 0; i < n; i++)
// {
// parent[i] = i;
// }
// if graph start from node 1
vector<int> parent(n + 1);
for (int i = 1; i <= n; i++)
{
parent[i] = i;
}
vector<int> rankArr(n, 0);
int cost = 0;
vector<pair<int, int>> mst;
for (auto it : edges)
{
if (findParent(it.v, parent) != findParent(it.u, parent))
{
cost += it.wt;
mst.push_back(make_pair(it.u, it.v));
unionn(it.u, it.v, parent, rankArr);
}
}
for (auto it : mst)
cout << it.first << " - " << it.second << endl;
// OR
// for (auto it = mst.begin(); it != mst.end(); it++)
// {
// cout << it->first << " - " << it->second << endl;
// }
return 0;
}