-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.c
723 lines (588 loc) · 13.4 KB
/
graph.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
#include "graph.h"
#include "zergmap.h"
#include "set.h"
#include "map.h"
#include "priority-queue.h"
#include "pathfinding.h"
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
struct edge {
struct node *to;
double distance;
struct edge *next;
};
struct node {
uint16_t id;
struct zerg_header *zerg;
double lat;
double lon;
bool health;
float alt;
bool kill;
struct edge *edges;
struct node *next;
};
struct graph_ {
struct node *nodes;
};
graph *graph_create()
{
graph *g = malloc(sizeof(*g));
if (!g) {
return NULL;
}
g->nodes = NULL;
return g;
}
graph *graph_clone(graph * g)
{
if (!g) {
return NULL;
}
graph *new = malloc(sizeof(*new));
if (!new) {
return NULL;
}
new->nodes = NULL;
struct node *curr = g->nodes;
while (curr) {
graph_add_vertex(new, curr->zerg);
curr = curr->next;
}
curr = g->nodes;
while (curr) {
struct edge *curr_edge = curr->edges;
while (curr_edge) {
graph_add_edge(new, curr->id, curr_edge->to->id,
curr_edge->distance);
curr_edge = curr_edge->next;
}
curr = curr->next;
}
return new;
}
// a vertex holds the zerg id, coordinates, a pointer to other zerg data, such
// as hit points, a flag indicating whether to remove, and initializes edge
// data. Vertexes prepend to the start of the graph.
bool graph_add_vertex(graph * g, struct zerg_header *zerg)
{
if (!g || !zerg) {
return false;
}
struct node *curr = g->nodes;
while (curr) {
if (curr->id == zerg->srcid) {
return false;
}
curr = curr->next;
}
struct node *new = NULL;
new = malloc(sizeof(*new));
if (!new) {
return false;
}
new->id = zerg->srcid;
new->lat = NAN;
new->lon = NAN;
new->alt = NAN;
if (zerg->gps_payload != NULL) {
new->lat = zerg->gps_payload->latitude;
new->lon = zerg->gps_payload->longitude;
new->alt = zerg->gps_payload->altitude;
}
new->kill = false;
new->health = false;
if (zerg->low_health) {
new->health = true;
}
new->zerg = zerg;
new->edges = NULL;
new->next = g->nodes;
g->nodes = new;
return true;
}
// update the coordinates of a zerg id already in the graph,
// checks if the difference between the old and new coordinates
// is outside the accuracy value
bool graph_update_coord(graph * g, uint16_t id, struct zerg_header *zerg)
{
if (!g || !zerg || !zerg->gps_payload) {
return false;
}
struct node *curr = g->nodes;
while (curr) {
if (curr->id == id) {
double difference = haversine(curr->lat, curr->lon,
zerg->gps_payload->
latitude,
zerg->gps_payload->
longitude);
// the haversine function returns kilometers; need value
// in meters for this comparison
if (difference * 1000 > zerg->gps_payload->accuracy) {
return false;
} else {
curr->lat = zerg->gps_payload->latitude;
curr->lon = zerg->gps_payload->longitude;
curr->alt = zerg->gps_payload->altitude;
return true;
}
}
curr = curr->next;
}
return false;
}
// checks if the graph has the specified zerg ID
bool graph_contains(const graph * g, uint16_t data)
{
if (!g) {
return false;
}
struct node *curr = g->nodes;
while (curr) {
if (curr->id == data) {
return true;
}
curr = curr->next;
}
return false;
}
// returns total number of nodes in the graph
size_t zerg_count(graph * g)
{
if (!g) {
return 0;
}
size_t count = 0;
struct node *curr = g->nodes;
while (curr) {
count++;
curr = curr->next;
}
return count;
}
bool graph_mark_remove(graph * g, uint16_t data)
{
if (!g) {
return false;
}
struct node *curr = g->nodes;
while (curr) {
if (curr->id == data) {
curr->kill = true;
return true;
}
curr = curr->next;
}
return false;
}
bool graph_is_marked(graph * g, uint16_t data)
{
if (!g) {
return false;
}
struct node *curr = g->nodes;
while (curr) {
if (curr->id == data) {
if (curr->kill == true) {
return true;
} else {
return false;
}
}
curr = curr->next;
}
return false;
}
// checks to see if a zerg is below a certain percentage of health
bool low_health(graph * g, uint16_t data, double percent)
{
if (!g) {
return false;
}
struct node *curr = g->nodes;
while (curr) {
if (curr->id == data) {
if ((double)curr->zerg->status_payload->hit_points
/ (double)curr->zerg->status_payload->max_hit_points
< percent / 100) {
return true;
}
}
curr = curr->next;
}
return false;
}
// iterates to the specified node, removes its edges, deletes the node
void graph_remove_vertex(graph * g, uint16_t data)
{
if (!g || !data) {
return;
}
struct node *to_delete = NULL;
struct node **curr = &g->nodes;
while (*curr) {
if ((*curr)->id == data) {
to_delete = *curr;
struct node *node_with_edges = g->nodes;
while (node_with_edges) {
struct edge *possible_incoming_edge =
node_with_edges->edges;
struct edge **curr_edge =
&node_with_edges->edges;
while (possible_incoming_edge) {
if (possible_incoming_edge->to ==
to_delete) {
*curr_edge =
possible_incoming_edge->
next;
free(possible_incoming_edge);
break;
}
curr_edge =
&possible_incoming_edge->next;
possible_incoming_edge =
possible_incoming_edge->next;
}
node_with_edges = node_with_edges->next;
}
*curr = to_delete->next;
struct edge *curr_edge = to_delete->edges;
while (curr_edge) {
struct edge *tmp = curr_edge->next;
free(curr_edge);
curr_edge = tmp;
}
free(to_delete);
return;
}
curr = &(*curr)->next;
}
}
// adds a connection and distance between a source and destination
bool graph_add_edge(graph * g, const uint16_t src, const uint16_t dst,
double dist)
{
if (!g || !src || !dst || isnan(dist)) {
return false;
}
struct node *src_node = NULL;
struct node *dst_node = NULL;
struct node *curr = g->nodes;
// While there are nodes left to check,
// and we have not found the nodes for the
// src or dst values, keep looking
while (curr && (!src_node || !dst_node)) {
if (curr->id == src) {
src_node = curr;
}
if (curr->id == dst) {
dst_node = curr;
}
curr = curr->next;
}
// If one of the two can't be found, don't add
if (!src_node || !dst_node) {
return false;
}
struct edge *duplicate_edge_detector = src_node->edges;
while (duplicate_edge_detector) {
if (duplicate_edge_detector->to == dst_node) {
return false;
}
duplicate_edge_detector = duplicate_edge_detector->next;
}
struct edge *new = malloc(sizeof(*new));
if (!new) {
return false;
}
new->to = dst_node;
new->distance = dist;
new->next = src_node->edges;
src_node->edges = new;
return true;
}
// calculates the distance between two coordinates
double haversine(double lat1, double lat2, double lon1, double lon2)
{
double dlon = 0;
double dlat = 0;
// degrees to radian conversion
double rad = 3.141592653589793 / 180;
// take the difference of the two latitudes and longitudes
dlon = lon2 - lon1;
dlat = lat2 - lat1;
// The variables a, c, and d split up the Haversine equation into
// multiple steps. 'a' calculates everything underneath the giant
// square root; 'c' takes the arcsine of the squareroot of a's result
// and multiplies by 2. The final step is to multiply by the mean
// radius of the planet.
double a = 0;
double c = 0;
double d = 0;
a = pow(sin(dlat / 2 * rad), 2) + cos(lat2 * rad)
* cos(lat1 * rad) * pow(sin(dlon / 2 * rad), 2);
c = 2 * asin(sqrt(a));
// multiply by radius of planet
d = 6371 * c;
return d;
}
// returns the distance between a source and destination id
double graph_edge_distance(const graph * g, const uint16_t src,
const uint16_t dst)
{
if (!g || !src || !dst) {
return NAN;
}
struct node *curr_node = g->nodes;
while (curr_node) {
if (curr_node->id == src) {
struct edge *curr_edge = curr_node->edges;
while (curr_edge) {
if (curr_edge->to->id == dst) {
return curr_edge->distance;
}
curr_edge = curr_edge->next;
}
return NAN;
}
curr_node = curr_node->next;
}
return NAN;
}
// changes the distance between a source and destination, used in suurballe
bool change_edge_distance(const graph * g, const uint16_t src,
const uint16_t dst, double dist)
{
if (!g || isnan(dist)) {
return false;
}
struct node *curr_node = g->nodes;
while (curr_node) {
if (curr_node->id == src) {
struct edge *curr_edge = curr_node->edges;
while (curr_edge) {
if (curr_edge->to->id == dst) {
curr_edge->distance = dist;
return true;
}
curr_edge = curr_edge->next;
}
}
curr_node = curr_node->next;
}
return false;
}
// removes an edge between a source and destination node
void graph_remove_edge(graph * g, const uint16_t src, const uint16_t dst)
{
if (!g || !src || !dst) {
return;
}
struct node *src_node = g->nodes;
while (src_node) {
if (src_node->id == src) {
struct edge **curr = &src_node->edges;
while (*curr) {
if ((*curr)->to->id == dst) {
struct edge *to_delete = *curr;
*curr = to_delete->next;
free(to_delete);
return;
}
curr = &(*curr)->next;
}
}
src_node = src_node->next;
}
}
// iterates through the graph and executes a user-specified function
void graph_iterate_vertices(graph * g, void (*func)(uint16_t id))
{
if(!g || !func) {
return;
}
struct node *curr = g->nodes;
while (curr) {
func(curr->id);
curr = curr->next;
}
}
// returns number of nodes a node has edges to, used in djikstra algorithm
// the *func is intended to be the visit_node function in pathfinding.c
size_t graph_neighbors(graph * g, const uint16_t vertex,
void (*func)(double distance, uint16_t dst))
{
if(!g || !vertex) {
return 0;
}
struct node *curr = g->nodes;
while (curr) {
if (curr->id == vertex) {
size_t count = 0;
struct edge *curr_edge = curr->edges;
while (curr_edge) {
if (func) {
func(curr_edge->distance,
curr_edge->to->id);
}
++count;
curr_edge = curr_edge->next;
}
return count;
}
curr = curr->next;
}
return 0;
}
// returns a priority queue containing all candidates to remove and the number
// of connections they have, including immediate neighbors and nodes that
// have two disjoint paths
pqueue *check_connectivity(graph * g)
{
if (!g) {
return NULL;
}
pqueue *to_remove = pqueue_create(MIN);
graph *new = g;
struct node *curr = new->nodes;
uint16_t saved_id = 0;
while (curr) {
double count = 0;
saved_id = curr->id;
struct node *to_iterate = new->nodes;
while (to_iterate) {
if (curr->id != to_iterate->id &&
suurballe(new, curr->id, to_iterate->id)) {
count++;
}
to_iterate = to_iterate->next;
}
pqueue_enqueue(to_remove, count, saved_id);
curr = curr->next;
}
return to_remove;
}
// used output at end of program; either it displays zerg IDs that should be
// removed, indicate that too many removals would be required, or that the
// graph is complete, meaning that none should be removed
void recommend_removal(graph * g, pqueue * pq, size_t zerg_count)
{
if (!g || !pq) {
return;
}
size_t total_recommended = 0;
set *remove = set_create();
for (size_t n = 0; n < zerg_count; n++) {
double priority = pqueue_get_next_priority(pq);
uint16_t zerg = pqueue_dequeue(pq);
// in a complete graph, all nodes should have nodes - 1 number
// of connections; decrease the priority threshold with each
// ID added
if (priority < zerg_count - 1 - total_recommended
&& zerg_count > 1) {
set_add(remove, zerg);
graph_remove_vertex(g, zerg);
total_recommended++;
}
pqueue *new = check_connectivity(g);
pqueue_destroy(pq);
pq = new;
if (total_recommended > zerg_count / 2) {
puts("Total removals exceeds half the total zerg\n");
pqueue_destroy(pq);
set_destroy(remove);
return;
}
}
if (total_recommended == 0) {
printf("Zerg are fully connected\n");
pqueue_destroy(pq);
set_destroy(remove);
return;
}
printf("Recommended Zerg IDs to remove: \n");
print_set(remove);
pqueue_destroy(pq);
set_destroy(remove);
}
// displays zerg ids that are below the specified health threshold
void show_low_health_zerg(graph * g)
{
if (!g) {
return;
}
bool print = false;
set *low = set_create();
struct node *curr = g->nodes;
while (curr) {
if (curr->health) {
set_add(low, curr->id);
print = true;
}
curr = curr->next;
}
if (print) {
printf("Low Health: \n");
print_set(low);
}
set_destroy(low);
}
// displays zerg that are not connected to other zerg
void show_no_gps(graph * g)
{
if (!g) {
return;
}
bool print = false;
set *no_gps = set_create();
struct node *curr = g->nodes;
while (curr) {
if (isnan(curr->lat) && isnan(curr->lon)) {
set_add(no_gps, curr->id);
print = true;
}
curr = curr->next;
}
if (print) {
printf("Zerg IDs not connected to other Zerg: \n");
print_set(no_gps);
}
set_destroy(no_gps);
}
// used for debugging purposes
void graph_print(graph * g)
{
if (!g) {
return;
}
struct node *curr = g->nodes;
while (curr) {
printf("%u: ", curr->id);
struct edge *curr_edge = curr->edges;
while (curr_edge) {
printf("%u, ", curr_edge->to->id);
curr_edge = curr_edge->next;
}
puts("");
curr = curr->next;
}
}
// frees edges, frees nodes, frees graph
void graph_destroy(graph * g)
{
struct node *curr_node = g->nodes;
while (curr_node) {
struct node *next = curr_node->next;
struct edge *curr_edge = curr_node->edges;
while (curr_edge) {
struct edge *tmp = curr_edge->next;
free(curr_edge);
curr_edge = tmp;
}
free(curr_node);
curr_node = next;
}
free(g);
}