-
Notifications
You must be signed in to change notification settings - Fork 15
/
A5.cpp
322 lines (249 loc) · 4.67 KB
/
A5.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <bits/stdc++.h>
using namespace std;
class Graph{
int **Gmat;
int n;
public:
Graph(int d){
n=d;
Gmat=new int*[n];
for(int i=0;i<n;i++)
Gmat[i]=new int[n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
Gmat[i][j]=-1;
}
void create();
void prims();
void kruskals();
};
void Graph::kruskals(){
int min;
int parent[n];
int result[n][n];
int u=0;
int v=0;
int noofedges=1;
int G[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
G[i][j]=Gmat[i][j];
for(int i=0;i<n;i++){
parent[i]=-1;
for(int j=0;j<n;j++){
if(G[i][j]==-1)
G[i][j]=999;
result[i][j]=0;
}}
while(noofedges<n){
min=999;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(G[i][j]<min)
{min=G[i][j];
u=i;
v=j;}
}
}
int x=u;
int y=v;
G[u][v]=G[v][u]=999;
int rt1,rt2;
while(u>=0){
rt1=u;
u=parent[u];
}
while(v>=0){
rt2=v;
v=parent[v];
}
if(rt1!=rt2)
{noofedges++;
result[x][y]=min;
result[y][x]=min;
parent[rt2]=rt1;
}
}
cout<<"\nMinimum spanning tree Matrix is: \n";
int sum=0;
for(int i=0;i<n;i++)
{
cout<<"\n";
for(int j=0;j<n;j++){
cout<<result[i][j]<<"\t";
sum=sum+result[i][j];
}
}
cout<<"\nCost is: "<<sum/2;
}
void Graph::prims(){
int result[n][n];
int visited[n];
int min;
int u=0;
int v=0;
int G[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
G[i][j]=Gmat[i][j];
for(int i=0;i<n;i++){
visited[i]=0;
for(int j=0;j<n;j++){
if(G[i][j]==-1)
G[i][j]=999;
result[i][j]=0;
}}
visited[0]=1;
for(int counter=0;counter<n-1;counter++)
{
min=999;
for(int i=0;i<n;i++)
{
if(visited[i]==1)
{
for(int j=0;j<n;j++)
{
if(visited[j]!=1)
{
if(min>G[i][j])
{
min=G[i][j];
u=i;
v=j;
}
}
}
}
}
visited[v]=1;
result[u][v]=min;
result[v][u]=min;
}
cout<<"\nMinimum spanning tree Matrix is: \n";
int sum=0;
for(int i=0;i<n;i++)
{
cout<<"\n";
for(int j=0;j<n;j++){
cout<<result[i][j]<<"\t";
sum=sum+result[i][j];
}
}
cout<<"\nCost is: "<<sum/2;
}
void Graph::create(){
char ch,ch1;
int u,v,w;
do{
cout<<"\nEnter starting office number";
cin>>u;
cout<<"\nEnter ending office number";
cin>>v;
if(u>=n){
cout<<"\nInvalid office number";
continue;
}
if(v>=n){
cout<<"\nInvalid office number";
continue;
}
if(Gmat[u][v]!=-1){
cout<<"\nAlready added\n Do you want to change amount?";
cin>>ch1;
if(ch1=='n'||ch1=='N')
continue;
}
cout<<"\nEnter Amount";
cin>>w;
if(w>0){
Gmat[u][v]=w;
Gmat[v][u]=w;
}
else{
cout<<"\nInvalid Amount\n ENTER AGAIN";
continue;
}
cout<<"\nDo you want to add another?";
cin>>ch;
}while(ch=='y'||ch=='Y');
}
int main(){
int n;
cout<<"\nEnter number of offices: ";
cin>>n;
Graph obj(n);
obj.create();
do{
cout<<"\nCompute MINIMUM SPANNING TREE by:\n\t 1. Prim's Algorithm\n\t 2. Kruskal's Algorithm\n3. EXIT";
cin>>n;
if(n==1)
obj.prims();
if(n==2)
obj.kruskals();
}while(n!=3);
return 0;
}
/*
shreyas@shreyas:~/2416$ g++ A5.cpp
shreyas@shreyas:~/2416$ ./a.out
Enter number of offices: 4
Enter starting office number0
Enter ending office number3
Enter Amount50
Do you want to add another?y
Enter starting office number1
Enter ending office number2
Enter Amount12
Do you want to add another?y
Enter starting office number0
Enter ending office number3
Already added
Do you want to change amount?n
Enter starting office number1
Enter ending office number2
Already added
Do you want to change amount?n
Enter starting office number0
Enter ending office number1
Enter Amount44
Do you want to add another?y
Enter starting office number0
Enter ending office number2
Enter Amount36
Do you want to add another?y
Enter starting office number1
Enter ending office number2
Already added
Do you want to change amount?n
Enter starting office number0
Enter ending office number3
Already added
Do you want to change amount?y
Enter Amount44
Do you want to add another?n
Compute MINIMUM SPANNING TREE by:
1. Prim's Algorithm
2. Kruskal's Algorithm
3. EXIT1
Minimum spanning tree Matrix is:
0 0 36 44
0 0 12 0
36 12 0 0
44 0 0 0
Cost is: 92
Compute MINIMUM SPANNING TREE by:
1. Prim's Algorithm
2. Kruskal's Algorithm
3. EXIT2
Minimum spanning tree Matrix is:
0 0 36 44
0 0 12 0
36 12 0 0
44 0 0 0
Cost is: 92
Compute MINIMUM SPANNING TREE by:
1. Prim's Algorithm
2. Kruskal's Algorithm
3. EXIT3
shreyas@shreyas:~/2416$
*/