test/planarity_test.cc
author Balazs Dezso <deba@google.com>
Wed, 17 Oct 2018 17:52:11 +0200
changeset 1400 6b79d93e812f
parent 1399 1e5da3fc4fbc
permissions -rw-r--r--
Planar drawing algorithm now works for less than 3 nodes (#611)
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@1400
    33
const int lgfn = 8;
deba@861
    34
const std::string lgf[lgfn] = {
deba@861
    35
  "@nodes\n"
deba@861
    36
  "label\n"
deba@1400
    37
  "@edges\n"
deba@1400
    38
  "     label\n",
deba@1400
    39
deba@1400
    40
  "@nodes\n"
deba@1400
    41
  "label\n"
deba@1400
    42
  "0\n"
deba@1400
    43
  "@edges\n"
deba@1400
    44
  "     label\n",
deba@1400
    45
deba@1400
    46
  "@nodes\n"
deba@1400
    47
  "label\n"
deba@1400
    48
  "0\n"
deba@1400
    49
  "1\n"
deba@1400
    50
  "@edges\n"
deba@1400
    51
  "     label\n"
deba@1400
    52
  "0 1  0\n",
deba@1400
    53
deba@1400
    54
  "@nodes\n"
deba@1400
    55
  "label\n"
deba@1400
    56
  "0\n"
deba@1400
    57
  "1\n"
deba@1400
    58
  "2\n"
deba@1400
    59
  "@edges\n"
deba@1400
    60
  "     label\n"
deba@1400
    61
  "0 1  0\n"
deba@1400
    62
  "1 2  1\n"
deba@1400
    63
  "2 0  2\n",
deba@1400
    64
deba@1400
    65
  "@nodes\n"
deba@1400
    66
  "label\n"
deba@861
    67
  "0\n"
deba@861
    68
  "1\n"
deba@861
    69
  "2\n"
deba@861
    70
  "3\n"
deba@861
    71
  "4\n"
deba@861
    72
  "@edges\n"
deba@861
    73
  "     label\n"
deba@861
    74
  "0 1  0\n"
deba@861
    75
  "0 2  0\n"
deba@861
    76
  "0 3  0\n"
deba@861
    77
  "0 4  0\n"
deba@861
    78
  "1 2  0\n"
deba@861
    79
  "1 3  0\n"
deba@861
    80
  "1 4  0\n"
deba@861
    81
  "2 3  0\n"
deba@861
    82
  "2 4  0\n"
deba@861
    83
  "3 4  0\n",
deba@861
    84
deba@861
    85
  "@nodes\n"
deba@861
    86
  "label\n"
deba@861
    87
  "0\n"
deba@861
    88
  "1\n"
deba@861
    89
  "2\n"
deba@861
    90
  "3\n"
deba@861
    91
  "4\n"
deba@861
    92
  "@edges\n"
deba@861
    93
  "     label\n"
deba@861
    94
  "0 1  0\n"
deba@861
    95
  "0 2  0\n"
deba@861
    96
  "0 3  0\n"
deba@861
    97
  "0 4  0\n"
deba@861
    98
  "1 2  0\n"
deba@861
    99
  "1 3  0\n"
deba@861
   100
  "2 3  0\n"
deba@861
   101
  "2 4  0\n"
deba@861
   102
  "3 4  0\n",
deba@861
   103
deba@861
   104
  "@nodes\n"
deba@861
   105
  "label\n"
deba@861
   106
  "0\n"
deba@861
   107
  "1\n"
deba@861
   108
  "2\n"
deba@861
   109
  "3\n"
deba@861
   110
  "4\n"
deba@861
   111
  "5\n"
deba@861
   112
  "@edges\n"
deba@861
   113
  "     label\n"
deba@861
   114
  "0 3  0\n"
deba@861
   115
  "0 4  0\n"
deba@861
   116
  "0 5  0\n"
deba@861
   117
  "1 3  0\n"
deba@861
   118
  "1 4  0\n"
deba@861
   119
  "1 5  0\n"
deba@861
   120
  "2 3  0\n"
deba@861
   121
  "2 4  0\n"
deba@861
   122
  "2 5  0\n",
deba@861
   123
deba@861
   124
  "@nodes\n"
deba@861
   125
  "label\n"
deba@861
   126
  "0\n"
deba@861
   127
  "1\n"
deba@861
   128
  "2\n"
deba@861
   129
  "3\n"
deba@861
   130
  "4\n"
deba@861
   131
  "5\n"
deba@861
   132
  "@edges\n"
deba@861
   133
  "     label\n"
deba@861
   134
  "0 3  0\n"
deba@861
   135
  "0 4  0\n"
deba@861
   136
  "0 5  0\n"
deba@861
   137
  "1 3  0\n"
deba@861
   138
  "1 4  0\n"
deba@861
   139
  "1 5  0\n"
deba@861
   140
  "2 3  0\n"
deba@861
   141
  "2 5  0\n"
deba@861
   142
};
deba@861
   143
deba@861
   144
deba@861
   145
deba@861
   146
typedef SmartGraph Graph;
deba@861
   147
GRAPH_TYPEDEFS(Graph);
deba@861
   148
deba@861
   149
typedef PlanarEmbedding<SmartGraph> PE;
deba@861
   150
typedef PlanarDrawing<SmartGraph> PD;
deba@861
   151
typedef PlanarColoring<SmartGraph> PC;
deba@861
   152
deba@861
   153
void checkEmbedding(const Graph& graph, PE& pe) {
deba@861
   154
  int face_num = 0;
deba@861
   155
deba@861
   156
  Graph::ArcMap<int> face(graph, -1);
deba@861
   157
deba@861
   158
  for (ArcIt a(graph); a != INVALID; ++a) {
deba@861
   159
    if (face[a] == -1) {
deba@861
   160
      Arc b = a;
deba@861
   161
      while (face[b] == -1) {
deba@861
   162
        face[b] = face_num;
deba@861
   163
        b = pe.next(graph.oppositeArc(b));
deba@861
   164
      }
deba@861
   165
      check(face[b] == face_num, "Wrong face");
deba@861
   166
      ++face_num;
deba@861
   167
    }
deba@861
   168
  }
deba@1400
   169
deba@1400
   170
  if (face_num != 0) {
deba@1400
   171
    check(face_num + countNodes(graph) - countConnectedComponents(graph) ==
deba@1400
   172
          countEdges(graph) + 1, "Euler test does not passed");
deba@1400
   173
  }
deba@861
   174
}
deba@861
   175
deba@861
   176
void checkKuratowski(const Graph& graph, PE& pe) {
deba@861
   177
  std::map<int, int> degs;
deba@861
   178
  for (NodeIt n(graph); n != INVALID; ++n) {
deba@861
   179
    int deg = 0;
deba@861
   180
    for (IncEdgeIt e(graph, n); e != INVALID; ++e) {
deba@861
   181
      if (pe.kuratowski(e)) {
deba@861
   182
        ++deg;
deba@861
   183
      }
deba@861
   184
    }
deba@861
   185
    ++degs[deg];
deba@861
   186
  }
deba@861
   187
  for (std::map<int, int>::iterator it = degs.begin(); it != degs.end(); ++it) {
deba@861
   188
    check(it->first == 0 || it->first == 2 ||
deba@861
   189
          (it->first == 3 && it->second == 6) ||
deba@861
   190
          (it->first == 4 && it->second == 5),
deba@861
   191
          "Wrong degree in Kuratowski graph");
deba@861
   192
  }
deba@861
   193
deba@861
   194
  // Not full test
deba@861
   195
  check((degs[3] == 0) != (degs[4] == 0), "Wrong Kuratowski graph");
deba@861
   196
}
deba@861
   197
deba@861
   198
bool intersect(Point<int> e1, Point<int> e2, Point<int> f1, Point<int> f2) {
deba@861
   199
  int l, r;
deba@861
   200
  if (std::min(e1.x, e2.x) > std::max(f1.x, f2.x)) return false;
deba@861
   201
  if (std::max(e1.x, e2.x) < std::min(f1.x, f2.x)) return false;
deba@861
   202
  if (std::min(e1.y, e2.y) > std::max(f1.y, f2.y)) return false;
deba@861
   203
  if (std::max(e1.y, e2.y) < std::min(f1.y, f2.y)) return false;
deba@861
   204
deba@861
   205
  l = (e2.x - e1.x) * (f1.y - e1.y) - (e2.y - e1.y) * (f1.x - e1.x);
deba@861
   206
  r = (e2.x - e1.x) * (f2.y - e1.y) - (e2.y - e1.y) * (f2.x - e1.x);
deba@861
   207
  if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
deba@861
   208
  l = (f2.x - f1.x) * (e1.y - f1.y) - (f2.y - f1.y) * (e1.x - f1.x);
deba@861
   209
  r = (f2.x - f1.x) * (e2.y - f1.y) - (f2.y - f1.y) * (e2.x - f1.x);
deba@861
   210
  if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
deba@861
   211
  return true;
deba@861
   212
}
deba@861
   213
deba@861
   214
bool collinear(Point<int> p, Point<int> q, Point<int> r) {
deba@861
   215
  int v;
deba@861
   216
  v = (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
deba@861
   217
  if (v != 0) return false;
deba@861
   218
  v = (q.x - p.x) * (r.x - p.x) + (q.y - p.y) * (r.y - p.y);
deba@861
   219
  if (v < 0) return false;
deba@861
   220
  return true;
deba@861
   221
}
deba@861
   222
deba@861
   223
void checkDrawing(const Graph& graph, PD& pd) {
deba@861
   224
  for (Graph::NodeIt n(graph); n != INVALID; ++n) {
deba@861
   225
    Graph::NodeIt m(n);
deba@861
   226
    for (++m; m != INVALID; ++m) {
deba@861
   227
      check(pd[m] != pd[n], "Two nodes with identical coordinates");
deba@861
   228
    }
deba@861
   229
  }
deba@861
   230
deba@861
   231
  for (Graph::EdgeIt e(graph); e != INVALID; ++e) {
deba@861
   232
    for (Graph::EdgeIt f(e); f != e; ++f) {
deba@861
   233
      Point<int> e1 = pd[graph.u(e)];
deba@861
   234
      Point<int> e2 = pd[graph.v(e)];
deba@861
   235
      Point<int> f1 = pd[graph.u(f)];
deba@861
   236
      Point<int> f2 = pd[graph.v(f)];
deba@861
   237
deba@861
   238
      if (graph.u(e) == graph.u(f)) {
deba@861
   239
        check(!collinear(e1, e2, f2), "Wrong drawing");
deba@861
   240
      } else if (graph.u(e) == graph.v(f)) {
deba@861
   241
        check(!collinear(e1, e2, f1), "Wrong drawing");
deba@861
   242
      } else if (graph.v(e) == graph.u(f)) {
deba@861
   243
        check(!collinear(e2, e1, f2), "Wrong drawing");
deba@861
   244
      } else if (graph.v(e) == graph.v(f)) {
deba@861
   245
        check(!collinear(e2, e1, f1), "Wrong drawing");
deba@861
   246
      } else {
deba@861
   247
        check(!intersect(e1, e2, f1, f2), "Wrong drawing");
deba@861
   248
      }
deba@861
   249
    }
deba@861
   250
  }
deba@861
   251
}
deba@861
   252
deba@861
   253
void checkColoring(const Graph& graph, PC& pc, int num) {
deba@861
   254
  for (NodeIt n(graph); n != INVALID; ++n) {
deba@861
   255
    check(pc.colorIndex(n) >= 0 && pc.colorIndex(n) < num,
deba@861
   256
          "Wrong coloring");
deba@861
   257
  }
deba@861
   258
  for (EdgeIt e(graph); e != INVALID; ++e) {
deba@861
   259
    check(pc.colorIndex(graph.u(e)) != pc.colorIndex(graph.v(e)),
deba@861
   260
          "Wrong coloring");
deba@861
   261
  }
deba@861
   262
}
deba@861
   263
deba@861
   264
int main() {
deba@861
   265
deba@861
   266
  for (int i = 0; i < lgfn; ++i) {
deba@861
   267
    std::istringstream lgfs(lgf[i]);
deba@861
   268
deba@861
   269
    SmartGraph graph;
deba@861
   270
    graphReader(graph, lgfs).run();
deba@861
   271
deba@861
   272
    check(simpleGraph(graph), "Test graphs must be simple");
deba@861
   273
deba@861
   274
    PE pe(graph);
deba@862
   275
    bool planar = pe.run();
deba@862
   276
    check(checkPlanarity(graph) == planar, "Planarity checking failed");
deba@862
   277
deba@862
   278
    if (planar) {
deba@861
   279
      checkEmbedding(graph, pe);
deba@861
   280
deba@1399
   281
      {
deba@1399
   282
        PlanarDrawing<Graph> pd(graph);
deba@1399
   283
        pd.run(pe.embeddingMap());
deba@1399
   284
        checkDrawing(graph, pd);
deba@1399
   285
      }
deba@861
   286
deba@1399
   287
      {
deba@1399
   288
        PlanarDrawing<Graph> pd(graph);
deba@1399
   289
        pd.run();
deba@1399
   290
        checkDrawing(graph, pd);
deba@1399
   291
      }
deba@1399
   292
deba@1399
   293
      {
deba@1399
   294
        PlanarColoring<Graph> pc(graph);
deba@1399
   295
        pc.runFiveColoring(pe.embeddingMap());
deba@1399
   296
        checkColoring(graph, pc, 5);
deba@1399
   297
      }
deba@1399
   298
deba@1399
   299
      {
deba@1399
   300
        PlanarColoring<Graph> pc(graph);
deba@1399
   301
        pc.runFiveColoring();
deba@1399
   302
        checkColoring(graph, pc, 5);
deba@1399
   303
      }
deba@861
   304
deba@861
   305
    } else {
deba@861
   306
      checkKuratowski(graph, pe);
deba@861
   307
    }
deba@861
   308
  }
deba@861
   309
deba@861
   310
  return 0;
deba@861
   311
}