test/planarity_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 06 May 2015 11:29:34 +0200
branch1.3
changeset 1345 4c8ec448d59f
parent 861 30cb42e3e43a
child 1399 1e5da3fc4fbc
permissions -rw-r--r--
Merge CMP0026 setting to branch 1.3
deba@861
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@861
     2
 *
deba@861
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@861
     4
 *
deba@861
     5
 * Copyright (C) 2003-2009
deba@861
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@861
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@861
     8
 *
deba@861
     9
 * Permission to use, modify and distribute this software is granted
deba@861
    10
 * provided that this copyright notice appears in all copies. For
deba@861
    11
 * precise terms see the accompanying LICENSE file.
deba@861
    12
 *
deba@861
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@861
    14
 * express or implied, and with no claim as to its suitability for any
deba@861
    15
 * purpose.
deba@861
    16
 *
deba@861
    17
 */
deba@861
    18
deba@861
    19
#include <iostream>
deba@861
    20
deba@861
    21
#include <lemon/planarity.h>
deba@861
    22
deba@861
    23
#include <lemon/smart_graph.h>
deba@861
    24
#include <lemon/lgf_reader.h>
deba@861
    25
#include <lemon/connectivity.h>
deba@861
    26
#include <lemon/dim2.h>
deba@861
    27
deba@861
    28
#include "test_tools.h"
deba@861
    29
deba@861
    30
using namespace lemon;
deba@861
    31
using namespace lemon::dim2;
deba@861
    32
deba@861
    33
const int lgfn = 4;
deba@861
    34
const std::string lgf[lgfn] = {
deba@861
    35
  "@nodes\n"
deba@861
    36
  "label\n"
deba@861
    37
  "0\n"
deba@861
    38
  "1\n"
deba@861
    39
  "2\n"
deba@861
    40
  "3\n"
deba@861
    41
  "4\n"
deba@861
    42
  "@edges\n"
deba@861
    43
  "     label\n"
deba@861
    44
  "0 1  0\n"
deba@861
    45
  "0 2  0\n"
deba@861
    46
  "0 3  0\n"
deba@861
    47
  "0 4  0\n"
deba@861
    48
  "1 2  0\n"
deba@861
    49
  "1 3  0\n"
deba@861
    50
  "1 4  0\n"
deba@861
    51
  "2 3  0\n"
deba@861
    52
  "2 4  0\n"
deba@861
    53
  "3 4  0\n",
deba@861
    54
deba@861
    55
  "@nodes\n"
deba@861
    56
  "label\n"
deba@861
    57
  "0\n"
deba@861
    58
  "1\n"
deba@861
    59
  "2\n"
deba@861
    60
  "3\n"
deba@861
    61
  "4\n"
deba@861
    62
  "@edges\n"
deba@861
    63
  "     label\n"
deba@861
    64
  "0 1  0\n"
deba@861
    65
  "0 2  0\n"
deba@861
    66
  "0 3  0\n"
deba@861
    67
  "0 4  0\n"
deba@861
    68
  "1 2  0\n"
deba@861
    69
  "1 3  0\n"
deba@861
    70
  "2 3  0\n"
deba@861
    71
  "2 4  0\n"
deba@861
    72
  "3 4  0\n",
deba@861
    73
deba@861
    74
  "@nodes\n"
deba@861
    75
  "label\n"
deba@861
    76
  "0\n"
deba@861
    77
  "1\n"
deba@861
    78
  "2\n"
deba@861
    79
  "3\n"
deba@861
    80
  "4\n"
deba@861
    81
  "5\n"
deba@861
    82
  "@edges\n"
deba@861
    83
  "     label\n"
deba@861
    84
  "0 3  0\n"
deba@861
    85
  "0 4  0\n"
deba@861
    86
  "0 5  0\n"
deba@861
    87
  "1 3  0\n"
deba@861
    88
  "1 4  0\n"
deba@861
    89
  "1 5  0\n"
deba@861
    90
  "2 3  0\n"
deba@861
    91
  "2 4  0\n"
deba@861
    92
  "2 5  0\n",
deba@861
    93
deba@861
    94
  "@nodes\n"
deba@861
    95
  "label\n"
deba@861
    96
  "0\n"
deba@861
    97
  "1\n"
deba@861
    98
  "2\n"
deba@861
    99
  "3\n"
deba@861
   100
  "4\n"
deba@861
   101
  "5\n"
deba@861
   102
  "@edges\n"
deba@861
   103
  "     label\n"
deba@861
   104
  "0 3  0\n"
deba@861
   105
  "0 4  0\n"
deba@861
   106
  "0 5  0\n"
deba@861
   107
  "1 3  0\n"
deba@861
   108
  "1 4  0\n"
deba@861
   109
  "1 5  0\n"
deba@861
   110
  "2 3  0\n"
deba@861
   111
  "2 5  0\n"
deba@861
   112
};
deba@861
   113
deba@861
   114
deba@861
   115
deba@861
   116
typedef SmartGraph Graph;
deba@861
   117
GRAPH_TYPEDEFS(Graph);
deba@861
   118
deba@861
   119
typedef PlanarEmbedding<SmartGraph> PE;
deba@861
   120
typedef PlanarDrawing<SmartGraph> PD;
deba@861
   121
typedef PlanarColoring<SmartGraph> PC;
deba@861
   122
deba@861
   123
void checkEmbedding(const Graph& graph, PE& pe) {
deba@861
   124
  int face_num = 0;
deba@861
   125
deba@861
   126
  Graph::ArcMap<int> face(graph, -1);
deba@861
   127
deba@861
   128
  for (ArcIt a(graph); a != INVALID; ++a) {
deba@861
   129
    if (face[a] == -1) {
deba@861
   130
      Arc b = a;
deba@861
   131
      while (face[b] == -1) {
deba@861
   132
        face[b] = face_num;
deba@861
   133
        b = pe.next(graph.oppositeArc(b));
deba@861
   134
      }
deba@861
   135
      check(face[b] == face_num, "Wrong face");
deba@861
   136
      ++face_num;
deba@861
   137
    }
deba@861
   138
  }
deba@861
   139
  check(face_num + countNodes(graph) - countConnectedComponents(graph) ==
deba@861
   140
        countEdges(graph) + 1, "Euler test does not passed");
deba@861
   141
}
deba@861
   142
deba@861
   143
void checkKuratowski(const Graph& graph, PE& pe) {
deba@861
   144
  std::map<int, int> degs;
deba@861
   145
  for (NodeIt n(graph); n != INVALID; ++n) {
deba@861
   146
    int deg = 0;
deba@861
   147
    for (IncEdgeIt e(graph, n); e != INVALID; ++e) {
deba@861
   148
      if (pe.kuratowski(e)) {
deba@861
   149
        ++deg;
deba@861
   150
      }
deba@861
   151
    }
deba@861
   152
    ++degs[deg];
deba@861
   153
  }
deba@861
   154
  for (std::map<int, int>::iterator it = degs.begin(); it != degs.end(); ++it) {
deba@861
   155
    check(it->first == 0 || it->first == 2 ||
deba@861
   156
          (it->first == 3 && it->second == 6) ||
deba@861
   157
          (it->first == 4 && it->second == 5),
deba@861
   158
          "Wrong degree in Kuratowski graph");
deba@861
   159
  }
deba@861
   160
deba@861
   161
  // Not full test
deba@861
   162
  check((degs[3] == 0) != (degs[4] == 0), "Wrong Kuratowski graph");
deba@861
   163
}
deba@861
   164
deba@861
   165
bool intersect(Point<int> e1, Point<int> e2, Point<int> f1, Point<int> f2) {
deba@861
   166
  int l, r;
deba@861
   167
  if (std::min(e1.x, e2.x) > std::max(f1.x, f2.x)) return false;
deba@861
   168
  if (std::max(e1.x, e2.x) < std::min(f1.x, f2.x)) return false;
deba@861
   169
  if (std::min(e1.y, e2.y) > std::max(f1.y, f2.y)) return false;
deba@861
   170
  if (std::max(e1.y, e2.y) < std::min(f1.y, f2.y)) return false;
deba@861
   171
deba@861
   172
  l = (e2.x - e1.x) * (f1.y - e1.y) - (e2.y - e1.y) * (f1.x - e1.x);
deba@861
   173
  r = (e2.x - e1.x) * (f2.y - e1.y) - (e2.y - e1.y) * (f2.x - e1.x);
deba@861
   174
  if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
deba@861
   175
  l = (f2.x - f1.x) * (e1.y - f1.y) - (f2.y - f1.y) * (e1.x - f1.x);
deba@861
   176
  r = (f2.x - f1.x) * (e2.y - f1.y) - (f2.y - f1.y) * (e2.x - f1.x);
deba@861
   177
  if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
deba@861
   178
  return true;
deba@861
   179
}
deba@861
   180
deba@861
   181
bool collinear(Point<int> p, Point<int> q, Point<int> r) {
deba@861
   182
  int v;
deba@861
   183
  v = (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
deba@861
   184
  if (v != 0) return false;
deba@861
   185
  v = (q.x - p.x) * (r.x - p.x) + (q.y - p.y) * (r.y - p.y);
deba@861
   186
  if (v < 0) return false;
deba@861
   187
  return true;
deba@861
   188
}
deba@861
   189
deba@861
   190
void checkDrawing(const Graph& graph, PD& pd) {
deba@861
   191
  for (Graph::NodeIt n(graph); n != INVALID; ++n) {
deba@861
   192
    Graph::NodeIt m(n);
deba@861
   193
    for (++m; m != INVALID; ++m) {
deba@861
   194
      check(pd[m] != pd[n], "Two nodes with identical coordinates");
deba@861
   195
    }
deba@861
   196
  }
deba@861
   197
deba@861
   198
  for (Graph::EdgeIt e(graph); e != INVALID; ++e) {
deba@861
   199
    for (Graph::EdgeIt f(e); f != e; ++f) {
deba@861
   200
      Point<int> e1 = pd[graph.u(e)];
deba@861
   201
      Point<int> e2 = pd[graph.v(e)];
deba@861
   202
      Point<int> f1 = pd[graph.u(f)];
deba@861
   203
      Point<int> f2 = pd[graph.v(f)];
deba@861
   204
deba@861
   205
      if (graph.u(e) == graph.u(f)) {
deba@861
   206
        check(!collinear(e1, e2, f2), "Wrong drawing");
deba@861
   207
      } else if (graph.u(e) == graph.v(f)) {
deba@861
   208
        check(!collinear(e1, e2, f1), "Wrong drawing");
deba@861
   209
      } else if (graph.v(e) == graph.u(f)) {
deba@861
   210
        check(!collinear(e2, e1, f2), "Wrong drawing");
deba@861
   211
      } else if (graph.v(e) == graph.v(f)) {
deba@861
   212
        check(!collinear(e2, e1, f1), "Wrong drawing");
deba@861
   213
      } else {
deba@861
   214
        check(!intersect(e1, e2, f1, f2), "Wrong drawing");
deba@861
   215
      }
deba@861
   216
    }
deba@861
   217
  }
deba@861
   218
}
deba@861
   219
deba@861
   220
void checkColoring(const Graph& graph, PC& pc, int num) {
deba@861
   221
  for (NodeIt n(graph); n != INVALID; ++n) {
deba@861
   222
    check(pc.colorIndex(n) >= 0 && pc.colorIndex(n) < num,
deba@861
   223
          "Wrong coloring");
deba@861
   224
  }
deba@861
   225
  for (EdgeIt e(graph); e != INVALID; ++e) {
deba@861
   226
    check(pc.colorIndex(graph.u(e)) != pc.colorIndex(graph.v(e)),
deba@861
   227
          "Wrong coloring");
deba@861
   228
  }
deba@861
   229
}
deba@861
   230
deba@861
   231
int main() {
deba@861
   232
deba@861
   233
  for (int i = 0; i < lgfn; ++i) {
deba@861
   234
    std::istringstream lgfs(lgf[i]);
deba@861
   235
deba@861
   236
    SmartGraph graph;
deba@861
   237
    graphReader(graph, lgfs).run();
deba@861
   238
deba@861
   239
    check(simpleGraph(graph), "Test graphs must be simple");
deba@861
   240
deba@861
   241
    PE pe(graph);
deba@862
   242
    bool planar = pe.run();
deba@862
   243
    check(checkPlanarity(graph) == planar, "Planarity checking failed");
deba@862
   244
deba@862
   245
    if (planar) {
deba@861
   246
      checkEmbedding(graph, pe);
deba@861
   247
deba@861
   248
      PlanarDrawing<Graph> pd(graph);
deba@862
   249
      pd.run(pe.embeddingMap());
deba@861
   250
      checkDrawing(graph, pd);
deba@861
   251
deba@861
   252
      PlanarColoring<Graph> pc(graph);
deba@862
   253
      pc.runFiveColoring(pe.embeddingMap());
deba@861
   254
      checkColoring(graph, pc, 5);
deba@861
   255
deba@861
   256
    } else {
deba@861
   257
      checkKuratowski(graph, pe);
deba@861
   258
    }
deba@861
   259
  }
deba@861
   260
deba@861
   261
  return 0;
deba@861
   262
}