-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathKruskal MST.cpp
72 lines (61 loc) · 1.67 KB
/
Kruskal MST.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
#include<bits/stdc++.h>
using namespace std;
class UnionFind //To check for a cycle in the graph.
{
private: vector<int> p,rank;
public:
UnionFind(int N)
{
rank.assign(N,0);
p.assign(N,0);
for(int i=0;i<N;i++)
p[i]=i;
}
int findSet(int i) {return (p[i]==i) ? i : (p[i]=findSet(p[i]));}
bool isSameSet(int a, int b) {return findSet(a)==findSet(b);}
void unionSet(int a,int b)
{
if(!isSameSet(a,b))
{
int x=findSet(a);
int y=findSet(b);
if(rank[x]>rank[y]) {p[y]=x; rank[x]++;}
else
{
p[x]=y;
if(rank[x]==rank[y]) rank[y]++;
}
}
}
};
int main()
{
vector<pair< int, pair<int,int> > > EdgeList;
cout<<"Enter the number of vertices: \n";
int V;
cin>>V;
cout<<"Enter the number of edges: \n";
int E;
cin>>E;
cout<<"Enter the edges with their weights: (Vertex 1 Vertex 2 Weight)\n";
for(int i=0;i<E;i++)
{
int u,v,w;
cin>>u>>v>>w;
EdgeList.push_back(make_pair(w,make_pair(u,v)));
}
sort(EdgeList.begin(),EdgeList.end());
int weight=0;
UnionFind UF(V);
for(int i=0;i<E;i++)
{
pair< int, pair<int,int> > front=EdgeList[i];
if(!UF.isSameSet(front.second.first, front.second.second))
{
weight+=front.first;
UF.unionSet(front.second.first, front.second.second);
}
}
cout<<"Final Weight of the MST is: "<<weight;
return 0;
}