test/max_clique_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 15 Nov 2010 07:52:53 +0100
changeset 914 35ba7236bd67
child 918 8583fb74238c
permissions -rw-r--r--
Merge
kpeter@904
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@904
     2
 *
kpeter@904
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@904
     4
 *
kpeter@904
     5
 * Copyright (C) 2003-2010
kpeter@904
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@904
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@904
     8
 *
kpeter@904
     9
 * Permission to use, modify and distribute this software is granted
kpeter@904
    10
 * provided that this copyright notice appears in all copies. For
kpeter@904
    11
 * precise terms see the accompanying LICENSE file.
kpeter@904
    12
 *
kpeter@904
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@904
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@904
    15
 * purpose.
kpeter@904
    16
 *
kpeter@904
    17
 */
kpeter@904
    18
kpeter@904
    19
#include <sstream>
kpeter@904
    20
#include <lemon/list_graph.h>
kpeter@904
    21
#include <lemon/full_graph.h>
kpeter@904
    22
#include <lemon/grid_graph.h>
kpeter@904
    23
#include <lemon/lgf_reader.h>
kpeter@904
    24
#include <lemon/grosso_locatelli_pullan_mc.h>
kpeter@904
    25
kpeter@904
    26
#include "test_tools.h"
kpeter@904
    27
kpeter@904
    28
using namespace lemon;
kpeter@904
    29
kpeter@904
    30
char test_lgf[] =
kpeter@904
    31
  "@nodes\n"
kpeter@904
    32
  "label max_clique\n"
kpeter@904
    33
  "1     0\n"
kpeter@904
    34
  "2     0\n"
kpeter@904
    35
  "3     0\n"
kpeter@904
    36
  "4     1\n"
kpeter@904
    37
  "5     1\n"
kpeter@904
    38
  "6     1\n"
kpeter@904
    39
  "7     1\n"
kpeter@904
    40
  "@edges\n"
kpeter@904
    41
  "    label\n"
kpeter@904
    42
  "1 2     1\n"
kpeter@904
    43
  "1 3     2\n"
kpeter@904
    44
  "1 4     3\n"
kpeter@904
    45
  "1 6     4\n"
kpeter@904
    46
  "2 3     5\n"
kpeter@904
    47
  "2 5     6\n"
kpeter@904
    48
  "2 7     7\n"
kpeter@904
    49
  "3 4     8\n"
kpeter@904
    50
  "3 5     9\n"
kpeter@904
    51
  "4 5    10\n"
kpeter@904
    52
  "4 6    11\n"
kpeter@904
    53
  "4 7    12\n"
kpeter@904
    54
  "5 6    13\n"
kpeter@904
    55
  "5 7    14\n"
kpeter@904
    56
  "6 7    15\n";
kpeter@904
    57
      
kpeter@904
    58
kpeter@904
    59
// Check with general graphs
kpeter@904
    60
template <typename Param>
kpeter@904
    61
void checkMaxCliqueGeneral(int max_sel, Param rule) {
kpeter@904
    62
  typedef ListGraph GR;
kpeter@904
    63
  typedef GrossoLocatelliPullanMc<GR> McAlg;
kpeter@904
    64
  typedef McAlg::CliqueNodeIt CliqueIt;
kpeter@904
    65
  
kpeter@904
    66
  // Basic tests
kpeter@904
    67
  {
kpeter@904
    68
    GR g;
kpeter@904
    69
    GR::NodeMap<bool> map(g);
kpeter@904
    70
    McAlg mc(g);
kpeter@904
    71
    check(mc.run(max_sel, rule) == 0, "Wrong clique size");
kpeter@904
    72
    check(mc.cliqueSize() == 0, "Wrong clique size");
kpeter@904
    73
    check(CliqueIt(mc) == INVALID, "Wrong CliqueNodeIt");
kpeter@904
    74
kpeter@904
    75
    GR::Node u = g.addNode();
kpeter@904
    76
    check(mc.run(max_sel, rule) == 1, "Wrong clique size");
kpeter@904
    77
    check(mc.cliqueSize() == 1, "Wrong clique size");
kpeter@904
    78
    mc.cliqueMap(map);
kpeter@904
    79
    check(map[u], "Wrong clique map");
kpeter@904
    80
    CliqueIt it1(mc);
kpeter@904
    81
    check(static_cast<GR::Node>(it1) == u && ++it1 == INVALID,
kpeter@904
    82
          "Wrong CliqueNodeIt");
kpeter@904
    83
    
kpeter@904
    84
    GR::Node v = g.addNode();
kpeter@904
    85
    check(mc.run(max_sel, rule) == 1, "Wrong clique size");
kpeter@904
    86
    check(mc.cliqueSize() == 1, "Wrong clique size");
kpeter@904
    87
    mc.cliqueMap(map);
kpeter@904
    88
    check((map[u] && !map[v]) || (map[v] && !map[u]), "Wrong clique map");
kpeter@904
    89
    CliqueIt it2(mc);
kpeter@904
    90
    check(it2 != INVALID && ++it2 == INVALID, "Wrong CliqueNodeIt");
kpeter@904
    91
kpeter@904
    92
    g.addEdge(u, v);
kpeter@904
    93
    check(mc.run(max_sel, rule) == 2, "Wrong clique size");
kpeter@904
    94
    check(mc.cliqueSize() == 2, "Wrong clique size");
kpeter@904
    95
    mc.cliqueMap(map);
kpeter@904
    96
    check(map[u] && map[v], "Wrong clique map");
kpeter@904
    97
    CliqueIt it3(mc);
kpeter@904
    98
    check(it3 != INVALID && ++it3 != INVALID && ++it3 == INVALID,
kpeter@904
    99
          "Wrong CliqueNodeIt");
kpeter@904
   100
  }
kpeter@904
   101
kpeter@904
   102
  // Test graph
kpeter@904
   103
  {
kpeter@904
   104
    GR g;
kpeter@904
   105
    GR::NodeMap<bool> max_clique(g);
kpeter@904
   106
    GR::NodeMap<bool> map(g);
kpeter@904
   107
    std::istringstream input(test_lgf);
kpeter@904
   108
    graphReader(g, input)
kpeter@904
   109
      .nodeMap("max_clique", max_clique)
kpeter@904
   110
      .run();
kpeter@904
   111
    
kpeter@904
   112
    McAlg mc(g);
kpeter@904
   113
    check(mc.run(max_sel, rule) == 4, "Wrong clique size");
kpeter@904
   114
    check(mc.cliqueSize() == 4, "Wrong clique size");
kpeter@904
   115
    mc.cliqueMap(map);
kpeter@904
   116
    for (GR::NodeIt n(g); n != INVALID; ++n) {
kpeter@904
   117
      check(map[n] == max_clique[n], "Wrong clique map");
kpeter@904
   118
    }
kpeter@904
   119
    int cnt = 0;
kpeter@904
   120
    for (CliqueIt n(mc); n != INVALID; ++n) {
kpeter@904
   121
      cnt++;
kpeter@904
   122
      check(map[n] && max_clique[n], "Wrong CliqueNodeIt");
kpeter@904
   123
    }
kpeter@904
   124
    check(cnt == 4, "Wrong CliqueNodeIt");
kpeter@904
   125
  }
kpeter@904
   126
}
kpeter@904
   127
kpeter@904
   128
// Check with full graphs
kpeter@904
   129
template <typename Param>
kpeter@904
   130
void checkMaxCliqueFullGraph(int max_sel, Param rule) {
kpeter@904
   131
  typedef FullGraph GR;
kpeter@904
   132
  typedef GrossoLocatelliPullanMc<FullGraph> McAlg;
kpeter@904
   133
  typedef McAlg::CliqueNodeIt CliqueIt;
kpeter@904
   134
  
kpeter@904
   135
  for (int size = 0; size <= 40; size = size * 3 + 1) {
kpeter@904
   136
    GR g(size);
kpeter@904
   137
    GR::NodeMap<bool> map(g);
kpeter@904
   138
    McAlg mc(g);
kpeter@904
   139
    check(mc.run(max_sel, rule) == size, "Wrong clique size");
kpeter@904
   140
    check(mc.cliqueSize() == size, "Wrong clique size");
kpeter@904
   141
    mc.cliqueMap(map);
kpeter@904
   142
    for (GR::NodeIt n(g); n != INVALID; ++n) {
kpeter@904
   143
      check(map[n], "Wrong clique map");
kpeter@904
   144
    }
kpeter@904
   145
    int cnt = 0;
kpeter@904
   146
    for (CliqueIt n(mc); n != INVALID; ++n) cnt++;
kpeter@904
   147
    check(cnt == size, "Wrong CliqueNodeIt");
kpeter@904
   148
  }
kpeter@904
   149
}
kpeter@904
   150
kpeter@904
   151
// Check with grid graphs
kpeter@904
   152
template <typename Param>
kpeter@904
   153
void checkMaxCliqueGridGraph(int max_sel, Param rule) {
kpeter@904
   154
  GridGraph g(5, 7);
kpeter@904
   155
  GridGraph::NodeMap<char> map(g);
kpeter@904
   156
  GrossoLocatelliPullanMc<GridGraph> mc(g);
kpeter@904
   157
  check(mc.run(max_sel, rule) == 2, "Wrong clique size");
kpeter@904
   158
  check(mc.cliqueSize() == 2, "Wrong clique size");
kpeter@904
   159
}
kpeter@904
   160
kpeter@904
   161
kpeter@904
   162
int main() {
kpeter@904
   163
  checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::RANDOM);
kpeter@904
   164
  checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::DEGREE_BASED);
kpeter@904
   165
  checkMaxCliqueGeneral(50, GrossoLocatelliPullanMc<ListGraph>::PENALTY_BASED);
kpeter@904
   166
kpeter@904
   167
  checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::RANDOM);
kpeter@904
   168
  checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::DEGREE_BASED);
kpeter@904
   169
  checkMaxCliqueFullGraph(50, GrossoLocatelliPullanMc<FullGraph>::PENALTY_BASED);
kpeter@904
   170
                       
kpeter@904
   171
  checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::RANDOM);
kpeter@904
   172
  checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::DEGREE_BASED);
kpeter@904
   173
  checkMaxCliqueGridGraph(50, GrossoLocatelliPullanMc<GridGraph>::PENALTY_BASED);
kpeter@904
   174
                       
kpeter@904
   175
  return 0;
kpeter@904
   176
}