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