dfmax.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* dfmax.c
  2. * Semi-Exhaustive Greedy Independent Set
  3. * from Matula and Johri
  4. *
  5. * written October 22, 1983 by DSJ
  6. * modified February 20, 1987 for checkpointing
  7. * modified August 1988 to find maximum independent sets
  8. * modified April 1993 for new data structures, bounding
  9. * modified September 1993 for dimacs .b input
  10. */
  11. #include <stdio.h>
  12. #include <sys/types.h>
  13. #include <time.h>
  14. //#include <boost/chrono.hpp>
  15. //#include <boost/timer/timer.hpp>
  16. #define INT 32 /* computer word size */
  17. #define CHARBITS 8
  18. #define edge(x,y) (bitmap[y/CHARBITS][x] & (1<<(y%CHARBITS)))
  19. #define NMAX 2400 /* maximum number of vertices handles */
  20. #define MAX_NR_VERTICES 2400 /* = NMAX */
  21. #define MAX_NR_VERTICESdiv8 300 /* = NMAX/8 */
  22. #define BOOL char
  23. #define MAX_PREAMBLE 10000
  24. #define REORDER
  25. unsigned mask[INT] =
  26. {
  27. 1, 1<<31, 1<<30, 1<<29, 1<<28, 1<<27, 1<<26, 1<<25, 1<<24,
  28. 1<<23, 1<<22, 1<<21, 1<<20, 1<<19, 1<<18, 1<<17, 1<<16,
  29. 1<<15, 1<<14, 1<<13, 1<<12, 1<<11, 1<<10, 1<<9, 1<<8,
  30. 1<<7, 1<<6, 1<<5, 1<<4, 1<<3, 1<<2, 1<<1
  31. }; /* CAUTION - assumes 32 bit machine */
  32. /* graph input parameters */
  33. int Nr_vert, Nr_edges;
  34. BOOL Bitmap[MAX_NR_VERTICES][MAX_NR_VERTICESdiv8];
  35. static char Preamble[MAX_PREAMBLE];
  36. char masks[ 8 ] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  37. unsigned char bitmap[NMAX/CHARBITS+1][NMAX+1];
  38. int N; /* number of vertices in graph */
  39. int mattype;
  40. double dens, maxdist, dens1, dens2;
  41. int chrom_num; /* for fixed-chrom-num graphs */
  42. double x_coord[NMAX+1], y_coord[NMAX+1];
  43. int width;
  44. int setlim; /* size at which exhaustive search begins */
  45. int vertex[NMAX]; /* array in which vertices reside and are moved */
  46. int degree[NMAX]; /* vertex degrees with resp. to uncolored vertices */
  47. int set[NMAX]; /* non-zero entries are members of current set */
  48. int bestset[NMAX]; /* non-zero entries constitute currently best ind. set */
  49. int bestsize; /* size of current best set */
  50. double bignum; /* 2**31 */
  51. void check(); /* checks to see that chosen color class is ind. set */
  52. void readgraph(); /* reads input graph in bitmapform */
  53. clock_t start;
  54. FILE *fopen();
  55. char inputname[40];
  56. main (argc,argv)
  57. int argc;
  58. char *argv[];
  59. {
  60. int i,j,cand,newcand,dmax;
  61. FILE *inputfile;
  62. setbuf(stdout,NULL);
  63. start = clock();
  64. /* Calculate bignum */
  65. bignum = (float)1024*1024;
  66. bignum *= 2048;
  67. /* read input */
  68. if (argc < 2) {
  69. printf("Usage: dfmax <filename> [setlim]\n");
  70. exit(1);
  71. }
  72. strcpy(inputname,argv[1]);
  73. if ((inputfile = fopen(argv[1], "rb")) == NULL) {
  74. printf("Input graph does not exist");
  75. exit(1);
  76. }
  77. readgraph(inputfile);
  78. /* initialize algorithmic parameters */
  79. setlim = 1;
  80. if (argc > 2) setlim = atoi(argv[2]);
  81. printf("DFMAX(%s) setlim=%d\n",argv[1],setlim);
  82. printf("Input Time: %8.2f (user) %8.2f (sys) %8.2f (real)\n", (double)(clock() - start) / CLOCKS_PER_SEC, 0, 0);
  83. #ifdef REORDER
  84. for (i=1;i<=N;i++) {
  85. degree[i] = 0;
  86. for (j=1;j<=N;j++)
  87. if (!edge(i,j)) degree[i]++;
  88. }
  89. dmax = -1;
  90. for (i=1;i<=N;i++)
  91. if (degree[i] > dmax) {
  92. dmax = degree[i];
  93. cand = i;
  94. }
  95. vertex[N] = cand;
  96. for (j=N-1;j>=1;j--) {
  97. degree[cand] = -9;
  98. dmax = -1;
  99. for (i=1;i<=N;i++) {
  100. if (!edge(cand,i)) degree[i]--;
  101. if (degree[i] > dmax) {
  102. dmax = degree[i];
  103. newcand = i;
  104. }
  105. }
  106. vertex[j] = cand = newcand;
  107. }
  108. #else
  109. for (j=1;j<=N;j++) vertex[j] = j;
  110. #endif
  111. bestsize = 0;
  112. bestsize = maxind(N,setlim,vertex,1);
  113. printf("%8.2f (user) %8.2f (sys) %8.2f (real)\n", (double)(clock() - start) / CLOCKS_PER_SEC, 0, 0);
  114. printf ("Best:");
  115. for (i=1; i<=bestsize; i++) {
  116. printf (" %d",bestset[i]);
  117. }
  118. printf ("\n");
  119. }
  120. int maxind(top,goal,array,depth)
  121. register int top, goal,depth;
  122. int *array;
  123. {
  124. int newarray[NMAX];
  125. int i,v,u,w,z;
  126. int best, restbest, newgoal;
  127. unsigned *bitloc;
  128. int *pnew, *pold;
  129. int canthrow;
  130. if (top <= 1) {
  131. if (top == 0) depth--;
  132. if (depth > bestsize) {
  133. bestsize = depth;
  134. if (top == 1) set[bestsize] = array[top];
  135. for (i=1;i<=bestsize;i++) bestset[i] = set[i];
  136. check();
  137. printf("Size = %2d found\n",bestsize);
  138. }
  139. return(top);
  140. }
  141. best = 1;
  142. newgoal = goal-1;
  143. if (newgoal <= 1) newgoal = 1;
  144. for (i = top; i >= goal; i--) {
  145. pnew = newarray;
  146. w = array[i];
  147. set[depth] = w;
  148. canthrow = i - goal;
  149. pold = array+1;
  150. while (pold<array+i) {
  151. z = *pold++;
  152. if (edge(z,w)) {
  153. *++pnew = z;
  154. } else {
  155. if (canthrow == 0) goto breakout;
  156. canthrow--;
  157. }
  158. }
  159. restbest = maxind(pnew-newarray,newgoal,newarray,depth+1);
  160. if (restbest >= newgoal) {
  161. best = newgoal = restbest+1;
  162. goal = best+1;
  163. }
  164. if (top == N) {
  165. printf("N = %3d best = %2d %8.2f (user) %8.2f (sys) %8.2f (real)\n",i,best, (double)(clock() - start) / CLOCKS_PER_SEC, 0, 0);
  166. }
  167. breakout:;
  168. }
  169. return(best);
  170. }
  171. BOOL get_edge( i, j )
  172. int i,j;
  173. {
  174. int byte, bit;
  175. char mask;
  176. int k;
  177. if (i<j) {
  178. k = i;
  179. i = j;
  180. j = k;
  181. }
  182. bit = 7-(j & 0x00000007);
  183. byte = j >> 3;
  184. mask = masks[bit];
  185. return( (Bitmap[i][byte] & mask)==mask );
  186. }
  187. void readgraph(fp)
  188. FILE *fp;
  189. {
  190. int i,j;
  191. int length = 0;
  192. unsigned temp;
  193. if (!fscanf(fp, "%d\n", &length))
  194. { printf("ERROR: Corrupted preamble.\n"); exit(10); }
  195. if(length >= MAX_PREAMBLE)
  196. { printf("ERROR: Too long preamble.\n"); exit(10); }
  197. fread(Preamble, 1, length, fp);
  198. Preamble[length] = '\0';
  199. if (!get_params())
  200. { printf("ERROR: Corrupted preamble.\n"); exit(10); }
  201. if (Nr_vert >NMAX) {
  202. printf("Too many vertices! Recompile with NMAX > %d\n",
  203. Nr_vert);
  204. exit(0);
  205. }
  206. for ( i = 0
  207. ; i < Nr_vert && fread(Bitmap[i], 1, (int)((i + 8)/8), fp)
  208. ; i++ );
  209. fclose(fp);
  210. N = Nr_vert;
  211. for (i = 0; i < N; i++)
  212. for (j = 0; j < N; j++)
  213. if (get_edge(i, j))
  214. bitmap[(j+1)/CHARBITS][i+1] |= (1 << ((j+1) % CHARBITS));
  215. }
  216. int get_params()
  217. /* getting Nr_vert and Nr_edge from the preamble string,
  218. containing Dimacs format "p ??? num num" */
  219. {
  220. char c, *tmp;
  221. char * pp = Preamble;
  222. int stop = 0;
  223. tmp = (char *)calloc(100, sizeof(char));
  224. Nr_vert = Nr_edges = 0;
  225. while (!stop && (c = *pp++) != '\0'){
  226. switch (c)
  227. {
  228. case 'c':
  229. while ((c = *pp++) != '\n' && c != '\0');
  230. break;
  231. case 'p':
  232. sscanf(pp, "%s %d %d\n", tmp, &Nr_vert, &Nr_edges);
  233. stop = 1;
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. free(tmp);
  240. if (Nr_vert == 0 || Nr_edges == 0)
  241. return 0; /* error */
  242. else
  243. return 1;
  244. }
  245. void check ()
  246. {
  247. /* checks that the vertices with color K are an independent set */
  248. register int i,j;
  249. for (i=1;i<=bestsize;i++)
  250. for (j=i+1;j<=bestsize;j++)
  251. if (!edge(bestset[i],bestset[j]))
  252. printf ("Edge in set\n");
  253. }