-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path22.KruskalAlgorithm.cpp
86 lines (69 loc) · 1.56 KB
/
22.KruskalAlgorithm.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
/*
* This function takes a graph from the user and finds a minimum
* spanning tree using
* Kruskal's Algorithm
*
* Coded by: Abdurrezak Efe
*
* */
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair< int , int> edge;
class Graph{
public:
vector <pair<int , edge >> edges;
int v;
Graph(int v, int e)
{
//edges.resize(e);
this->v = v;
}
void add_edge(int x, int y, int w)
{
edge ee = {x,y};
pair<int, edge> newedge= {w, ee};
edges.push_back(newedge);
}
};
//function to what set a vertex belongs to
int find_parent(int x, int par[])
{
if(par[x] == x)
return x;
return par[x] = find_parent(par[x], par);
}
void Kruskal(Graph g)
{
int par[g.v+1];
for(int i=0;i<g.v+1;i++)
par[i] = i;
//sorting edges acoording to their weights
sort(g.edges.begin(),g.edges.end());
vector<pair<int,int>> mst;
for(int i=0;i<g.edges.size();i++)
{
int u,v;
u = g.edges[i].second.first;
v = g.edges[i].second.second;
if(find_parent(u, par) != find_parent(v, par))
{
mst.push_back(g.edges[i].second);
par[find_parent(v, par)] = find_parent(u, par);
}
}
for(int i=0;i<mst.size();i++)
cout << mst[i].first << " " << mst[i].second << endl;
}
int main()
{
int v,e;
cin >> v >> e;
//constructing the graph
Graph g(v,e);
int x,y,w;
for(int i=0;i<e;i++)
cin >> x >> y >> w, g.add_edge(x,y,w);
Kruskal(g);
}