alpar@1: /* glpnet08.c */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Two subroutines sub() and wclique() below are intended to find a alpar@1: * maximum weight clique in a given undirected graph. These subroutines alpar@1: * are slightly modified version of the program WCLIQUE developed by alpar@1: * Patric Ostergard and based alpar@1: * on ideas from the article "P. R. J. Ostergard, A new algorithm for alpar@1: * the maximum-weight clique problem, submitted for publication", which alpar@1: * in turn is a generalization of the algorithm for unweighted graphs alpar@1: * presented in "P. R. J. Ostergard, A fast algorithm for the maximum alpar@1: * clique problem, submitted for publication". alpar@1: * alpar@1: * USED WITH PERMISSION OF THE AUTHOR OF THE ORIGINAL CODE. alpar@1: * alpar@1: * Changes were made by Andrew Makhorin . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include "glpenv.h" alpar@1: #include "glpnet.h" alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * wclique - find maximum weight clique with Ostergard's algorithm alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * int wclique(int n, const int w[], const unsigned char a[], alpar@1: * int ind[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine wclique finds a maximum weight clique in an undirected alpar@1: * graph with Ostergard's algorithm. alpar@1: * alpar@1: * INPUT PARAMETERS alpar@1: * alpar@1: * n is the number of vertices, n > 0. alpar@1: * alpar@1: * w[i], i = 1,...,n, is a weight of vertex i. alpar@1: * alpar@1: * a[*] is the strict (without main diagonal) lower triangle of the alpar@1: * graph adjacency matrix in packed format. alpar@1: * alpar@1: * OUTPUT PARAMETER alpar@1: * alpar@1: * ind[k], k = 1,...,size, is the number of a vertex included in the alpar@1: * clique found, 1 <= ind[k] <= n, where size is the number of vertices alpar@1: * in the clique returned on exit. alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * The routine returns the clique size, i.e. the number of vertices in alpar@1: * the clique. */ alpar@1: alpar@1: struct csa alpar@1: { /* common storage area */ alpar@1: int n; alpar@1: /* number of vertices */ alpar@1: const int *wt; /* int wt[0:n-1]; */ alpar@1: /* weights */ alpar@1: const unsigned char *a; alpar@1: /* adjacency matrix (packed lower triangle without main diag.) */ alpar@1: int record; alpar@1: /* weight of best clique */ alpar@1: int rec_level; alpar@1: /* number of vertices in best clique */ alpar@1: int *rec; /* int rec[0:n-1]; */ alpar@1: /* best clique so far */ alpar@1: int *clique; /* int clique[0:n-1]; */ alpar@1: /* table for pruning */ alpar@1: int *set; /* int set[0:n-1]; */ alpar@1: /* current clique */ alpar@1: }; alpar@1: alpar@1: #define n (csa->n) alpar@1: #define wt (csa->wt) alpar@1: #define a (csa->a) alpar@1: #define record (csa->record) alpar@1: #define rec_level (csa->rec_level) alpar@1: #define rec (csa->rec) alpar@1: #define clique (csa->clique) alpar@1: #define set (csa->set) alpar@1: alpar@1: #if 0 alpar@1: static int is_edge(struct csa *csa, int i, int j) alpar@1: { /* if there is arc (i,j), the routine returns true; otherwise alpar@1: false; 0 <= i, j < n */ alpar@1: int k; alpar@1: xassert(0 <= i && i < n); alpar@1: xassert(0 <= j && j < n); alpar@1: if (i == j) return 0; alpar@1: if (i < j) k = i, i = j, j = k; alpar@1: k = (i * (i - 1)) / 2 + j; alpar@1: return a[k / CHAR_BIT] & alpar@1: (unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT)); alpar@1: } alpar@1: #else alpar@1: #define is_edge(csa, i, j) ((i) == (j) ? 0 : \ alpar@1: (i) > (j) ? is_edge1(i, j) : is_edge1(j, i)) alpar@1: #define is_edge1(i, j) is_edge2(((i) * ((i) - 1)) / 2 + (j)) alpar@1: #define is_edge2(k) (a[(k) / CHAR_BIT] & \ alpar@1: (unsigned char)(1 << ((CHAR_BIT - 1) - (k) % CHAR_BIT))) alpar@1: #endif alpar@1: alpar@1: static void sub(struct csa *csa, int ct, int table[], int level, alpar@1: int weight, int l_weight) alpar@1: { int i, j, k, curr_weight, left_weight, *p1, *p2, *newtable; alpar@1: newtable = xcalloc(n, sizeof(int)); alpar@1: if (ct <= 0) alpar@1: { /* 0 or 1 elements left; include these */ alpar@1: if (ct == 0) alpar@1: { set[level++] = table[0]; alpar@1: weight += l_weight; alpar@1: } alpar@1: if (weight > record) alpar@1: { record = weight; alpar@1: rec_level = level; alpar@1: for (i = 0; i < level; i++) rec[i] = set[i]; alpar@1: } alpar@1: goto done; alpar@1: } alpar@1: for (i = ct; i >= 0; i--) alpar@1: { if ((level == 0) && (i < ct)) goto done; alpar@1: k = table[i]; alpar@1: if ((level > 0) && (clique[k] <= (record - weight))) alpar@1: goto done; /* prune */ alpar@1: set[level] = k; alpar@1: curr_weight = weight + wt[k]; alpar@1: l_weight -= wt[k]; alpar@1: if (l_weight <= (record - curr_weight)) alpar@1: goto done; /* prune */ alpar@1: p1 = newtable; alpar@1: p2 = table; alpar@1: left_weight = 0; alpar@1: while (p2 < table + i) alpar@1: { j = *p2++; alpar@1: if (is_edge(csa, j, k)) alpar@1: { *p1++ = j; alpar@1: left_weight += wt[j]; alpar@1: } alpar@1: } alpar@1: if (left_weight <= (record - curr_weight)) continue; alpar@1: sub(csa, p1 - newtable - 1, newtable, level + 1, curr_weight, alpar@1: left_weight); alpar@1: } alpar@1: done: xfree(newtable); alpar@1: return; alpar@1: } alpar@1: alpar@1: int wclique(int _n, const int w[], const unsigned char _a[], int ind[]) alpar@1: { struct csa _csa, *csa = &_csa; alpar@1: int i, j, p, max_wt, max_nwt, wth, *used, *nwt, *pos; alpar@1: glp_long timer; alpar@1: n = _n; alpar@1: xassert(n > 0); alpar@1: wt = &w[1]; alpar@1: a = _a; alpar@1: record = 0; alpar@1: rec_level = 0; alpar@1: rec = &ind[1]; alpar@1: clique = xcalloc(n, sizeof(int)); alpar@1: set = xcalloc(n, sizeof(int)); alpar@1: used = xcalloc(n, sizeof(int)); alpar@1: nwt = xcalloc(n, sizeof(int)); alpar@1: pos = xcalloc(n, sizeof(int)); alpar@1: /* start timer */ alpar@1: timer = xtime(); alpar@1: /* order vertices */ alpar@1: for (i = 0; i < n; i++) alpar@1: { nwt[i] = 0; alpar@1: for (j = 0; j < n; j++) alpar@1: if (is_edge(csa, i, j)) nwt[i] += wt[j]; alpar@1: } alpar@1: for (i = 0; i < n; i++) alpar@1: used[i] = 0; alpar@1: for (i = n-1; i >= 0; i--) alpar@1: { max_wt = -1; alpar@1: max_nwt = -1; alpar@1: for (j = 0; j < n; j++) alpar@1: { if ((!used[j]) && ((wt[j] > max_wt) || (wt[j] == max_wt alpar@1: && nwt[j] > max_nwt))) alpar@1: { max_wt = wt[j]; alpar@1: max_nwt = nwt[j]; alpar@1: p = j; alpar@1: } alpar@1: } alpar@1: pos[i] = p; alpar@1: used[p] = 1; alpar@1: for (j = 0; j < n; j++) alpar@1: if ((!used[j]) && (j != p) && (is_edge(csa, p, j))) alpar@1: nwt[j] -= wt[p]; alpar@1: } alpar@1: /* main routine */ alpar@1: wth = 0; alpar@1: for (i = 0; i < n; i++) alpar@1: { wth += wt[pos[i]]; alpar@1: sub(csa, i, pos, 0, 0, wth); alpar@1: clique[pos[i]] = record; alpar@1: if (xdifftime(xtime(), timer) >= 5.0 - 0.001) alpar@1: { /* print current record and reset timer */ alpar@1: xprintf("level = %d (%d); best = %d\n", i+1, n, record); alpar@1: timer = xtime(); alpar@1: } alpar@1: } alpar@1: xfree(clique); alpar@1: xfree(set); alpar@1: xfree(used); alpar@1: xfree(nwt); alpar@1: xfree(pos); alpar@1: /* return the solution found */ alpar@1: for (i = 1; i <= rec_level; i++) ind[i]++; alpar@1: return rec_level; alpar@1: } alpar@1: alpar@1: #undef n alpar@1: #undef wt alpar@1: #undef a alpar@1: #undef record alpar@1: #undef rec_level alpar@1: #undef rec alpar@1: #undef clique alpar@1: #undef set alpar@1: alpar@1: /* eof */