-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosettable.cc
505 lines (478 loc) · 11.4 KB
/
cosettable.cc
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
/* cosettable.cc: the CosetTable class.
Copyright 2011 Kenneth S. Brown.
This file is part of Toddcox.
Toddcox 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 (GPLv3+).
Toddcox 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 Toddcox; if not, see <http://gnu.org/licenses/>.
Written by Ken Brown <kbrown@cornell.edu>. */
#include <iostream>
#include <iomanip>
#include <set>
#include <exception>
#include <stdexcept>
#include <algorithm>
#include <cstdlib>
#include "cosettable.h"
#include "gens_and_words.h"
#include "stack.h"
#include "equivreln.h"
using namespace std;
// Constructor
CosetTable::CosetTable (int NG, vector<string> rel, vector<string> gen_H,
bool felsch) : NGENS (NG), p (EquivReln (1))
{
row r (NGENS, -1);
tab.push_back (r);
for (int i = 0; i < gen_H.size (); i++)
{
word w;
// Caller should check that strings are valid.
string_to_word (w, gen_H[i], NGENS);
generator_of_H.push_back (w);
}
vector<string> R;
for (int i = 0; i < rel.size (); i++)
{
word w;
string s = rel[i];
string_to_word (w, s, NGENS);
relator.push_back (w);
if (felsch)
{
generator_of_H.push_back (w);
// Accumulate s, s^{-1}, and all cyclic conjugates
string sinv;
transform (s.rbegin (), s.rend (), back_inserter (sinv), switch_case);
for (int i = 0; i < s.size (); i++)
{
R.push_back (s);
R.push_back (sinv);
rotate (s);
rotate (sinv);
}
}
}
// Group elements of R according to first letter after removing duplicates.
if (felsch)
{
typedef vector<string>::const_iterator VI;
VI itu = unique (R.begin (), R.end ());
for (VI it = R.begin (); it != itu; it++)
{
word w;
string_to_word (w, *it, NGENS);
relator_grouped[w[0]].push_back (w);
}
}
}
// Define coset k acted on by x to be new coset; exit if can't
// allocate memory.
void
CosetTable::define (coset k, gen x, bool save)
{
const int l = tab.size (); // index of new coset
tab[k][x] = l;
row r (NGENS, -1);
r[inv (x)] = k;
try
{
tab.push_back (r);
p.add (); // p(l) = l
}
catch (bad_alloc)
{
cerr << "\n\nCoset table has size " << tab.size ()
<< "; memory exhausted.\n";
exit (EXIT_FAILURE);
}
if (save)
{
deduction ded = {k, x};
deduction_stack.push (ded);
}
}
// Use standard numbering for coset tables, starting with 1 instead of 0.
ostream&
operator<< (ostream& os, const CosetTable::row& r)
{
os << setw (4) << r[0] + 1;
for (CosetTable::gen x = 1; x < r.size (); x++)
os << " " << setw (3) << r[x] + 1;
return os;
}
ostream&
operator<< (ostream& os, const CosetTable& C)
{
os << " ";
for (CosetTable::gen x = 0; x < C.NGENS; x++)
os << setw (4) << gens[x];
os << endl;
for (CosetTable::coset k = 0; k < C.tab.size (); k++)
if (C.isalive (k))
os << setw (2) << k + 1 << ": " << C.tab[k] << endl;
return os;
}
#if 0
void
CosetTable::debug_print () const
{
cout << " ";
for (gen x = 0; x < NGENS; x++)
cout << setw (4) << gens[x];
cout << endl;
for (CosetTable::coset k = 0; k < tab.size (); k++)
{
cout << setw (2) << k << setw (3) << p[k] << ": ";
tab[k].print (false); // Don't standardize coset numbering
cout << endl;
}
return;
}
#endif
void
CosetTable::coincidence (coset k, coset l, bool save)
{
merge (k, l);
while (!q.empty ())
{
coset e = q.front ();
q.pop ();
// Transfer all info about e
for (gen x = 0; x < NGENS; x++)
{
if (!isdefined (e, x))
continue;
gen y = inv (x);
coset f = tab[e][x]; // x: e --> f, y: f --> e
undefine (f, y); // remove arrow f --> e
coset e1 = p.rep (e);
coset f1 = p.rep (f);
// insert arrows x: e1 --> f1 and y: f1 --> e1
if (isdefined (e1, x))
merge (f1, tab[e1][x]);
else if (isdefined (f1, y))
merge (e1, tab[f1][y]);
else
{
tab[e1][x] = f1;
tab[f1][y] = e1;
if (save)
{
deduction ded = {e1, x};
deduction_stack.push (ded);
}
}
}
}
}
void
CosetTable::scan_and_fill (coset k, const word& w, bool save)
{
int i = 0, j = w.size () - 1; // Starting pos for forward and backward scans
coset f = k, b = k; // Starting cosets for scans
for (;;)
{
// Scan forward
while (i <= j && isdefined (f, w[i]))
f = tab[f][w[i++]];
if (i > j) // Scan completed, possibly with coincidence
{
if (f != b)
coincidence (f, b, save);
return;
}
// Scan backward
while (j >= i && isdefined (b, inv (w[j])))
b = tab[b][inv (w[j--])];
if (j < i) // Scan completed with coincidence
{
coincidence (f, b, save);
return;
}
if (j == i) // Scan completed with deduction
{
tab[f][w[i]] = b;
tab[b][inv (w[i])] = f;
if (save)
{
deduction d = {f, w[i]};
deduction_stack.push (d);
}
return;
}
// Scan is incomplete; make a definition to allow it to get further.
define (f, w[i], save);
}
}
void
CosetTable::scan (coset k, const word& w, bool save)
{
int i = 0, j = w.size () - 1; // Starting pos for forward and backward scans
coset f = k, b = k; // Starting cosets for scans
// Scan forward
while (i <= j && isdefined (f, w[i]))
f = tab[f][w[i++]];
if (i > j) // Scan completed, possibly with coincidence
{
if (f != b)
coincidence (f, b, save);
return;
}
// Scan backward
while (j >= i && isdefined (b, inv (w[j])))
b = tab[b][inv (w[j--])];
if (j < i) // Scan completed with coincidence
coincidence (f, b, save);
else if (j == i) // Scan completed with deduction
{
tab[f][w[i]] = b;
tab[b][inv (w[i])] = f;
if (save)
{
deduction d = {f, w[i]};
deduction_stack.push (d);
}
}
// else scan is incomplete and yields no information
}
void
CosetTable::enumerate (int method)
{
if (method == 0)
hlt ();
else if (method < 0)
felsch ();
else
{
try
{
hlt_plus (method);
}
catch (Threshold_Exceeded)
{
cerr << "\nSorry, can't recover enough memory.\n"
<< "Please try again with a bigger threshold.\n";
exit (EXIT_FAILURE);
}
}
}
// HLT algorithm
void
CosetTable::hlt ()
{
for (int i = 0; i < generator_of_H.size (); i++)
scan_and_fill (0, generator_of_H[i]);
// Must recompute tab.size() after each iteration. Note that an
// iterator wouldn't work well here because elements keep getting
// added to tab.
for (coset k = 0; k < tab.size (); k++)
{
for (int i = 0; i < relator.size () && isalive (k); i++)
scan_and_fill (k, relator[i]);
if (isalive (k))
for (gen x = 0; x < NGENS; x++)
if (!isdefined (k, x))
define (k, x);
}
}
// HLT algorithm with lookahead.
void
CosetTable::hlt_plus (int threshold)
{
// Try to reserve space for a table of size threshold to avoid the
// overhead of reallocation.
try
{
tab.reserve (threshold);
}
catch (bad_alloc)
{
; // No harm if it fails; just means threshold will be useless.
}
catch (length_error)
{
; // ditto
}
for (int i = 0; i < generator_of_H.size (); i++)
scan_and_fill (0, generator_of_H[i]);
for (coset k = 0; k < tab.size (); k++)
{
if (!isalive (k))
continue;
if (tab.size () > threshold)
{
const int n = tab.size ();
cout << "\nThreshold exceeded; table size is "
<< n << ". Looking ahead...\n";
lookahead (k);
// Move to next live coset, in case k died
while (k < n && !isalive (k))
k++;
if (k == n)
return;
k = compress (k);
cout << "Table size is now " << tab.size () << ".";
if (tab.size () > threshold)
throw Threshold_Exceeded ();
cout << " Continuing.\n";
}
for (int i = 0; i < relator.size () && isalive (k); i++)
scan_and_fill (k, relator[i]);
if (isalive (k))
for (gen x = 0; x < NGENS; x++)
if (!isdefined (k, x))
define (k, x);
}
}
// Felsch algorithm
void
CosetTable::felsch ()
{
for (int i = 0; i < generator_of_H.size (); i++)
scan_and_fill (0, generator_of_H[i], true);
process_deductions ();
for (coset k = 0; k < tab.size (); k++)
{
for (gen x = 0; x < NGENS && isalive (k); x++)
if (!isdefined (k, x))
{
define (k, x, true);
process_deductions ();
}
}
}
void
CosetTable::process_deductions ()
{
while (!deduction_stack.is_empty ())
{
if (deduction_stack.is_full ())
{
lookahead ();
deduction_stack.erase ();
return;
}
deduction d;
deduction_stack.pop (d);
coset k = d.c;
gen x = d.x;
vector<word> relx = relator_grouped[x];
const int n = relx.size ();
for (int i = 0; i < n && isalive (k); i++)
scan (k, relx[i], true);
// No need to continue with this deduction if k died.
if (!isalive (k))
continue;
k = tab[k][x];
x = inv (x);
relx = relator_grouped[x];
for (int i = 0; i < n && isalive (k); i++)
scan (k, relx[i], true);
}
}
int
CosetTable::getnlive () const
{
int count = 0;
const int n = tab.size ();
for (coset k = 0; k < n; k++)
if (isalive (k))
count++;
return count;
}
void
CosetTable::lookahead (coset start)
{
const int n = tab.size ();
for (coset k = start; k < n; k++)
for (int i = 0; i < relator.size () && isalive (k); i++)
scan (k, relator[i]);
}
// When compress is called after lookahead in hlt_plus, we have some
// current (live) coset that we are about to process. After
// compression, we need to resume processing at the same coset, which
// may have been renumbered; compress returns the new number (or -1 if
// a current live coset wasn't specified).
CosetTable::coset
CosetTable::compress (coset current)
{
coset l = 0;
const int n = tab.size ();
coset ret = -1;
for (coset k = 0; k < n; k++)
if (isalive (k))
{
if (k == current)
ret = l;
if (k > l) // Replace k by l in table
{
for (gen x = 0; x < NGENS; x++)
{
coset m = tab[k][x];
if (m == k)
tab[l][x] = l;
else
{
tab[l][x] = m;
if (m >= 0)
tab[m][inv (x)] = l;
}
}
}
l++;
}
p = EquivReln (l);
if (l < n)
tab.erase (tab.begin () + l, tab.end ());
return ret;
}
// Swap two distinct live rows of the coset table
void
CosetTable::swap (coset k, coset l)
{
for (gen x = 0; x < NGENS; x++)
{
const coset temp = tab[k][x];
tab[k][x] = tab[l][x];
tab[l][x] = temp;
const int n = tab.size ();
for (coset m = 0; m < n; m++)
{
if (!isalive (m))
continue;
if (tab[m][x] == k)
tab[m][x] = l;
else if (tab[m][x] == l)
tab[m][x] = k;
}
}
}
// Standardize a complete compressed coset table
void
CosetTable::standardize ()
{
const int n = tab.size ();
if (n <= 2)
return;
coset goal = 1; // next coset number we want to find in the table
for (tab_iter it = tab.begin (); it != tab.end (); it++)
for (gen x = 0; x < NGENS; x++)
{
coset l = (*it)[x];
if (l >= goal)
{
if (l > goal)
swap (l, goal);
goal++;
if (goal == n - 1)
return;
}
}
}