-
Notifications
You must be signed in to change notification settings - Fork 156
/
Primes_Algorithm.cpp
221 lines (194 loc) · 4.91 KB
/
Primes_Algorithm.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#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 data[20][20];
graph(int vt)
{
v = vt;
}
void prims_algorithm();
};
void graph::prims_algorithm()
{
int sv;
int visited[v];
int i,j,k;
edge ed[20];
edge mst[20];
edge discarded_edge[20];
int mst_ctr = 0;
int edge_ctr =0;
int mst_flag = 0;
int discarded_ctr = 0;
int discarded_flag = 0;
disjointset d(v);
for(i=0;i<v;i++)
{
visited[i] = 0;
}
cout<<"\n Enter the start vertex : ";
cin>>sv;
sv = sv - 1;
visited[sv] = 1;
while(1)
{
int visited_flag = 0;
for(i=0;i<v;i++)
{
if(visited[i] == 1)
visited_flag++;
}
if(visited_flag == v)
break;
edge_ctr = 0;
for(i=0; i<v;i++)
{
if(visited[i] == 1)
{
for(j=0;j<v;j++)
{
if(data[i][j] != 999)
{
// before adding that edge in ed array check whether it has already
// been added in MST array
mst_flag = 0;
for(int k=0;k<mst_ctr;k++)
{
if((mst[k].v1 == i && mst[k].v2 == j) || (mst[k].v1 == j && mst[k].v2 == i))
{
mst_flag = 1;
break;
}
}
discarded_flag = 0;
for(int k=0;k<discarded_ctr;k++)
{
if((discarded_edge[k].v1 == i && discarded_edge[k].v2 == j) || (discarded_edge[k].v1 == j && discarded_edge[k].v2 == i))
{
discarded_flag = 1;
break;
}
}
//edge is not in MST array and is not present in discarded array
if(mst_flag == 0 && discarded_flag == 0)
{
ed[edge_ctr].v1 = i;
ed[edge_ctr].v2 = j;
ed[edge_ctr].wt = data[i][j];
edge_ctr++;
}
}
}
}
}
edge min_edge;
min_edge.v1 =0;
min_edge.v2 =0;
min_edge.wt = 999;
for(k=0;k<edge_ctr;k++)
{
if(ed[k].wt < min_edge.wt)
{
min_edge.v1 = ed[k].v1;
min_edge.v2 = ed[k].v2;
min_edge.wt = ed[k].wt;
}
}
// we will get min wt edge in min_edge variable
int r1 = d.find_root(min_edge.v1);
int r2 = d.find_root(min_edge.v2);
if(r1 != r2)
{
mst[mst_ctr].v1 = min_edge.v1;
mst[mst_ctr].v2 = min_edge.v2;
mst[mst_ctr].wt = min_edge.wt;
mst_ctr++;
d.take_union(min_edge.v1, min_edge.v2);
visited[min_edge.v1] = 1;
visited[min_edge.v2] = 1;
}
else // including the edge in MST will create a cycle so discard it
{
discarded_edge[discarded_ctr].v1 = min_edge.v1;
discarded_edge[discarded_ctr].v2 = min_edge.v2;
discarded_edge[discarded_ctr].wt = min_edge.wt;
discarded_ctr++;
}
}
cout<<"\n MST is: ";
for(i=0;i<mst_ctr;i++)
{
cout<<endl<<" "<<mst[i].v1+1<<" to "<<mst[i].v2+1<<" ==> "<<mst[i].wt;
}
}
int main()
{
int v;
cout<<"\n Enter the number of vertices in the graph: ";
cin>>v;
graph g(v);
for(int i=0;i<v;i++)
g.data[i][i] = 999;
for(int i=0;i<v;i++)
{
for(int j=i+1;j<v;j++)
{
cout<<"\n Enter the cost of edge between "<<i+1<<" to "<<j+1<<" : ";
cin>>g.data[i][j];
g.data[j][i] = g.data[i][j];
}
}
g.prims_algorithm();
return 0;
}