test/connectivity_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 16 Nov 2015 08:46:42 +0100
changeset 1156 f51c01a1b88e
parent 1091 9eac00ea588f
permissions -rw-r--r--
Remove unnecessary test include from lemon/vf2.h (#603)
kpeter@649
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@649
     2
 *
kpeter@649
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@649
     4
 *
alpar@1092
     5
 * Copyright (C) 2003-2013
kpeter@649
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@649
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@649
     8
 *
kpeter@649
     9
 * Permission to use, modify and distribute this software is granted
kpeter@649
    10
 * provided that this copyright notice appears in all copies. For
kpeter@649
    11
 * precise terms see the accompanying LICENSE file.
kpeter@649
    12
 *
kpeter@649
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@649
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@649
    15
 * purpose.
kpeter@649
    16
 *
kpeter@649
    17
 */
kpeter@649
    18
kpeter@649
    19
#include <lemon/connectivity.h>
kpeter@649
    20
#include <lemon/list_graph.h>
kpeter@649
    21
#include <lemon/adaptors.h>
kpeter@649
    22
kpeter@649
    23
#include "test_tools.h"
kpeter@649
    24
kpeter@649
    25
using namespace lemon;
kpeter@649
    26
kpeter@649
    27
kpeter@649
    28
int main()
kpeter@649
    29
{
kpeter@649
    30
  typedef ListDigraph Digraph;
kpeter@649
    31
  typedef Undirector<Digraph> Graph;
alpar@877
    32
kpeter@649
    33
  {
kpeter@649
    34
    Digraph d;
kpeter@649
    35
    Digraph::NodeMap<int> order(d);
kpeter@649
    36
    Graph g(d);
alpar@877
    37
kpeter@649
    38
    check(stronglyConnected(d), "The empty digraph is strongly connected");
kpeter@649
    39
    check(countStronglyConnectedComponents(d) == 0,
kpeter@649
    40
          "The empty digraph has 0 strongly connected component");
kpeter@649
    41
    check(connected(g), "The empty graph is connected");
kpeter@649
    42
    check(countConnectedComponents(g) == 0,
kpeter@649
    43
          "The empty graph has 0 connected component");
kpeter@649
    44
kpeter@649
    45
    check(biNodeConnected(g), "The empty graph is bi-node-connected");
kpeter@649
    46
    check(countBiNodeConnectedComponents(g) == 0,
kpeter@649
    47
          "The empty graph has 0 bi-node-connected component");
kpeter@649
    48
    check(biEdgeConnected(g), "The empty graph is bi-edge-connected");
kpeter@649
    49
    check(countBiEdgeConnectedComponents(g) == 0,
kpeter@649
    50
          "The empty graph has 0 bi-edge-connected component");
alpar@877
    51
kpeter@649
    52
    check(dag(d), "The empty digraph is DAG.");
kpeter@649
    53
    check(checkedTopologicalSort(d, order), "The empty digraph is DAG.");
kpeter@649
    54
    check(loopFree(d), "The empty digraph is loop-free.");
kpeter@649
    55
    check(parallelFree(d), "The empty digraph is parallel-free.");
kpeter@649
    56
    check(simpleGraph(d), "The empty digraph is simple.");
kpeter@649
    57
kpeter@649
    58
    check(acyclic(g), "The empty graph is acyclic.");
kpeter@649
    59
    check(tree(g), "The empty graph is tree.");
kpeter@649
    60
    check(bipartite(g), "The empty graph is bipartite.");
kpeter@649
    61
    check(loopFree(g), "The empty graph is loop-free.");
kpeter@649
    62
    check(parallelFree(g), "The empty graph is parallel-free.");
kpeter@649
    63
    check(simpleGraph(g), "The empty graph is simple.");
kpeter@649
    64
  }
kpeter@649
    65
kpeter@649
    66
  {
kpeter@649
    67
    Digraph d;
kpeter@649
    68
    Digraph::NodeMap<int> order(d);
kpeter@649
    69
    Graph g(d);
kpeter@649
    70
    Digraph::Node n = d.addNode();
alpar@1083
    71
    ::lemon::ignore_unused_variable_warning(n);
kpeter@649
    72
kpeter@649
    73
    check(stronglyConnected(d), "This digraph is strongly connected");
kpeter@649
    74
    check(countStronglyConnectedComponents(d) == 1,
kpeter@649
    75
          "This digraph has 1 strongly connected component");
kpeter@649
    76
    check(connected(g), "This graph is connected");
kpeter@649
    77
    check(countConnectedComponents(g) == 1,
kpeter@649
    78
          "This graph has 1 connected component");
kpeter@649
    79
kpeter@649
    80
    check(biNodeConnected(g), "This graph is bi-node-connected");
kpeter@649
    81
    check(countBiNodeConnectedComponents(g) == 0,
kpeter@649
    82
          "This graph has 0 bi-node-connected component");
kpeter@649
    83
    check(biEdgeConnected(g), "This graph is bi-edge-connected");
kpeter@649
    84
    check(countBiEdgeConnectedComponents(g) == 1,
kpeter@649
    85
          "This graph has 1 bi-edge-connected component");
alpar@877
    86
kpeter@649
    87
    check(dag(d), "This digraph is DAG.");
kpeter@649
    88
    check(checkedTopologicalSort(d, order), "This digraph is DAG.");
kpeter@649
    89
    check(loopFree(d), "This digraph is loop-free.");
kpeter@649
    90
    check(parallelFree(d), "This digraph is parallel-free.");
kpeter@649
    91
    check(simpleGraph(d), "This digraph is simple.");
kpeter@649
    92
kpeter@649
    93
    check(acyclic(g), "This graph is acyclic.");
kpeter@649
    94
    check(tree(g), "This graph is tree.");
kpeter@649
    95
    check(bipartite(g), "This graph is bipartite.");
kpeter@649
    96
    check(loopFree(g), "This graph is loop-free.");
kpeter@649
    97
    check(parallelFree(g), "This graph is parallel-free.");
kpeter@649
    98
    check(simpleGraph(g), "This graph is simple.");
kpeter@649
    99
  }
kpeter@649
   100
kpeter@649
   101
  {
deba@1089
   102
    ListGraph g;
deba@1089
   103
    ListGraph::NodeMap<bool> map(g);
deba@1089
   104
deba@1089
   105
    ListGraph::Node n1 = g.addNode();
deba@1089
   106
    ListGraph::Node n2 = g.addNode();
deba@1089
   107
deba@1089
   108
    ListGraph::Edge e1 = g.addEdge(n1, n2);
deba@1089
   109
    ::lemon::ignore_unused_variable_warning(e1);
deba@1089
   110
    check(biNodeConnected(g), "Graph is bi-node-connected");
deba@1089
   111
deba@1089
   112
    ListGraph::Node n3 = g.addNode();
deba@1089
   113
    ::lemon::ignore_unused_variable_warning(n3);
deba@1089
   114
    check(!biNodeConnected(g), "Graph is not bi-node-connected");
deba@1089
   115
  }
deba@1089
   116
deba@1089
   117
deba@1089
   118
  {
kpeter@649
   119
    Digraph d;
kpeter@649
   120
    Digraph::NodeMap<int> order(d);
kpeter@649
   121
    Graph g(d);
alpar@877
   122
kpeter@649
   123
    Digraph::Node n1 = d.addNode();
kpeter@649
   124
    Digraph::Node n2 = d.addNode();
kpeter@649
   125
    Digraph::Node n3 = d.addNode();
kpeter@649
   126
    Digraph::Node n4 = d.addNode();
kpeter@649
   127
    Digraph::Node n5 = d.addNode();
kpeter@649
   128
    Digraph::Node n6 = d.addNode();
alpar@877
   129
kpeter@649
   130
    d.addArc(n1, n3);
kpeter@649
   131
    d.addArc(n3, n2);
kpeter@649
   132
    d.addArc(n2, n1);
kpeter@649
   133
    d.addArc(n4, n2);
kpeter@649
   134
    d.addArc(n4, n3);
kpeter@649
   135
    d.addArc(n5, n6);
kpeter@649
   136
    d.addArc(n6, n5);
kpeter@649
   137
kpeter@649
   138
    check(!stronglyConnected(d), "This digraph is not strongly connected");
kpeter@649
   139
    check(countStronglyConnectedComponents(d) == 3,
kpeter@649
   140
          "This digraph has 3 strongly connected components");
kpeter@649
   141
    check(!connected(g), "This graph is not connected");
kpeter@649
   142
    check(countConnectedComponents(g) == 2,
kpeter@649
   143
          "This graph has 2 connected components");
kpeter@649
   144
kpeter@649
   145
    check(!dag(d), "This digraph is not DAG.");
kpeter@649
   146
    check(!checkedTopologicalSort(d, order), "This digraph is not DAG.");
kpeter@649
   147
    check(loopFree(d), "This digraph is loop-free.");
kpeter@649
   148
    check(parallelFree(d), "This digraph is parallel-free.");
kpeter@649
   149
    check(simpleGraph(d), "This digraph is simple.");
kpeter@649
   150
kpeter@649
   151
    check(!acyclic(g), "This graph is not acyclic.");
kpeter@649
   152
    check(!tree(g), "This graph is not tree.");
kpeter@649
   153
    check(!bipartite(g), "This graph is not bipartite.");
kpeter@649
   154
    check(loopFree(g), "This graph is loop-free.");
kpeter@649
   155
    check(!parallelFree(g), "This graph is not parallel-free.");
kpeter@649
   156
    check(!simpleGraph(g), "This graph is not simple.");
alpar@877
   157
kpeter@649
   158
    d.addArc(n3, n3);
alpar@877
   159
kpeter@649
   160
    check(!loopFree(d), "This digraph is not loop-free.");
kpeter@649
   161
    check(!loopFree(g), "This graph is not loop-free.");
kpeter@649
   162
    check(!simpleGraph(d), "This digraph is not simple.");
alpar@877
   163
kpeter@649
   164
    d.addArc(n3, n2);
alpar@877
   165
kpeter@649
   166
    check(!parallelFree(d), "This digraph is not parallel-free.");
kpeter@649
   167
  }
alpar@877
   168
kpeter@649
   169
  {
kpeter@649
   170
    Digraph d;
kpeter@649
   171
    Digraph::ArcMap<bool> cutarcs(d, false);
kpeter@649
   172
    Graph g(d);
alpar@877
   173
kpeter@649
   174
    Digraph::Node n1 = d.addNode();
kpeter@649
   175
    Digraph::Node n2 = d.addNode();
kpeter@649
   176
    Digraph::Node n3 = d.addNode();
kpeter@649
   177
    Digraph::Node n4 = d.addNode();
kpeter@649
   178
    Digraph::Node n5 = d.addNode();
kpeter@649
   179
    Digraph::Node n6 = d.addNode();
kpeter@649
   180
    Digraph::Node n7 = d.addNode();
kpeter@649
   181
    Digraph::Node n8 = d.addNode();
kpeter@649
   182
kpeter@649
   183
    d.addArc(n1, n2);
kpeter@649
   184
    d.addArc(n5, n1);
kpeter@649
   185
    d.addArc(n2, n8);
kpeter@649
   186
    d.addArc(n8, n5);
kpeter@649
   187
    d.addArc(n6, n4);
kpeter@649
   188
    d.addArc(n4, n6);
kpeter@649
   189
    d.addArc(n2, n5);
kpeter@649
   190
    d.addArc(n1, n8);
kpeter@649
   191
    d.addArc(n6, n7);
kpeter@649
   192
    d.addArc(n7, n6);
alpar@877
   193
kpeter@649
   194
    check(!stronglyConnected(d), "This digraph is not strongly connected");
kpeter@649
   195
    check(countStronglyConnectedComponents(d) == 3,
kpeter@649
   196
          "This digraph has 3 strongly connected components");
kpeter@649
   197
    Digraph::NodeMap<int> scomp1(d);
kpeter@649
   198
    check(stronglyConnectedComponents(d, scomp1) == 3,
kpeter@649
   199
          "This digraph has 3 strongly connected components");
kpeter@649
   200
    check(scomp1[n1] != scomp1[n3] && scomp1[n1] != scomp1[n4] &&
kpeter@649
   201
          scomp1[n3] != scomp1[n4], "Wrong stronglyConnectedComponents()");
kpeter@649
   202
    check(scomp1[n1] == scomp1[n2] && scomp1[n1] == scomp1[n5] &&
kpeter@649
   203
          scomp1[n1] == scomp1[n8], "Wrong stronglyConnectedComponents()");
kpeter@649
   204
    check(scomp1[n4] == scomp1[n6] && scomp1[n4] == scomp1[n7],
kpeter@649
   205
          "Wrong stronglyConnectedComponents()");
kpeter@649
   206
    Digraph::ArcMap<bool> scut1(d, false);
kpeter@649
   207
    check(stronglyConnectedCutArcs(d, scut1) == 0,
kpeter@649
   208
          "This digraph has 0 strongly connected cut arc.");
kpeter@649
   209
    for (Digraph::ArcIt a(d); a != INVALID; ++a) {
kpeter@649
   210
      check(!scut1[a], "Wrong stronglyConnectedCutArcs()");
kpeter@649
   211
    }
kpeter@649
   212
kpeter@649
   213
    check(!connected(g), "This graph is not connected");
kpeter@649
   214
    check(countConnectedComponents(g) == 3,
kpeter@649
   215
          "This graph has 3 connected components");
kpeter@649
   216
    Graph::NodeMap<int> comp(g);
kpeter@649
   217
    check(connectedComponents(g, comp) == 3,
kpeter@649
   218
          "This graph has 3 connected components");
kpeter@649
   219
    check(comp[n1] != comp[n3] && comp[n1] != comp[n4] &&
kpeter@649
   220
          comp[n3] != comp[n4], "Wrong connectedComponents()");
kpeter@649
   221
    check(comp[n1] == comp[n2] && comp[n1] == comp[n5] &&
kpeter@649
   222
          comp[n1] == comp[n8], "Wrong connectedComponents()");
kpeter@649
   223
    check(comp[n4] == comp[n6] && comp[n4] == comp[n7],
kpeter@649
   224
          "Wrong connectedComponents()");
kpeter@649
   225
kpeter@649
   226
    cutarcs[d.addArc(n3, n1)] = true;
kpeter@649
   227
    cutarcs[d.addArc(n3, n5)] = true;
kpeter@649
   228
    cutarcs[d.addArc(n3, n8)] = true;
kpeter@649
   229
    cutarcs[d.addArc(n8, n6)] = true;
kpeter@649
   230
    cutarcs[d.addArc(n8, n7)] = true;
kpeter@649
   231
kpeter@649
   232
    check(!stronglyConnected(d), "This digraph is not strongly connected");
kpeter@649
   233
    check(countStronglyConnectedComponents(d) == 3,
kpeter@649
   234
          "This digraph has 3 strongly connected components");
kpeter@649
   235
    Digraph::NodeMap<int> scomp2(d);
kpeter@649
   236
    check(stronglyConnectedComponents(d, scomp2) == 3,
kpeter@649
   237
          "This digraph has 3 strongly connected components");
kpeter@649
   238
    check(scomp2[n3] == 0, "Wrong stronglyConnectedComponents()");
kpeter@649
   239
    check(scomp2[n1] == 1 && scomp2[n2] == 1 && scomp2[n5] == 1 &&
kpeter@649
   240
          scomp2[n8] == 1, "Wrong stronglyConnectedComponents()");
kpeter@649
   241
    check(scomp2[n4] == 2 && scomp2[n6] == 2 && scomp2[n7] == 2,
kpeter@649
   242
          "Wrong stronglyConnectedComponents()");
kpeter@649
   243
    Digraph::ArcMap<bool> scut2(d, false);
kpeter@649
   244
    check(stronglyConnectedCutArcs(d, scut2) == 5,
kpeter@649
   245
          "This digraph has 5 strongly connected cut arcs.");
kpeter@649
   246
    for (Digraph::ArcIt a(d); a != INVALID; ++a) {
kpeter@649
   247
      check(scut2[a] == cutarcs[a], "Wrong stronglyConnectedCutArcs()");
kpeter@649
   248
    }
kpeter@649
   249
  }
kpeter@649
   250
kpeter@649
   251
  {
kpeter@649
   252
    // DAG example for topological sort from the book New Algorithms
kpeter@649
   253
    // (T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein)
kpeter@649
   254
    Digraph d;
kpeter@649
   255
    Digraph::NodeMap<int> order(d);
alpar@877
   256
kpeter@649
   257
    Digraph::Node belt = d.addNode();
kpeter@649
   258
    Digraph::Node trousers = d.addNode();
kpeter@649
   259
    Digraph::Node necktie = d.addNode();
kpeter@649
   260
    Digraph::Node coat = d.addNode();
kpeter@649
   261
    Digraph::Node socks = d.addNode();
kpeter@649
   262
    Digraph::Node shirt = d.addNode();
kpeter@649
   263
    Digraph::Node shoe = d.addNode();
kpeter@649
   264
    Digraph::Node watch = d.addNode();
kpeter@649
   265
    Digraph::Node pants = d.addNode();
alpar@1083
   266
    ::lemon::ignore_unused_variable_warning(watch);
kpeter@649
   267
kpeter@649
   268
    d.addArc(socks, shoe);
kpeter@649
   269
    d.addArc(pants, shoe);
kpeter@649
   270
    d.addArc(pants, trousers);
kpeter@649
   271
    d.addArc(trousers, shoe);
kpeter@649
   272
    d.addArc(trousers, belt);
kpeter@649
   273
    d.addArc(belt, coat);
kpeter@649
   274
    d.addArc(shirt, belt);
kpeter@649
   275
    d.addArc(shirt, necktie);
kpeter@649
   276
    d.addArc(necktie, coat);
alpar@877
   277
kpeter@649
   278
    check(dag(d), "This digraph is DAG.");
kpeter@649
   279
    topologicalSort(d, order);
kpeter@649
   280
    for (Digraph::ArcIt a(d); a != INVALID; ++a) {
kpeter@649
   281
      check(order[d.source(a)] < order[d.target(a)],
kpeter@649
   282
            "Wrong topologicalSort()");
kpeter@649
   283
    }
kpeter@649
   284
  }
kpeter@649
   285
kpeter@649
   286
  {
kpeter@649
   287
    ListGraph g;
kpeter@649
   288
    ListGraph::NodeMap<bool> map(g);
alpar@877
   289
kpeter@649
   290
    ListGraph::Node n1 = g.addNode();
kpeter@649
   291
    ListGraph::Node n2 = g.addNode();
kpeter@649
   292
    ListGraph::Node n3 = g.addNode();
kpeter@649
   293
    ListGraph::Node n4 = g.addNode();
kpeter@649
   294
    ListGraph::Node n5 = g.addNode();
kpeter@649
   295
    ListGraph::Node n6 = g.addNode();
kpeter@649
   296
    ListGraph::Node n7 = g.addNode();
kpeter@649
   297
kpeter@649
   298
    g.addEdge(n1, n3);
kpeter@649
   299
    g.addEdge(n1, n4);
kpeter@649
   300
    g.addEdge(n2, n5);
kpeter@649
   301
    g.addEdge(n3, n6);
kpeter@649
   302
    g.addEdge(n4, n6);
kpeter@649
   303
    g.addEdge(n4, n7);
kpeter@649
   304
    g.addEdge(n5, n7);
alpar@877
   305
kpeter@649
   306
    check(bipartite(g), "This graph is bipartite");
kpeter@649
   307
    check(bipartitePartitions(g, map), "This graph is bipartite");
alpar@877
   308
kpeter@649
   309
    check(map[n1] == map[n2] && map[n1] == map[n6] && map[n1] == map[n7],
kpeter@649
   310
          "Wrong bipartitePartitions()");
kpeter@649
   311
    check(map[n3] == map[n4] && map[n3] == map[n5],
kpeter@649
   312
          "Wrong bipartitePartitions()");
kpeter@649
   313
  }
kpeter@649
   314
kpeter@649
   315
  return 0;
kpeter@649
   316
}