-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriplet_dist.h
205 lines (174 loc) · 6.78 KB
/
triplet_dist.h
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
//
// Created by kostas on 4/21/19.
//
#ifndef QTD_TRIPLET_DIST_H
#define QTD_TRIPLET_DIST_H
#include "common.h"
#include "T1.h"
#include "T2.h"
void relabelT2(T2node* cur, int & curDFSid){
if(cur->children.size() == 0){
cur->minDFSid = cur->maxDFSid = curDFSid;
dfsIDs[cur->id] = curDFSid;
cur->id = curDFSid++;
return;
}
for(T2node* child: cur->children){
relabelT2(child, curDFSid);
}
cur->minDFSid = cur->children[0]->minDFSid;
cur->maxDFSid = cur->children[cur->children.size()-1]->maxDFSid;
}
void prepareT1andC(){
for(int i=0;i<T1size;i++){
T1node* cur = &T1[i];
if(cur->children.size() == 0){
cur->id = dfsIDs[cur->id];
int leafID = cur->id;
cur = cur->parent;
while(cur!= nullptr){
cur->numLeaves++;
C[cur->id - n - 1][leafID] = 1;
cur = cur->parent;
}
}
else {
int lastLeaf = -1;
for (int j = 0; j < cur->children.size(); j++) {
T1node *child = cur->children[j];
if (child->children.size() == 0) {
if (lastLeaf == -1) lastLeaf = j;
}
else{
if(lastLeaf!=-1)
swap(cur->children[lastLeaf++], cur->children[j]);
}
}
}
}
for(int i=0;i<q;i++){
for(int j=1;j<=n;j++){
C[i][j] += C[i][j-1];
}
}
}
void preprocessing(){
/*
*
* build T2 with the labels changed
*
*/
dfsIDs.resize(n+1);
int curDFSid = 1;
relabelT2(&T2[0], curDFSid);
C.resize(q, vector<int>(n + 1, 0));
prepareT1andC();
}
//given a coloring, find the shared triplets, algorithm 4 from the paper
void findSharedTripletsGivenAColoring(T1node * u){
int uID = u->id - n - 1;
if(u->children[0]->children.size() == 0){
//cur has no children that are internal nodes
u->red = C[uID][coloring.redMax] - C[uID][coloring.redMin-1];
u->blue = C[uID][coloring.blueMax] - C[uID][coloring.blueMin-1];
u->green = 0;
if(coloring.greenMax != -1)
u->green = C[uID][coloring.greenMax] - C[uID][coloring.greenMin-1];
u->redI = u->blueI = u->greenI = 0;
u->redL = u->red;
u->blueL = u->blue;
u->greenL = u->green;
u->white = totalWhite - u->numLeaves + u->red + u->blue + u->green;
u->redblue = u->redgreen = u->bluegreen = u->redbluegreen = 0;
//add contribution of fans
sharedTriplets = sharedTriplets + u->redL*u->blueL*u->greenL;
//add contribution of resolved
sharedTriplets = sharedTriplets + u->redL*u->blueL*u->white;
return;
}
//we don't need to explicitly define I, since in the preprocessing step we made sure that every child that is an internal node
//appears first in the list of the children of u.
for(int i=0;i<u->children.size();i++){
if(u->children[i]->children.size() == 0) break;
findSharedTripletsGivenAColoring(u->children[i]);
}
//dynamic programming
u->red = C[uID][coloring.redMax] - C[uID][coloring.redMin-1];
u->blue = C[uID][coloring.blueMax] - C[uID][coloring.blueMin-1];
u->green = 0;
if(coloring.greenMax != -1)
u->green = C[uID][coloring.greenMax] - C[uID][coloring.greenMin-1];
T1node* w = u->children[0];
int wID = w->id - n - 1;
u->redI = w->red;
u->blueI = w->blue;
u->greenI = w->green;
u->redL = u->red - (C[wID][coloring.redMax] - C[wID][coloring.redMin-1]);
u->blueL = u->blue - (C[wID][coloring.blueMax] - C[wID][coloring.blueMin-1]);
u->greenL = u->green;
if(coloring.greenMax!=-1)
u->greenL = u->greenL - (C[wID][coloring.greenMax] - C[wID][coloring.greenMin-1]);
u->white = totalWhite - u->numLeaves + u->red + u->blue + u->green;
u->redblue = u->redgreen = u->bluegreen = u->redbluegreen = 0;
for(int i=1;i<u->children.size();i++){
w = u->children[i];
if(w->children.size() == 0) break;
u->redbluegreen = u->redbluegreen + u->redblue*w->green + u->redgreen*w->blue + u->bluegreen*w->red;
u->redblue = u->redblue + u->redI*w->blue + u->blueI*w->red;
u->redgreen = u->redgreen + u->redI*w->green + u->greenI*w->red;
u->bluegreen = u->bluegreen + u->blueI*w->green + u->greenI*w->blue;
u->redI = u->redI + w->red;
u->blueI = u->blueI + w->blue;
u->greenI = u->greenI + w->green;
wID = w->id - n -1;
u->redL = u->redL - (C[wID][coloring.redMax] - C[wID][coloring.redMin-1]);
u->blueL = u->blueL - (C[wID][coloring.blueMax] - C[wID][coloring.blueMin-1]);
if(coloring.greenMax != -1)
u->greenL = u->greenL - (C[wID][coloring.greenMax] - C[wID][coloring.greenMin-1]);
}
//add contribution of fans (Algorithm 5 in paper)
sharedTriplets = sharedTriplets + u->redbluegreen
+ u->redblue*u->greenL + u->redgreen*u->blueL + u->bluegreen*u->redL
+ u->redI*u->blueL*u->greenL + u->blueI*u->redL*u->greenL + u->greenI*u->redL*u->blueL
+ u->redL*u->blueL*u->greenL;
//add contribution of resolved
sharedTriplets = sharedTriplets + u->redblue*u->white
+ u->redI*u->blueL*u->white + u->blueI*u->redL*u->white
+ u->redL*u->blueL*u->white;
}
ulli computeTripletDistance(){
sharedTriplets = 0;
for(int i=0;i<T2size;i++){
if(i == 0 && T2[i].children.size() == 2) continue; //small optimization if there are only two children, no fans and resolved can be rooted in the root.
T2node * cur = &T2[i];
if(cur->children.size() == 0) continue;
coloring.redMin = cur->children[0]->minDFSid;
for(int j=1;j<cur->children.size();j++){
coloring.redMax = cur->children[j-1]->maxDFSid;
totalWhite = n - (coloring.redMax - coloring.redMin + 1);
T2node* v = cur->children[j];
coloring.blueMin = v->minDFSid;
coloring.blueMax = v->maxDFSid;
totalWhite = totalWhite - (coloring.blueMax - coloring.blueMin + 1);
coloring.greenMax = coloring.greenMin = -1;
if(j+1 < cur->children.size()){
coloring.greenMin = cur->children[j+1]->minDFSid;
coloring.greenMax = cur->children[cur->children.size()-1]->maxDFSid;
totalWhite = totalWhite - (coloring.greenMax - coloring.greenMin + 1);
}
findSharedTripletsGivenAColoring(&T1[0]);
}
}
ulli n1, n2;
n1 = n-1;
n2 = n-2;
if(n%2==0) n = n/2;
else n1 = n1/2;
if(n2%3==0) n2 = n2/3;
else if(n1%3==0) n1 = n1/3;
else n = n/3;
delete[] T1;
delete[] T2;
return n*n1*n2 - sharedTriplets;
}
#endif //QTD_TRIPLET_DIST_H