File: isomorphic.cpp

package info (click to toggle)
regina-normal 4.6-1.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 18,696 kB
  • ctags: 8,499
  • sloc: cpp: 71,120; ansic: 12,923; sh: 10,624; perl: 3,294; makefile: 959; python: 188
file content (447 lines) | stat: -rw-r--r-- 17,663 bytes parent folder | download
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

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2009, Ben Burton                                   *
 *  For further details contact Ben Burton (bab@debian.org).              *
 *                                                                        *
 *  This program 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 2 of the    *
 *  License, or (at your option) any later version.                       *
 *                                                                        *
 *  This program 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 this program; if not, write to the Free            *
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,       *
 *  MA 02110-1301, USA.                                                   *
 *                                                                        *
 **************************************************************************/

/* end stub */

#include <map>
#include <queue>
#include "triangulation/nisomorphism.h"
#include "triangulation/ntriangulation.h"

namespace regina {

std::auto_ptr<NIsomorphism> NTriangulation::isIsomorphicTo(
        const NTriangulation& other) const {
    std::list<NIsomorphism*> results;
    if (findIsomorphisms(other, results, true, true))
        return std::auto_ptr<NIsomorphism>(results.front());
    else
        return std::auto_ptr<NIsomorphism>(0);
}

std::auto_ptr<NIsomorphism> NTriangulation::isContainedIn(
        const NTriangulation& other) const {
    std::list<NIsomorphism*> results;
    if (findIsomorphisms(other, results, false, true))
        return std::auto_ptr<NIsomorphism>(results.front());
    else
        return std::auto_ptr<NIsomorphism>(0);
}

unsigned long NTriangulation::findAllSubcomplexesIn(
        const NTriangulation& other, std::list<NIsomorphism*>& results) const {
    return findIsomorphisms(other, results, false, false);
}

unsigned long NTriangulation::findIsomorphisms(
        const NTriangulation& other, std::list<NIsomorphism*>& results,
        bool completeIsomorphism, bool firstOnly) const {
    if (! calculatedSkeleton)
        calculateSkeleton();
    if (! other.calculatedSkeleton)
        other.calculateSkeleton();

    // Deal with the empty triangulation first.
    if (tetrahedra.empty()) {
        if (completeIsomorphism && ! other.tetrahedra.empty())
            return 0;
        results.push_back(new NIsomorphism(0));
        return 1;
    }

    // Basic property checks.  Unfortunately, if we allow boundary
    // incomplete isomorphisms then we can't test that many properties.
    if (completeIsomorphism) {
        // Must be boundary complete, 1-to-1 and onto.
        // That is, combinatorially the two triangulations must be
        // identical.
        if (tetrahedra.size() != other.tetrahedra.size())
            return 0;
        if (faces.size() != other.faces.size())
            return 0;
        if (edges.size() != other.edges.size())
            return 0;
        if (vertices.size() != other.vertices.size())
            return 0;
        if (components.size() != other.components.size())
            return 0;
        if (boundaryComponents.size() != other.boundaryComponents.size())
            return 0;
        if (orientable ^ other.orientable)
            return 0;

        // Test degree sequences and the like.
        std::map<unsigned long, unsigned long> map1;
        std::map<unsigned long, unsigned long> map2;
        std::map<unsigned long, unsigned long>::iterator mapIt;

        {
            EdgeIterator it;
            for (it = edges.begin(); it != edges.end(); it++) {
                // Find this degree, or insert it with frequency 0 if it's
                // not already present.
                mapIt = map1.insert(
                    std::make_pair((*it)->getNumberOfEmbeddings(), 0)).first;
                (*mapIt).second++;
            }
            for (it = other.edges.begin(); it != other.edges.end(); it++) {
                mapIt = map2.insert(
                    std::make_pair((*it)->getNumberOfEmbeddings(), 0)).first;
                (*mapIt).second++;
            }
            if (! (map1 == map2))
                return 0;
            map1.clear();
            map2.clear();
        }
        {
            VertexIterator it;
            for (it = vertices.begin(); it != vertices.end(); it++) {
                mapIt = map1.insert(
                    std::make_pair((*it)->getNumberOfEmbeddings(), 0)).first;
                (*mapIt).second++;
            }
            for (it = other.vertices.begin();
                    it != other.vertices.end(); it++) {
                mapIt = map2.insert(
                    std::make_pair((*it)->getNumberOfEmbeddings(), 0)).first;
                (*mapIt).second++;
            }
            if (! (map1 == map2))
                return 0;
            map1.clear();
            map2.clear();
        }
        {
            ComponentIterator it;
            for (it = components.begin(); it != components.end(); it++) {
                mapIt = map1.insert(
                    std::make_pair((*it)->getNumberOfTetrahedra(), 0)).first;
                (*mapIt).second++;
            }
            for (it = other.components.begin();
                    it != other.components.end(); it++) {
                mapIt = map2.insert(
                    std::make_pair((*it)->getNumberOfTetrahedra(), 0)).first;
                (*mapIt).second++;
            }
            if (! (map1 == map2))
                return 0;
            map1.clear();
            map2.clear();
        }
        {
            BoundaryComponentIterator it;
            for (it = boundaryComponents.begin();
                    it != boundaryComponents.end(); it++) {
                mapIt = map1.insert(
                    std::make_pair((*it)->getNumberOfFaces(), 0)).first;
                (*mapIt).second++;
            }
            for (it = other.boundaryComponents.begin();
                    it != other.boundaryComponents.end(); it++) {
                mapIt = map2.insert(
                    std::make_pair((*it)->getNumberOfFaces(), 0)).first;
                (*mapIt).second++;
            }
            if (! (map1 == map2))
                return 0;
            map1.clear();
            map2.clear();
        }
    } else {
        // May be boundary incomplete, and need not be onto.
        // Not much we can test for unfortunately.
        if (tetrahedra.size() > other.tetrahedra.size())
            return 0;
        if ((! orientable) && other.orientable)
            return 0;
    }

    // Start searching for the isomorphism.
    // From the tests above, we are guaranteed that both triangulations
    // have at least one tetrahedron.
    unsigned long nResults = 0;
    unsigned long nTetrahedra = tetrahedra.size();
    unsigned long nDestTetrahedra = other.tetrahedra.size();
    unsigned long nComponents = components.size();
    unsigned i;

    NIsomorphism iso(nTetrahedra);
    for (i = 0; i < nTetrahedra; i++)
        iso.tetImage(i) = -1;

    // Which source component does each destination component correspond to?
    long* whichComp = new long[nDestTetrahedra];
    std::fill(whichComp, whichComp + nDestTetrahedra, -1);

    // The image of the first source tetrahedron of each component.  The
    // remaining images can be derived by following gluings.
    unsigned long* startTet = new unsigned long[nComponents];
    std::fill(startTet, startTet + nComponents, 0);

    int* startPerm = new int[nComponents];
    std::fill(startPerm, startPerm + nComponents, 0);

    // The tetrahedra whose neighbours must be processed when filling
    // out the current component.
    std::queue<long> toProcess;

    // Temporary variables.
    unsigned long compSize;
    NTetrahedron* tet;
    NTetrahedron* adj;
    NTetrahedron* destTet;
    NTetrahedron* destAdj;
    unsigned long tetIndex, adjIndex;
    unsigned long destTetIndex, destAdjIndex;
    NPerm tetPerm, adjPerm;
    int face;
    bool broken;

    long comp = 0;
    while (comp >= 0) {
        // Continue trying to find a mapping for the current component.
        // The next mapping to try is the one that starts with
        // startTet[comp] and startPerm[comp].
        if (comp == static_cast<long>(nComponents)) {
            // We have an isomorphism!!!
            results.push_back(new NIsomorphism(iso));

            if (firstOnly) {
                delete[] whichComp;
                delete[] startTet;
                delete[] startPerm;
                return 1;
            } else
                nResults++;

            // Back down to the previous component, and clear the
            // mapping for that previous component so we can make way
            // for a new one.
            // Since nComponents > 0, we are guaranteed that comp > 0 also.
            comp--;

            for (i = 0; i < nTetrahedra; i++)
                if (iso.tetImage(i) >= 0 &&
                        whichComp[iso.tetImage(i)] == comp) {
                    whichComp[iso.tetImage(i)] = -1;
                    iso.tetImage(i) = -1;
                }
            startPerm[comp]++;

            continue;
        }

        // Sort out the results of any previous startPerm++.
        if (startPerm[comp] == 24) {
            // Move on to the next destination tetrahedron.
            startTet[comp]++;
            startPerm[comp] = 0;
        }

        // Be sure we're looking at a tetrahedron we can use.
        compSize = components[comp]->getNumberOfTetrahedra();
        if (completeIsomorphism) {
            // Conditions:
            // 1) The destination tetrahedron is unused.
            // 2) The component sizes match precisely.
            while (startTet[comp] < nDestTetrahedra &&
                    (whichComp[startTet[comp]] >= 0 ||
                     other.tetrahedra[startTet[comp]]->getComponent()->
                     getNumberOfTetrahedra() != compSize))
                startTet[comp]++;
        } else {
            // Conditions:
            // 1) The destination tetrahedron is unused.
            // 2) The destination component is at least as large as
            // the source component.
            while (startTet[comp] < nDestTetrahedra &&
                    (whichComp[startTet[comp]] >= 0 ||
                     other.tetrahedra[startTet[comp]]->getComponent()->
                     getNumberOfTetrahedra() < compSize))
                startTet[comp]++;
        }

        // Have we run out of possibilities?
        if (startTet[comp] == nDestTetrahedra) {
            // No more possibilities for filling this component.
            // Move back to the previous component, and clear the
            // mapping for that previous component.
            startTet[comp] = 0;
            startPerm[comp] = 0;

            comp--;
            if (comp >= 0) {
                for (i = 0; i < nTetrahedra; i++)
                    if (iso.tetImage(i) >= 0 &&
                            whichComp[iso.tetImage(i)] == comp) {
                        whichComp[iso.tetImage(i)] = -1;
                        iso.tetImage(i) = -1;
                    }
                startPerm[comp]++;
            }

            continue;
        }

        // Try to fill the image of this component based on the selected
        // image of its first source tetrahedron.
        // Note that there is only one way of doing this (as seen by
        // following adjacent tetrahedron gluings).  It either works or
        // it doesn't.
        tetIndex = tetrahedronIndex(components[comp]->getTetrahedron(0));

        whichComp[startTet[comp]] = comp;
        iso.tetImage(tetIndex) = startTet[comp];
        iso.facePerm(tetIndex) = NPerm::S4[startPerm[comp]];
        toProcess.push(tetIndex);

        broken = false;
        while ((! broken) && (! toProcess.empty())) {
            tetIndex = toProcess.front();
            toProcess.pop();
            tet = tetrahedra[tetIndex];
            tetPerm = iso.facePerm(tetIndex);
            destTetIndex = iso.tetImage(tetIndex);
            destTet = other.tetrahedra[destTetIndex];

            // If we are after a complete isomorphism, we might as well
            // test whether the edge and vertex degrees match.
            if (completeIsomorphism && ! compatibleTets(tet, destTet,
                    tetPerm)) {
                broken = true;
                break;
            }

            for (face = 0; face < 4; face++) {
                adj = tet->adjacentTetrahedron(face);
                if (adj) {
                    // There is an adjacent source tetrahedron.
                    // Is there an adjacent destination tetrahedron?
                    destAdj = destTet->adjacentTetrahedron(tetPerm[face]);
                    if (! destAdj) {
                        broken = true;
                        break;
                    }
                    // Work out what the isomorphism *should* say.
                    adjIndex = tetrahedronIndex(adj);
                    destAdjIndex = other.tetrahedronIndex(destAdj);
                    adjPerm =
                        destTet->adjacentGluing(tetPerm[face]) *
                        tetPerm *
                        tet->adjacentGluing(face).inverse();

                    if (iso.tetImage(adjIndex) >= 0) {
                        // We've already decided upon an image for this
                        // source tetrahedron.  Does it match?
                        if (static_cast<long>(destAdjIndex) !=
                                iso.tetImage(adjIndex) ||
                                adjPerm != iso.facePerm(adjIndex)) {
                            broken = true;
                            break;
                        }
                    } else if (whichComp[destAdjIndex] >= 0) {
                        // We haven't decided upon an image for this
                        // source tetrahedron but the destination
                        // tetrahedron has already been used.
                        broken = true;
                        break;
                    } else {
                        // We haven't seen either the source or the
                        // destination tetrahedron.
                        whichComp[destAdjIndex] = comp;
                        iso.tetImage(adjIndex) = destAdjIndex;
                        iso.facePerm(adjIndex) = adjPerm;
                        toProcess.push(adjIndex);
                    }
                } else if (completeIsomorphism) {
                    // There is no adjacent source tetrahedron, and we
                    // are after a boundary complete isomorphism.
                    // There had better be no adjacent destination
                    // tetrahedron also.
                    if (destTet->adjacentTetrahedron(tetPerm[face])) {
                        broken = true;
                        break;
                    }
                }
            }
        }

        if (! broken) {
            // Therefore toProcess is empty.
            // The image for this component was successfully filled out.
            // Move on to the next component.
            comp++;
        } else {
            // The image for this component was not successfully filled out.
            // Undo our partially created image, and then try another
            // starting image for this component.
            while (! toProcess.empty())
                toProcess.pop();

            for (i = 0; i < nTetrahedra; i++)
                if (iso.tetImage(i) >= 0 &&
                        whichComp[iso.tetImage(i)] == comp) {
                    whichComp[iso.tetImage(i)] = -1;
                    iso.tetImage(i) = -1;
                }

            startPerm[comp]++;
        }
    }

    // All out of options.
    delete[] whichComp;
    delete[] startTet;
    delete[] startPerm;
    return nResults;
}

bool NTriangulation::compatibleTets(NTetrahedron* src, NTetrahedron* dest,
        NPerm p) {
    for (int edge = 0; edge < 6; edge++) {
        if (src->getEdge(edge)->getNumberOfEmbeddings() !=
                dest->getEdge(NEdge::edgeNumber[p[NEdge::edgeVertex[edge][0]]]
                    [p[NEdge::edgeVertex[edge][1]]])
                ->getNumberOfEmbeddings())
            return false;
    }

    for (int vertex = 0; vertex < 4; vertex++) {
        if (src->getVertex(vertex)->getNumberOfEmbeddings() !=
                dest->getVertex(p[vertex])->getNumberOfEmbeddings())
            return false;
        if (src->getVertex(vertex)->getLink() !=
                dest->getVertex(p[vertex])->getLink())
            return false;
    }

    return true;
}

} // namespace regina