-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkruskal.cpp
More file actions
86 lines (78 loc) · 1.35 KB
/
kruskal.cpp
File metadata and controls
86 lines (78 loc) · 1.35 KB
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
/*
* Kruskal Algorithm for Minimum Spanning Tree
* Uses Disjoint Sets Data Structure
* O(ElgV)
* Author: Saurabh Odhyan
*/
#include<iostream>
#include<vector>
using namespace std;
#define MAX 1000
int p[MAX],rank[MAX];
void create_set(int x)
{
p[x]=x;
rank[x]=0;
}
int find_set(int x)
{
if(p[x]!=x) p[x]=find_set(p[x]);
return p[x];
}
void merge_set(int x,int y)
{
int px=find_set(x);
int py=find_set(y);
if(rank[px]>rank[py])
p[py]=px;
else if(rank[px]<rank[py])
p[px]=py;
else if(px!=py)
{
p[py]=px;
rank[px]++;
}
}
struct Edge{
int v1,
v2,
w;
Edge(){}
Edge(int _v1,int _v2,int _w){
v1=_v1;
v2=_v2;
w=_w;
}
bool operator<(const Edge& E) const{
return w<E.w;
}
};
int main(){
vector<Edge> E;
int V, //no of vertices
N; //no of edges
cin>>V>>N;
int x,y,w;
for(int i=0;i<N;i++){
cin>>x>>y>>w;
E.push_back(Edge(x,y,w));
}
//Kruskal
for(int i=0;i<V;i++)
create_set(i);
sort(E.begin(),E.end()); //sort edges according to edge weight
int n=0, //no of vertices added to minimum spanning tree
res=0; //weight of minimum spanning tree
for(int i=0;i<N;i++){
int x=E[i].v1;
int y=E[i].v2;
if(find_set(x)!=find_set(y)){
merge_set(x,y);
res+=E[i].w;
cout<<x<<" - "<<y<<" : "<<E[i].w<<endl;
n++;
}
if(n==V-1) break;
}
cout<<res<<endl;
}