glpnet09.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* glpnet09.c */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #include "glpapi.h"
  24. #include "glpnet.h"
  25. /***********************************************************************
  26. * NAME
  27. *
  28. * kellerman - cover edges by cliques with Kellerman's heuristic
  29. *
  30. * SYNOPSIS
  31. *
  32. * #include "glpnet.h"
  33. * int kellerman(int n, int (*func)(void *info, int i, int ind[]),
  34. * void *info, glp_graph *H);
  35. *
  36. * DESCRIPTION
  37. *
  38. * The routine kellerman implements Kellerman's heuristic algorithm
  39. * to find a minimal set of cliques which cover all edges of specified
  40. * graph G = (V, E).
  41. *
  42. * The parameter n specifies the number of vertices |V|, n >= 0.
  43. *
  44. * Formal routine func specifies the set of edges E in the following
  45. * way. Running the routine kellerman calls the routine func and passes
  46. * to it parameter i, which is the number of some vertex, 1 <= i <= n.
  47. * In response the routine func should store numbers of all vertices
  48. * adjacent to vertex i to locations ind[1], ind[2], ..., ind[len] and
  49. * return the value of len, which is the number of adjacent vertices,
  50. * 0 <= len <= n. Self-loops are allowed, but ignored. Multiple edges
  51. * are not allowed.
  52. *
  53. * The parameter info is a transit pointer (magic cookie) passed to the
  54. * formal routine func as its first parameter.
  55. *
  56. * The result provided by the routine kellerman is the bipartite graph
  57. * H = (V union C, F), which defines the covering found. (The program
  58. * object of type glp_graph specified by the parameter H should be
  59. * previously created with the routine glp_create_graph. On entry the
  60. * routine kellerman erases the content of this object with the routine
  61. * glp_erase_graph.) Vertices of first part V correspond to vertices of
  62. * the graph G and have the same ordinal numbers 1, 2, ..., n. Vertices
  63. * of second part C correspond to cliques and have ordinal numbers
  64. * n+1, n+2, ..., n+k, where k is the total number of cliques in the
  65. * edge covering found. Every edge f in F in the program object H is
  66. * represented as arc f = (i->j), where i in V and j in C, which means
  67. * that vertex i of the graph G is in clique C[j], 1 <= j <= k. (Thus,
  68. * if two vertices of the graph G are in the same clique, these vertices
  69. * are adjacent in G, and corresponding edge is covered by that clique.)
  70. *
  71. * RETURNS
  72. *
  73. * The routine Kellerman returns k, the total number of cliques in the
  74. * edge covering found.
  75. *
  76. * REFERENCE
  77. *
  78. * For more details see: glpk/doc/notes/keller.pdf (in Russian). */
  79. struct set
  80. { /* set of vertices */
  81. int size;
  82. /* size (cardinality) of the set, 0 <= card <= n */
  83. int *list; /* int list[1+n]; */
  84. /* the set contains vertices list[1,...,size] */
  85. int *pos; /* int pos[1+n]; */
  86. /* pos[i] > 0 means that vertex i is in the set and
  87. list[pos[i]] = i; pos[i] = 0 means that vertex i is not in
  88. the set */
  89. };
  90. int kellerman(int n, int (*func)(void *info, int i, int ind[]),
  91. void *info, void /* glp_graph */ *H_)
  92. { glp_graph *H = H_;
  93. struct set W_, *W = &W_, V_, *V = &V_;
  94. glp_arc *a;
  95. int i, j, k, m, t, len, card, best;
  96. xassert(n >= 0);
  97. /* H := (V, 0; 0), where V is the set of vertices of graph G */
  98. glp_erase_graph(H, H->v_size, H->a_size);
  99. glp_add_vertices(H, n);
  100. /* W := 0 */
  101. W->size = 0;
  102. W->list = xcalloc(1+n, sizeof(int));
  103. W->pos = xcalloc(1+n, sizeof(int));
  104. memset(&W->pos[1], 0, sizeof(int) * n);
  105. /* V := 0 */
  106. V->size = 0;
  107. V->list = xcalloc(1+n, sizeof(int));
  108. V->pos = xcalloc(1+n, sizeof(int));
  109. memset(&V->pos[1], 0, sizeof(int) * n);
  110. /* main loop */
  111. for (i = 1; i <= n; i++)
  112. { /* W must be empty */
  113. xassert(W->size == 0);
  114. /* W := { j : i > j and (i,j) in E } */
  115. len = func(info, i, W->list);
  116. xassert(0 <= len && len <= n);
  117. for (t = 1; t <= len; t++)
  118. { j = W->list[t];
  119. xassert(1 <= j && j <= n);
  120. if (j >= i) continue;
  121. xassert(W->pos[j] == 0);
  122. W->list[++W->size] = j, W->pos[j] = W->size;
  123. }
  124. /* on i-th iteration we need to cover edges (i,j) for all
  125. j in W */
  126. /* if W is empty, it is a special case */
  127. if (W->size == 0)
  128. { /* set k := k + 1 and create new clique C[k] = { i } */
  129. k = glp_add_vertices(H, 1) - n;
  130. glp_add_arc(H, i, n + k);
  131. continue;
  132. }
  133. /* try to include vertex i into existing cliques */
  134. /* V must be empty */
  135. xassert(V->size == 0);
  136. /* k is the number of cliques found so far */
  137. k = H->nv - n;
  138. for (m = 1; m <= k; m++)
  139. { /* do while V != W; since here V is within W, we can use
  140. equivalent condition: do while |V| < |W| */
  141. if (V->size == W->size) break;
  142. /* check if C[m] is within W */
  143. for (a = H->v[n + m]->in; a != NULL; a = a->h_next)
  144. { j = a->tail->i;
  145. if (W->pos[j] == 0) break;
  146. }
  147. if (a != NULL) continue;
  148. /* C[m] is within W, expand clique C[m] with vertex i */
  149. /* C[m] := C[m] union {i} */
  150. glp_add_arc(H, i, n + m);
  151. /* V is a set of vertices whose incident edges are already
  152. covered by existing cliques */
  153. /* V := V union C[m] */
  154. for (a = H->v[n + m]->in; a != NULL; a = a->h_next)
  155. { j = a->tail->i;
  156. if (V->pos[j] == 0)
  157. V->list[++V->size] = j, V->pos[j] = V->size;
  158. }
  159. }
  160. /* remove from set W the vertices whose incident edges are
  161. already covered by existing cliques */
  162. /* W := W \ V, V := 0 */
  163. for (t = 1; t <= V->size; t++)
  164. { j = V->list[t], V->pos[j] = 0;
  165. if (W->pos[j] != 0)
  166. { /* remove vertex j from W */
  167. if (W->pos[j] != W->size)
  168. { int jj = W->list[W->size];
  169. W->list[W->pos[j]] = jj;
  170. W->pos[jj] = W->pos[j];
  171. }
  172. W->size--, W->pos[j] = 0;
  173. }
  174. }
  175. V->size = 0;
  176. /* now set W contains only vertices whose incident edges are
  177. still not covered by existing cliques; create new cliques
  178. to cover remaining edges until set W becomes empty */
  179. while (W->size > 0)
  180. { /* find clique C[m], 1 <= m <= k, which shares maximal
  181. number of vertices with W; to break ties choose clique
  182. having smallest number m */
  183. m = 0, best = -1;
  184. k = H->nv - n;
  185. for (t = 1; t <= k; t++)
  186. { /* compute cardinality of intersection of W and C[t] */
  187. card = 0;
  188. for (a = H->v[n + t]->in; a != NULL; a = a->h_next)
  189. { j = a->tail->i;
  190. if (W->pos[j] != 0) card++;
  191. }
  192. if (best < card)
  193. m = t, best = card;
  194. }
  195. xassert(m > 0);
  196. /* set k := k + 1 and create new clique:
  197. C[k] := (W intersect C[m]) union { i }, which covers all
  198. edges incident to vertices from (W intersect C[m]) */
  199. k = glp_add_vertices(H, 1) - n;
  200. for (a = H->v[n + m]->in; a != NULL; a = a->h_next)
  201. { j = a->tail->i;
  202. if (W->pos[j] != 0)
  203. { /* vertex j is in both W and C[m]; include it in new
  204. clique C[k] */
  205. glp_add_arc(H, j, n + k);
  206. /* remove vertex j from W, since edge (i,j) will be
  207. covered by new clique C[k] */
  208. if (W->pos[j] != W->size)
  209. { int jj = W->list[W->size];
  210. W->list[W->pos[j]] = jj;
  211. W->pos[jj] = W->pos[j];
  212. }
  213. W->size--, W->pos[j] = 0;
  214. }
  215. }
  216. /* include vertex i to new clique C[k] to cover edges (i,j)
  217. incident to all vertices j just removed from W */
  218. glp_add_arc(H, i, n + k);
  219. }
  220. }
  221. /* free working arrays */
  222. xfree(W->list);
  223. xfree(W->pos);
  224. xfree(V->list);
  225. xfree(V->pos);
  226. /* return the number of cliques in the edge covering found */
  227. return H->nv - n;
  228. }
  229. /* eof */