forked from TsinghuaDatabaseGroup/VTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtree_knn_demo.cpp
1902 lines (1838 loc) · 70.4 KB
/
vtree_knn_demo.cpp
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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <metis.h>
#include <queue>
#include <sys/time.h>
#include <vector>
using namespace std;
const bool DEBUG_ = false;
const char *Edge_File;
const int Partition_Part = 4; // fanout of the vtree
const int Naive_Split_Limit = 33; // number of leaf nodes
bool Save_Order_Matrix = false; // Whether need to support route Matrix(If true can have additional usage)
const int INF = 0x3fffffff;
const bool RevE = false; // Whether need to do some copy, Directed Graph:false,Undirected Graph:true
const bool Distance_Offset = false; // Whether consider the offset of the vehicle.
const bool DEBUG1 = false;
bool NWFIX;
const bool Optimization_KNN_Cut = true; // Optimization for KNN
const bool Memory_Optimization = true; // Optimization for memory (Usually on)
const bool INDEX_Optimization =
true; // Optimization of Index, if need to store more information turn off (usually no need)
struct timeval tv;
long long ts, te;
void TIME_TICK_START() {
gettimeofday(&tv, nullptr);
ts = tv.tv_sec * 1000000 + tv.tv_usec;
}
void TIME_TICK_END() {
gettimeofday(&tv, nullptr);
te = tv.tv_sec * 1000000 + tv.tv_usec;
}
void TIME_TICK_PRINT(const char *T, int N = 1) { printf("%s RESULT: \t%lld\t (us)\r\n", (T), (te - ts) / N); }
void TIME_TICK_ALL_PRINT(const char *T, int N = 1) { printf("%s RESULT: \t%lld\t (us)\r\n", (T), (te - ts)); }
template <typename A, typename B>
istream &operator>>(istream &in, pair<A, B> &p) {
in >> p.first >> p.second;
return in;
}
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
out << p.first << ' ' << p.second << ' ';
return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
out << v.size() << ' ';
for (int i = 0; i < v.size(); i++)
out << v[i] << ' ';
return out;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &v) {
int n;
in >> n;
v.clear();
while (n--) {
T a;
in >> a;
v.push_back(a);
}
return in;
}
template <typename A, typename B>
ostream &operator<<(ostream &out, const map<A, B> &h) {
out << h.size() << ' ';
typename map<A, B>::const_iterator iter;
for (iter = h.begin(); iter != h.end(); iter++)
out << iter->first << ' ' << iter->second << ' ';
return out;
}
template <typename A, typename B>
istream &operator>>(istream &in, map<A, B> &h) {
int n;
in >> n;
h.clear();
A a;
B b;
while (n--) {
in >> a >> b;
h[a] = b;
}
return in;
}
template <typename T>
void save(ostream &out, T a) {
out.write((char *)&a, sizeof(a));
}
template <typename T>
void save(ostream &out, vector<T> &a) {
save(out, (int)a.size());
for (int i = 0; i < a.size(); i++)
save(out, a[i]);
}
template <typename A, typename B>
void save(ostream &out, const pair<A, B> &p) {
save(out, p.first);
save(out, p.second);
}
template <typename A, typename B>
void save(ostream &out, const map<A, B> &h) {
save(out, (int)h.size());
typename map<A, B>::const_iterator iter;
for (iter = h.begin(); iter != h.end(); iter++) {
save(out, iter->first);
save(out, iter->second);
}
}
template <typename T>
void read(istream &in, T &a) {
in.read((char *)&a, sizeof(a));
}
template <typename T>
void read(istream &in, vector<T> &a) {
int n;
read(in, n);
a = vector<T>(n);
for (int i = 0; i < n; i++)
read(in, a[i]);
}
template <typename A, typename B>
void read(istream &in, pair<A, B> &p) {
read(in, p.first);
read(in, p.second);
}
template <typename A, typename B>
void read(istream &in, map<A, B> &h) {
int n;
read(in, n);
h.clear();
A a;
B b;
while (n--) {
read(in, a);
read(in, b);
h[a] = b;
}
}
struct Graph // Struct of the graph
{
int n, m; // n vertices and m edges, the number starts from 0 to n-1
int tot;
vector<int> id; // id[i] is the real number of vertex [i]
vector<int> head, list, next, cost; // Adjacent Table
Graph() { clear(); }
~Graph() { clear(); }
friend ostream &operator<<(ostream &out, const Graph &G) // Storage the structure(stdout)
{
out << G.n << ' ' << G.m << ' ' << G.tot << ' ' << G.id << ' ' << G.head << ' ' << G.list << ' ' << G.next
<< ' ' << G.cost << ' ';
return out;
}
friend istream &operator>>(istream &in, Graph &G) // Read Instruction(stdout)
{
in >> G.n >> G.m >> G.tot >> G.id >> G.head >> G.list >> G.next >> G.cost;
return in;
}
void add_D(int a, int b, int c) // add a edge a->b with weight c (directed)
{
tot++;
list[tot] = b;
cost[tot] = c;
next[tot] = head[a];
head[a] = tot;
}
void add(int a, int b, int c) // add a edge with weight c in undirected graph a<->b
{
add_D(a, b, c);
add_D(b, a, c);
}
void init(int N, int M, int t = 1) {
clear();
n = N;
m = M;
tot = t;
head = vector<int>(N);
id = vector<int>(N);
list = vector<int>(M * 2 + 2);
next = vector<int>(M * 2 + 2);
cost = vector<int>(M * 2 + 2);
}
void clear() {
n = m = tot = 0;
head.clear();
list.clear();
next.clear();
cost.clear();
id.clear();
}
// Graph Partition
vector<int> color; // 01 color vector
vector<int> con; // Whether is connected
vector<int> Split(Graph *G[], int nparts) // Partition the graph to two parts, and stores to G1, G2,2 METIS
// algorithm,npart is the number of parts
{
vector<int> color(n);
int i;
/*if(n<Naive_Split_Limit)
{
return Split_Naive(*G[0],*G[1]);
}*/
if (DEBUG1)
printf("Begin-Split\n");
if (n == nparts) {
for (i = 0; i < n; i++)
color[i] = i;
}
else {
idx_t options[METIS_NOPTIONS];
memset(options, 0, sizeof(options));
{
METIS_SetDefaultOptions(options);
options[METIS_OPTION_PTYPE] = METIS_PTYPE_KWAY; // _RB
options[METIS_OPTION_OBJTYPE] = METIS_OBJTYPE_CUT; // _VOL
options[METIS_OPTION_CTYPE] = METIS_CTYPE_SHEM; // _RM
options[METIS_OPTION_IPTYPE] = METIS_IPTYPE_RANDOM; // _GROW _EDGE _NODE
options[METIS_OPTION_RTYPE] = METIS_RTYPE_FM; // _GREEDY _SEP2SIDED _SEP1SIDED
// options[METIS_OPTION_NCUTS] = 1;
// options[METIS_OPTION_NITER] = 10;
/* balance factor, used to be 500 */
options[METIS_OPTION_UFACTOR] = 500;
// options[METIS_OPTION_MINCONN];
options[METIS_OPTION_CONTIG] = 1;
// options[METIS_OPTION_SEED];
options[METIS_OPTION_NUMBERING] = 0;
// options[METIS_OPTION_DBGLVL] = 0;
}
idx_t nvtxs = n;
idx_t ncon = 1;
// transform
int *xadj = new idx_t[n + 1];
int *adj = new idx_t[n + 1];
int *adjncy = new idx_t[tot - 1];
int *adjwgt = new idx_t[tot - 1];
int *part = new idx_t[n];
int xadj_pos = 1;
int xadj_accum = 0;
int adjncy_pos = 0;
// xadj, adjncy, adjwgt
xadj[0] = 0;
int i = 0;
for (int i = 0; i < n; i++) {
for (int j = head[i]; j; j = next[j]) {
int enid = list[j];
xadj_accum++;
adjncy[adjncy_pos] = enid;
adjwgt[adjncy_pos] = cost[j];
adjncy_pos++;
}
xadj[xadj_pos++] = xadj_accum;
}
// adjust nodes number started by 0
// adjwgt -> 1
for (int i = 0; i < adjncy_pos; i++) {
adjwgt[i] = 1;
}
// nparts
int objval = 0;
// METIS
METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, nullptr, nullptr, adjwgt, &nparts, nullptr, nullptr, options, &objval,
part);
for (int i = 0; i < n; i++)
color[i] = part[i];
delete[] xadj;
delete[] adj;
delete[] adjncy;
delete[] adjwgt;
delete[] part;
}
// Partation
int j;
vector<int> new_id;
vector<int> tot(nparts, 0), m(nparts, 0);
for (i = 0; i < n; i++)
new_id.push_back(tot[color[i]]++);
for (i = 0; i < n; i++)
for (j = head[i]; j; j = next[j])
if (color[list[j]] == color[i])
m[color[i]]++;
for (int t = 0; t < nparts; t++) {
(*G[t]).init(tot[t], m[t]);
for (i = 0; i < n; i++)
if (color[i] == t)
for (j = head[i]; j; j = next[j])
if (color[list[j]] == color[i])
(*G[t]).add_D(new_id[i], new_id[list[j]], cost[j]);
}
for (i = 0; i < tot.size(); i++)
tot[i] = 0;
for (i = 0; i < n; i++)
(*G[color[i]]).id[tot[color[i]]++] = id[i];
if (DEBUG1)
printf("Split_over\n");
return color;
}
} G;
struct Matrix // Distance Matrix
{
Matrix() : n(0), a(nullptr) {}
~Matrix() { clear(); }
int n; // n*n Matrix
int *a;
friend ostream &operator<<(ostream &out, const Matrix &M) {
out << M.n << ' ';
for (int i = 0; i < M.n; i++)
for (int j = 0; j < M.n; j++)
out << M.a[i * M.n + j] << ' ';
return out;
}
friend istream &operator>>(istream &in, Matrix &M) {
in >> M.n;
M.a = new int[M.n * M.n];
for (int i = 0; i < M.n; i++)
for (int j = 0; j < M.n; j++)
in >> M.a[i * M.n + j];
return in;
}
void save_binary(ostream &out) {
save(out, n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
save(out, a[i * n + j]);
}
void read_binary(istream &in) {
read(in, n);
a = new int[n * n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
read(in, a[i * n + j]);
}
void cover(int x) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i * n + j] = x;
}
void init(int N) {
clear();
n = N;
a = new int[n * n];
for (int i = 0; i < n * n; i++)
a[i] = INF;
for (int i = 0; i < n; i++)
a[i * n + i] = 0;
}
void clear() { delete[] a; }
void floyd() // Using floyd to calculate the matrix
{
int i, j, k;
for (k = 0; k < n; k++)
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (a[i * n + j] > a[i * n + k] + a[k * n + j])
a[i * n + j] = a[i * n + k] + a[k * n + j];
}
void floyd(Matrix &order) // Calculate the matrix with floyd algorithm, and record result to order(route found)
{
int i, j, k;
for (k = 0; k < n; k++)
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (a[i * n + j] > a[i * n + k] + a[k * n + j]) {
a[i * n + j] = a[i * n + k] + a[k * n + j];
order.a[i * n + j] = k;
}
}
Matrix &operator=(const Matrix &m) {
if (this != (&m)) {
init(m.n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i * n + j] = m.a[i * n + j];
}
return *this;
}
int *operator[](int x) { return a + x * n; }
};
struct Node
{
Node() { clear(); }
int part; // number of son nodes
int n, father,
deep; // n: the number of subgraph, the id of father node,son[2]left son and right son,deep: current deepth
vector<int> son;
Graph G; // subgraph
vector<int> color; // which subgraph the node belongs to
Matrix dist, order; // border distance matrix ,border pass route with floyd k,order=(-1:connected
// directly)|(-2:connected in parents)|(-3:connected in son nodes)|(-INF:none)
map<int, pair<int, int>>
borders; // first:real id of border ;second:<number of border in borders, corresponding nodes(0~n-1)>
vector<int> border_in_father, border_in_son, border_id, border_id_innode; //
// the number of borders in father node and son node, raw id in graph G, number of border in sub node
vector<int> path_record; // temp record for route
int cache_vertex_id, cache_bound; // the start id stored in cache, the updated bound in cache(bound had updated end)
vector<int> cache_dist;
// cache_dist stores the distance from cache_id to all the borders, only be correct when it small or equal to
// cache_bound
vector<int> border_son_id; // the number of son node which border belongs to
int min_border_dist; // min cached distance of current border in the cache (for KNN cut)
vector<pair<int, int>> min_car_dist; // the nearest car and the node_id to the border <car_dist,node_id>
friend ostream &operator<<(ostream &out, const Node &N) {
out << N.n << ' ' << N.father << ' ' << N.part << ' ' << N.deep << ' ' << N.cache_vertex_id << ' '
<< N.cache_bound << ' ' << N.min_border_dist << ' ';
for (int i = 0; i < N.part; i++)
out << N.son[i] << ' ';
if (INDEX_Optimization) {
out << N.color << ' ' << N.dist << ' ' << N.borders << ' ';
if (Save_Order_Matrix)
out << N.order << ' ';
}
else {
out << N.color << ' ' << N.dist << ' ' << N.borders << ' ' << N.border_in_father << ' ' << N.border_in_son
<< ' ' << N.border_id << ' ' << N.border_id_innode << ' ' << N.path_record << ' ' << N.cache_dist << ' '
<< N.min_car_dist << ' ' << N.border_son_id << ' ';
if (Save_Order_Matrix)
out << N.order << ' ';
}
return out;
}
friend istream &operator>>(istream &in, Node &N) {
in >> N.n >> N.father >> N.part >> N.deep >> N.cache_vertex_id >> N.cache_bound >> N.min_border_dist;
N.son.clear();
N.son = vector<int>(N.part);
for (int i = 0; i < N.part; i++)
in >> N.son[i];
if (INDEX_Optimization) {
in >> N.color >> N.dist >> N.borders;
if (Save_Order_Matrix)
in >> N.order;
}
else {
in >> N.color >> N.dist >> N.borders >> N.border_in_father >> N.border_in_son >> N.border_id >>
N.border_id_innode >> N.path_record >> N.cache_dist >> N.min_car_dist >> N.border_son_id;
if (Save_Order_Matrix)
in >> N.order;
}
return in;
}
void save_binary(ostream &out) {
save(out, n);
save(out, father);
save(out, part);
save(out, deep);
save(out, cache_vertex_id);
save(out, cache_bound);
save(out, min_border_dist);
for (int i = 0; i < part; i++)
save(out, son[i]);
if (INDEX_Optimization) {
save(out, color);
dist.save_binary(out);
if (Save_Order_Matrix)
order.save_binary(out);
save(out, borders);
}
else {
save(out, color);
dist.save_binary(out);
if (Save_Order_Matrix)
order.save_binary(out);
save(out, borders);
save(out, border_in_father);
save(out, border_in_son);
save(out, border_id);
save(out, border_id_innode);
save(out, path_record);
save(out, cache_dist);
save(out, min_car_dist);
save(out, border_son_id);
}
}
void read_binary(istream &in) {
read(in, n);
read(in, father);
read(in, part);
read(in, deep);
read(in, cache_vertex_id);
read(in, cache_bound);
read(in, min_border_dist);
son.clear();
son = vector<int>(part);
for (int i = 0; i < part; i++)
read(in, son[i]);
// printf("read_binary Node n=%d father=%d part=%d deep=%d\n",n,father,part,deep);
if (INDEX_Optimization) {
read(in, color);
dist.read_binary(in);
if (Save_Order_Matrix)
order.read_binary(in);
read(in, borders);
}
else {
read(in, color);
dist.read_binary(in);
if (Save_Order_Matrix)
order.read_binary(in);
read(in, borders);
read(in, border_in_father);
read(in, border_in_son);
read(in, border_id);
read(in, border_id_innode);
read(in, path_record);
read(in, cache_dist);
read(in, min_car_dist);
read(in, border_son_id);
}
}
void init(int n) {
part = n;
son = vector<int>(n);
for (int i = 0; i < n; i++)
son[i] = 0;
}
void clear() {
part = n = father = deep = 0;
son.clear();
dist.clear();
order.clear();
G.clear();
color.clear();
borders.clear();
border_in_father.clear();
border_in_son.clear();
border_id.clear();
border_id_innode.clear();
cache_dist.clear();
cache_vertex_id = -1;
path_record.clear();
cache_dist.clear();
border_son_id.clear();
min_car_dist.clear();
}
void make_border_edge() // update the direct connected edge of border to dist(build_dist1)
{
int i, j;
map<int, pair<int, int>>::iterator iter;
for (iter = borders.begin(); iter != borders.end(); iter++) {
i = iter->second.second;
for (j = G.head[i]; j; j = G.next[j])
if (color[i] != color[G.list[j]]) {
int id1, id2;
id1 = iter->second.first;
id2 = borders[G.id[G.list[j]]].first;
if (dist[id1][id2] > G.cost[j]) {
dist[id1][id2] = G.cost[j];
order[id1][id2] = -1;
}
}
}
}
};
struct G_Tree
{
int root;
vector<int> id_in_node; // which node the vertex belongs to
vector<vector<int>> car_in_node; // Record the vehicles in the vertex.
vector<int> car_offset; // The offset of the vehicle in the vertex.
struct Interface
{
// int cnt1,cnt2;
G_Tree *tree; // point to the G_Tree(var)
int tot, size, id_tot; // tot: up bound of vir subscript, size:size of vector,id_tot up bound of memory
int node_size; // size of vector node
Node *node;
vector<int> id; // id of node in vector id
// sub node information
vector<int> father; // father node of vir node
vector<int> border_in_father; // vir node border_in_father
vector<int> G_id; // real id of the leaf which n=1 in node[0]
void build_node0(int x) // init node[0] with x
{
// cnt2++;
node[0].borders.clear();
node[0].borders[G_id[x]] = make_pair(0, 0);
node[0].border_id[0] = G_id[x];
node[0].border_in_father[0] = border_in_father[x];
node[0].father = father[x];
node[0].deep = node[id[father[x]]].deep + 1;
{
int node_id = G_id[x];
if (tree->car_in_node[node_id].size() > 0)
node[0].min_car_dist[0] = make_pair(0, node_id);
else
node[0].min_car_dist[0] = make_pair(INF, -1);
}
}
Node &operator[](int x) {
// cnt1++;
if (id[x] == 0 && Memory_Optimization)
build_node0(x);
return node[id[x]];
}
friend ostream &operator<<(ostream &out, Interface &I) {
out << I.tot << ' ' << I.size << ' ' << I.id_tot << ' ' << I.node_size << ' ' << Save_Order_Matrix << ' ';
for (int i = 0; i < I.node_size; i++)
out << I.node[i] << ' ';
out << I.id << ' ' << I.father << ' ' << I.border_in_father << ' ' << I.G_id << ' ';
return out;
}
friend istream &operator>>(istream &in, Interface &I) {
in >> I.tot >> I.size >> I.id_tot >> I.node_size >> Save_Order_Matrix;
delete[] I.node;
I.node = new Node[I.node_size];
for (int i = 0; i < I.node_size; i++)
in >> I.node[i];
in >> I.id >> I.father >> I.border_in_father >> I.G_id;
// Initialize node[0]
I.node[0].border_id.clear();
I.node[0].border_id.push_back(0);
I.node[0].border_in_father.clear();
I.node[0].border_in_father.push_back(0);
I.node[0].min_car_dist.clear();
I.node[0].min_car_dist.push_back(make_pair(0, 0));
return in;
}
void save_binary(ostream &out) {
save(out, tot);
save(out, size);
save(out, id_tot);
save(out, node_size);
save(out, Save_Order_Matrix);
for (int i = 0; i < node_size; i++) {
node[i].save_binary(out);
}
save(out, id);
save(out, father);
save(out, border_in_father);
save(out, G_id);
}
void read_binary(istream &in) {
read(in, tot);
read(in, size);
read(in, id_tot);
read(in, node_size);
read(in, Save_Order_Matrix);
delete[] node;
node = new Node[node_size];
for (int i = 0; i < node_size; i++) {
node[i].read_binary(in);
}
read(in, id);
read(in, father);
read(in, border_in_father);
read(in, G_id);
// initialize node[0]
node[0].border_id.clear();
node[0].border_id.push_back(0);
node[0].border_in_father.clear();
node[0].border_in_father.push_back(0);
node[0].min_car_dist.clear();
node[0].min_car_dist.push_back(make_pair(0, 0));
}
~Interface() { delete[] node; }
} node;
friend ostream &operator<<(ostream &out, G_Tree &T) {
out << T.root << ' ' << T.id_in_node << ' ' << T.car_in_node << ' ' << T.car_offset << ' ';
out << T.node << ' ';
return out;
}
friend istream &operator>>(istream &in, G_Tree &T) {
printf("load_begin\n");
fflush(stdout);
in >> T.root >> T.id_in_node >> T.car_in_node >> T.car_offset;
in >> T.node;
T.node.tree = &T;
if (INDEX_Optimization) {
printf("build_border_in_father_son\n");
fflush(stdout);
T.build_border_in_father_son();
}
return in;
}
void save_binary(ostream &out) {
save(out, root);
save(out, id_in_node);
save(out, car_in_node);
save(out, car_offset);
node.save_binary(out);
}
void read_binary(istream &in) {
read(in, root);
read(in, id_in_node);
read(in, car_in_node);
read(in, car_offset);
node.read_binary(in);
node.tree = this;
if (INDEX_Optimization) {
printf("build_border_in_father_son\n");
fflush(stdout);
build_border_in_father_son();
}
}
void add_border(int x, int id, int id2)
// add a real id to the boundary set of x, in vir is id2, its corresponding id of border
{
map<int, pair<int, int>>::iterator iter;
iter = node[x].borders.find(id);
if (iter == node[x].borders.end()) {
pair<int, int> second = make_pair((int)node[x].borders.size(), id2);
node[x].borders[id] = second;
}
}
void make_border(int x, const vector<int> &color) // calculate the set of x
{
for (int i = 0; i < node[x].G.n; i++) {
int id = node[x].G.id[i];
for (int j = node[x].G.head[i]; j; j = node[x].G.next[j])
if (color[i] != color[node[x].G.list[j]]) {
add_border(x, id, i);
break;
}
}
}
void build(int x = 1, int f = 1,
const Graph &g = G) // Build tree,current x,number of subgraphs f,current subgraph g
{
if (x == 1) // x root
{
node.tree = this;
node.size = G.n * 2 + 2;
printf("malloc!\n");
fflush(stdout);
node.node = new Node[node.size];
printf("malloc end!\n");
fflush(stdout);
node.id = vector<int>(node.size);
for (int i = 0; i < node.size; i++)
node.id[i] = i;
node.tot = 2;
root = 1;
node[x].deep = 1;
node[1].G = g;
node.node_size = node.size;
}
else // x not root
{
node[x].deep = node[node[x].father].deep + 1;
}
node[x].n = node[x].G.n;
if (node[x].G.n < Naive_Split_Limit)
node[x].init(node[x].n);
else
node[x].init(Partition_Part);
if (node[x].n > 50)
printf("x=%d deep=%d n=%d ", x, node[x].deep, node[x].G.n);
if (node[x].n > f) {
// id of sub node
int top = node.tot;
for (int i = 0; i < node[x].part; i++) {
node[x].son[i] = top + i;
node[top + i].father = x;
}
node.tot += node[x].part;
// add border between two graph
Graph **graph;
graph = new Graph *[node[x].part];
for (int i = 0; i < node[x].part; i++)
graph[i] = &node[node[x].son[i]].G;
node[x].color = node[x].G.Split(graph, node[x].part);
delete[] graph;
make_border(x, node[x].color);
if (node[x].n > 50)
printf("border=%zu\n", node[x].borders.size());
// give the value of border to subgraph
map<int, pair<int, int>>::iterator iter;
for (iter = node[x].borders.begin(); iter != node[x].borders.end(); iter++) {
// printf("(%d,%d,%d)",iter->first,iter->second.first,iter->second.second);
node[x].color[iter->second.second] = -node[x].color[iter->second.second] - 1;
}
// cout<<endl;
vector<int> tot(node[x].part, 0);
for (int i = 0; i < node[x].n; i++) {
if (node[x].color[i] < 0) {
node[x].color[i] = -node[x].color[i] - 1;
add_border(node[x].son[node[x].color[i]], node[x].G.id[i], tot[node[x].color[i]]);
}
tot[node[x].color[i]]++;
}
// iterate subnode
for (int i = 0; i < node[x].part; i++)
build(node[x].son[i]);
}
else if (node[x].n > 50)
cout << endl;
node[x].dist.init(node[x].borders.size());
node[x].order.init(node[x].borders.size());
node[x].order.cover(-INF);
if (x == 1) // construct dist by root of x
{
for (int i = 1; i < min(1000, node.tot - 1); i++)
if (node[i].n > 50) {
printf("x=%d deep=%d n=%d ", i, node[i].deep, node[i].G.n);
}
printf("begin_build_border_in_father_son\n");
build_border_in_father_son();
printf("begin_build_dist\n");
build_dist1(root);
printf("begin_build_dist2\n");
build_dist2(root);
// calculate the leaf node id of vertex in
id_in_node.clear();
for (int i = 0; i < node[root].G.n; i++)
id_in_node.push_back(-1);
for (int i = 1; i < node.tot; i++)
if (node[i].G.n == 1)
id_in_node[node[i].G.id[0]] = i;
{
// 建立car_in_node;
vector<int> empty_vector;
empty_vector.clear();
car_in_node.clear();
for (int i = 0; i < G.n; i++)
car_in_node.push_back(empty_vector);
}
if (Memory_Optimization)
Memory_Compress();
}
}
void build_dist1(int x = 1) // from down to top merge dist in Graph
{
// Calculate dist in subgraph and set to x
for (int i = 0; i < node[x].part; i++)
if (node[x].son[i])
build_dist1(node[x].son[i]);
if (node[x].son[0]) // not leaf
{
// construct the edge between x subnode
node[x].make_border_edge();
// construct the real dist inside
node[x].dist.floyd(node[x].order);
}
else
; // leaf
// give the inside weight to father
if (node[x].father) {
int y = node[x].father, i, j;
map<int, pair<int, int>>::iterator x_iter1, y_iter1;
vector<int> id_in_fa(node[x].borders.size());
// calculate the id of border in father border, no exist:-1
for (x_iter1 = node[x].borders.begin(); x_iter1 != node[x].borders.end(); x_iter1++) {
y_iter1 = node[y].borders.find(x_iter1->first);
if (y_iter1 == node[y].borders.end())
id_in_fa[x_iter1->second.first] = -1;
else
id_in_fa[x_iter1->second.first] = y_iter1->second.first;
}
// tell the all connection weight to father
for (i = 0; i < (int)node[x].borders.size(); i++)
for (j = 0; j < (int)node[x].borders.size(); j++)
if (id_in_fa[i] != -1 && id_in_fa[j] != -1) {
int *p = &node[y].dist[id_in_fa[i]][id_in_fa[j]];
if ((*p) > node[x].dist[i][j]) {
(*p) = node[x].dist[i][j];
node[y].order[id_in_fa[i]][id_in_fa[j]] = -3;
}
}
}
return;
}
void build_dist2(int x = 1) // Calculate from top to down as in paper
{
if (x != root)
node[x].dist.floyd(node[x].order);
if (node[x].son[0]) {
// calculate the border id in subgraph of current border
vector<int> id_(node[x].borders.size());
vector<int> color_(node[x].borders.size());
map<int, pair<int, int>>::iterator iter1, iter2;
for (iter1 = node[x].borders.begin(); iter1 != node[x].borders.end(); iter1++) {
int c = node[x].color[iter1->second.second];
color_[iter1->second.first] = c;
int y = node[x].son[c];
id_[iter1->second.first] = node[y].borders[iter1->first].first;
}
// Recalculate the subgraph weight
for (int i = 0; i < (int)node[x].borders.size(); i++)
for (int j = 0; j < (int)node[x].borders.size(); j++)
if (color_[i] == color_[j]) {
int y = node[x].son[color_[i]];
int *p = &node[y].dist[id_[i]][id_[j]];
if ((*p) > node[x].dist[i][j]) {
(*p) = node[x].dist[i][j];
node[y].order[id_[i]][id_[j]] = -2;
}
}
// Recursive sub nodes
for (int i = 0; i < node[x].part; i++)
if (node[x].son[i])
build_dist2(node[x].son[i]);
}
}
void build_border_in_father_son()
// calculate the id of border in father/son
{
int i, j, x, y;
// Construct cache
for (x = 1; x < node.tot; x++) {
// printf("x=%d node.id[x]=%d size=%d\n",x,node.id[x],node.node_size);fflush(stdout);
// node[x].write();fflush(stdout);
node[x].cache_dist.clear();
node[x].min_car_dist.clear();
node[x].cache_vertex_id = -1;
for (int j = 0; j < node[x].borders.size(); j++) {
node[x].cache_dist.push_back(0);
node[x].min_car_dist.push_back(make_pair(INF, -1));
}
node[x].border_id.clear();
node[x].border_id_innode.clear();
for (i = 0; i < node[x].borders.size(); i++)
node[x].border_id.push_back(0);
for (i = 0; i < node[x].borders.size(); i++)
node[x].border_id_innode.push_back(0);
for (map<int, pair<int, int>>::iterator iter = node[x].borders.begin(); iter != node[x].borders.end();
iter++) {
node[x].border_id[iter->second.first] = iter->first;
node[x].border_id_innode[iter->second.first] = iter->second.second;
}
node[x].border_in_father.clear();
node[x].border_in_son.clear();
for (i = 0; i < node[x].borders.size(); i++) {
node[x].border_in_father.push_back(-1);
node[x].border_in_son.push_back(-1);
}
if (node[x].father) {
y = node[x].father;
map<int, pair<int, int>>::iterator iter;
for (iter = node[x].borders.begin(); iter != node[x].borders.end(); iter++) {
map<int, pair<int, int>>::iterator iter2;
iter2 = node[y].borders.find(iter->first);
if (iter2 != node[y].borders.end())
node[x].border_in_father[iter->second.first] = iter2->second.first;
}
}
if (node[x].son[0]) {
map<int, pair<int, int>>::iterator iter;
for (iter = node[x].borders.begin(); iter != node[x].borders.end(); iter++) {
y = node[x].son[node[x].color[iter->second.second]];
map<int, pair<int, int>>::iterator iter2;
iter2 = node[y].borders.find(iter->first);
if (iter2 != node[y].borders.end())
node[x].border_in_son[iter->second.first] = iter2->second.first;
}
node[x].border_son_id.clear();
for (int i = 0; i < node[x].borders.size(); i++)
node[x].border_son_id.push_back(node[x].son[node[x].color[node[x].border_id_innode[i]]]);
}
}
}
void Memory_Compress() // Compress the tree in memory make the node with n=1 visual
{
printf("Begin Memory Compress! node_size=%d\n", node.node_size);
int cnt = 0;
for (int i = 0; i < node.tot; i++)
if (i == 0 || node.node[i].n > 1)
cnt++;
Node *new_node = new Node[cnt + 1];
node.node_size = cnt + 1;
node.id_tot = 1;
node.border_in_father = node.G_id = node.father = vector<int>(node.tot);
for (int i = 0; i < node.tot; i++) {
if (node.node[i].n > 1 || i == 0) {
node.id[i] = (node.id_tot++);
new_node[node.id[i]] = node.node[i];
}
else {
// node.node[i].write();
new_node[node.id[i] = 0] = node.node[i];
node.G_id[i] = node.node[i].G.id[0];
node.father[i] = node.node[i].father;
node.border_in_father[i] = node.node[i].border_in_father[0];
}
}
delete[] node.node;
node.node = new_node;
printf("End Memory Compress! node_size=%d\n", node.node_size);
fflush(stdout);
}
void push_borders_up(int x, vector<int> &dist1, int type)