/* PermsEquivs.c */
/* find the isomorphism from black graph onto red graph */

#include <stdio.h>
void ALTPERM(int*,int);

void main(void)
{
 int 

  PERM[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13},
   I,J,K,CNT=0,M,

 TABLEA[8][9]=
 { {0,0,0,0,0,0,0,0}, {0,0,1,1,0,0,1,1}, {0,1,0,1,1,0,0,1}, {0,1,1,0,1,1,0,0},
   {0,0,1,1,0,1,1,0}, {0,0,0,1,1,0,1,1}, {0,1,0,0,1,1,0,1}, {0,1,1,0,0,1,1,0} },
 TABLEB[8][9]=
 { {0,0,0,0,0,0,0,0}, {0,0,1,0,1,1,0,1}, {0,1,0,1,0,1,1,0}, {0,0,1,0,1,0,1,1},
   {0,1,0,1,0,1,0,1}, {0,1,1,0,1,0,1,0}, {0,0,1,1,0,1,0,1}, {0,1,0,1,1,0,1,0} };


 for (K=1; K<=5040; K++)
  {
   ALTPERM(PERM,7);   /* generate all 7! = 5040 permutations of 1,2,3,4,5,6,7 */   
   M=1;
   for (I=1; I<8; I++)
    for (J=1; J<8; J++) if (TABLEA[I][J] != TABLEB[PERM[I]][PERM[J]]) M=0;
   if (M)
    {for (J=1; J<8; J++) printf("%d ",PERM[J]); CNT++;  printf("  ");}
  } /* next K */

 printf("\n%d = number of permutations that presrve links\n",CNT);
 getchar();
}


/**************/

void ALTPERM(int *A,int N)
    /* Receive integer N, 2<=N<14, and a pointer A to an array of length 14;
       N! repeated calls to ALTPERM produce all permutations of length N;
       The first permutation called is odd; after that the called permutations
        alternate between even,odd; the last permutation is in normal
        increasing order 1,2,...,N and is even */
{
 int I,J,K,COPY;
  static
   int C[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0},
       D[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1};


   K=0; I=N;
  NEW:      /* construct a new PERMutation from the old one */
     C[I] = J = C[I] + D[I];
     if (J==I) {D[I]=-1; goto LAB;}
     if (J) goto FIN;
     D[I]=1; K++;
  LAB:
     if (I>2) {I--; goto NEW;}
     J=1;
  FIN:
     J+=K; COPY=A[J]; A[J]=A[J+1]; A[J+1]=COPY;
 return;
}
