lemon/grosso_locatelli_pullan_mc.h
changeset 999 c279b19abc62
child 1022 8583fb74238c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/grosso_locatelli_pullan_mc.h	Fri Jul 23 06:29:37 2010 +0200
     1.3 @@ -0,0 +1,680 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2010
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_GROSSO_LOCATELLI_PULLAN_MC_H
    1.23 +#define LEMON_GROSSO_LOCATELLI_PULLAN_MC_H
    1.24 +
    1.25 +/// \ingroup approx_algs
    1.26 +///
    1.27 +/// \file
    1.28 +/// \brief The iterated local search algorithm of Grosso, Locatelli, and Pullan
    1.29 +/// for the maximum clique problem
    1.30 +
    1.31 +#include <vector>
    1.32 +#include <limits>
    1.33 +#include <lemon/core.h>
    1.34 +#include <lemon/random.h>
    1.35 +
    1.36 +namespace lemon {
    1.37 +
    1.38 +  /// \addtogroup approx_algs
    1.39 +  /// @{
    1.40 +
    1.41 +  /// \brief Implementation of the iterated local search algorithm of Grosso,
    1.42 +  /// Locatelli, and Pullan for the maximum clique problem
    1.43 +  ///
    1.44 +  /// \ref GrossoLocatelliPullanMc implements the iterated local search
    1.45 +  /// algorithm of Grosso, Locatelli, and Pullan for solving the \e maximum
    1.46 +  /// \e clique \e problem \ref grosso08maxclique.
    1.47 +  /// It is to find the largest complete subgraph (\e clique) in an
    1.48 +  /// undirected graph, i.e., the largest set of nodes where each
    1.49 +  /// pair of nodes is connected.
    1.50 +  ///
    1.51 +  /// This class provides a simple but highly efficient and robust heuristic
    1.52 +  /// method that quickly finds a large clique, but not necessarily the
    1.53 +  /// largest one.
    1.54 +  ///
    1.55 +  /// \tparam GR The undirected graph type the algorithm runs on.
    1.56 +  ///
    1.57 +  /// \note %GrossoLocatelliPullanMc provides three different node selection
    1.58 +  /// rules, from which the most powerful one is used by default.
    1.59 +  /// For more information, see \ref SelectionRule.
    1.60 +  template <typename GR>
    1.61 +  class GrossoLocatelliPullanMc
    1.62 +  {
    1.63 +  public:
    1.64 +
    1.65 +    /// \brief Constants for specifying the node selection rule.
    1.66 +    ///
    1.67 +    /// Enum type containing constants for specifying the node selection rule
    1.68 +    /// for the \ref run() function.
    1.69 +    ///
    1.70 +    /// During the algorithm, nodes are selected for addition to the current
    1.71 +    /// clique according to the applied rule.
    1.72 +    /// In general, the PENALTY_BASED rule turned out to be the most powerful
    1.73 +    /// and the most robust, thus it is the default option.
    1.74 +    /// However, another selection rule can be specified using the \ref run()
    1.75 +    /// function with the proper parameter.
    1.76 +    enum SelectionRule {
    1.77 +
    1.78 +      /// A node is selected randomly without any evaluation at each step.
    1.79 +      RANDOM,
    1.80 +
    1.81 +      /// A node of maximum degree is selected randomly at each step.
    1.82 +      DEGREE_BASED,
    1.83 +
    1.84 +      /// A node of minimum penalty is selected randomly at each step.
    1.85 +      /// The node penalties are updated adaptively after each stage of the
    1.86 +      /// search process.
    1.87 +      PENALTY_BASED
    1.88 +    };
    1.89 +
    1.90 +  private:
    1.91 +
    1.92 +    TEMPLATE_GRAPH_TYPEDEFS(GR);
    1.93 +
    1.94 +    typedef std::vector<int> IntVector;
    1.95 +    typedef std::vector<char> BoolVector;
    1.96 +    typedef std::vector<BoolVector> BoolMatrix;
    1.97 +    // Note: vector<char> is used instead of vector<bool> for efficiency reasons
    1.98 +
    1.99 +    const GR &_graph;
   1.100 +    IntNodeMap _id;
   1.101 +
   1.102 +    // Internal matrix representation of the graph
   1.103 +    BoolMatrix _gr;
   1.104 +    int _n;
   1.105 +
   1.106 +    // The current clique
   1.107 +    BoolVector _clique;
   1.108 +    int _size;
   1.109 +
   1.110 +    // The best clique found so far
   1.111 +    BoolVector _best_clique;
   1.112 +    int _best_size;
   1.113 +
   1.114 +    // The "distances" of the nodes from the current clique.
   1.115 +    // _delta[u] is the number of nodes in the clique that are
   1.116 +    // not connected with u.
   1.117 +    IntVector _delta;
   1.118 +
   1.119 +    // The current tabu set
   1.120 +    BoolVector _tabu;
   1.121 +
   1.122 +    // Random number generator
   1.123 +    Random _rnd;
   1.124 +
   1.125 +  private:
   1.126 +
   1.127 +    // Implementation of the RANDOM node selection rule.
   1.128 +    class RandomSelectionRule
   1.129 +    {
   1.130 +    private:
   1.131 +
   1.132 +      // References to the algorithm instance
   1.133 +      const BoolVector &_clique;
   1.134 +      const IntVector  &_delta;
   1.135 +      const BoolVector &_tabu;
   1.136 +      Random &_rnd;
   1.137 +
   1.138 +      // Pivot rule data
   1.139 +      int _n;
   1.140 +
   1.141 +    public:
   1.142 +
   1.143 +      // Constructor
   1.144 +      RandomSelectionRule(GrossoLocatelliPullanMc &mc) :
   1.145 +        _clique(mc._clique), _delta(mc._delta), _tabu(mc._tabu),
   1.146 +        _rnd(mc._rnd), _n(mc._n)
   1.147 +      {}
   1.148 +
   1.149 +      // Return a node index for a feasible add move or -1 if no one exists
   1.150 +      int nextFeasibleAddNode() const {
   1.151 +        int start_node = _rnd[_n];
   1.152 +        for (int i = start_node; i != _n; i++) {
   1.153 +          if (_delta[i] == 0 && !_tabu[i]) return i;
   1.154 +        }
   1.155 +        for (int i = 0; i != start_node; i++) {
   1.156 +          if (_delta[i] == 0 && !_tabu[i]) return i;
   1.157 +        }
   1.158 +        return -1;
   1.159 +      }
   1.160 +
   1.161 +      // Return a node index for a feasible swap move or -1 if no one exists
   1.162 +      int nextFeasibleSwapNode() const {
   1.163 +        int start_node = _rnd[_n];
   1.164 +        for (int i = start_node; i != _n; i++) {
   1.165 +          if (!_clique[i] && _delta[i] == 1 && !_tabu[i]) return i;
   1.166 +        }
   1.167 +        for (int i = 0; i != start_node; i++) {
   1.168 +          if (!_clique[i] && _delta[i] == 1 && !_tabu[i]) return i;
   1.169 +        }
   1.170 +        return -1;
   1.171 +      }
   1.172 +
   1.173 +      // Return a node index for an add move or -1 if no one exists
   1.174 +      int nextAddNode() const {
   1.175 +        int start_node = _rnd[_n];
   1.176 +        for (int i = start_node; i != _n; i++) {
   1.177 +          if (_delta[i] == 0) return i;
   1.178 +        }
   1.179 +        for (int i = 0; i != start_node; i++) {
   1.180 +          if (_delta[i] == 0) return i;
   1.181 +        }
   1.182 +        return -1;
   1.183 +      }
   1.184 +
   1.185 +      // Update internal data structures between stages (if necessary)
   1.186 +      void update() {}
   1.187 +
   1.188 +    }; //class RandomSelectionRule
   1.189 +
   1.190 +
   1.191 +    // Implementation of the DEGREE_BASED node selection rule.
   1.192 +    class DegreeBasedSelectionRule
   1.193 +    {
   1.194 +    private:
   1.195 +
   1.196 +      // References to the algorithm instance
   1.197 +      const BoolVector &_clique;
   1.198 +      const IntVector  &_delta;
   1.199 +      const BoolVector &_tabu;
   1.200 +      Random &_rnd;
   1.201 +
   1.202 +      // Pivot rule data
   1.203 +      int _n;
   1.204 +      IntVector _deg;
   1.205 +
   1.206 +    public:
   1.207 +
   1.208 +      // Constructor
   1.209 +      DegreeBasedSelectionRule(GrossoLocatelliPullanMc &mc) :
   1.210 +        _clique(mc._clique), _delta(mc._delta), _tabu(mc._tabu),
   1.211 +        _rnd(mc._rnd), _n(mc._n), _deg(_n)
   1.212 +      {
   1.213 +        for (int i = 0; i != _n; i++) {
   1.214 +          int d = 0;
   1.215 +          BoolVector &row = mc._gr[i];
   1.216 +          for (int j = 0; j != _n; j++) {
   1.217 +            if (row[j]) d++;
   1.218 +          }
   1.219 +          _deg[i] = d;
   1.220 +        }
   1.221 +      }
   1.222 +
   1.223 +      // Return a node index for a feasible add move or -1 if no one exists
   1.224 +      int nextFeasibleAddNode() const {
   1.225 +        int start_node = _rnd[_n];
   1.226 +        int node = -1, max_deg = -1;
   1.227 +        for (int i = start_node; i != _n; i++) {
   1.228 +          if (_delta[i] == 0 && !_tabu[i] && _deg[i] > max_deg) {
   1.229 +            node = i;
   1.230 +            max_deg = _deg[i];
   1.231 +          }
   1.232 +        }
   1.233 +        for (int i = 0; i != start_node; i++) {
   1.234 +          if (_delta[i] == 0 && !_tabu[i] && _deg[i] > max_deg) {
   1.235 +            node = i;
   1.236 +            max_deg = _deg[i];
   1.237 +          }
   1.238 +        }
   1.239 +        return node;
   1.240 +      }
   1.241 +
   1.242 +      // Return a node index for a feasible swap move or -1 if no one exists
   1.243 +      int nextFeasibleSwapNode() const {
   1.244 +        int start_node = _rnd[_n];
   1.245 +        int node = -1, max_deg = -1;
   1.246 +        for (int i = start_node; i != _n; i++) {
   1.247 +          if (!_clique[i] && _delta[i] == 1 && !_tabu[i] &&
   1.248 +              _deg[i] > max_deg) {
   1.249 +            node = i;
   1.250 +            max_deg = _deg[i];
   1.251 +          }
   1.252 +        }
   1.253 +        for (int i = 0; i != start_node; i++) {
   1.254 +          if (!_clique[i] && _delta[i] == 1 && !_tabu[i] &&
   1.255 +              _deg[i] > max_deg) {
   1.256 +            node = i;
   1.257 +            max_deg = _deg[i];
   1.258 +          }
   1.259 +        }
   1.260 +        return node;
   1.261 +      }
   1.262 +
   1.263 +      // Return a node index for an add move or -1 if no one exists
   1.264 +      int nextAddNode() const {
   1.265 +        int start_node = _rnd[_n];
   1.266 +        int node = -1, max_deg = -1;
   1.267 +        for (int i = start_node; i != _n; i++) {
   1.268 +          if (_delta[i] == 0 && _deg[i] > max_deg) {
   1.269 +            node = i;
   1.270 +            max_deg = _deg[i];
   1.271 +          }
   1.272 +        }
   1.273 +        for (int i = 0; i != start_node; i++) {
   1.274 +          if (_delta[i] == 0 && _deg[i] > max_deg) {
   1.275 +            node = i;
   1.276 +            max_deg = _deg[i];
   1.277 +          }
   1.278 +        }
   1.279 +        return node;
   1.280 +      }
   1.281 +
   1.282 +      // Update internal data structures between stages (if necessary)
   1.283 +      void update() {}
   1.284 +
   1.285 +    }; //class DegreeBasedSelectionRule
   1.286 +
   1.287 +
   1.288 +    // Implementation of the PENALTY_BASED node selection rule.
   1.289 +    class PenaltyBasedSelectionRule
   1.290 +    {
   1.291 +    private:
   1.292 +
   1.293 +      // References to the algorithm instance
   1.294 +      const BoolVector &_clique;
   1.295 +      const IntVector  &_delta;
   1.296 +      const BoolVector &_tabu;
   1.297 +      Random &_rnd;
   1.298 +
   1.299 +      // Pivot rule data
   1.300 +      int _n;
   1.301 +      IntVector _penalty;
   1.302 +
   1.303 +    public:
   1.304 +
   1.305 +      // Constructor
   1.306 +      PenaltyBasedSelectionRule(GrossoLocatelliPullanMc &mc) :
   1.307 +        _clique(mc._clique), _delta(mc._delta), _tabu(mc._tabu),
   1.308 +        _rnd(mc._rnd), _n(mc._n), _penalty(_n, 0)
   1.309 +      {}
   1.310 +
   1.311 +      // Return a node index for a feasible add move or -1 if no one exists
   1.312 +      int nextFeasibleAddNode() const {
   1.313 +        int start_node = _rnd[_n];
   1.314 +        int node = -1, min_p = std::numeric_limits<int>::max();
   1.315 +        for (int i = start_node; i != _n; i++) {
   1.316 +          if (_delta[i] == 0 && !_tabu[i] && _penalty[i] < min_p) {
   1.317 +            node = i;
   1.318 +            min_p = _penalty[i];
   1.319 +          }
   1.320 +        }
   1.321 +        for (int i = 0; i != start_node; i++) {
   1.322 +          if (_delta[i] == 0 && !_tabu[i] && _penalty[i] < min_p) {
   1.323 +            node = i;
   1.324 +            min_p = _penalty[i];
   1.325 +          }
   1.326 +        }
   1.327 +        return node;
   1.328 +      }
   1.329 +
   1.330 +      // Return a node index for a feasible swap move or -1 if no one exists
   1.331 +      int nextFeasibleSwapNode() const {
   1.332 +        int start_node = _rnd[_n];
   1.333 +        int node = -1, min_p = std::numeric_limits<int>::max();
   1.334 +        for (int i = start_node; i != _n; i++) {
   1.335 +          if (!_clique[i] && _delta[i] == 1 && !_tabu[i] &&
   1.336 +              _penalty[i] < min_p) {
   1.337 +            node = i;
   1.338 +            min_p = _penalty[i];
   1.339 +          }
   1.340 +        }
   1.341 +        for (int i = 0; i != start_node; i++) {
   1.342 +          if (!_clique[i] && _delta[i] == 1 && !_tabu[i] &&
   1.343 +              _penalty[i] < min_p) {
   1.344 +            node = i;
   1.345 +            min_p = _penalty[i];
   1.346 +          }
   1.347 +        }
   1.348 +        return node;
   1.349 +      }
   1.350 +
   1.351 +      // Return a node index for an add move or -1 if no one exists
   1.352 +      int nextAddNode() const {
   1.353 +        int start_node = _rnd[_n];
   1.354 +        int node = -1, min_p = std::numeric_limits<int>::max();
   1.355 +        for (int i = start_node; i != _n; i++) {
   1.356 +          if (_delta[i] == 0 && _penalty[i] < min_p) {
   1.357 +            node = i;
   1.358 +            min_p = _penalty[i];
   1.359 +          }
   1.360 +        }
   1.361 +        for (int i = 0; i != start_node; i++) {
   1.362 +          if (_delta[i] == 0 && _penalty[i] < min_p) {
   1.363 +            node = i;
   1.364 +            min_p = _penalty[i];
   1.365 +          }
   1.366 +        }
   1.367 +        return node;
   1.368 +      }
   1.369 +
   1.370 +      // Update internal data structures between stages (if necessary)
   1.371 +      void update() {}
   1.372 +
   1.373 +    }; //class PenaltyBasedSelectionRule
   1.374 +
   1.375 +  public:
   1.376 +
   1.377 +    /// \brief Constructor.
   1.378 +    ///
   1.379 +    /// Constructor.
   1.380 +    /// The global \ref rnd "random number generator instance" is used
   1.381 +    /// during the algorithm.
   1.382 +    ///
   1.383 +    /// \param graph The undirected graph the algorithm runs on.
   1.384 +    GrossoLocatelliPullanMc(const GR& graph) :
   1.385 +      _graph(graph), _id(_graph), _rnd(rnd)
   1.386 +    {}
   1.387 +
   1.388 +    /// \brief Constructor with random seed.
   1.389 +    ///
   1.390 +    /// Constructor with random seed.
   1.391 +    ///
   1.392 +    /// \param graph The undirected graph the algorithm runs on.
   1.393 +    /// \param seed Seed value for the internal random number generator
   1.394 +    /// that is used during the algorithm.
   1.395 +    GrossoLocatelliPullanMc(const GR& graph, int seed) :
   1.396 +      _graph(graph), _id(_graph), _rnd(seed)
   1.397 +    {}
   1.398 +
   1.399 +    /// \brief Constructor with random number generator.
   1.400 +    ///
   1.401 +    /// Constructor with random number generator.
   1.402 +    ///
   1.403 +    /// \param graph The undirected graph the algorithm runs on.
   1.404 +    /// \param random A random number generator that is used during the
   1.405 +    /// algorithm.
   1.406 +    GrossoLocatelliPullanMc(const GR& graph, const Random& random) :
   1.407 +      _graph(graph), _id(_graph), _rnd(random)
   1.408 +    {}
   1.409 +
   1.410 +    /// \name Execution Control
   1.411 +    /// @{
   1.412 +
   1.413 +    /// \brief Runs the algorithm.
   1.414 +    ///
   1.415 +    /// This function runs the algorithm.
   1.416 +    ///
   1.417 +    /// \param step_num The maximum number of node selections (steps)
   1.418 +    /// during the search process.
   1.419 +    /// This parameter controls the running time and the success of the
   1.420 +    /// algorithm. For larger values, the algorithm runs slower but it more
   1.421 +    /// likely finds larger cliques. For smaller values, the algorithm is
   1.422 +    /// faster but probably gives worse results.
   1.423 +    /// \param rule The node selection rule. For more information, see
   1.424 +    /// \ref SelectionRule.
   1.425 +    ///
   1.426 +    /// \return The size of the found clique.
   1.427 +    int run(int step_num = 100000,
   1.428 +            SelectionRule rule = PENALTY_BASED)
   1.429 +    {
   1.430 +      init();
   1.431 +      switch (rule) {
   1.432 +        case RANDOM:
   1.433 +          return start<RandomSelectionRule>(step_num);
   1.434 +        case DEGREE_BASED:
   1.435 +          return start<DegreeBasedSelectionRule>(step_num);
   1.436 +        case PENALTY_BASED:
   1.437 +          return start<PenaltyBasedSelectionRule>(step_num);
   1.438 +      }
   1.439 +      return 0; // avoid warning
   1.440 +    }
   1.441 +
   1.442 +    /// @}
   1.443 +
   1.444 +    /// \name Query Functions
   1.445 +    /// @{
   1.446 +
   1.447 +    /// \brief The size of the found clique
   1.448 +    ///
   1.449 +    /// This function returns the size of the found clique.
   1.450 +    ///
   1.451 +    /// \pre run() must be called before using this function.
   1.452 +    int cliqueSize() const {
   1.453 +      return _best_size;
   1.454 +    }
   1.455 +
   1.456 +    /// \brief Gives back the found clique in a \c bool node map
   1.457 +    ///
   1.458 +    /// This function gives back the characteristic vector of the found
   1.459 +    /// clique in the given node map.
   1.460 +    /// It must be a \ref concepts::WriteMap "writable" node map with
   1.461 +    /// \c bool (or convertible) value type.
   1.462 +    ///
   1.463 +    /// \pre run() must be called before using this function.
   1.464 +    template <typename CliqueMap>
   1.465 +    void cliqueMap(CliqueMap &map) const {
   1.466 +      for (NodeIt n(_graph); n != INVALID; ++n) {
   1.467 +        map[n] = static_cast<bool>(_best_clique[_id[n]]);
   1.468 +      }
   1.469 +    }
   1.470 +
   1.471 +    /// \brief Iterator to list the nodes of the found clique
   1.472 +    ///
   1.473 +    /// This iterator class lists the nodes of the found clique.
   1.474 +    /// Before using it, you must allocate a GrossoLocatelliPullanMc instance
   1.475 +    /// and call its \ref GrossoLocatelliPullanMc::run() "run()" method.
   1.476 +    ///
   1.477 +    /// The following example prints out the IDs of the nodes in the found
   1.478 +    /// clique.
   1.479 +    /// \code
   1.480 +    ///   GrossoLocatelliPullanMc<Graph> mc(g);
   1.481 +    ///   mc.run();
   1.482 +    ///   for (GrossoLocatelliPullanMc<Graph>::CliqueNodeIt n(mc);
   1.483 +    ///        n != INVALID; ++n)
   1.484 +    ///   {
   1.485 +    ///     std::cout << g.id(n) << std::endl;
   1.486 +    ///   }
   1.487 +    /// \endcode
   1.488 +    class CliqueNodeIt
   1.489 +    {
   1.490 +    private:
   1.491 +      NodeIt _it;
   1.492 +      BoolNodeMap _map;
   1.493 +
   1.494 +    public:
   1.495 +
   1.496 +      /// Constructor
   1.497 +
   1.498 +      /// Constructor.
   1.499 +      /// \param mc The algorithm instance.
   1.500 +      CliqueNodeIt(const GrossoLocatelliPullanMc &mc)
   1.501 +       : _map(mc._graph)
   1.502 +      {
   1.503 +        mc.cliqueMap(_map);
   1.504 +        for (_it = NodeIt(mc._graph); _it != INVALID && !_map[_it]; ++_it) ;
   1.505 +      }
   1.506 +
   1.507 +      /// Conversion to \c Node
   1.508 +      operator Node() const { return _it; }
   1.509 +
   1.510 +      bool operator==(Invalid) const { return _it == INVALID; }
   1.511 +      bool operator!=(Invalid) const { return _it != INVALID; }
   1.512 +
   1.513 +      /// Next node
   1.514 +      CliqueNodeIt &operator++() {
   1.515 +        for (++_it; _it != INVALID && !_map[_it]; ++_it) ;
   1.516 +        return *this;
   1.517 +      }
   1.518 +
   1.519 +      /// Postfix incrementation
   1.520 +
   1.521 +      /// Postfix incrementation.
   1.522 +      ///
   1.523 +      /// \warning This incrementation returns a \c Node, not a
   1.524 +      /// \c CliqueNodeIt as one may expect.
   1.525 +      typename GR::Node operator++(int) {
   1.526 +        Node n=*this;
   1.527 +        ++(*this);
   1.528 +        return n;
   1.529 +      }
   1.530 +
   1.531 +    };
   1.532 +
   1.533 +    /// @}
   1.534 +
   1.535 +  private:
   1.536 +
   1.537 +    // Adds a node to the current clique
   1.538 +    void addCliqueNode(int u) {
   1.539 +      if (_clique[u]) return;
   1.540 +      _clique[u] = true;
   1.541 +      _size++;
   1.542 +      BoolVector &row = _gr[u];
   1.543 +      for (int i = 0; i != _n; i++) {
   1.544 +        if (!row[i]) _delta[i]++;
   1.545 +      }
   1.546 +    }
   1.547 +
   1.548 +    // Removes a node from the current clique
   1.549 +    void delCliqueNode(int u) {
   1.550 +      if (!_clique[u]) return;
   1.551 +      _clique[u] = false;
   1.552 +      _size--;
   1.553 +      BoolVector &row = _gr[u];
   1.554 +      for (int i = 0; i != _n; i++) {
   1.555 +        if (!row[i]) _delta[i]--;
   1.556 +      }
   1.557 +    }
   1.558 +
   1.559 +    // Initialize data structures
   1.560 +    void init() {
   1.561 +      _n = countNodes(_graph);
   1.562 +      int ui = 0;
   1.563 +      for (NodeIt u(_graph); u != INVALID; ++u) {
   1.564 +        _id[u] = ui++;
   1.565 +      }
   1.566 +      _gr.clear();
   1.567 +      _gr.resize(_n, BoolVector(_n, false));
   1.568 +      ui = 0;
   1.569 +      for (NodeIt u(_graph); u != INVALID; ++u) {
   1.570 +        for (IncEdgeIt e(_graph, u); e != INVALID; ++e) {
   1.571 +          int vi = _id[_graph.runningNode(e)];
   1.572 +          _gr[ui][vi] = true;
   1.573 +          _gr[vi][ui] = true;
   1.574 +        }
   1.575 +        ++ui;
   1.576 +      }
   1.577 +
   1.578 +      _clique.clear();
   1.579 +      _clique.resize(_n, false);
   1.580 +      _size = 0;
   1.581 +      _best_clique.clear();
   1.582 +      _best_clique.resize(_n, false);
   1.583 +      _best_size = 0;
   1.584 +      _delta.clear();
   1.585 +      _delta.resize(_n, 0);
   1.586 +      _tabu.clear();
   1.587 +      _tabu.resize(_n, false);
   1.588 +    }
   1.589 +
   1.590 +    // Executes the algorithm
   1.591 +    template <typename SelectionRuleImpl>
   1.592 +    int start(int max_select) {
   1.593 +      // Options for the restart rule
   1.594 +      const bool delta_based_restart = true;
   1.595 +      const int restart_delta_limit = 4;
   1.596 +
   1.597 +      if (_n == 0) return 0;
   1.598 +      if (_n == 1) {
   1.599 +        _best_clique[0] = true;
   1.600 +        _best_size = 1;
   1.601 +        return _best_size;
   1.602 +      }
   1.603 +
   1.604 +      // Iterated local search
   1.605 +      SelectionRuleImpl sel_method(*this);
   1.606 +      int select = 0;
   1.607 +      IntVector restart_nodes;
   1.608 +
   1.609 +      while (select < max_select) {
   1.610 +
   1.611 +        // Perturbation/restart
   1.612 +        if (delta_based_restart) {
   1.613 +          restart_nodes.clear();
   1.614 +          for (int i = 0; i != _n; i++) {
   1.615 +            if (_delta[i] >= restart_delta_limit)
   1.616 +              restart_nodes.push_back(i);
   1.617 +          }
   1.618 +        }
   1.619 +        int rs_node = -1;
   1.620 +        if (restart_nodes.size() > 0) {
   1.621 +          rs_node = restart_nodes[_rnd[restart_nodes.size()]];
   1.622 +        } else {
   1.623 +          rs_node = _rnd[_n];
   1.624 +        }
   1.625 +        BoolVector &row = _gr[rs_node];
   1.626 +        for (int i = 0; i != _n; i++) {
   1.627 +          if (_clique[i] && !row[i]) delCliqueNode(i);
   1.628 +        }
   1.629 +        addCliqueNode(rs_node);
   1.630 +
   1.631 +        // Local search
   1.632 +        _tabu.clear();
   1.633 +        _tabu.resize(_n, false);
   1.634 +        bool tabu_empty = true;
   1.635 +        int max_swap = _size;
   1.636 +        while (select < max_select) {
   1.637 +          select++;
   1.638 +          int u;
   1.639 +          if ((u = sel_method.nextFeasibleAddNode()) != -1) {
   1.640 +            // Feasible add move
   1.641 +            addCliqueNode(u);
   1.642 +            if (tabu_empty) max_swap = _size;
   1.643 +          }
   1.644 +          else if ((u = sel_method.nextFeasibleSwapNode()) != -1) {
   1.645 +            // Feasible swap move
   1.646 +            int v = -1;
   1.647 +            BoolVector &row = _gr[u];
   1.648 +            for (int i = 0; i != _n; i++) {
   1.649 +              if (_clique[i] && !row[i]) {
   1.650 +                v = i;
   1.651 +                break;
   1.652 +              }
   1.653 +            }
   1.654 +            addCliqueNode(u);
   1.655 +            delCliqueNode(v);
   1.656 +            _tabu[v] = true;
   1.657 +            tabu_empty = false;
   1.658 +            if (--max_swap <= 0) break;
   1.659 +          }
   1.660 +          else if ((u = sel_method.nextAddNode()) != -1) {
   1.661 +            // Non-feasible add move
   1.662 +            addCliqueNode(u);
   1.663 +          }
   1.664 +          else break;
   1.665 +        }
   1.666 +        if (_size > _best_size) {
   1.667 +          _best_clique = _clique;
   1.668 +          _best_size = _size;
   1.669 +          if (_best_size == _n) return _best_size;
   1.670 +        }
   1.671 +        sel_method.update();
   1.672 +      }
   1.673 +
   1.674 +      return _best_size;
   1.675 +    }
   1.676 +
   1.677 +  }; //class GrossoLocatelliPullanMc
   1.678 +
   1.679 +  ///@}
   1.680 +
   1.681 +} //namespace lemon
   1.682 +
   1.683 +#endif //LEMON_GROSSO_LOCATELLI_PULLAN_MC_H