-
Notifications
You must be signed in to change notification settings - Fork 0
/
randmst.c
348 lines (311 loc) · 7.36 KB
/
randmst.c
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
// CS124 - Programming Assignment 1
// Joel Kwartler & Arianna Benson
#include <stdio.h>
#include <time.h>
#include <limits.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include "header.h"
// returns a random float between 0 and 1 inclusive
float randNum()
{
float rando = rand();
float randNum = (rando / RAND_MAX);
return randNum;
}
// generates weight for pruning
float getK(int numpoints, int dim, int flag)
{
// prune nothing if flag is on
if (flag == 1 || numpoints < 1024)
{
return 1.0;
}
if (dim == 2)
{
return 0.521;
}
if (dim == 3)
{
return 0.662;
}
if (dim == 4)
{
return 0.777;
}
return 1.0;
}
// generates 0-dimensional edge weights
void gen0Dim(int len, edge* edgeWeights[len+1], float k)
{
// instantiate edgeweights as NULL
for (int i = 1; i <= len; i++)
{
edgeWeights[i] = NULL;
}
// fill array of linked lists with edgeweights
for (int i = 1; i <= len; i++)
{
edge* root = edgeWeights[i];
for (int j = i; j <= len; j++)
{
float w = randNum();
// if we haven't pruned the edge
if (w < k)
{
// create our node, affix to beginning
edge* new = malloc(sizeof(edge));
new->to = j;
new->weight = w;
new->next = root;
root = new;
// ensure mirroring
edge* new2 = malloc(sizeof(edge));
new2->to = i;
new2->weight = new->weight;
new2->next = edgeWeights[j];
edgeWeights[j] = new2;
}
}
edgeWeights[i] = root;
}
}
float dist2d(float x1, float y1, float x2, float y2)
{
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
// generates 2-dimensional edge weights
void gen2Dim(int len, edge* edgeWeights[len+1], float k)
{
// a place for our x and y coordinates
float *xs = malloc(sizeof(float) * (len+1));
float *ys = malloc(sizeof(float) * (len+1));
// generate our points
for (int i = 1; i <= len; i++)
{
xs[i] = randNum();
ys[i] = randNum();
}
// instantiate edgeweights as NULL
for (int i = 1; i <= len; i++)
{
edgeWeights[i] = NULL;
}
// fill array of linked lists with edgeweights
for (int i = 1; i <= len; i++)
{
edge* root = edgeWeights[i];
// j wil loop through other nodes
for (int j = i; j <= len; j++)
{
float w = dist2d(xs[i],ys[i],xs[j],ys[j]);
// if we haven't pruned the edge
if (w < k)
{
edge* new = malloc(sizeof(edge));
new->to = j;
new->weight = w;
new->next = root;
root = new;
// ensure mirroring
edge* new2 = malloc(sizeof(edge));
new2->to = i;
new2->weight = new->weight;
new2->next = edgeWeights[j];
edgeWeights[j] = new2;
}
}
edgeWeights[i] = root;
}
// free storage
free(xs);
free(ys);
}
float dist3d(float x1, float y1, float z1, float x2, float y2, float z2)
{
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
}
// generates 3-dimensional edge weights
void gen3Dim(int len, edge* edgeWeights[len+1], float k)
{
// a place for our x, y, and z coordinates
float *xs = malloc(sizeof(float) * (len+1));
float *ys = malloc(sizeof(float) * (len+1));
float *zs = malloc(sizeof(float) * (len+1));
// generate our points
for (int i = 1; i <= len; i++)
{
xs[i] = randNum();
ys[i] = randNum();
zs[i] = randNum();
}
// instantiate edgeweights as NULL
for (int i = 1; i <= len; i++)
{
edgeWeights[i] = NULL;
}
// fill array of linked lists with edgeweights
for (int i = 1; i <= len; i++)
{
edge* root = edgeWeights[i];
// j will loop through other nodes
for (int j = i; j <= len; j++)
{
float w = dist3d(xs[i],ys[i],zs[i],xs[j],ys[j],zs[j]);
// if we haven't pruned the edge
if (w < k)
{
edge* new = malloc(sizeof(edge));
new->to = j;
new->weight = w;
new->next = root;
root = new;
// ensure mirroring
edge* new2 = malloc(sizeof(edge));
new2->to = i;
new2->weight = new->weight;
new2->next = edgeWeights[j];
edgeWeights[j] = new2;
}
}
edgeWeights[i] = root;
}
// free storage
free(xs);
free(ys);
free(zs);
}
float dist4d(float x1, float y1, float z1, float t1, float x2, float y2, float z2, float t2)
{
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1)+(t2-t1)*(t2-t1));
}
// generates 4-dimensional edge weights
void gen4Dim(int len, edge* edgeWeights[len+1], float k)
{
// a place for our x, y, z, and t coordinates
float *xs = malloc(sizeof(float) * (len+1));
float *ys = malloc(sizeof(float) * (len+1));
float *zs = malloc(sizeof(float) * (len+1));
float *ts = malloc(sizeof(float) * (len+1));
// generate our points
for (int i = 1; i <= len; i++)
{
xs[i] = randNum();
ys[i] = randNum();
zs[i] = randNum();
ts[i] = randNum();
}
// instantiate edgeweights as NULL
for (int i = 1; i <= len; i++)
{
edgeWeights[i] = NULL;
}
// fill array of linked lists with edgeweights
for (int i = 1; i <= len; i++)
{
edge* root = edgeWeights[i];
// loop through all other nodes
for (int j = i; j <= len; j++)
{
float w = dist4d(xs[i],ys[i],zs[i],ts[i], xs[j],ys[j],zs[j],ts[j]);
// if we haven't pruned the edge
if (w < k)
{
// create our node, affix to beginning
edge* new = malloc(sizeof(edge));
new->to = j;
new->weight = w;
new->next = root;
root = new;
// ensure mirroring
edge* new2 = malloc(sizeof(edge));
new2->to = i;
new2->weight = new->weight;
new2->next = edgeWeights[j];
edgeWeights[j] = new2;
}
}
edgeWeights[i] = root;
}
// free storage
free(xs);
free(ys);
free(zs);
free(ts);
}
int main(int argc, char* argv[])
{
// make sure format was correct
if (argc != 5)
{
printf("wrong syntax; should be 'randmst 0 numpoints numtrials dimension\r\n'");
return 0;
}
// get our arguments
int flag = atoi(argv[1]);
int numpoints = atoi(argv[2]);
int numtrials = atoi(argv[3]);
int dimension = atoi(argv[4]);
printf("numpoints %d, numtrials %d, dimension %d, flag %d \r\n",
numpoints, numtrials, dimension, flag);
// seed the random number generator
srand(time(NULL));
float avgMST;
// begin timing
clock_t start = clock();
clock_t diff;
node* nodes[numpoints];
// get a k function to prune edges longer than k
float k = getK(numpoints, dimension, flag);
printf("k was %f \n", k);
// iterate through trials
for (int t = 0; t < numtrials; t++)
{
// create all of our nodes
for (int i = 0; i <= numpoints; i++)
{
nodes[i] = malloc(sizeof(node));
nodes[i]->num = i;
nodes[i]->key = 10 + i;
nodes[i]->parent = NULL;
nodes[i]->inQueue = true;
nodes[i]->loc = i;
}
// let the 0th node be the size of the heap
nodes[0]->key = numpoints;
// set the root's key to 0
nodes[1]->key = 0;
// create list of linked lists
edge* edgeWeights[numpoints+1];
// match on dimension type
if (dimension == 0)
{
gen0Dim(numpoints, edgeWeights, k);
}
else if (dimension == 2)
{
gen2Dim(numpoints, edgeWeights, k);
}
else if (dimension == 3)
{
gen3Dim(numpoints, edgeWeights, k);
}
else
{
gen4Dim(numpoints, edgeWeights, k);
}
// once we have generated all of our nodes and edges, run prim's!
float x = prim(numpoints, nodes, edgeWeights);
// update this so we can take an average
avgMST += x;
}
// end timing
diff = clock() - start;
int msec = diff * 1000 / CLOCKS_PER_SEC;
// calculate average MST length
avgMST = avgMST / numtrials;
// output results
printf("%i trials of dim%i with %i points took %d seconds and %d milliseconds \n avgMST size was: %2.6f \n",
numtrials, dimension, numpoints, msec/1000, msec%1000, avgMST);
}