dfmax_boost.cpp 7.1 KB

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