-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharithmeticity.cpp
354 lines (286 loc) · 10.9 KB
/
arithmeticity.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
/*
Copyright (C) 2013-2017
Rafael Guglielmetti, [email protected]
*/
/*
This file is part of CoxIter.
CoxIter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
CoxIter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CoxIter. If not, see <http://www.gnu.org/licenses/>.
*/
#include "arithmeticity.h"
Arithmeticity::Arithmeticity()
: verticesCount(0), notArithmetic(false), ci(0), listCycles(false) {}
Arithmeticity::~Arithmeticity() {}
void Arithmeticity::test(CoxIter &ci_, const bool &listCycles_) {
ci = &ci_;
listCycles = listCycles_;
allCycles.clear();
if (ci->get_isCocompact() !=
0) // If the graph is cocompact (1) or if we don't know (-1)
{
error = "GROUP COCOMPACTNESS";
ci->set_isArithmetic(-1);
return;
}
size_t i, j;
coxeterMatrix = ci->get_coxeterMatrix();
verticesCount = ci->get_verticesCount();
referencesToLabels.clear();
notArithmetic = false;
// ------------------------------------------------------
// Cycles consisting of two elements
bool isSqrt2(false), isSqrt3(false);
for (i = 0; i < verticesCount; i++) {
referencesToLabels.push_back(i);
for (j = 0; j < i; j++) {
if (i == j)
continue;
if (coxeterMatrix[i][j] != 1 && coxeterMatrix[i][j] != 0 &&
coxeterMatrix[i][j] != 2 && coxeterMatrix[i][j] != 3 &&
coxeterMatrix[i][j] != 4 && coxeterMatrix[i][j] != 6) {
if (ci->get_debug())
cout << "\tNot arithmetic: 2*G(" << ci->get_vertexLabel(i) << ","
<< ci->get_vertexLabel(j) << ") = pi/" << coxeterMatrix[i][j]
<< endl;
ci->set_isArithmetic(0);
return;
}
if (coxeterMatrix[i][j] == 4)
isSqrt2 = true;
else if (coxeterMatrix[i][j] == 6)
isSqrt3 = true;
else if (coxeterMatrix[i][j] == 1) {
string cycle("4 * " + string("l") + to_string(j) + "m" + to_string(i) +
"^2");
auto it(lower_bound(allCycles.begin(), allCycles.end(), cycle));
if (it == allCycles.end() || *it != cycle)
allCycles.insert(it, cycle);
}
}
}
// ------------------------------------------------------
// Here, we know that m_{ij} \in {2,3,4,6,infty}
// If true, the group is arithmetic
if (!isSqrt2 && !isSqrt3 && !ci->get_hasDottedLine()) {
ci->set_isArithmetic(1);
return;
}
while (collapseQueues() && verticesCount)
;
if (verticesCount <= 2) {
ci->set_isArithmetic(1);
return;
}
testCycles();
return;
}
unsigned int Arithmeticity::collapseQueues() {
vector<unsigned int> verticesToRemove;
vector<unsigned int> neighboursCount(
verticesCount, 0); // For each vertex, number of neighbours
unsigned int i, j, k;
unsigned int previousVertex, currrentVertex;
// ------------------------------------------------------
// We count the number of neighbours of each vertex
for (i = 0; i < verticesCount; i++) {
for (j = 0; j < verticesCount; j++) {
if (coxeterMatrix[i][j] != 2)
neighboursCount[i]++;
}
}
// ------------------------------------------------------
// We determine wich vertices we have to remove
for (i = 0; i < verticesCount; i++) {
if (neighboursCount[i] == 1) // If this is a queue
{
verticesToRemove.push_back(i);
// vertex next to the queue
for (currrentVertex = 0; currrentVertex < verticesCount;
currrentVertex++) {
if (coxeterMatrix[i][currrentVertex] != 2 &&
neighboursCount[currrentVertex] == 2) {
verticesToRemove.push_back(currrentVertex);
break;
}
}
if (currrentVertex ==
verticesCount) // If we cannot remove more than one vertex
continue;
previousVertex = i;
// Here: i ------- iCurrentVertex
// We continue the path
while (true) {
for (k = 0; k < verticesCount; k++) {
if (coxeterMatrix[currrentVertex][k] != 2 &&
neighboursCount[k] == 2 && k != previousVertex) {
verticesToRemove.push_back(k);
previousVertex = currrentVertex;
currrentVertex = k;
break;
}
}
if (k == verticesCount) // The end of the path
break;
}
}
}
// ------------------------------------------------------
// We remove the vertices
sort(verticesToRemove.begin(), verticesToRemove.end());
verticesToRemove = vector<unsigned int>(
verticesToRemove.begin(),
unique(verticesToRemove.begin(), verticesToRemove.end()));
if (verticesToRemove.size() == verticesCount) // If we remove all the vertices
{
coxeterMatrix = vector<vector<unsigned int>>(0, vector<unsigned int>(0));
verticesCount = 0;
return verticesToRemove.size();
}
reverse(verticesToRemove.begin(), verticesToRemove.end());
for (vector<unsigned int>::const_iterator it(verticesToRemove.begin());
it != verticesToRemove.end(); ++it) {
coxeterMatrix.erase(coxeterMatrix.begin() + *it);
referencesToLabels.erase(referencesToLabels.begin() + *it);
for (vector<vector<unsigned int>>::iterator itRow(coxeterMatrix.begin());
itRow != coxeterMatrix.end(); ++itRow)
itRow->erase(itRow->begin() + *it);
}
verticesCount -= verticesToRemove.size();
return verticesToRemove.size();
}
void Arithmeticity::testCycles() {
for (unsigned int i(0); i < verticesCount; i++) {
path.clear();
visitedVertices = vector<bool>(verticesCount, false);
visitedEdges =
vector<vector<bool>>(verticesCount, vector<bool>(verticesCount, false));
findCycles(i, i);
if (notArithmetic) {
ci->set_isArithmetic(0);
return;
}
}
if (!ci->get_hasDottedLine())
ci->set_isArithmetic(1);
else
ci->set_isArithmetic(-1);
}
void Arithmeticity::findCycles(const unsigned int &root,
const unsigned int &from) {
path.push_back(root); // We add the vertex to the path
for (unsigned int i(path[0]); i < verticesCount; i++) {
// If i is a neighbour and if we did not visit this edge
if (coxeterMatrix[root][i] != 2 && !visitedEdges[root][i]) {
if (i == path[0]) {
if (path[1] <
path[path.size() - 1]) // We do not to test each cycle twice
testCycle();
if (notArithmetic) {
if (ci->get_debug()) {
cout << "\tNot arithmetic\n\t\tCycle: ";
for (vector<unsigned int>::const_iterator it(path.begin());
it != path.end();
++it) // We display the components of the cycle
cout << (it == path.begin() ? "" : ", ")
<< ci->get_vertexLabel(referencesToLabels[*it]);
cout << endl;
}
return;
}
} else if (find(path.begin(), path.end(), i) == path.end()) {
visitedEdges[root][i] = visitedEdges[i][root] = true;
findCycles(i, root);
if (notArithmetic)
return;
}
}
}
if (from != root)
visitedEdges[root][from] = visitedEdges[from][root] = false;
path.pop_back();
}
void Arithmeticity::testCycle() {
unsigned int pathSize(path.size());
if (!listCycles) {
bool isSqrt2CountEven = coxeterMatrix[path[0]][path[pathSize - 1]] != 4;
bool isSqrt3CountEven = coxeterMatrix[path[0]][path[pathSize - 1]] != 6;
for (unsigned int i(1); i < pathSize; i++) {
if (coxeterMatrix[path[i]][path[i - 1]] == 4)
isSqrt2CountEven = isSqrt2CountEven ? false : true;
else if (coxeterMatrix[path[i]][path[i - 1]] == 6)
isSqrt3CountEven = isSqrt3CountEven ? false : true;
else if (coxeterMatrix[path[i]][path[i - 1]] ==
1) // Because of the dotted line we cannot say anything for this
// cycle
return;
}
// Because of the dotted line we cannot say anything for this cycle
if (coxeterMatrix[path[0]][path[pathSize - 1]] == 1)
return;
notArithmetic = !isSqrt2CountEven || !isSqrt3CountEven;
} else {
unsigned int pathSize(path.size());
int dottedCount(coxeterMatrix[path[0]][path[pathSize - 1]] == 1 ? 1 : 0);
int twoCount(coxeterMatrix[path[0]][path[pathSize - 1]] == 0 ? 1 : 0);
int sqrt2Count(coxeterMatrix[path[0]][path[pathSize - 1]] == 4 ? 1 : 0);
int sqrt3Count(coxeterMatrix[path[0]][path[pathSize - 1]] == 6 ? 1 : 0);
for (unsigned int i(1); i < pathSize; i++) {
if (coxeterMatrix[path[i]][path[i - 1]] == 0)
twoCount++;
else if (coxeterMatrix[path[i]][path[i - 1]] == 4)
sqrt2Count++;
else if (coxeterMatrix[path[i]][path[i - 1]] == 6)
sqrt3Count++;
else if (coxeterMatrix[path[i]][path[i - 1]] ==
1) // Because of the dotted line we cannot say anything for this
// cycle
dottedCount++;
}
if (dottedCount == 0)
notArithmetic = (sqrt2Count % 2) || (sqrt3Count % 2);
else {
string temp;
twoCount += dottedCount;
if (sqrt2Count > 1)
twoCount += ((sqrt2Count % 2) ? sqrt2Count - 1 : sqrt2Count) / 2;
if (twoCount)
temp += (temp == "" ? "" : " * ") + string("2^") + to_string(twoCount);
if (sqrt3Count > 1)
temp += (temp == "" ? "" : " * ") + string("3^") +
to_string(((sqrt2Count % 2) ? sqrt2Count - 1 : sqrt2Count) / 2);
if (sqrt2Count % 2)
temp += (temp == "" ? "" : " * ") + string("Sqrt[2]");
if (sqrt3Count % 2)
temp += (temp == "" ? "" : " * ") + string("Sqrt[3]");
for (unsigned int i(1); i < pathSize; i++) {
if (coxeterMatrix[path[i]][path[i - 1]] == 1)
temp += (temp == "" ? "" : " * ") + string("l") +
to_string(min(referencesToLabels[path[i]],
referencesToLabels[path[i - 1]])) +
"m" +
to_string(max(referencesToLabels[path[i]],
referencesToLabels[path[i - 1]]));
}
if (coxeterMatrix[path[0]][path[pathSize - 1]] == 1)
temp += (temp == "" ? "" : " * ") + string("l") +
to_string(min(referencesToLabels[path[0]],
referencesToLabels[path[pathSize - 1]])) +
"m" +
to_string(max(referencesToLabels[path[0]],
referencesToLabels[path[pathSize - 1]]));
const auto it(lower_bound(allCycles.begin(), allCycles.end(), temp));
if (it == allCycles.end() || *it != temp)
allCycles.insert(it, temp);
}
}
}
vector<string> Arithmeticity::get_allCycles() { return allCycles; }
string Arithmeticity::get_error() { return error; }