forked from waynebhayes/BLANT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-orbit-maps.c
205 lines (186 loc) · 4.75 KB
/
make-orbit-maps.c
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
#include <sys/file.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include "blant.h"
static int k;
static int canon_list[MAX_CANONICALS];
Boolean suitablePerm(int permutation[], int adj[k][k+1]);
void permuteNodes(int permutation[], int adj[k][k+1], long *D);
void makeOrbit(int permutation[], long orbit[]);
void getCycle(int permutation[], int cycle[], int seed, int current, Boolean visited[]);
Boolean nextPermutation(int permutation[])
{
int t, i;
for(i=k-1;i>0;i--)
{
if(permutation[i]>permutation[i-1])
{
int j;
for(j=k-1;j>i-1;j--){
if(permutation[i-1]<permutation[j]){
int t=permutation[i-1];
permutation[i-1]=permutation[j];
permutation[j]=t;
break;
}
}
int l=i;
for(j=k-1;j>l;j--)
{
if(i<j){
t=permutation[i];
permutation[i]=permutation[j];
permutation[j]=t;
i++;
}
}
return 1;
}
}
return 0;
}
void getDecimal(int adj[k][k+1],long *D){
int i, j, bitPos=0;
*D=0;
#if LOWER_TRIANGLE
for(i=k-1;i>0;i--)
for(j=i-1;j>=0;j--)
#else // UPPER_TRIANGLE
for(i=k-2;i>=0;i--)
for(j=k-1;j>i;j--)
#endif
{
if(adj[i][j]==1) *D+= (1 << bitPos);
bitPos++;
}
}
void getGraph(long int decimal, int adj[k][k+1]){
int i,j;
#if LOWER_TRIANGLE
for(i=k-1;i>0;i--)
for(j=i-1;j>=0;j--)
#else // UPPER_TRIANGLE
for(i=k-2;i>=0;i--)
for(j=k-1;j>i;j--)
#endif
{
adj[i][j]=decimal%2;
adj[j][i]=adj[i][j];
decimal/=2;
}
for(i=0; i<k; i++)
for(j=0; j<k; j++)
adj[i][k]+=adj[i][j];
}
void orbits(long int Decimal,long orbit[]){
int permutation[k], i, j;
//initially,the permutation is (0, 1, ..., numNodes_-1)
//and each node is in its own orbit. we'll merge orbits later
for(i = 0; i < k; i++){
permutation[i]=i;
orbit[i]=i;
}
int adj[k][k+1];
for(i=0; i<k; i++)
for(j=0; j<k+1; j++)
adj[i][j]=0;
getGraph(Decimal,adj);
long d=0;
while( nextPermutation(permutation) ){
d=0;
//ruling out searching into unnecessary permutations
if(!suitablePerm(permutation,adj)) continue;
permuteNodes(permutation,adj,&d);
//Check for automorphism
if(Decimal == d){
makeOrbit(permutation, orbit);
}
}
}
Boolean suitablePerm(int permutation[], int adj[k][k+1]){
int node;
for(node = 0; node < k; node++){
if(adj[node][k] != adj[permutation[node]][k]){
return false;
}
}
return true;
}
void permuteNodes(int permutation[], int adj[k][k+1], long* D){
//To store adjMatrix_ after permutation
int pAdj[k][k+1], i, j;
for(i=0; i<k; i++)
for(j=0; j<k+1; j++)
pAdj[i][j]=0;
//Apply the permutation
for(i = 0; i < k; i++){
for(j =i+1; j < k; j++){
int pi = permutation[i], pj = permutation[j];
pAdj[pi][pj] = adj[i][j];
pAdj[pj][pi] = adj[i][j];
}
}
getDecimal(pAdj, D);
}
void makeOrbit(int permutation[], long orbit[]){
int i, j;
Boolean visited[k];
for(i=0;i<k;i++)
visited[i]=0;
for(i = 0; i < k; i++){
if(!visited[i]){
//finding out each cycle at a time
int cycle[k+1];
cycle[0]=0;
int minOrbit=i;
getCycle(permutation, cycle, i, i, visited);
for(j=1; j<=cycle[0]; j++)
minOrbit = MIN(orbit[cycle[j]], minOrbit);
for(j=1; j<=cycle[0]; j++)
orbit[cycle[j]] = minOrbit;
}
}
}
void getCycle(int permutation[], int cycle[], int seed, int current, Boolean visited[]){
cycle[++cycle[0]]=current;
visited[current] = true;
if(permutation[current] != seed)
getCycle(permutation, cycle, seed, permutation[current], visited);
}
void main(int argc, char* argv[]){
if(argc != 2) {
fprintf(stderr, "USAGE: %s k\n", argv[0]);
exit(1);
}
k=atoi(argv[1]);
assert(k > 2 && k <= 8);
//reading data from canon_list file
char BUF[BUFSIZ], tmpName[BUFSIZ];
tmpnam(tmpName);
FILE *tmpfp = fopen(tmpName,"w");
int numCanon = canonListPopulate(BUF, canon_list, NULL, k);
//making orbit_map file
int numOrbits = 0, i, j;
for(i=0; i<numCanon; i++){
long orbit[k];
orbits(canon_list[i],orbit);
for(j=0;j<k;j++){
if(orbit[j]==j)
orbit[j]=numOrbits++;
else
orbit[j]=orbit[orbit[j]];
}
for(j=0;j<k;j++)
fprintf(tmpfp, "%d ", orbit[j]);
fprintf(tmpfp, "\n");
}
fclose(tmpfp);
printf("%d\n", numOrbits); fflush(stdout);
sprintf(BUF, "cat %s; /bin/rm -f %s\n", tmpName, tmpName);
system(BUF);
}