-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUtils.c
2271 lines (1870 loc) · 48.4 KB
/
Utils.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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********
1. prtarr - Print a 1D array
2. display - Print a 2D array
3. topSortKahn - Topsort using Kahn's algorithm
4. detectCycle - Detect cycles using Kahn's algorithm
5. topSort_DFS - Topsorting algorithm using DFS :: utilty function
6. topSort - Topsorting algorithm using DFS
7. swap - Dijkstra's algorithm using binary heaps :: utility
8. getNode - Dijkstra's algorithm using binary heaps :: utility
9. heapifyPair - Dijkstra's algorithm using binary heaps :: utility
10. dijkstraHeap - Dijkstra's algorithm using binary heaps :: utility
11. a2dp - Convert a 2D array to a double pointer
12. addDummy - Johnson's algorithm :: utility
13. findMin - Dijkstra's algorithm using linear search :: utility
14. dijkstraLinear - Dijkstra's algorithm using linear search
15. min - Find minimum of two numbers
16. bellmanFord - Bellman-Ford algorithm
17. johnson - Johnson's algorithm
18. fillArray - Fill a 2D array with a given value
19. createSparseTable - Create a sparse table for range queries
20. rangeQueryOverlap - Range queries for overlapping associative functions
21. rangeQueryCascading - Range queries for non-overlapping associative functions
22. addChild - Add a child to a node :: utility
23. buildTree - rootTree :: utility
24. rootTree - Root a tree at a given index :: utility
25. createTree - Convert a 2D array to a tree by finding it's centers
26. eulerianTour - Create an eulerian tour from a given tree
27. cmpfunc - A comparator function for qsort :: utility
28. findEncodingUtils - Generate a AHU Encoding for a given tree taking a 2D array as input :: utils
29. findEncoding - Generate a AHU Encoding for a given tree taking a 2D array as input
30. lca - Calculate the lowest common ancestor using Eulerian tour and range minimum query
31. revAdjacency - Reverse a given adjacency matrix :: utility
32. kosarajuUtility - Kosaraju's algorithm for finding strongly connected components :: utility
33. scckosaraju - Kosaraju's algorithm for finding strongly connected components
34. tarjandfs - Tarjan's algorithm for finding strongly connected components :: utility
35. tarjanUtility - Tarjan's algorithm for finding strongly connected components :: utility
36. sccTarjan - Tarjan's algorithm for finding stongly connected components
37. find - Find method for Union-Find data structure
38. uni - Union method for Union-Find data structure
39. lbit - Calculate the value of the least significant bit
40. calpfx - Calculate the prefix sum from a fenwick tree :: utility
41. pointUpdate - Point Update method for a fenwick tree :: utility
42. rangeCascading - Calculate the 'sum' range query from a fenwick tree :: utility
43. createFTree - Construct a Fenwick tree for a given array
44. max - Calculate the maximum of two numbers
45. findDepth - find the depth of a tree :: utility
46. createParentTable - Create parent table for a given tree for Binary Uplifting algorithm :: utility
47. binaryLifting - Binary Lifting algorithm for finding the lowest common ancestor of two given nodes
48. fillSegmentTree - Fill a segment Tree recursively :: utility
49. createSegmentTree - create a segment tree from a given array
51. intersects - Check if two bounds intersect each other :: utility
52. sumQuery - Find the range sum query in a segment tree :: utility
53. updateQuery - Update an index of an array and reflect that in the segment tree
54. rangeQuerySegment - Find the range sum query in a segment tree
55. fordFulklersonDFS - Find all augmenting paths using DFS in a residual graph coupled with a delta for capacity scaling :: utility
56. findDelta - Find the value of delta for capacity scaling
57. fordFulklerson - Find the maximum flow using Ford Fulkerson algorithm
58. edmondsKarpBFS - Find all augmenting paths using BFS in a residual graph coupled with a delta for capacity scaling :: utility
59. edmondsKarp - Find the maximum flow using Edmonds Karp algorithm
60. dinicDFS - Find an augmenting path in a level graph :: utility
61. dinicBFS - create a level graph coupled with capacity scaling:: utility
62. dinic - Find the maximum flow using Dinic's algorithm
63. findNext - Find the next node with an excess flow (if there exists one) :: utility
64. pushRelabel - Find the maximum flow with the Push-Relabel algorithm
65. augmentMatching - Augment the current matching with the augmenting path provided in Hopcroft Karp's algorithm:: utility
66. createLevelGraph - Create an alternating Breadth-First-Search tree :: utility
67. findPath - find an augmenting path from the BFS tree :: utility
68. hopcroftKarp - Return a maximum matching of a given bipartite graph
69. comparator - comparator function for comparing two nodes in descending order according to it's degrees :: utility
70. welshPowell - Return the chromatic number of an undirected graph given the graph in the form of an adjacency matrix
71. getTrieNode - Initialise and return a trie node : utility
72. setFailureLink - Initialise the failure link of a trie node with the given link :: utility
73. setDictionaryLink - Initialise the dictionary link of a trie node with the given link :: utility
74. setEnd - Initialise the is_end flag of a trie node to decalre it as a vocabulary word:: utility
75. setWord - Initialise the vocab word of a trie node whose is_end flag is true:: utility
76. hasLink - Returns if a given trie node has a child at a particular position :: utility
77. substr - Implements the substring function of C++ :: utility
78. addVocab - Adds the given vocabulary word to the trie tree :: utility
79. getWord - Returns the node containing the end character of the given word if it exists and null if it doesn't :: utility
80. hasWord - Checks if the trie has the given word :: utility
81. addFailureLinks - Adds failure links to the given trie tree :: utility
82. addDictionaryLinks - Adds dictionary links to the given trie tree :: utility
83. create_trie_aho_corasick - Creates the AHO - CORASICK automaton
84. getHashCode - Returns the hash value of a string :: utility
85.search_for_words - Searches the AHO - CORASICK automaton for the occurance of vocabulary words
86. knuth_morris_pratt - Returns the indices of the text string where the pattern is found using the Knutt-Morris-Pratt algorithm
87. calculate_z_value - Returns an array containing z-values of a string
88. z_algorithm - Returns the indices of the text string where the pattern is found using the Z-algorithm
89. main - Main!!
**********/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "stdbool.h"
#include "time.h"
#include "math.h"
#define N 100
#define INF 9999999
#define and &&
#define or ||
#define none NULL
struct Stack{
int arr[N];
int top;
};
typedef struct Stack S;
struct queue{
int arr[N];
int front, rear;
};
typedef struct queue q;
bool isEmpty(q* que)
{
return (que->front > que->rear or que->front == -1)?true:false;
}
void push(q* que, int elem)
{
if(que->rear == -1)
que->front = 0;
que->arr[++que->rear] = elem;
}
int dequeue(q* que)
{
return que->arr[que->front++];
}
struct p{
int dist;
int node;
};
typedef struct p pair;
void prtarr(int* arr, int V)
{
int j;
for(j = 0; j < V; j++)
printf("%d ", arr[j]);
printf("\n");
}
void display(int** arr, int V)
{
int i, j;
for(i = 0; i < V; i++)
{
for(j = 0; j < V; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
}
int* topSortKahn(int** adj, int V)
{
int i,j;
int in[V];
int* ordering;
ordering = (int*)malloc(sizeof(int) * V);
memset(in, 0, sizeof(in));
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
if(adj[i][j] < INF and adj[i][j] > 0)
in[j] += 1;
q* que = (q*)malloc(sizeof(q));
que->front = -1;
que->rear = -1;
for(i = 0; i < V; i++)
if(in[i] == 0)
push(que, i);
j = 0;
while(!isEmpty(que))
{
int node = dequeue(que);
ordering[j++] = node;
for(i = 0; i < V; i++)
if(adj[node][i] < INF and adj[node][i] > 0)
{
in[i] -= 1;
if(in[i] == 0)
push(que, i);
}
}
return ordering;
}
bool detectCycle(int** adj, int V)
{
int i,j;
int in[V];
memset(in, 0, sizeof(in));
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
if(adj[i][j] < INF and adj[i][j] > 0)
in[j] += 1;
q* que = (q*)malloc(sizeof(q));
que->front = -1;
que->rear = -1;
for(i = 0; i < V; i++)
if(in[i] == 0)
push(que, i);
j = 0;
while(!isEmpty(que))
{
int node = dequeue(que);
j++;
for(i = 0; i < V; i++)
if(adj[node][i] < INF and adj[node][i] > 0)
{
in[i] -= 1;
if(in[i] == 0)
push(que, i);
}
}
return !(j == V);
}
void topSort_DFS(int** adj, bool* visited, int* ordering, int* idx, int node, int V)
{
int i;
visited[node] = true;
for(i = 0; i < V; i++)
if(adj[node][i] < INF and !visited[i])
topSort_DFS(adj, visited, ordering, idx, i, V);
ordering[*idx] = node;
*idx -= 1;
}
int* topSort(int** adj, int V)
{
int i;
int idx = V - 1;
bool* visited;
visited = (bool*)malloc(sizeof(bool) * V);
for(i = 0; i < V; i++)
visited[i] = false;
int* ordering;
ordering = (int*)malloc(sizeof(int) * V);
while(idx > -1)
{
int node = rand() % V;
if(!visited[node])
topSort_DFS(adj, visited, ordering, &idx, node, V);
}
return ordering;
}
void swap(pair* x, pair* y)
{
pair t;
t = *x;
*x = *y;
*y = t;
}
pair* getNode(int distance, int location)
{
pair* xy = (pair*)malloc(sizeof(pair));
xy->dist = distance;
xy->node = location;
return xy;
}
void heapifyPair(pair* arr[N], bool visited[N], int V, int i)
{
int left = 2 * i + 1;
int right = 2 * i + 2;
if(left < V)
heapifyPair(arr, visited, V, left);
if(right < V)
heapifyPair(arr, visited, V, right);
if(left < V and !visited[arr[left]->node] and arr[left]->dist < arr[i]->dist)
swap(arr[left], arr[i]);
if(right < V and !visited[arr[right]->node] and arr[right]->dist < arr[i]->dist)
swap(arr[right], arr[i]);
}
int* dijkstraHeap(int** adj, int V, int source)
{
int i, counter = 0, top = -1;
bool* visited;
visited = (bool*)malloc(sizeof(bool)*V);
for(i = 0; i < V; i++)
visited[i] = false;
pair* arr[N];
int* distance;
distance = (int*)malloc(sizeof(int)*V);
for(i = 0; i < V; i++)
distance[i] = INF;
distance[source] = 0;
for(i = 0; i < V; i++)
arr[++top] = getNode(distance[i], i);
while(counter < V)
{
heapifyPair(arr, visited, top + 1, counter);
int location = arr[counter]->node;
visited[location] = true;
for(i = 0; i < V; i++)
{
if(adj[location][i] < INF and distance[i] > distance[location] + adj[location][i])
{
distance[i] = distance[location] + adj[location][i];
arr[++top] = getNode(distance[i], i);
}
}
counter++;
}
return distance;
}
int** a2dp(int adj[N][N], int V)
{
int i, j;
int** adj_;
adj_ = (int**)malloc(sizeof(int*) * V);
for(i = 0; i < V; i++)
adj_[i] = (int*)malloc(sizeof(int) * V);
for(i = 0; i < V; i++)
{
for(j = 0; j < V; j++)
{
adj_[i][j] = adj[i][j];
}
}
return adj_;
}
int** addDummy(int** adj, int n)
{
int i, j;
int** adj_;
adj_ = (int**)malloc(sizeof(int*) * (n + 1));
for(i = 0; i < n + 1; i++)
adj_[i] = (int*)malloc(sizeof(int) * (n + 1));
for(i = 0; i < n + 1; i++)
adj_[0][i] = 0;
for(i = 1; i < n + 1; i++)
adj_[i][0] = INF;
for(i = 1; i < n + 1; i++)
for(j = 1; j < n + 1; j++)
adj_[i][j] = adj[i -1][j - 1];
return adj_;
}
int findMin(int* arr, bool* visited, int n)
{
int m = INF;
int i, location;
for(i = 0; i < n; i++)
{
if(!visited[i] and m > arr[i])
{
m = arr[i];
location = i;
}
}
return location;
}
int* dijkstraLinear(int** adj, int V, int source)
{
int i, counter = 0;
bool* visited;
visited = (bool*)malloc(sizeof(bool)*V);
for(i = 0; i < V; i++)
visited[i] = false;
int* distance;
distance = (int*)malloc(sizeof(int)*V);
for(i = 0; i < V; i++)
distance[i] = INF;
distance[source] = 0;
while(counter < V)
{
int location = findMin(distance, visited, V);
visited[location] = true;
for(i = 0; i < V; i++)
{
if(adj[location][i] < INF and distance[i] > distance[location] + adj[location][i])
distance[i] = distance[location] + adj[location][i];
}
counter++;
}
return distance;
}
int min(int a, int b)
{
return (a>b)?b:a;
}
int* bellmanFord(int** adj, int V, int source)
{
int i, j, k;
int* distance;
distance = (int*)malloc(sizeof(int)*V);
for(i = 0; i < V; i++)
distance[i] = INF;
distance[source] = 0;
for(i = 1; i < V; i++)
{
for(j = 0; j < V; j++)
{
int minDist = INF;
for(k = 0; k < V; k++)
if(adj[k][j] < INF)
minDist = min(minDist, distance[k] + adj[k][j]);
distance[j] = min(distance[j], minDist);
}
}
return distance;
}
int** johnson(int** adj, int V)
{
int i, j;
int** adj_;
int** apsp;
apsp =(int**)malloc(sizeof(int*) * V);
for(i = 0; i < V; i++)
apsp[i] = (int*)malloc(sizeof(int) * V);
adj_ = addDummy(adj, V);
int* distance;
distance = bellmanFord(adj_, V + 1, 0);
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
if(adj[i][j] < INF)
adj[i][j] += distance[i + 1] - distance[j + 1];
for(i = 0; i < V; i++)
apsp[i] = dijkstraHeap(adj, V, i);
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
if(apsp[i][j] < INF)
apsp[i][j] += distance[j + 1] - distance[i + 1];
display(apsp, V);
return apsp;
}
void fillArray(int** arr, int R, int C, int fill)
{
int i,j;
for(i = 0; i < R; i++)
for(j = 0; j < C; j++)
arr[i][j] = fill;
}
struct sparse_index{
int** stbl;
int** idxtbl;
};
typedef struct sparse_index sidx;
sidx* createSparseTable(int* arr, int S, int (*func)(int, int))
{
int i, j;
int p = log(S) / log(2);
int** stbl;
stbl = (int**)malloc(sizeof(int*) * (p + 1));
for(i = 0; i <= p; i++)
stbl[i] = (int*)malloc(sizeof(int) * S);
int** idxtbl;
idxtbl = (int**)malloc(sizeof(int*) * (p + 1));
for(i = 0; i <= p; i++)
idxtbl[i] = (int*)malloc(sizeof(int) * S);
fillArray(stbl, p + 1, S, INF);
fillArray(idxtbl, p + 1, S, -1);
for(i = 0; i < S; i++)
{
stbl[0][i] = arr[i];
idxtbl[0][i] = i;
}
for(i = 1; i <= p; i++)
for(j = 0; j + (1 << i) <= S; j++)
{
int left = stbl[i-1][j];
int right = stbl[i -1][j + (1 << (i - 1))];
stbl[i][j] = func(left, right);
if(left <= right)
idxtbl[i][j] = idxtbl[i-1][j];
else
idxtbl[i][j] = idxtbl[i -1][j + (1 << (i - 1))];
}
sidx* sdx = (sidx*)malloc(sizeof(sidx));
sdx->idxtbl = idxtbl;
sdx->stbl = stbl;
return sdx;
}
int rangeQueryOverlap(int* arr, int S, int (*func)(int, int), int lower, int upper, bool getIndex)
{
int index;
int range = upper - lower + 1;
int p = log(range) / log(2);
int** stbl;
int** idxtbl;
sidx* sdx = createSparseTable(arr, S, func);
stbl = sdx->stbl;
idxtbl = sdx->idxtbl;
int left = stbl[p][lower];
int right = stbl[p][upper + 1 - (1 << p)];
if(left <= right)
index = idxtbl[p][lower];
else
index = idxtbl[p][upper + 1 - (1 << p)];
return !getIndex? func(left, right): index;
}
int rangeQueryCascading(int* arr, int S, int (*func)(int, int), int lower, int upper)
{
int i;
int range = upper - lower + 1;
int p = log(range) / log(2);
int** stbl;
int** idxtbl;
sidx* sdx = createSparseTable(arr, S, func);
stbl = sdx->stbl;
idxtbl = sdx->idxtbl;
int res = stbl[p][lower];
int last = lower + (1 << p);
for(i = p - 1; i >= 0; i--)
if(range & (1 << i))
{
res = func(res, stbl[i][last]);
last += (1 << i);
}
return res;
}
struct treeNode{
int idx;
struct treeNode* parent;
struct treeNode* children[N];
int child;
char str[N];
};
typedef struct treeNode tn;
void addChild(tn* node, int idx)
{
tn* n = (tn*)malloc(sizeof(tn));
n->idx = idx;
n->child = 0;
n->parent = node;
node->children[node->child++] = n;
}
void buildTree(int** adj, int V, tn* node)
{
int i;
for(i = 0; i < V; i++)
if(adj[node->idx][i])
{
if(node->parent == NULL or i != node->parent->idx)
{
addChild(node, i);
buildTree(adj, V, node->children[node->child - 1]);
}
}
}
tn* rootTree(int** adj, int V, int node)
{
tn* root = (tn*)malloc(sizeof(tn));
root->idx = node;
root->child = 0;
root->parent = NULL;
buildTree(adj, V, root);
return root;
}
tn* createTree(int** adj, int V, int choice)
{
int i, j;
q* leaves = (q*)malloc(sizeof(q));
memset(leaves->arr, -1, sizeof(leaves->arr));
leaves->front = leaves->rear = -1;
int* isLeaf = (int*)malloc(V * sizeof(int));
memset(isLeaf, 0, V * sizeof(int));
int* in = (int*)malloc(V * sizeof(int));
memset(in, 0, V * sizeof(int));
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
if(adj[i][j] == 1)
in[j] += 1;
for(i = 0; i < V; i++)
if(in[i] < 2)
{
isLeaf[i] = 1;
push(leaves, i);
}
j=0;
while(j < V - 2)
{
int leaf = dequeue(leaves);
j++;
for(i = 0; i < V; i++)
if(adj[leaf][i] == 1 and !isLeaf[i])
{
in[i] -= 1;
if(in[i] < 2)
push(leaves, i);
}
}
return (choice == 0 or leaves->arr[leaves->front + 1] == -1)? rootTree(adj, V, leaves->arr[leaves->front]): rootTree(adj, V, leaves->arr[leaves->front + 1]);
}
void eulerianTour(tn* node, int* tour ,int* idx, int* depth, int level, int* last)
{
tour[*idx] = node->idx;
depth[*idx] = level;
*idx += 1;
last[node->idx] = *idx - 1;
int i;
for(i = 0; i < node->child; i++)
{
eulerianTour(node->children[i], tour, idx, depth, level + 1, last);
tour[*idx] = node->idx;
depth[*idx] = level;
*idx += 1;
last[node->idx] = *idx - 1;
}
}
int cmpfunc(const void* str1, const void* str2)
{
const char** s1 = (const char**)str1;
const char** s2 = (const char**)str2;
return strlen(*s2) - strlen(*s1);
}
void findEncodingUtils(tn* node)
{
if(node->child == 0)
strcpy(node->str, "()");
else
{
int i;
char* substr[node->child];
for(i = 0; i < node->child; i++)
{
findEncodingUtils(node->children[i]);
substr[i] = node->children[i]->str;
}
qsort(substr, node->child, sizeof(char*), cmpfunc);
for(i = 1; i < node->child; i++)
strcat(substr[0], substr[i]);
char tmp[N] = "(";
strcat(tmp, substr[0]);
strcat(tmp, ")");
strcpy(node->str, tmp);
}
}
char* findEncoding(int** adj, int V, int choice)
{
tn* root = createTree(adj, V, choice);
findEncodingUtils(root);
return root->str;
}
int lca(int a, int b, int** adj, int V, int choice)
{
int depth[N];
int tour[N];
int idx = 0;
int last[N];
tn* root = createTree(adj, V, choice);
eulerianTour(root, tour, &idx, depth, 0, last);
int lower = (last[a] > last[b])? last[b]: last[a];
int upper = (last[a] > last[b])? last[a]: last[b];
int res = rangeQueryOverlap(depth, idx, min, lower, upper, true);
return tour[res];
}
int** revAdjacency(int** adj, int V)
{
int i, j;
int** radj;
radj = (int**)malloc(sizeof(int*) * V);
for(i = 0; i < V; i++)
radj[i] = (int*)malloc(sizeof(int) * V);
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
radj[j][i] = adj[i][j];
return radj;
}
void kosarajuUtility(bool* visited, int** adj, int V, int* who, int node)
{
int i;
visited[node] = true;
for(i = 0; i < V; i++)
if(adj[node][i] < INF and !visited[i])
{
who[i] = who[node];
kosarajuUtility(visited, adj, V, who, i);
}
}
int* sccKosaraju(int** adj, int V)
{
int i;
int* ordering;
ordering = topSort(adj, V);
int** radj;
radj = revAdjacency(adj, V);
bool* visited;
visited = (bool*)malloc(sizeof(bool) * V);
for(i = 0; i < V; i++)
visited[i] = false;
int* who;
who = (int*)malloc(sizeof(int) * V);
memset(who, -1, sizeof(int) * V);
for(i = 0; i < V; i++)
if(!visited[ordering[i]])
{
who[ordering[i]] = ordering[i];
kosarajuUtility(visited, radj, V, who, ordering[i]);
}
return who;
}
void tarjandfs(int** adj, bool* visited, int V, int node, int* ordering, int* counter)
{
visited[node] = true;
ordering[*counter] = node;
*counter += 1;
int i;
for(i = 0; i < V; i++)
if(!visited[i] and adj[node][i] < INF)
tarjandfs(adj, visited, V, i, ordering, counter);
}
void tarjanUtility(S* stk, int** adj, int V, bool* visited, int* onStack, int* lowLink, int node, int* counter)
{
int i, tmp;
tmp = *counter;
stk->arr[++stk->top] = node;
visited[node] = true;
onStack[node] = 1;
lowLink[node] = tmp;
*counter += 1;
for(i = 0; i < V; i++)
if(adj[node][i] < INF)
{
if(!visited[i])
tarjanUtility(stk, adj, V, visited, onStack, lowLink, i, counter);
if(onStack[i])
lowLink[node] = min(lowLink[node], lowLink[i]);
}
if(lowLink[node] == tmp)
while(stk->top != -1)
{
int pop = stk->arr[stk->top--];
onStack[pop] = 0;
if(pop == node)
break;
lowLink[pop] = tmp;
}
}
int* sccTarjan(int** adj, int V)
{
int i, counter = 0;
int* ordering;
ordering = (int*)malloc(sizeof(int) * V);
int* who;
who = (int*)malloc(sizeof(int) * V);
S* stk = (S*)malloc(sizeof(S));
memset(stk->arr, 0, sizeof(stk->arr));
stk->top = -1;
bool* visited;
visited = (bool*)malloc(sizeof(bool) * V);
for(i = 0; i < V; i++)
visited[i] = false;
int* onStack;
onStack = (int*)malloc(sizeof(int) * V);
int* lowLink;
lowLink = (int*)malloc(sizeof(int) * V);
for(i = 0; i < V; i++)
if(!visited[i])
tarjandfs(adj, visited, V, i, ordering, &counter);
counter = 0;
for(i = 0; i < V; i++)
visited[i] = false;
for(i = 0; i < V; i++)
if(!visited[i])
tarjanUtility(stk, adj, V, visited, onStack, lowLink, i, &counter);
for(i = 0; i < V; i++)
who[i] = ordering[lowLink[i]];
return who;
}
S* find(int* unionFind, int a)
{
S* stk = (S*)malloc(sizeof(S));
stk->top = -1;
stk->arr[++stk->top] = a;
int tmpr = unionFind[a];
if(unionFind[tmpr] == tmpr)
{
stk->arr[++stk->top] = tmpr;
return stk;
}
while(unionFind[tmpr] != tmpr)
{
stk->arr[++stk->top] = tmpr;
tmpr = unionFind[tmpr];
}
return stk;
}
void uni(int* unionFind, int a, int b, int* countCmp)
{
S* ra = find(unionFind, a);
S* rb = find(unionFind, b);
if(ra->arr[ra->top] != rb->arr[rb->top])
{
if(ra->top <= rb->top)
while(ra->top != -1)
{
int pop = ra->arr[ra->top--];
unionFind[pop] = rb->arr[rb->top];
}
else
while(rb->top != -1)
{
int pop = rb->arr[rb->top--];
unionFind[pop] = ra->arr[ra->top];
}
*countCmp -= 1;
}
}
int lbit(int x)
{
return x & -x;
}
int calpfx(int* ftree, int x)
{
int sum = 0;
while(x != 0)
{
sum += ftree[x];
x -= lbit(x);
}
return sum;
}
void pointUpdate(int* ftree, int S, int idx, int x)
{
while(idx <= S)
{
ftree[idx] += x;
idx += lbit(idx);
}
}