3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Two subroutines sub() and wclique() below are intended to find a
7 * maximum weight clique in a given undirected graph. These subroutines
8 * are slightly modified version of the program WCLIQUE developed by
9 * Patric Ostergard <http://www.tcs.hut.fi/~pat/wclique.html> and based
10 * on ideas from the article "P. R. J. Ostergard, A new algorithm for
11 * the maximum-weight clique problem, submitted for publication", which
12 * in turn is a generalization of the algorithm for unweighted graphs
13 * presented in "P. R. J. Ostergard, A fast algorithm for the maximum
14 * clique problem, submitted for publication".
16 * USED WITH PERMISSION OF THE AUTHOR OF THE ORIGINAL CODE.
18 * Changes were made by Andrew Makhorin <mao@gnu.org>.
20 * GLPK is free software: you can redistribute it and/or modify it
21 * under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * GLPK is distributed in the hope that it will be useful, but WITHOUT
26 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
28 * License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
32 ***********************************************************************/
37 /***********************************************************************
40 * wclique - find maximum weight clique with Ostergard's algorithm
44 * int wclique(int n, const int w[], const unsigned char a[],
49 * The routine wclique finds a maximum weight clique in an undirected
50 * graph with Ostergard's algorithm.
54 * n is the number of vertices, n > 0.
56 * w[i], i = 1,...,n, is a weight of vertex i.
58 * a[*] is the strict (without main diagonal) lower triangle of the
59 * graph adjacency matrix in packed format.
63 * ind[k], k = 1,...,size, is the number of a vertex included in the
64 * clique found, 1 <= ind[k] <= n, where size is the number of vertices
65 * in the clique returned on exit.
69 * The routine returns the clique size, i.e. the number of vertices in
73 { /* common storage area */
75 /* number of vertices */
76 const int *wt; /* int wt[0:n-1]; */
78 const unsigned char *a;
79 /* adjacency matrix (packed lower triangle without main diag.) */
81 /* weight of best clique */
83 /* number of vertices in best clique */
84 int *rec; /* int rec[0:n-1]; */
85 /* best clique so far */
86 int *clique; /* int clique[0:n-1]; */
87 /* table for pruning */
88 int *set; /* int set[0:n-1]; */
95 #define record (csa->record)
96 #define rec_level (csa->rec_level)
97 #define rec (csa->rec)
98 #define clique (csa->clique)
99 #define set (csa->set)
102 static int is_edge(struct csa *csa, int i, int j)
103 { /* if there is arc (i,j), the routine returns true; otherwise
104 false; 0 <= i, j < n */
106 xassert(0 <= i && i < n);
107 xassert(0 <= j && j < n);
108 if (i == j) return 0;
109 if (i < j) k = i, i = j, j = k;
110 k = (i * (i - 1)) / 2 + j;
111 return a[k / CHAR_BIT] &
112 (unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT));
115 #define is_edge(csa, i, j) ((i) == (j) ? 0 : \
116 (i) > (j) ? is_edge1(i, j) : is_edge1(j, i))
117 #define is_edge1(i, j) is_edge2(((i) * ((i) - 1)) / 2 + (j))
118 #define is_edge2(k) (a[(k) / CHAR_BIT] & \
119 (unsigned char)(1 << ((CHAR_BIT - 1) - (k) % CHAR_BIT)))
122 static void sub(struct csa *csa, int ct, int table[], int level,
123 int weight, int l_weight)
124 { int i, j, k, curr_weight, left_weight, *p1, *p2, *newtable;
125 newtable = xcalloc(n, sizeof(int));
127 { /* 0 or 1 elements left; include these */
129 { set[level++] = table[0];
135 for (i = 0; i < level; i++) rec[i] = set[i];
139 for (i = ct; i >= 0; i--)
140 { if ((level == 0) && (i < ct)) goto done;
142 if ((level > 0) && (clique[k] <= (record - weight)))
143 goto done; /* prune */
145 curr_weight = weight + wt[k];
147 if (l_weight <= (record - curr_weight))
148 goto done; /* prune */
152 while (p2 < table + i)
154 if (is_edge(csa, j, k))
156 left_weight += wt[j];
159 if (left_weight <= (record - curr_weight)) continue;
160 sub(csa, p1 - newtable - 1, newtable, level + 1, curr_weight,
163 done: xfree(newtable);
167 int wclique(int _n, const int w[], const unsigned char _a[], int ind[])
168 { struct csa _csa, *csa = &_csa;
169 int i, j, p, max_wt, max_nwt, wth, *used, *nwt, *pos;
178 clique = xcalloc(n, sizeof(int));
179 set = xcalloc(n, sizeof(int));
180 used = xcalloc(n, sizeof(int));
181 nwt = xcalloc(n, sizeof(int));
182 pos = xcalloc(n, sizeof(int));
186 for (i = 0; i < n; i++)
188 for (j = 0; j < n; j++)
189 if (is_edge(csa, i, j)) nwt[i] += wt[j];
191 for (i = 0; i < n; i++)
193 for (i = n-1; i >= 0; i--)
196 for (j = 0; j < n; j++)
197 { if ((!used[j]) && ((wt[j] > max_wt) || (wt[j] == max_wt
198 && nwt[j] > max_nwt)))
206 for (j = 0; j < n; j++)
207 if ((!used[j]) && (j != p) && (is_edge(csa, p, j)))
212 for (i = 0; i < n; i++)
214 sub(csa, i, pos, 0, 0, wth);
215 clique[pos[i]] = record;
216 if (xdifftime(xtime(), timer) >= 5.0 - 0.001)
217 { /* print current record and reset timer */
218 xprintf("level = %d (%d); best = %d\n", i+1, n, record);
227 /* return the solution found */
228 for (i = 1; i <= rec_level; i++) ind[i]++;