glpapi18.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* glpapi18.c (maximum clique problem) */
  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. static void set_edge(int nv, unsigned char a[], int i, int j)
  26. { int k;
  27. xassert(1 <= j && j < i && i <= nv);
  28. k = ((i - 1) * (i - 2)) / 2 + (j - 1);
  29. a[k / CHAR_BIT] |=
  30. (unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT));
  31. return;
  32. }
  33. int glp_wclique_exact(glp_graph *G, int v_wgt, double *sol, int v_set)
  34. { /* find maximum weight clique with exact algorithm */
  35. glp_arc *e;
  36. int i, j, k, len, x, *w, *ind, ret = 0;
  37. unsigned char *a;
  38. double s, t;
  39. if (v_wgt >= 0 && v_wgt > G->v_size - (int)sizeof(double))
  40. xerror("glp_wclique_exact: v_wgt = %d; invalid parameter\n",
  41. v_wgt);
  42. if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
  43. xerror("glp_wclique_exact: v_set = %d; invalid parameter\n",
  44. v_set);
  45. if (G->nv == 0)
  46. { /* empty graph has only empty clique */
  47. if (sol != NULL) *sol = 0.0;
  48. return 0;
  49. }
  50. /* allocate working arrays */
  51. w = xcalloc(1+G->nv, sizeof(int));
  52. ind = xcalloc(1+G->nv, sizeof(int));
  53. len = G->nv; /* # vertices */
  54. len = len * (len - 1) / 2; /* # entries in lower triangle */
  55. len = (len + (CHAR_BIT - 1)) / CHAR_BIT; /* # bytes needed */
  56. a = xcalloc(len, sizeof(char));
  57. memset(a, 0, len * sizeof(char));
  58. /* determine vertex weights */
  59. s = 0.0;
  60. for (i = 1; i <= G->nv; i++)
  61. { if (v_wgt >= 0)
  62. { memcpy(&t, (char *)G->v[i]->data + v_wgt, sizeof(double));
  63. if (!(0.0 <= t && t <= (double)INT_MAX && t == floor(t)))
  64. { ret = GLP_EDATA;
  65. goto done;
  66. }
  67. w[i] = (int)t;
  68. }
  69. else
  70. w[i] = 1;
  71. s += (double)w[i];
  72. }
  73. if (s > (double)INT_MAX)
  74. { ret = GLP_EDATA;
  75. goto done;
  76. }
  77. /* build the adjacency matrix */
  78. for (i = 1; i <= G->nv; i++)
  79. { for (e = G->v[i]->in; e != NULL; e = e->h_next)
  80. { j = e->tail->i;
  81. /* there exists edge (j,i) in the graph */
  82. if (i > j) set_edge(G->nv, a, i, j);
  83. }
  84. for (e = G->v[i]->out; e != NULL; e = e->t_next)
  85. { j = e->head->i;
  86. /* there exists edge (i,j) in the graph */
  87. if (i > j) set_edge(G->nv, a, i, j);
  88. }
  89. }
  90. /* find maximum weight clique in the graph */
  91. len = wclique(G->nv, w, a, ind);
  92. /* compute the clique weight */
  93. s = 0.0;
  94. for (k = 1; k <= len; k++)
  95. { i = ind[k];
  96. xassert(1 <= i && i <= G->nv);
  97. s += (double)w[i];
  98. }
  99. if (sol != NULL) *sol = s;
  100. /* mark vertices included in the clique */
  101. if (v_set >= 0)
  102. { x = 0;
  103. for (i = 1; i <= G->nv; i++)
  104. memcpy((char *)G->v[i]->data + v_set, &x, sizeof(int));
  105. x = 1;
  106. for (k = 1; k <= len; k++)
  107. { i = ind[k];
  108. memcpy((char *)G->v[i]->data + v_set, &x, sizeof(int));
  109. }
  110. }
  111. done: /* free working arrays */
  112. xfree(w);
  113. xfree(ind);
  114. xfree(a);
  115. return ret;
  116. }
  117. /* eof */