alpar@1
|
1 |
/* glpapi18.c (maximum clique problem) */
|
alpar@1
|
2 |
|
alpar@1
|
3 |
/***********************************************************************
|
alpar@1
|
4 |
* This code is part of GLPK (GNU Linear Programming Kit).
|
alpar@1
|
5 |
*
|
alpar@1
|
6 |
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@1
|
7 |
* 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
|
alpar@1
|
8 |
* Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@1
|
9 |
* E-mail: <mao@gnu.org>.
|
alpar@1
|
10 |
*
|
alpar@1
|
11 |
* GLPK is free software: you can redistribute it and/or modify it
|
alpar@1
|
12 |
* under the terms of the GNU General Public License as published by
|
alpar@1
|
13 |
* the Free Software Foundation, either version 3 of the License, or
|
alpar@1
|
14 |
* (at your option) any later version.
|
alpar@1
|
15 |
*
|
alpar@1
|
16 |
* GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@1
|
17 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@1
|
18 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@1
|
19 |
* License for more details.
|
alpar@1
|
20 |
*
|
alpar@1
|
21 |
* You should have received a copy of the GNU General Public License
|
alpar@1
|
22 |
* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@1
|
23 |
***********************************************************************/
|
alpar@1
|
24 |
|
alpar@1
|
25 |
#include "glpapi.h"
|
alpar@1
|
26 |
#include "glpnet.h"
|
alpar@1
|
27 |
|
alpar@1
|
28 |
static void set_edge(int nv, unsigned char a[], int i, int j)
|
alpar@1
|
29 |
{ int k;
|
alpar@1
|
30 |
xassert(1 <= j && j < i && i <= nv);
|
alpar@1
|
31 |
k = ((i - 1) * (i - 2)) / 2 + (j - 1);
|
alpar@1
|
32 |
a[k / CHAR_BIT] |=
|
alpar@1
|
33 |
(unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT));
|
alpar@1
|
34 |
return;
|
alpar@1
|
35 |
}
|
alpar@1
|
36 |
|
alpar@1
|
37 |
int glp_wclique_exact(glp_graph *G, int v_wgt, double *sol, int v_set)
|
alpar@1
|
38 |
{ /* find maximum weight clique with exact algorithm */
|
alpar@1
|
39 |
glp_arc *e;
|
alpar@1
|
40 |
int i, j, k, len, x, *w, *ind, ret = 0;
|
alpar@1
|
41 |
unsigned char *a;
|
alpar@1
|
42 |
double s, t;
|
alpar@1
|
43 |
if (v_wgt >= 0 && v_wgt > G->v_size - (int)sizeof(double))
|
alpar@1
|
44 |
xerror("glp_wclique_exact: v_wgt = %d; invalid parameter\n",
|
alpar@1
|
45 |
v_wgt);
|
alpar@1
|
46 |
if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
|
alpar@1
|
47 |
xerror("glp_wclique_exact: v_set = %d; invalid parameter\n",
|
alpar@1
|
48 |
v_set);
|
alpar@1
|
49 |
if (G->nv == 0)
|
alpar@1
|
50 |
{ /* empty graph has only empty clique */
|
alpar@1
|
51 |
if (sol != NULL) *sol = 0.0;
|
alpar@1
|
52 |
return 0;
|
alpar@1
|
53 |
}
|
alpar@1
|
54 |
/* allocate working arrays */
|
alpar@1
|
55 |
w = xcalloc(1+G->nv, sizeof(int));
|
alpar@1
|
56 |
ind = xcalloc(1+G->nv, sizeof(int));
|
alpar@1
|
57 |
len = G->nv; /* # vertices */
|
alpar@1
|
58 |
len = len * (len - 1) / 2; /* # entries in lower triangle */
|
alpar@1
|
59 |
len = (len + (CHAR_BIT - 1)) / CHAR_BIT; /* # bytes needed */
|
alpar@1
|
60 |
a = xcalloc(len, sizeof(char));
|
alpar@1
|
61 |
memset(a, 0, len * sizeof(char));
|
alpar@1
|
62 |
/* determine vertex weights */
|
alpar@1
|
63 |
s = 0.0;
|
alpar@1
|
64 |
for (i = 1; i <= G->nv; i++)
|
alpar@1
|
65 |
{ if (v_wgt >= 0)
|
alpar@1
|
66 |
{ memcpy(&t, (char *)G->v[i]->data + v_wgt, sizeof(double));
|
alpar@1
|
67 |
if (!(0.0 <= t && t <= (double)INT_MAX && t == floor(t)))
|
alpar@1
|
68 |
{ ret = GLP_EDATA;
|
alpar@1
|
69 |
goto done;
|
alpar@1
|
70 |
}
|
alpar@1
|
71 |
w[i] = (int)t;
|
alpar@1
|
72 |
}
|
alpar@1
|
73 |
else
|
alpar@1
|
74 |
w[i] = 1;
|
alpar@1
|
75 |
s += (double)w[i];
|
alpar@1
|
76 |
}
|
alpar@1
|
77 |
if (s > (double)INT_MAX)
|
alpar@1
|
78 |
{ ret = GLP_EDATA;
|
alpar@1
|
79 |
goto done;
|
alpar@1
|
80 |
}
|
alpar@1
|
81 |
/* build the adjacency matrix */
|
alpar@1
|
82 |
for (i = 1; i <= G->nv; i++)
|
alpar@1
|
83 |
{ for (e = G->v[i]->in; e != NULL; e = e->h_next)
|
alpar@1
|
84 |
{ j = e->tail->i;
|
alpar@1
|
85 |
/* there exists edge (j,i) in the graph */
|
alpar@1
|
86 |
if (i > j) set_edge(G->nv, a, i, j);
|
alpar@1
|
87 |
}
|
alpar@1
|
88 |
for (e = G->v[i]->out; e != NULL; e = e->t_next)
|
alpar@1
|
89 |
{ j = e->head->i;
|
alpar@1
|
90 |
/* there exists edge (i,j) in the graph */
|
alpar@1
|
91 |
if (i > j) set_edge(G->nv, a, i, j);
|
alpar@1
|
92 |
}
|
alpar@1
|
93 |
}
|
alpar@1
|
94 |
/* find maximum weight clique in the graph */
|
alpar@1
|
95 |
len = wclique(G->nv, w, a, ind);
|
alpar@1
|
96 |
/* compute the clique weight */
|
alpar@1
|
97 |
s = 0.0;
|
alpar@1
|
98 |
for (k = 1; k <= len; k++)
|
alpar@1
|
99 |
{ i = ind[k];
|
alpar@1
|
100 |
xassert(1 <= i && i <= G->nv);
|
alpar@1
|
101 |
s += (double)w[i];
|
alpar@1
|
102 |
}
|
alpar@1
|
103 |
if (sol != NULL) *sol = s;
|
alpar@1
|
104 |
/* mark vertices included in the clique */
|
alpar@1
|
105 |
if (v_set >= 0)
|
alpar@1
|
106 |
{ x = 0;
|
alpar@1
|
107 |
for (i = 1; i <= G->nv; i++)
|
alpar@1
|
108 |
memcpy((char *)G->v[i]->data + v_set, &x, sizeof(int));
|
alpar@1
|
109 |
x = 1;
|
alpar@1
|
110 |
for (k = 1; k <= len; k++)
|
alpar@1
|
111 |
{ i = ind[k];
|
alpar@1
|
112 |
memcpy((char *)G->v[i]->data + v_set, &x, sizeof(int));
|
alpar@1
|
113 |
}
|
alpar@1
|
114 |
}
|
alpar@1
|
115 |
done: /* free working arrays */
|
alpar@1
|
116 |
xfree(w);
|
alpar@1
|
117 |
xfree(ind);
|
alpar@1
|
118 |
xfree(a);
|
alpar@1
|
119 |
return ret;
|
alpar@1
|
120 |
}
|
alpar@1
|
121 |
|
alpar@1
|
122 |
/* eof */
|