/* Head of main program */
/* add or remove names of subprograms, symmetric graph-array GRAPH */


#include <stdio.h>
#define NN 1000        /* for larger general arrays */
#define MM 100         /* for smaller arrays */

int Nints, Nobj, Npairs, Nisol;


void main(void)
{

 int I,J,K,Nisoobj,CNT,ERR,  *P,
  NEGOBJ[MM],  /* locations in GROUP array of objects with neg int-names */
  NUMPOS[MM],  /* NUMPOS[K]=number neighbors (pos nums) of object K */

 /* Place zero before and after integer data for the graph, no zero inside */
  GRAPH[]={0,
  -1,11,20,34,  -2,6,30,  -3,5,7,12,   -4,23,  -5,3,18,32,  -6,2,10,19,27,
  -7,3,9,  -8,11,17,23,  -9,7,24,31,  -10,6,  -11,1,8,34,  -12,3,21,  -13,22,30,
  -14,34,  -15,24,25,32,   -16,  -17,8,  -18,5,32,  -19,6,  -20,1,26,
  -21,12,31,  -22,13,27,  -23,4,8,   -24,9,15,  -25,15,  -26,20,  -27,6,7,22,30,
  -28,31,  -29,  -30,2,13,27,  -31,9,21,28,  -32,5,15,18,  -33,  -34,1,11,14,
  -35,
       0};

 /************************ Get numbers *******************************/

 Nints=Nobj=Npairs=0;  

 P=&GRAPH[1];

 while (*P)
  {
   Nints++;
   if (*P<0) Nobj++; else Npairs++;
   P++;
  }


 printf("Number of objects in Graph = %d\n",Nobj);

 Nisoobj=0; puts("Isolated objects are:");
 for (K=1; K<=Nints; K++)
  if ((GRAPH[K]<0) && (GRAPH[K+1]<=0)) {printf("%d,",-GRAPH[K]); Nisoobj++;}
 puts("");
 printf("Number of isolated objects = %d\n",Nisoobj);
 printf("Number of links in the graph = %d\n",Npairs/2);
 printf("Number of linked pairs in the array = %d\n",Npairs);

 puts("");
 GRAPH[0]=Nints;
 printf("Graph-Array = %d (first number in array, for computer use\n",GRAPH[0]);
 for (K=1; K<=Nints; K++)
  {if (GRAPH[K]<0) printf("  "); printf("%d,",GRAPH[K]);}
 printf("\n             %d    end of array marker\n",GRAPH[Nints+1]);


  puts("\n");
 for (K=1; K<MM; K++) NUMPOS[K]=0;
 P=GRAPH;  P++;
 while (*P)
  {
   /* assume *P<0 here */   /* printf("%d ",*P); */

    K=-*P; CNT=0; P++;
    while (*P>0) {CNT++; P++;}
    NUMPOS[K]=CNT;
  }

  puts("after -object x,y is the number of neighbors:");
  for (K=1; K<=Nobj; K++)
   printf("-%d,%d ",K,NUMPOS[K]);
  puts("\n");

  for (K=1; K<MM; K++) NEGOBJ[K]=0;
  for (K=1; K<=Nints; K++) if (GRAPH[K]<0) NEGOBJ[-GRAPH[K]]=K;

  puts("after -object x,y is the index of the object in the GRAPH array");
  for (K=1; K<=Nobj; K++) printf("%-d,%d  ",K,NEGOBJ[K]);
  puts("\n");
  

 /*********** Check graph for links bidirectional (symmetry) ***********/
  ERR=0;
  for (K=1; K<=Nobj; K++)
   {
    if (!NUMPOS[K]) goto NXT;
    J = GRAPH[NEGOBJ[K]+NUMPOS[K]];   /* J = largest neighbor of K */
    CNT=NUMPOS[J];                    /* dont change num of neighbrs of J */
    while (CNT)
     {
      I=GRAPH[NEGOBJ[J]+ CNT];
    /* printf("# CNT=%d I=%d J=%d K=%d\n",CNT,I,J,K); */
      CNT--;
      if (I==K) goto NXT;            /* link K-J is bidirectional */
     }
    printf("link %d<-%d NOT bidirectional\n",J,K); ERR++;
  /*  printf("CNT=%d I=%d J=%d K=%d\n",CNT,I,J,K); getchar(); */
 NXT:
  }
 if (ERR) {printf("%d links not bidirectional\n",ERR); goto FIN;}
 else puts("All links bidirectional, Graph-array is symmetric\n");



FIN:
 getchar();
}
