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 <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <memory.h>
// Create the edge type
struct Edge
{
int from;
int to;
int weight;
bool inTheCut;
};
typedef Edge* pEdge;
// adjacency list
struct ListIt
{
int value;
ListIt *p_next;
};
// typedef for the pointer type
typedef ListIt* pListIt;
pEdge edges;
pEdge edgesPr;
int n,m;
int *tree;
int *ancient;
int **distanceMatrix;
pListIt* adjacencyList; // Here we save the relations.... A 100000 is the maximum vertexes
void read();
void print();
void Kruskal();
int Prim(int at);
bool add(pListIt &dest, int val);
int compareEdges(const void* el1, const void* el2);
int min_vag();
int main()
{
read();
printf("\n");
print();
//Kruskal();
printf("\n\n With Prims Technique: \n\n");
Prim(1);
return 0;
}
//************************************
// Method: read - public access
// Returns: void
//
// Purpose: Read in the data from In.txt
//************************************
void read()
{
FILE *f;
freopen_s(&f, "In.txt", "r", stdin);
freopen_s(&f, "Out.txt", "w", stdout);
scanf_s("%d", &n);
scanf_s("%d", &m);
tree = (int*) calloc(n+1, sizeof(int));
ancient = (int*) calloc(n+1, sizeof(int));
edges = (pEdge)malloc(m*sizeof(Edge));
adjacencyList = (pListIt*) calloc(n+1,sizeof(pListIt));
distanceMatrix = (int**) calloc(n+1, sizeof(int*));
for (int j = 1; j <=n; ++j)
{
distanceMatrix[j] = (int*) calloc(n+1, sizeof(int));
}
int from,to,distance;
for(int i =0; i< m; ++i)
{
scanf_s("%d%d%d", &from, &to, & distance);
add(adjacencyList[from], to);
add(adjacencyList[to], from);
edges[i].from = from;
edges[i].to = to;
edges[i].weight = distance;
}
}
//************************************
// Method: print - public access
// Parameter:
// Returns: void
//
// Purpose: Print the list of edges
//************************************
void print()
{
printf("\n The list of the edges:\n ");
for(int i =0; i < m; ++i)
{
if(!(i % 1)) // how many in a row... for now 1 edge per row
{
printf("\n");
}
printf(" %d ~ %d -> %d ", edges[i].from, edges[i].to, edges[i].weight);
}
printf("\n");
}
void Kruskal()
{
int i = 0, overallWeight = 0 , j =0;
int fromTree = 0, toTree = 0, changeNr = 0;
// sort the edge list
qsort((void*)edges,m,sizeof(Edge),compareEdges);
printf("\n");
print();
printf("\n");
printf("\nThe list of the edges with the algorithm of Kruskall:\n");
// build up the individual list
for ( i =1; i <= n; ++i)
tree[i] = i;
for ( i= 0; i < m && changeNr < n; ++i)
{
fromTree = tree[edges[i].from];
toTree = tree[edges[i].to];
if( fromTree != toTree)
{
++changeNr;
overallWeight += edges[i].weight;
printf(" %d) %d ~ %d -> %d \n", changeNr, edges[i].from, edges[i].to, edges[i].weight);
//Union of the trees
for ( j = 0; j < m; ++j)
if(tree[j] == toTree)
tree[j] = fromTree;
}
}
printf( " \n The minimal weight %d ", overallWeight);
}
//************************************
// Method: compareEdges - public access
// Parameter:
// const void * edge1
// const void * edge2
// Returns: int
//
// Purpose: Compare two edge types according to their weight
//************************************
int compareEdges(const void* edge1, const void * edge2)
{
pEdge first = (pEdge)edge1;
pEdge second = (pEdge)edge2;
return first->weight - second->weight;
}
//************************************
// Method: Prim - public access
// Parameter:
// int at
// Returns: int
//
// Purpose: Prims Technique to generate the minimal spanning tree
//************************************
int Prim(int at)
{
int i = 0;
memset(tree, 0, (n+1)*sizeof(int));
tree[at] = 1;
ancient[at] = 0 ;
// sort the edge list
qsort((void*)edges,m,sizeof(Edge),compareEdges);
// build up the tree
for (i = 0; i < m; ++i)
{
distanceMatrix[edges[i].from][edges[i].to ] = i;
distanceMatrix[edges[i].to ][edges[i].from] = i;
if (edges[i].from == at || edges[i].to == at)
{
edges[i].inTheCut = true;
}
else
{
edges[i].inTheCut = false;
}
}
int overallWeight = 0;
int curent = 0;
int addedVertex = 1;
int neighborVertex = 0;
int neighborEdge = 0;
pListIt p = NULL;
int j = 0;
for(i = 1; i < n; ++i)
{
for ( j =0 ; j < m; ++j)
{
if (edges[j].inTheCut)
{
curent = j;
break;
}
}
printf( " %d) %d ~ %d -> %d \n",i, edges[curent].from,edges[curent].to, edges[curent].weight);
overallWeight += edges[curent].weight;
if (tree[edges[curent].from] == 1)
{
addedVertex = edges[curent].to;
ancient[addedVertex] = edges[curent].from;
}
tree[addedVertex] = 1;
for (p = adjacencyList[addedVertex]; p != NULL; p = p -> p_next)
{
neighborVertex = p->value;
neighborEdge = distanceMatrix[addedVertex][neighborVertex];
if (tree[neighborVertex] == 1)
edges[neighborEdge].inTheCut = false; // reset if both ends are there
else
edges[neighborEdge].inTheCut = true; // set if only one is there
}
}
printf( "\n\n The overall Weight: %d " , overallWeight);
return 0;
}
// Here we need to make sure that the items are added in // correct order
bool add(pListIt &dest, int val)
{
//create the item
pListIt p;
p = (pListIt) malloc(sizeof(ListIt));
p -> value = val;
if(!dest) // first item addition
{
p -> p_next = NULL;
dest = p;
}
else
{
pListIt find = dest;
pListIt at = NULL;
// first find the first greater number, insert before
while(find && find->value <= val)
{
if( find->value == val) // do not add a duplicate
{
free(p);
return false;
}
at = find;
find = find->p_next;
}
// insert at
if (at) // insert at a valid point
{
p->p_next = at->p_next;
at->p_next = p;
}
else // insert at the start
{
p->p_next = dest;
dest = p;
}
}
return true;
_getch();
} |