-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo.cpp
364 lines (319 loc) · 6.51 KB
/
algo.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
class graph
{
int G[10][10];
int S[10][10], Lj[10][10], Pj[10][10], n, i, j, k;
public:
void get_data();
void mst();
void print();
};
void graph::get_data()
{
cout<<"\nEnter the total number of vertices: ";
cin>>n;
cout<<"\nEnter the adjacency matrix (If there is no edge betweent two vertex then enter 100): \n\n";
for (i = 0; i < n; i++)
{
cout<<"___\n";
for (j = 0; j < n; j++)
{
cin>>G[i][j];
}
}
}
void graph::mst()
{
// Previous row min
int old_min;
// Min value
int min = 100;
// Initial row
int row = 0;
// Rows search state
int selected[n];
// Initial rows search state
for (i = 0; i < n; i++)
{
selected[i] = false;
}
// S initialization
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
S[i][j] = 100;
}
}
// Filled Pj first row
for (j = 1; j < n; j++)
{
if (G[0][j] != 100) Pj[0][j] = 1;
else Pj[0][j] = 100;
}
// Row holder
int tmp_row;
// Disordered search for all rows
for (int count = 0; count < n; count++)
{
// S update
for (j = 0; j < n; j++)
{
// Count next iterations
if (count != 0)
{
// Check previous row col value not 0
if (S[count - 1][j] != 100)
{
// If true, copy it into the same col of the newer row
S[count][j] = S[count - 1][j];
}
// Check col == solution value
else if (j == row)
{
// Restoring proper value
S[count][j] = row + 1;
}
// Fill it with 0 when all check fails
else
{
S[count][j] = 100;
}
}
// Count 1° iteration
else
{
// Check col == solution value
if (j == row)
{
// Restoring proper value
S[count][j] = row + 1;
}
}
}
// Lj update
for (j = 1; j < n; j++)
{
// Count next iterations
if (count != 0)
{
// Check if previous value to compare isn't 0
if (Lj[count - 1][j] != 100)
{
// If the new value is less
// and j isn't already inside S
// the previous value is replaced with the new one
if (G[row][j] + old_min < Lj[count - 1][j] && selected[j] == false)
{
Lj[count][j] = G[row][j] + old_min;
}
// Previous value remains
else
{
Lj[count][j] = Lj[count - 1][j];
}
}
else if (Lj[count - 1][j] == 100)
{
if (G[row][j] == 100)
{
Lj[count][j] = G[row][j];
}
else
{
Lj[count][j] = G[row][j] + old_min;
}
}
}
else
{
// Count 1° iteration
// Copying first row from G to Lj
Lj[0][j] = G[0][j];
}
}
for (j = 1; j < n; j++)
{
// Count next iterations
if (count != 0)
{
// Check if previous value to compare isn't 0
if (Pj[count - 1][j] != 100)
{
// If the new value is less
// and j isn't already inside S
// the previous value is replaced with the new one
if (G[row][j] + old_min < Lj[count - 1][j] && selected[j] == false)
{
Pj[count][j] = row + 1;
}
// Previous value remains
else
{
Pj[count][j] = Pj[count - 1][j];
}
}
else if (Pj[count - 1][j] == 100)
{
// Setting pj row
if (G[row][j] != 100)
{
Pj[count][j] = row + 1;
}
else
{
Pj[count][j] = 100;
}
}
}
else
{
// Count 1° iteration
// Filling Pj first row
if (G[0][j] != 100)
{
Pj[0][j] = 1;
}
else
{
Pj[0][j] = 100;
}
}
}
// Old min reset
old_min = 100;
// Min search
for (j = 1; j < n; j++)
{
// Checks:
// Lj value not 0
// Lj row wasn't already searched
// Lj Lj[count][j];value is smaller than min
if (Lj[count][j] != 100 && selected[j] == false && Lj[count][j] < min)
{
// New min value
min = Lj[count][j];
// Saving min for shorter path algo
old_min = Lj[count][j];
// Saving index
tmp_row = j;
}
}
// Row update
row = tmp_row;
// Selected state update
selected[row] = true;
// Min reset
min = 100;
}
}
void graph::print()
{
cout<<"\n\n------- G: -------\n\n";
for (i = 0; i < n; i++)
{
cout<<"|";
for (j = 0; j < n; j++)
{
if (G[i][j] == 100)
{
cout<<" -";
}
else
{
if (G[i][j] >= 10)
{
cout<<" "<<G[i][j];
}
else
{
cout<<" "<<G[i][j];
}
}
}
cout<<"|\n";
}
cout<<"\n\n------- S: -------\n\n";
for (i = 0; i < n; i++)
{
cout<<"|";
for (j = 0; j < n; j++)
{
if (S[i][j] == 100)
{
cout<<" -";
}
else
{
if (S[i][j] >= 10)
{
cout<<" "<<S[i][j];
}
else
{
cout<<" "<<S[i][j];
}
}
}
cout<<"|\n";
}
cout<<"\n\n------- Lj: -------\n\n";
for (i = 0; i < n; i++)
{
cout<<"|";
for (j = 1; j < n; j++)
{
if (Lj[i][j] == 100)
{
cout<<" -";
}
else
{
if (Lj[i][j] >= 10)
{
cout<<" "<<Lj[i][j];
}
else
{
cout<<" "<<Lj[i][j];
}
}
}
cout<<"|\n";
}
cout<<"\n\n------- Pj: -------\n\n";
for (i = 0; i < n; i++)
{
cout<<"|";
for (j = 1; j < n; j++)
{
if (Pj[i][j] == 100)
{
cout<<" -";
}
else
{
if (Pj[i][j] >= 10)
{
cout<<" "<<Pj[i][j];
}
else
{
cout<<" "<<Pj[i][j];
}
}
}
cout<<"|\n";
}
}
int main()
{
graph graph;
graph.get_data();
graph.mst();
graph.print();
return 0;
}