aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/api/wcliqex.c
blob: 53c2d5215274ea160e4990e2cd31995501f2d46f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* wcliqex.c (find maximum weight clique with exact algorithm) */

/***********************************************************************
*  This code is part of GLPK (GNU Linear Programming Kit).
*
*  Copyright (C) 2009-2016 Andrew Makhorin, Department for Applied
*  Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
*  reserved. E-mail: <mao@gnu.org>.
*
*  GLPK is free software: you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
*  License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

#include "env.h"
#include "glpk.h"
#include "wclique.h"

static void set_edge(int nv, unsigned char a[], int i, int j)
{     int k;
      xassert(1 <= j && j < i && i <= nv);
      k = ((i - 1) * (i - 2)) / 2 + (j - 1);
      a[k / CHAR_BIT] |=
         (unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT));
      return;
}

int glp_wclique_exact(glp_graph *G, int v_wgt, double *sol, int v_set)
{     /* find maximum weight clique with exact algorithm */
      glp_arc *e;
      int i, j, k, len, x, *w, *ind, ret = 0;
      unsigned char *a;
      double s, t;
      if (v_wgt >= 0 && v_wgt > G->v_size - (int)sizeof(double))
         xerror("glp_wclique_exact: v_wgt = %d; invalid parameter\n",
            v_wgt);
      if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
         xerror("glp_wclique_exact: v_set = %d; invalid parameter\n",
            v_set);
      if (G->nv == 0)
      {  /* empty graph has only empty clique */
         if (sol != NULL) *sol = 0.0;
         return 0;
      }
      /* allocate working arrays */
      w = xcalloc(1+G->nv, sizeof(int));
      ind = xcalloc(1+G->nv, sizeof(int));
      len = G->nv; /* # vertices */
      len = len * (len - 1) / 2; /* # entries in lower triangle */
      len = (len + (CHAR_BIT - 1)) / CHAR_BIT; /* # bytes needed */
      a = xcalloc(len, sizeof(char));
      memset(a, 0, len * sizeof(char));
      /* determine vertex weights */
      s = 0.0;
      for (i = 1; i <= G->nv; i++)
      {  if (v_wgt >= 0)
         {  memcpy(&t, (char *)G->v[i]->data + v_wgt, sizeof(double));
            if (!(0.0 <= t && t <= (double)INT_MAX && t == floor(t)))
            {  ret = GLP_EDATA;
               goto done;
            }
            w[i] = (int)t;
         }
         else
            w[i] = 1;
         s += (double)w[i];
      }
      if (s > (double)INT_MAX)
      {  ret = GLP_EDATA;
         goto done;
      }
      /* build the adjacency matrix */
      for (i = 1; i <= G->nv; i++)
      {  for (e = G->v[i]->in; e != NULL; e = e->h_next)
         {  j = e->tail->i;
            /* there exists edge (j,i) in the graph */
            if (i > j) set_edge(G->nv, a, i, j);
         }
         for (e = G->v[i]->out; e != NULL; e = e->t_next)
         {  j = e->head->i;
            /* there exists edge (i,j) in the graph */
            if (i > j) set_edge(G->nv, a, i, j);
         }
      }
      /* find maximum weight clique in the graph */
      len = wclique(G->nv, w, a, ind);
      /* compute the clique weight */
      s = 0.0;
      for (k = 1; k <= len; k++)
      {  i = ind[k];
         xassert(1 <= i && i <= G->nv);
         s += (double)w[i];
      }
      if (sol != NULL) *sol = s;
      /* mark vertices included in the clique */
      if (v_set >= 0)
      {  x = 0;
         for (i = 1; i <= G->nv; i++)
            memcpy((char *)G->v[i]->data + v_set, &x, sizeof(int));
         x = 1;
         for (k = 1; k <= len; k++)
         {  i = ind[k];
            memcpy((char *)G->v[i]->data + v_set, &x, sizeof(int));
         }
      }
done: /* free working arrays */
      xfree(w);
      xfree(ind);
      xfree(a);
      return ret;
}

/* eof */