-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathKruskal_Algorithm.cpp
More file actions
171 lines (151 loc) · 3.09 KB
/
Copy pathKruskal_Algorithm.cpp
File metadata and controls
171 lines (151 loc) · 3.09 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
using namespace std;
class disjointset
{
public:
int djset[20];
disjointset(int v)
{
for(int i=0;i<=v;i++)
{
djset[i] = i;
}
}
int find_root(int v)
{
while(v != djset[v])
{
v = djset[v];
}
return v;
}
void take_union(int v1, int v2)
{
int r1 = find_root(v1);
int r2 = find_root(v2);
if(v1 == r1 && v2 == r2)
{
djset[v1] = v2;
}
else if(v1 != r1 && v2 == r2)
{
djset[v2] = v1;
}
else if(v1 == r1 && v2!= r2)
{
djset[v1] = v2;
}
else if(v1 != r1 && v2 != r2)
{
djset[r1] = r2;
}
}
};
class edge
{
public:
int v1;
int v2;
int wt;
};
class graph
{
public:
int v;
int e;
edge ed[20];
graph(int vertices, int edges)
{
v = vertices;
e = edges;
}
void accept_graph();
void display_graph();
void kruskal_mst();
void sort_edges();
};
void graph::sort_edges()
{
edge temp;
for(int i=0;i<e;i++)
{
for(int j=0;j<e-i-1;j++)
{
if(ed[j].wt > ed[j+1].wt)
{
temp.v1 = ed[j].v1;
temp.v2 = ed[j].v2;
temp.wt = ed[j].wt;
ed[j].v1 = ed[j+1].v1;
ed[j].v2 = ed[j+1].v2;
ed[j].wt = ed[j+1].wt;
ed[j+1].v1 = temp.v1;
ed[j+1].v2 = temp.v2;
ed[j+1].wt = temp.wt;
}
}
}
}
void graph::kruskal_mst()
{
edge mst[20];
int mst_ctr = 0;
int mst_cost = 0;
disjointset dj(v);
sort_edges();
cout<<"\n Edges after sorting: ";
display_graph();
cout<<"\n";
for(int i=0;i<e;i++)
{
int r1 = dj.find_root(ed[i].v1);
int r2 = dj.find_root(ed[i].v2);
if(r1 != r2)
{
mst[mst_ctr].v1 = ed[i].v1;
mst[mst_ctr].v2 = ed[i].v2;
mst[mst_ctr].wt = ed[i].wt;
mst_ctr++;
mst_cost = mst_cost + ed[i].wt;
dj.take_union(ed[i].v1,ed[i].v2);
}
}
cout<<"\n MST is : ";
for(int i=0;i<mst_ctr;i++)
{
cout<<"\n "<<mst[i].v1<<" "<<mst[i].v2<<" "<<mst[i].wt;
}
cout<<"\n Total cost of MST is: "<<mst_cost;
}
void graph::accept_graph()
{
for(int i=0;i<e;i++)
{
cout<<"\n Enter v1 :";
cin>>ed[i].v1;
cout<<"\n Enter v2 :";
cin>>ed[i].v2;
cout<<"\n Enter weight :";
cin>>ed[i].wt;
}
}
void graph::display_graph()
{
for(int i=0;i<e;i++)
{
cout<<"\n "<<ed[i].v1<<" "<<ed[i].v2<<" "<<ed[i].wt;
}
}
int main() //Main Function
{
int v, e; //Variable Declaration
cout<<"Minimum Spanning Tree Using Krushkal Algorithm:-\n";
cout<<"\n Enter the number of vertics : ";
cin>>v;
cout<<"\n Enter the number of edges : ";
cin>>e;
graph g(v, e); //Function Call
g.accept_graph();
g.display_graph();
g.kruskal_mst();
}