glpnet08.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* glpnet08.c */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Two subroutines sub() and wclique() below are intended to find a
  6. * maximum weight clique in a given undirected graph. These subroutines
  7. * are slightly modified version of the program WCLIQUE developed by
  8. * Patric Ostergard <http://www.tcs.hut.fi/~pat/wclique.html> and based
  9. * on ideas from the article "P. R. J. Ostergard, A new algorithm for
  10. * the maximum-weight clique problem, submitted for publication", which
  11. * in turn is a generalization of the algorithm for unweighted graphs
  12. * presented in "P. R. J. Ostergard, A fast algorithm for the maximum
  13. * clique problem, submitted for publication".
  14. *
  15. * USED WITH PERMISSION OF THE AUTHOR OF THE ORIGINAL CODE.
  16. *
  17. * Changes were made by Andrew Makhorin <mao@gnu.org>.
  18. *
  19. * GLPK is free software: you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation, either version 3 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  25. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  26. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  27. * License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  31. ***********************************************************************/
  32. #include "glpenv.h"
  33. #include "glpnet.h"
  34. /***********************************************************************
  35. * NAME
  36. *
  37. * wclique - find maximum weight clique with Ostergard's algorithm
  38. *
  39. * SYNOPSIS
  40. *
  41. * int wclique(int n, const int w[], const unsigned char a[],
  42. * int ind[]);
  43. *
  44. * DESCRIPTION
  45. *
  46. * The routine wclique finds a maximum weight clique in an undirected
  47. * graph with Ostergard's algorithm.
  48. *
  49. * INPUT PARAMETERS
  50. *
  51. * n is the number of vertices, n > 0.
  52. *
  53. * w[i], i = 1,...,n, is a weight of vertex i.
  54. *
  55. * a[*] is the strict (without main diagonal) lower triangle of the
  56. * graph adjacency matrix in packed format.
  57. *
  58. * OUTPUT PARAMETER
  59. *
  60. * ind[k], k = 1,...,size, is the number of a vertex included in the
  61. * clique found, 1 <= ind[k] <= n, where size is the number of vertices
  62. * in the clique returned on exit.
  63. *
  64. * RETURNS
  65. *
  66. * The routine returns the clique size, i.e. the number of vertices in
  67. * the clique. */
  68. struct csa
  69. { /* common storage area */
  70. int n;
  71. /* number of vertices */
  72. const int *wt; /* int wt[0:n-1]; */
  73. /* weights */
  74. const unsigned char *a;
  75. /* adjacency matrix (packed lower triangle without main diag.) */
  76. int record;
  77. /* weight of best clique */
  78. int rec_level;
  79. /* number of vertices in best clique */
  80. int *rec; /* int rec[0:n-1]; */
  81. /* best clique so far */
  82. int *clique; /* int clique[0:n-1]; */
  83. /* table for pruning */
  84. int *set; /* int set[0:n-1]; */
  85. /* current clique */
  86. };
  87. #define n (csa->n)
  88. #define wt (csa->wt)
  89. #define a (csa->a)
  90. #define record (csa->record)
  91. #define rec_level (csa->rec_level)
  92. #define rec (csa->rec)
  93. #define clique (csa->clique)
  94. #define set (csa->set)
  95. #if 0
  96. static int is_edge(struct csa *csa, int i, int j)
  97. { /* if there is arc (i,j), the routine returns true; otherwise
  98. false; 0 <= i, j < n */
  99. int k;
  100. xassert(0 <= i && i < n);
  101. xassert(0 <= j && j < n);
  102. if (i == j) return 0;
  103. if (i < j) k = i, i = j, j = k;
  104. k = (i * (i - 1)) / 2 + j;
  105. return a[k / CHAR_BIT] &
  106. (unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT));
  107. }
  108. #else
  109. #define is_edge(csa, i, j) ((i) == (j) ? 0 : \
  110. (i) > (j) ? is_edge1(i, j) : is_edge1(j, i))
  111. #define is_edge1(i, j) is_edge2(((i) * ((i) - 1)) / 2 + (j))
  112. #define is_edge2(k) (a[(k) / CHAR_BIT] & \
  113. (unsigned char)(1 << ((CHAR_BIT - 1) - (k) % CHAR_BIT)))
  114. #endif
  115. static void sub(struct csa *csa, int ct, int table[], int level,
  116. int weight, int l_weight)
  117. { int i, j, k, curr_weight, left_weight, *p1, *p2, *newtable;
  118. newtable = xcalloc(n, sizeof(int));
  119. if (ct <= 0)
  120. { /* 0 or 1 elements left; include these */
  121. if (ct == 0)
  122. { set[level++] = table[0];
  123. weight += l_weight;
  124. }
  125. if (weight > record)
  126. { record = weight;
  127. rec_level = level;
  128. for (i = 0; i < level; i++) rec[i] = set[i];
  129. }
  130. goto done;
  131. }
  132. for (i = ct; i >= 0; i--)
  133. { if ((level == 0) && (i < ct)) goto done;
  134. k = table[i];
  135. if ((level > 0) && (clique[k] <= (record - weight)))
  136. goto done; /* prune */
  137. set[level] = k;
  138. curr_weight = weight + wt[k];
  139. l_weight -= wt[k];
  140. if (l_weight <= (record - curr_weight))
  141. goto done; /* prune */
  142. p1 = newtable;
  143. p2 = table;
  144. left_weight = 0;
  145. while (p2 < table + i)
  146. { j = *p2++;
  147. if (is_edge(csa, j, k))
  148. { *p1++ = j;
  149. left_weight += wt[j];
  150. }
  151. }
  152. if (left_weight <= (record - curr_weight)) continue;
  153. sub(csa, p1 - newtable - 1, newtable, level + 1, curr_weight,
  154. left_weight);
  155. }
  156. done: xfree(newtable);
  157. return;
  158. }
  159. int wclique(int _n, const int w[], const unsigned char _a[], int ind[])
  160. { struct csa _csa, *csa = &_csa;
  161. int i, j, p, max_wt, max_nwt, wth, *used, *nwt, *pos;
  162. glp_long timer;
  163. n = _n;
  164. xassert(n > 0);
  165. wt = &w[1];
  166. a = _a;
  167. record = 0;
  168. rec_level = 0;
  169. rec = &ind[1];
  170. clique = xcalloc(n, sizeof(int));
  171. set = xcalloc(n, sizeof(int));
  172. used = xcalloc(n, sizeof(int));
  173. nwt = xcalloc(n, sizeof(int));
  174. pos = xcalloc(n, sizeof(int));
  175. /* start timer */
  176. timer = xtime();
  177. /* order vertices */
  178. for (i = 0; i < n; i++)
  179. { nwt[i] = 0;
  180. for (j = 0; j < n; j++)
  181. if (is_edge(csa, i, j)) nwt[i] += wt[j];
  182. }
  183. for (i = 0; i < n; i++)
  184. used[i] = 0;
  185. for (i = n-1; i >= 0; i--)
  186. { max_wt = -1;
  187. max_nwt = -1;
  188. for (j = 0; j < n; j++)
  189. { if ((!used[j]) && ((wt[j] > max_wt) || (wt[j] == max_wt
  190. && nwt[j] > max_nwt)))
  191. { max_wt = wt[j];
  192. max_nwt = nwt[j];
  193. p = j;
  194. }
  195. }
  196. pos[i] = p;
  197. used[p] = 1;
  198. for (j = 0; j < n; j++)
  199. if ((!used[j]) && (j != p) && (is_edge(csa, p, j)))
  200. nwt[j] -= wt[p];
  201. }
  202. /* main routine */
  203. wth = 0;
  204. for (i = 0; i < n; i++)
  205. { wth += wt[pos[i]];
  206. sub(csa, i, pos, 0, 0, wth);
  207. clique[pos[i]] = record;
  208. if (xdifftime(xtime(), timer) >= 5.0 - 0.001)
  209. { /* print current record and reset timer */
  210. xprintf("level = %d (%d); best = %d\n", i+1, n, record);
  211. timer = xtime();
  212. }
  213. }
  214. xfree(clique);
  215. xfree(set);
  216. xfree(used);
  217. xfree(nwt);
  218. xfree(pos);
  219. /* return the solution found */
  220. for (i = 1; i <= rec_level; i++) ind[i]++;
  221. return rec_level;
  222. }
  223. #undef n
  224. #undef wt
  225. #undef a
  226. #undef record
  227. #undef rec_level
  228. #undef rec
  229. #undef clique
  230. #undef set
  231. /* eof */