-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·686 lines (594 loc) · 17.3 KB
/
main.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
/**
* @file main.c
* @brief Implementation of a Red-Black Tree and Dijkstra's algorithm.
*
* This file contains the implementation of a Red-Black Tree and Dijkstra's algorithm
* for finding the shortest paths in a graph. The program reads input from the standard
* input, processes the data, and performs various operations including insertion into
* the Red-Black Tree, rotations, and heap operations.
*
* @details
* The program defines several structures and functions to manage the Red-Black Tree and
* perform Dijkstra's algorithm. The main components include:
* - Red-Black Tree node structure and related functions (insertion, rotations, fixing violations).
* - Heap operations for managing nodes during Dijkstra's algorithm.
* - Functions for reading and processing input data.
* - Main function to control the flow of the program.
*
* @note
* The program uses several global variables to manage the state of the tree and heap.
* The input is read using `getchar_unlocked` for faster I/O operations.
*
* @author
* Lorenzo Paleari
*
* @date
* 2021
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int K;
int Nnodes;
int ContrStmp = 0;
int HeapSize = 0;
typedef enum{FALSE, TRUE} boolean;
typedef enum{RED, BLACK} Colour;
struct node {
struct node* p;
struct node* r;
struct node* l;
int Key;
Colour color;
int index;
};
typedef struct{
int dist;
int index;
}Node;
struct node* root = NULL;
/**
* @brief Inserts a node into a red-black tree.
*
* This function takes a red-black tree (RBT) and a node, and inserts the node into the RBT
* while maintaining the RBT properties.
*
* @param T A pointer to the root of the red-black tree.
* @param X A pointer to the node to be inserted into the red-black tree.
* @return A pointer to the root of the red-black tree after the insertion.
*/
struct node* insert(struct node* T, struct node* X){
struct node* Curr;
struct node* Pre;
if (T == NULL)
return X;
Pre = NULL;
Curr = T;
while (Curr != NULL){
Pre = Curr;
if (X -> Key < Curr -> Key)
Curr = Curr -> l;
else
Curr = Curr -> r;
}
X -> p = Pre;
if (X -> Key < Pre -> Key)
Pre -> l = X;
else
Pre -> r = X;
return T;
}
/**
* @brief Performs a left rotation on the given node.
*
* This function takes a pointer to a node and performs a left rotation
* to balance the tree structure. It is typically used in self-balancing
* binary search trees such as AVL trees or Red-Black trees.
*
* @param X A pointer to the node to be rotated.
*/
void LeftRotate(struct node* X){
struct node* Dx = X -> r;
X -> r = Dx -> l;
if (Dx -> l != NULL)
Dx -> l -> p = X;
Dx -> p = X -> p;
if (X -> p == NULL){
root = Dx;
}
else if (X == X -> p -> l) {
X -> p -> l = Dx;
}
else {
X -> p -> r = Dx;
}
Dx -> l = X;
X -> p = Dx;
}
/**
* @brief Performs a right rotation on the given node.
*
* This function takes a pointer to a node and performs a right rotation,
* which is a common operation in balanced binary search trees such as AVL trees.
*
* @param X A pointer to the node to be rotated.
*/
void RightRotate(struct node* X){
struct node* Sx = X -> l;
X -> l = Sx -> r;
if (Sx -> r != NULL)
Sx -> r -> p = X;
Sx -> p = X -> p;
if (X -> p == NULL){
root = Sx;
}
else if (X == X -> p -> r) {
X -> p -> r = Sx;
}
else {
X -> p -> l = Sx;
}
Sx -> r = X;
X -> p = Sx;
}
/**
* @brief Repairs the Red-Black Tree after insertion.
*
* This function is responsible for fixing any violations of the Red-Black Tree
* properties that may occur after a new node is inserted into the tree.
*
* @param root A pointer to the root node of the Red-Black Tree.
* @param New A pointer to the newly inserted node that may cause violations.
*/
void RBinsert_and_repair(struct node* root, struct node* New){
while ((New != root) && (New -> color != BLACK) && (New -> p -> color == RED)){
if (New -> p == New -> p -> p -> l){
struct node* Y = New -> p -> p -> r;
if ((Y != NULL) && (Y -> color == RED)){
New -> p -> color = BLACK;
New -> p -> p -> color = RED;
Y -> color = BLACK;
New = New -> p -> p;
}
else {
if (New == New -> p -> r) {
New = New -> p;
LeftRotate(New);
}
Colour app = New -> p -> color;
New -> p -> color = New -> p -> p -> color;
New -> p -> p -> color = app;
RightRotate(New -> p -> p);
}
} else {
struct node* Y = New -> p -> p -> l;
if ((Y != NULL) && (Y -> color == RED))
{
New -> p -> color = BLACK;
New -> p -> p -> color = RED;
Y -> color = BLACK;
New = New -> p -> p;
}
else {
if (New == New -> p -> l) {
New = New -> p;
RightRotate(New);
}
Colour app = New -> p -> color;
New -> p -> color = New -> p -> p -> color;
New -> p -> p -> color = app;
LeftRotate(New -> p -> p);
}
}
}
root -> color = BLACK;
}
/**
* @brief Prints the contents of the binary tree.
*
* This function traverses the binary tree pointed to by T and prints its contents.
*
* @param T Pointer to the root node of the binary tree.
*/
void output_print(struct node *T){
if (T == NULL)
return;
output_print(T->l);
if (ContrStmp == 0){
int Somma = T->index;
int array[10] = {0,0,0,0,0,0,0,0,0,0};
if (Somma == 0){
putchar_unlocked('0');
} else {
int i = 0;
while (Somma > 0) {
array[i] = Somma % 10;
i++;
Somma = Somma / 10;
}
i--;
while (i >= 0) {
putchar_unlocked(array[i] + '0');
i--;
}
}
ContrStmp++;
} else if (ContrStmp < K) {
putchar_unlocked(' ');
int Somma = T->index;
int array[10] = {0,0,0,0,0,0,0,0,0,0};
int i = 0;
if (Somma == 0){
putchar_unlocked('0');
} else {
while (Somma > 0) {
array[i] = Somma % 10;
i++;
Somma = Somma / 10;
}
i--;
while (i >= 0) {
putchar_unlocked(array[i] + '0');
i--;
}
ContrStmp++;
}
}
output_print(T->r);
}
/**
* @brief Finds the minimum node in the binary tree.
*
* This function traverses the binary tree to find the node with the minimum key value.
*
* @param T Pointer to the root node of the binary tree.
* @return Pointer to the node with the minimum key value.
*/
struct node* min_val(struct node *T){
struct node* Curr;
Curr = T;
while (Curr -> l != NULL){
Curr = Curr -> l;
}
return Curr;
}
/**
* @brief Finds the successor of a given node in the binary tree.
*
* This function finds the successor of a given node in the binary tree.
*
* @param minimo Pointer to the node whose successor is to be found.
* @param Contatore Counter to keep track of the number of nodes visited.
* @return Key value of the successor node.
*/
int successor(struct node* minimo, int Contatore){
int CIAO;
if (Contatore == K){
CIAO = minimo -> Key;
return CIAO;
}
struct node * y;
if (minimo -> r != NULL) {
Contatore++;
y = min_val(minimo->r);
CIAO = successor(y, Contatore);
return CIAO;
}
else {
y = minimo->p;
while (y != NULL && y -> r == minimo){
minimo = y;
y = y -> p;
}
Contatore++;
CIAO = successor(y, Contatore);
return CIAO;
}
}
/**
* @brief Inserts a node into a max heap.
*
* This function inserts a node into a max heap and maintains the heap property.
*
* @param A Array representing the heap.
* @param a Node to be inserted into the heap.
*/
void InsertMAX(Node A[], Node a){
int i;
Node c;
HeapSize++;
A[HeapSize].dist = a.dist;
A[HeapSize].index = a.index;
i = HeapSize;
while ((i>1) && (A[i/2].dist > A[i].dist)){
c = A[i/2];
A[i/2] = A[i];
A[i] = c;
i = i/2;
}
}
/**
* @brief Maintains the min-heap property.
*
* This function maintains the min-heap property by ensuring that the parent node
* is smaller than its child nodes.
*
* @param A Array representing the heap.
* @param key Index of the node to be heapified.
*/
void Min_Heapify(Node A[], int key) {
int posmin;
Node appoggio;
int l = 2*key;
int r = 2*key + 1;
if ((l <= HeapSize)&&(A[l].dist<A[key].dist))
posmin = l;
else
posmin = key;
if ((r <= HeapSize)&&(A[r].dist<A[posmin].dist))
posmin = r;
if (posmin != key){
appoggio = A[posmin];
A[posmin] = A[key];
A[key] = appoggio;
Min_Heapify(A, posmin);
}
}
/**
* @brief Deletes the minimum node from the heap.
*
* This function deletes the minimum node from the heap and returns it.
*
* @param A Array representing the heap.
* @return The minimum node from the heap.
*/
Node Cancella_Min(Node A[]){
Node max;
if (HeapSize < 1) {
max.dist = -1;
max.index = -1;
return max;
}
max.dist = A[1].dist;
max.index = A[1].index;
A[1] = A[HeapSize];
HeapSize = HeapSize - 1;
Min_Heapify(A, 1);
return max;
}
/**
* @brief Maintains the min-heap property after insertion.
*
* This function maintains the min-heap property by ensuring that the parent node
* is smaller than its child nodes after a new node is inserted.
*
* @param A Array representing the heap.
* @param key Index of the node to be heapified.
*/
void Min_Heapify2(Node A[], int key){
Node appogg;
while ((key > 1)&&(A[key/2].dist>A[key].dist)){
appogg = A[key/2];
A[key/2] = A[key];
A[key] = appogg;
key = key/2;
}
}
/**
* @brief Modifies the heap with a new distance value.
*
* This function modifies the heap by updating the distance value of a node
* and maintaining the heap property.
*
* @param A Array representing the heap.
* @param distance New distance value.
* @param Index Index of the node to be modified.
*/
void ModificaHeap(Node A[], int distance, int Index){
boolean Flag = FALSE;
int i = 1;
while ((i <= HeapSize) && (Flag != TRUE)){
if (A[i].index == Index) {
A[i].dist = distance;
Flag = TRUE;
i++;
} else {
i++;
}
}
i--;
Min_Heapify2(A, i);
}
/**
* @brief Performs Dijkstra's algorithm to find the shortest paths.
*
* This function performs Dijkstra's algorithm to find the shortest paths
* from a source node to all other nodes in the graph.
*
* @param Visited Array indicating whether a node has been visited.
* @param Table 2D array representing the adjacency matrix of the graph.
* @param Distances Array representing the shortest distances from the source node.
* @param A Array representing the heap.
* @return The sum of the shortest distances.
*/
int Djikstra(boolean Visited[], int Table[][Nnodes], int Distances[], Node A[]){
int sum = 0;
Node New;
Node u = Cancella_Min(A);
while (u.dist != -1){
Visited[u.index] = TRUE;
sum = sum + u.dist;
for (int i = 1; i < Nnodes; i++){
if ((Visited[i] != TRUE) && (Distances[i] != 0) && (Table[u.index][i] != 0) && (Distances[i] > u.dist + Table[u.index][i])){
Distances[i] = u.dist + Table[u.index][i];
ModificaHeap(A, Distances[i], i);
} else if ((Distances[i] == 0) && (Table[u.index][i] != 0) && (Visited[i] != TRUE)){
New.index = i;
New.dist = u.dist + Table[u.index][i];
Distances[i] = New.dist;
InsertMAX(A, New);
}
}
u = Cancella_Min(A);
}
return sum;
}
/**
* @brief Fills the adjacency matrix with input data.
*
* This function reads input data and fills the adjacency matrix representing the graph.
*
* @param Table 2D array representing the adjacency matrix of the graph.
*/
void init_table(int Table[][Nnodes]) {
int ritorno, Result;
for (int i = 1; i < Nnodes; i++) {
Result = 0;
ritorno = getchar_unlocked();
while(ritorno != ','){
Result = 10*Result + ritorno - '0';
ritorno = getchar_unlocked();
}
Table[i][0] = Result;
int j = 1;
ritorno = getchar_unlocked();
while(ritorno != 10){
Result = 0;
while (ritorno != ',' && ritorno != 10) {
Result = 10 * Result + ritorno - '0';
ritorno = getchar_unlocked();
}
if (ritorno != 10) {
ritorno = getchar_unlocked();
}
Table[i][j] = Result;
j++;
}
}
}
/**
* @brief Fills the distances array and heap with input data.
*
* This function reads input data and fills the distances array and heap
* representing the initial distances from the source node.
*
* @param Distances Array representing the shortest distances from the source node.
* @param A Array representing the heap.
*/
void init_heaps(int Distances[], Node A[]){
Node a;
int Result = 0;
int ritorno;
ritorno = getchar_unlocked();
while(ritorno != ','){
Result = 10*Result + ritorno - '0';
ritorno = getchar_unlocked();
}
Distances[0] = Result;
int y = 1;
ritorno = getchar_unlocked();
while (ritorno != 10){
Result = 0;
while (ritorno != ',' && ritorno != 10) {
Result = 10 * Result + ritorno - '0';
ritorno = getchar_unlocked();
}
Distances[y] = Result;
if (Distances[y] != 0) {
a.dist = Distances[y];
a.index = y;
InsertMAX(A, a);
}
if (ritorno != 10) {
ritorno = getchar_unlocked();
}
y++;
}
}
/**
* @brief Main function to control the flow of the program.
*
* This function reads input data, processes it, and performs various operations
* including insertion into the Red-Black Tree, rotations, and heap operations.
*
* @return Exit status of the program.
*/
int main() {
int trash;
int sum;
int val_max = 1200000;
int count = 0;
trash = scanf("%d", &Nnodes);
trash = scanf("%d", &K);
trash = scanf("%d", &trash);
boolean Visited[Nnodes];
Visited[0] = TRUE;
int Table[Nnodes][Nnodes];
int Distances[Nnodes];
Node A[Nnodes];
struct node* min;
for (int i = 0; i < Nnodes; i++) {
Table[0][i] = 0;
}
char command[15];
while (NULL != fgets(command, sizeof(command), stdin)){
if (command[0] == 'T'){
ContrStmp = 0;
output_print(root);
printf("\n");
} else {
HeapSize = 0;
for (int i = 1; i < Nnodes; i++){
Visited[i] = FALSE;
}
init_heaps(Distances, A);
init_table(Table);
sum = Djikstra(Visited, Table, Distances, A);
if (K < 20000) {
if (count < K) {
struct node *added = (struct node *) malloc(sizeof(struct node));
added -> r = NULL;
added -> l = NULL;
added -> p = NULL;
added -> Key = sum;
added -> index = count;
added -> color = RED;
root = insert(root, added);
RBinsert_and_repair(root, added);
}
if ((count >= K) && (sum < val_max)) {
struct node *added = (struct node *) malloc(sizeof(struct node));
added -> r = NULL;
added -> l = NULL;
added -> p = NULL;
added -> Key = sum;
added -> index = count;
added -> color = RED;
root = insert(root, added);
RBinsert_and_repair(root, added);
int contatore = 1;
min = min_val(root);
val_max = successor(min, contatore);
}
count++;
if (count == K) {
int contatore = 1;
min = min_val(root);
val_max = successor(min, contatore);
}
} else {
struct node *added = (struct node *) malloc(sizeof(struct node));
added -> r = NULL;
added -> l = NULL;
added -> p = NULL;
added -> Key = sum;
added -> index = count;
added -> color = RED;
root = insert(root, added);
RBinsert_and_repair(root, added);
count++;
}
}
}
return 0;
}