src/glpnet08.c
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 05 Dec 2010 17:35:23 +0100
changeset 2 4c8956a7bdf4
permissions -rw-r--r--
Set up CMAKE build environment
alpar@1
     1
/* glpnet08.c */
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
*  Two subroutines sub() and wclique() below are intended to find a
alpar@1
     7
*  maximum weight clique in a given undirected graph. These subroutines
alpar@1
     8
*  are slightly modified version of the program WCLIQUE developed by
alpar@1
     9
*  Patric Ostergard <http://www.tcs.hut.fi/~pat/wclique.html> and based
alpar@1
    10
*  on ideas from the article "P. R. J. Ostergard, A new algorithm for
alpar@1
    11
*  the maximum-weight clique problem, submitted for publication", which
alpar@1
    12
*  in turn is a generalization of the algorithm for unweighted graphs
alpar@1
    13
*  presented in "P. R. J. Ostergard, A fast algorithm for the maximum
alpar@1
    14
*  clique problem, submitted for publication".
alpar@1
    15
*
alpar@1
    16
*  USED WITH PERMISSION OF THE AUTHOR OF THE ORIGINAL CODE.
alpar@1
    17
*
alpar@1
    18
*  Changes were made by Andrew Makhorin <mao@gnu.org>.
alpar@1
    19
*
alpar@1
    20
*  GLPK is free software: you can redistribute it and/or modify it
alpar@1
    21
*  under the terms of the GNU General Public License as published by
alpar@1
    22
*  the Free Software Foundation, either version 3 of the License, or
alpar@1
    23
*  (at your option) any later version.
alpar@1
    24
*
alpar@1
    25
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
alpar@1
    26
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
alpar@1
    27
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
alpar@1
    28
*  License for more details.
alpar@1
    29
*
alpar@1
    30
*  You should have received a copy of the GNU General Public License
alpar@1
    31
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
alpar@1
    32
***********************************************************************/
alpar@1
    33
alpar@1
    34
#include "glpenv.h"
alpar@1
    35
#include "glpnet.h"
alpar@1
    36
alpar@1
    37
/***********************************************************************
alpar@1
    38
*  NAME
alpar@1
    39
*
alpar@1
    40
*  wclique - find maximum weight clique with Ostergard's algorithm
alpar@1
    41
*
alpar@1
    42
*  SYNOPSIS
alpar@1
    43
*
alpar@1
    44
*  int wclique(int n, const int w[], const unsigned char a[],
alpar@1
    45
*     int ind[]);
alpar@1
    46
*
alpar@1
    47
*  DESCRIPTION
alpar@1
    48
*
alpar@1
    49
*  The routine wclique finds a maximum weight clique in an undirected
alpar@1
    50
*  graph with Ostergard's algorithm.
alpar@1
    51
*
alpar@1
    52
*  INPUT PARAMETERS
alpar@1
    53
*
alpar@1
    54
*  n is the number of vertices, n > 0.
alpar@1
    55
*
alpar@1
    56
*  w[i], i = 1,...,n, is a weight of vertex i.
alpar@1
    57
*
alpar@1
    58
*  a[*] is the strict (without main diagonal) lower triangle of the
alpar@1
    59
*  graph adjacency matrix in packed format.
alpar@1
    60
*
alpar@1
    61
*  OUTPUT PARAMETER
alpar@1
    62
*
alpar@1
    63
*  ind[k], k = 1,...,size, is the number of a vertex included in the
alpar@1
    64
*  clique found, 1 <= ind[k] <= n, where size is the number of vertices
alpar@1
    65
*  in the clique returned on exit.
alpar@1
    66
*
alpar@1
    67
*  RETURNS
alpar@1
    68
*
alpar@1
    69
*  The routine returns the clique size, i.e. the number of vertices in
alpar@1
    70
*  the clique. */
alpar@1
    71
alpar@1
    72
struct csa
alpar@1
    73
{     /* common storage area */
alpar@1
    74
      int n;
alpar@1
    75
      /* number of vertices */
alpar@1
    76
      const int *wt; /* int wt[0:n-1]; */
alpar@1
    77
      /* weights */
alpar@1
    78
      const unsigned char *a;
alpar@1
    79
      /* adjacency matrix (packed lower triangle without main diag.) */
alpar@1
    80
      int record;
alpar@1
    81
      /* weight of best clique */
alpar@1
    82
      int rec_level;
alpar@1
    83
      /* number of vertices in best clique */
alpar@1
    84
      int *rec; /* int rec[0:n-1]; */
alpar@1
    85
      /* best clique so far */
alpar@1
    86
      int *clique; /* int clique[0:n-1]; */
alpar@1
    87
      /* table for pruning */
alpar@1
    88
      int *set; /* int set[0:n-1]; */
alpar@1
    89
      /* current clique */
alpar@1
    90
};
alpar@1
    91
alpar@1
    92
#define n         (csa->n)
alpar@1
    93
#define wt        (csa->wt)
alpar@1
    94
#define a         (csa->a)
alpar@1
    95
#define record    (csa->record)
alpar@1
    96
#define rec_level (csa->rec_level)
alpar@1
    97
#define rec       (csa->rec)
alpar@1
    98
#define clique    (csa->clique)
alpar@1
    99
#define set       (csa->set)
alpar@1
   100
alpar@1
   101
#if 0
alpar@1
   102
static int is_edge(struct csa *csa, int i, int j)
alpar@1
   103
{     /* if there is arc (i,j), the routine returns true; otherwise
alpar@1
   104
         false; 0 <= i, j < n */
alpar@1
   105
      int k;
alpar@1
   106
      xassert(0 <= i && i < n);
alpar@1
   107
      xassert(0 <= j && j < n);
alpar@1
   108
      if (i == j) return 0;
alpar@1
   109
      if (i < j) k = i, i = j, j = k;
alpar@1
   110
      k = (i * (i - 1)) / 2 + j;
alpar@1
   111
      return a[k / CHAR_BIT] &
alpar@1
   112
         (unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT));
alpar@1
   113
}
alpar@1
   114
#else
alpar@1
   115
#define is_edge(csa, i, j) ((i) == (j) ? 0 : \
alpar@1
   116
      (i) > (j) ? is_edge1(i, j) : is_edge1(j, i))
alpar@1
   117
#define is_edge1(i, j) is_edge2(((i) * ((i) - 1)) / 2 + (j))
alpar@1
   118
#define is_edge2(k) (a[(k) / CHAR_BIT] & \
alpar@1
   119
      (unsigned char)(1 << ((CHAR_BIT - 1) - (k) % CHAR_BIT)))
alpar@1
   120
#endif
alpar@1
   121
alpar@1
   122
static void sub(struct csa *csa, int ct, int table[], int level,
alpar@1
   123
      int weight, int l_weight)
alpar@1
   124
{     int i, j, k, curr_weight, left_weight, *p1, *p2, *newtable;
alpar@1
   125
      newtable = xcalloc(n, sizeof(int));
alpar@1
   126
      if (ct <= 0)
alpar@1
   127
      {  /* 0 or 1 elements left; include these */
alpar@1
   128
         if (ct == 0)
alpar@1
   129
         {  set[level++] = table[0];
alpar@1
   130
            weight += l_weight;
alpar@1
   131
         }
alpar@1
   132
         if (weight > record)
alpar@1
   133
         {  record = weight;
alpar@1
   134
            rec_level = level;
alpar@1
   135
            for (i = 0; i < level; i++) rec[i] = set[i];
alpar@1
   136
         }
alpar@1
   137
         goto done;
alpar@1
   138
      }
alpar@1
   139
      for (i = ct; i >= 0; i--)
alpar@1
   140
      {  if ((level == 0) && (i < ct)) goto done;
alpar@1
   141
         k = table[i];
alpar@1
   142
         if ((level > 0) && (clique[k] <= (record - weight)))
alpar@1
   143
            goto done; /* prune */
alpar@1
   144
         set[level] = k;
alpar@1
   145
         curr_weight = weight + wt[k];
alpar@1
   146
         l_weight -= wt[k];
alpar@1
   147
         if (l_weight <= (record - curr_weight))
alpar@1
   148
            goto done; /* prune */
alpar@1
   149
         p1 = newtable;
alpar@1
   150
         p2 = table;
alpar@1
   151
         left_weight = 0;
alpar@1
   152
         while (p2 < table + i)
alpar@1
   153
         {  j = *p2++;
alpar@1
   154
            if (is_edge(csa, j, k))
alpar@1
   155
            {  *p1++ = j;
alpar@1
   156
               left_weight += wt[j];
alpar@1
   157
            }
alpar@1
   158
         }
alpar@1
   159
         if (left_weight <= (record - curr_weight)) continue;
alpar@1
   160
         sub(csa, p1 - newtable - 1, newtable, level + 1, curr_weight,
alpar@1
   161
            left_weight);
alpar@1
   162
      }
alpar@1
   163
done: xfree(newtable);
alpar@1
   164
      return;
alpar@1
   165
}
alpar@1
   166
alpar@1
   167
int wclique(int _n, const int w[], const unsigned char _a[], int ind[])
alpar@1
   168
{     struct csa _csa, *csa = &_csa;
alpar@1
   169
      int i, j, p, max_wt, max_nwt, wth, *used, *nwt, *pos;
alpar@1
   170
      glp_long timer;
alpar@1
   171
      n = _n;
alpar@1
   172
      xassert(n > 0);
alpar@1
   173
      wt = &w[1];
alpar@1
   174
      a = _a;
alpar@1
   175
      record = 0;
alpar@1
   176
      rec_level = 0;
alpar@1
   177
      rec = &ind[1];
alpar@1
   178
      clique = xcalloc(n, sizeof(int));
alpar@1
   179
      set = xcalloc(n, sizeof(int));
alpar@1
   180
      used = xcalloc(n, sizeof(int));
alpar@1
   181
      nwt = xcalloc(n, sizeof(int));
alpar@1
   182
      pos = xcalloc(n, sizeof(int));
alpar@1
   183
      /* start timer */
alpar@1
   184
      timer = xtime();
alpar@1
   185
      /* order vertices */
alpar@1
   186
      for (i = 0; i < n; i++)
alpar@1
   187
      {  nwt[i] = 0;
alpar@1
   188
         for (j = 0; j < n; j++)
alpar@1
   189
            if (is_edge(csa, i, j)) nwt[i] += wt[j];
alpar@1
   190
      }
alpar@1
   191
      for (i = 0; i < n; i++)
alpar@1
   192
         used[i] = 0;
alpar@1
   193
      for (i = n-1; i >= 0; i--)
alpar@1
   194
      {  max_wt = -1;
alpar@1
   195
         max_nwt = -1;
alpar@1
   196
         for (j = 0; j < n; j++)
alpar@1
   197
         {  if ((!used[j]) && ((wt[j] > max_wt) || (wt[j] == max_wt
alpar@1
   198
               && nwt[j] > max_nwt)))
alpar@1
   199
            {  max_wt = wt[j];
alpar@1
   200
               max_nwt = nwt[j];
alpar@1
   201
               p = j;
alpar@1
   202
            }
alpar@1
   203
         }
alpar@1
   204
         pos[i] = p;
alpar@1
   205
         used[p] = 1;
alpar@1
   206
         for (j = 0; j < n; j++)
alpar@1
   207
            if ((!used[j]) && (j != p) && (is_edge(csa, p, j)))
alpar@1
   208
               nwt[j] -= wt[p];
alpar@1
   209
      }
alpar@1
   210
      /* main routine */
alpar@1
   211
      wth = 0;
alpar@1
   212
      for (i = 0; i < n; i++)
alpar@1
   213
      {  wth += wt[pos[i]];
alpar@1
   214
         sub(csa, i, pos, 0, 0, wth);
alpar@1
   215
         clique[pos[i]] = record;
alpar@1
   216
         if (xdifftime(xtime(), timer) >= 5.0 - 0.001)
alpar@1
   217
         {  /* print current record and reset timer */
alpar@1
   218
            xprintf("level = %d (%d); best = %d\n", i+1, n, record);
alpar@1
   219
            timer = xtime();
alpar@1
   220
         }
alpar@1
   221
      }
alpar@1
   222
      xfree(clique);
alpar@1
   223
      xfree(set);
alpar@1
   224
      xfree(used);
alpar@1
   225
      xfree(nwt);
alpar@1
   226
      xfree(pos);
alpar@1
   227
      /* return the solution found */
alpar@1
   228
      for (i = 1; i <= rec_level; i++) ind[i]++;
alpar@1
   229
      return rec_level;
alpar@1
   230
}
alpar@1
   231
alpar@1
   232
#undef n
alpar@1
   233
#undef wt
alpar@1
   234
#undef a
alpar@1
   235
#undef record
alpar@1
   236
#undef rec_level
alpar@1
   237
#undef rec
alpar@1
   238
#undef clique
alpar@1
   239
#undef set
alpar@1
   240
alpar@1
   241
/* eof */