test/connectivity_test.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 25 Aug 2010 21:34:58 +0200
changeset 742 16d466589b54
child 761 f1398882a928
child 792 761fe0846f49
permissions -rw-r--r--
Backport the CMAKE related changesets from the main branch

[e20fecd1945f] Add check target for CMake (#388)
[e74b5db4f2c6] Put the output of chg-len.py in the version string (#389)
[7d166b8b8018] Safer call of ./scripts/chg-len.py on Windows (#389)
[659ba4805a48] Make CMAKE config more consistent with configure.ac (#390)
[63e4468c680e] Add 'Maintainer' CMAKE build type (#388, #390)
[a725503acfe9] Allow CPACK configuration on all platforms
[10242c611190] Create and install lemon.pc (#393)
[48e29534cf03] The deafult target doesn't build lp_test and mip_test (#388)
[0fbbdd578c06] Full path for DoxygenLayout.xml in Doxyfile.in (#395)
[481496e6d71f] SOURCE_BROWSER Doxygen switch is configurable from CMAKE (#395)
[09282720100b] update-external-tags CMAKE target (#395)
kpeter@647
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@647
     2
 *
kpeter@647
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@647
     4
 *
kpeter@647
     5
 * Copyright (C) 2003-2009
kpeter@647
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@647
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@647
     8
 *
kpeter@647
     9
 * Permission to use, modify and distribute this software is granted
kpeter@647
    10
 * provided that this copyright notice appears in all copies. For
kpeter@647
    11
 * precise terms see the accompanying LICENSE file.
kpeter@647
    12
 *
kpeter@647
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@647
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@647
    15
 * purpose.
kpeter@647
    16
 *
kpeter@647
    17
 */
kpeter@647
    18
kpeter@647
    19
#include <lemon/connectivity.h>
kpeter@647
    20
#include <lemon/list_graph.h>
kpeter@647
    21
#include <lemon/adaptors.h>
kpeter@647
    22
kpeter@647
    23
#include "test_tools.h"
kpeter@647
    24
kpeter@647
    25
using namespace lemon;
kpeter@647
    26
kpeter@647
    27
kpeter@647
    28
int main()
kpeter@647
    29
{
kpeter@647
    30
  typedef ListDigraph Digraph;
kpeter@647
    31
  typedef Undirector<Digraph> Graph;
kpeter@647
    32
  
kpeter@647
    33
  {
kpeter@647
    34
    Digraph d;
kpeter@647
    35
    Digraph::NodeMap<int> order(d);
kpeter@647
    36
    Graph g(d);
kpeter@647
    37
    
kpeter@647
    38
    check(stronglyConnected(d), "The empty digraph is strongly connected");
kpeter@647
    39
    check(countStronglyConnectedComponents(d) == 0,
kpeter@647
    40
          "The empty digraph has 0 strongly connected component");
kpeter@647
    41
    check(connected(g), "The empty graph is connected");
kpeter@647
    42
    check(countConnectedComponents(g) == 0,
kpeter@647
    43
          "The empty graph has 0 connected component");
kpeter@647
    44
kpeter@647
    45
    check(biNodeConnected(g), "The empty graph is bi-node-connected");
kpeter@647
    46
    check(countBiNodeConnectedComponents(g) == 0,
kpeter@647
    47
          "The empty graph has 0 bi-node-connected component");
kpeter@647
    48
    check(biEdgeConnected(g), "The empty graph is bi-edge-connected");
kpeter@647
    49
    check(countBiEdgeConnectedComponents(g) == 0,
kpeter@647
    50
          "The empty graph has 0 bi-edge-connected component");
kpeter@647
    51
          
kpeter@647
    52
    check(dag(d), "The empty digraph is DAG.");
kpeter@647
    53
    check(checkedTopologicalSort(d, order), "The empty digraph is DAG.");
kpeter@647
    54
    check(loopFree(d), "The empty digraph is loop-free.");
kpeter@647
    55
    check(parallelFree(d), "The empty digraph is parallel-free.");
kpeter@647
    56
    check(simpleGraph(d), "The empty digraph is simple.");
kpeter@647
    57
kpeter@647
    58
    check(acyclic(g), "The empty graph is acyclic.");
kpeter@647
    59
    check(tree(g), "The empty graph is tree.");
kpeter@647
    60
    check(bipartite(g), "The empty graph is bipartite.");
kpeter@647
    61
    check(loopFree(g), "The empty graph is loop-free.");
kpeter@647
    62
    check(parallelFree(g), "The empty graph is parallel-free.");
kpeter@647
    63
    check(simpleGraph(g), "The empty graph is simple.");
kpeter@647
    64
  }
kpeter@647
    65
kpeter@647
    66
  {
kpeter@647
    67
    Digraph d;
kpeter@647
    68
    Digraph::NodeMap<int> order(d);
kpeter@647
    69
    Graph g(d);
kpeter@647
    70
    Digraph::Node n = d.addNode();
kpeter@647
    71
kpeter@647
    72
    check(stronglyConnected(d), "This digraph is strongly connected");
kpeter@647
    73
    check(countStronglyConnectedComponents(d) == 1,
kpeter@647
    74
          "This digraph has 1 strongly connected component");
kpeter@647
    75
    check(connected(g), "This graph is connected");
kpeter@647
    76
    check(countConnectedComponents(g) == 1,
kpeter@647
    77
          "This graph has 1 connected component");
kpeter@647
    78
kpeter@647
    79
    check(biNodeConnected(g), "This graph is bi-node-connected");
kpeter@647
    80
    check(countBiNodeConnectedComponents(g) == 0,
kpeter@647
    81
          "This graph has 0 bi-node-connected component");
kpeter@647
    82
    check(biEdgeConnected(g), "This graph is bi-edge-connected");
kpeter@647
    83
    check(countBiEdgeConnectedComponents(g) == 1,
kpeter@647
    84
          "This graph has 1 bi-edge-connected component");
kpeter@647
    85
          
kpeter@647
    86
    check(dag(d), "This digraph is DAG.");
kpeter@647
    87
    check(checkedTopologicalSort(d, order), "This digraph is DAG.");
kpeter@647
    88
    check(loopFree(d), "This digraph is loop-free.");
kpeter@647
    89
    check(parallelFree(d), "This digraph is parallel-free.");
kpeter@647
    90
    check(simpleGraph(d), "This digraph is simple.");
kpeter@647
    91
kpeter@647
    92
    check(acyclic(g), "This graph is acyclic.");
kpeter@647
    93
    check(tree(g), "This graph is tree.");
kpeter@647
    94
    check(bipartite(g), "This graph is bipartite.");
kpeter@647
    95
    check(loopFree(g), "This graph is loop-free.");
kpeter@647
    96
    check(parallelFree(g), "This graph is parallel-free.");
kpeter@647
    97
    check(simpleGraph(g), "This graph is simple.");
kpeter@647
    98
  }
kpeter@647
    99
kpeter@647
   100
  {
kpeter@647
   101
    Digraph d;
kpeter@647
   102
    Digraph::NodeMap<int> order(d);
kpeter@647
   103
    Graph g(d);
kpeter@647
   104
    
kpeter@647
   105
    Digraph::Node n1 = d.addNode();
kpeter@647
   106
    Digraph::Node n2 = d.addNode();
kpeter@647
   107
    Digraph::Node n3 = d.addNode();
kpeter@647
   108
    Digraph::Node n4 = d.addNode();
kpeter@647
   109
    Digraph::Node n5 = d.addNode();
kpeter@647
   110
    Digraph::Node n6 = d.addNode();
kpeter@647
   111
    
kpeter@647
   112
    d.addArc(n1, n3);
kpeter@647
   113
    d.addArc(n3, n2);
kpeter@647
   114
    d.addArc(n2, n1);
kpeter@647
   115
    d.addArc(n4, n2);
kpeter@647
   116
    d.addArc(n4, n3);
kpeter@647
   117
    d.addArc(n5, n6);
kpeter@647
   118
    d.addArc(n6, n5);
kpeter@647
   119
kpeter@647
   120
    check(!stronglyConnected(d), "This digraph is not strongly connected");
kpeter@647
   121
    check(countStronglyConnectedComponents(d) == 3,
kpeter@647
   122
          "This digraph has 3 strongly connected components");
kpeter@647
   123
    check(!connected(g), "This graph is not connected");
kpeter@647
   124
    check(countConnectedComponents(g) == 2,
kpeter@647
   125
          "This graph has 2 connected components");
kpeter@647
   126
kpeter@647
   127
    check(!dag(d), "This digraph is not DAG.");
kpeter@647
   128
    check(!checkedTopologicalSort(d, order), "This digraph is not DAG.");
kpeter@647
   129
    check(loopFree(d), "This digraph is loop-free.");
kpeter@647
   130
    check(parallelFree(d), "This digraph is parallel-free.");
kpeter@647
   131
    check(simpleGraph(d), "This digraph is simple.");
kpeter@647
   132
kpeter@647
   133
    check(!acyclic(g), "This graph is not acyclic.");
kpeter@647
   134
    check(!tree(g), "This graph is not tree.");
kpeter@647
   135
    check(!bipartite(g), "This graph is not bipartite.");
kpeter@647
   136
    check(loopFree(g), "This graph is loop-free.");
kpeter@647
   137
    check(!parallelFree(g), "This graph is not parallel-free.");
kpeter@647
   138
    check(!simpleGraph(g), "This graph is not simple.");
kpeter@647
   139
    
kpeter@647
   140
    d.addArc(n3, n3);
kpeter@647
   141
    
kpeter@647
   142
    check(!loopFree(d), "This digraph is not loop-free.");
kpeter@647
   143
    check(!loopFree(g), "This graph is not loop-free.");
kpeter@647
   144
    check(!simpleGraph(d), "This digraph is not simple.");
kpeter@647
   145
    
kpeter@647
   146
    d.addArc(n3, n2);
kpeter@647
   147
    
kpeter@647
   148
    check(!parallelFree(d), "This digraph is not parallel-free.");
kpeter@647
   149
  }
kpeter@647
   150
  
kpeter@647
   151
  {
kpeter@647
   152
    Digraph d;
kpeter@647
   153
    Digraph::ArcMap<bool> cutarcs(d, false);
kpeter@647
   154
    Graph g(d);
kpeter@647
   155
    
kpeter@647
   156
    Digraph::Node n1 = d.addNode();
kpeter@647
   157
    Digraph::Node n2 = d.addNode();
kpeter@647
   158
    Digraph::Node n3 = d.addNode();
kpeter@647
   159
    Digraph::Node n4 = d.addNode();
kpeter@647
   160
    Digraph::Node n5 = d.addNode();
kpeter@647
   161
    Digraph::Node n6 = d.addNode();
kpeter@647
   162
    Digraph::Node n7 = d.addNode();
kpeter@647
   163
    Digraph::Node n8 = d.addNode();
kpeter@647
   164
kpeter@647
   165
    d.addArc(n1, n2);
kpeter@647
   166
    d.addArc(n5, n1);
kpeter@647
   167
    d.addArc(n2, n8);
kpeter@647
   168
    d.addArc(n8, n5);
kpeter@647
   169
    d.addArc(n6, n4);
kpeter@647
   170
    d.addArc(n4, n6);
kpeter@647
   171
    d.addArc(n2, n5);
kpeter@647
   172
    d.addArc(n1, n8);
kpeter@647
   173
    d.addArc(n6, n7);
kpeter@647
   174
    d.addArc(n7, n6);
kpeter@647
   175
   
kpeter@647
   176
    check(!stronglyConnected(d), "This digraph is not strongly connected");
kpeter@647
   177
    check(countStronglyConnectedComponents(d) == 3,
kpeter@647
   178
          "This digraph has 3 strongly connected components");
kpeter@647
   179
    Digraph::NodeMap<int> scomp1(d);
kpeter@647
   180
    check(stronglyConnectedComponents(d, scomp1) == 3,
kpeter@647
   181
          "This digraph has 3 strongly connected components");
kpeter@647
   182
    check(scomp1[n1] != scomp1[n3] && scomp1[n1] != scomp1[n4] &&
kpeter@647
   183
          scomp1[n3] != scomp1[n4], "Wrong stronglyConnectedComponents()");
kpeter@647
   184
    check(scomp1[n1] == scomp1[n2] && scomp1[n1] == scomp1[n5] &&
kpeter@647
   185
          scomp1[n1] == scomp1[n8], "Wrong stronglyConnectedComponents()");
kpeter@647
   186
    check(scomp1[n4] == scomp1[n6] && scomp1[n4] == scomp1[n7],
kpeter@647
   187
          "Wrong stronglyConnectedComponents()");
kpeter@647
   188
    Digraph::ArcMap<bool> scut1(d, false);
kpeter@647
   189
    check(stronglyConnectedCutArcs(d, scut1) == 0,
kpeter@647
   190
          "This digraph has 0 strongly connected cut arc.");
kpeter@647
   191
    for (Digraph::ArcIt a(d); a != INVALID; ++a) {
kpeter@647
   192
      check(!scut1[a], "Wrong stronglyConnectedCutArcs()");
kpeter@647
   193
    }
kpeter@647
   194
kpeter@647
   195
    check(!connected(g), "This graph is not connected");
kpeter@647
   196
    check(countConnectedComponents(g) == 3,
kpeter@647
   197
          "This graph has 3 connected components");
kpeter@647
   198
    Graph::NodeMap<int> comp(g);
kpeter@647
   199
    check(connectedComponents(g, comp) == 3,
kpeter@647
   200
          "This graph has 3 connected components");
kpeter@647
   201
    check(comp[n1] != comp[n3] && comp[n1] != comp[n4] &&
kpeter@647
   202
          comp[n3] != comp[n4], "Wrong connectedComponents()");
kpeter@647
   203
    check(comp[n1] == comp[n2] && comp[n1] == comp[n5] &&
kpeter@647
   204
          comp[n1] == comp[n8], "Wrong connectedComponents()");
kpeter@647
   205
    check(comp[n4] == comp[n6] && comp[n4] == comp[n7],
kpeter@647
   206
          "Wrong connectedComponents()");
kpeter@647
   207
kpeter@647
   208
    cutarcs[d.addArc(n3, n1)] = true;
kpeter@647
   209
    cutarcs[d.addArc(n3, n5)] = true;
kpeter@647
   210
    cutarcs[d.addArc(n3, n8)] = true;
kpeter@647
   211
    cutarcs[d.addArc(n8, n6)] = true;
kpeter@647
   212
    cutarcs[d.addArc(n8, n7)] = true;
kpeter@647
   213
kpeter@647
   214
    check(!stronglyConnected(d), "This digraph is not strongly connected");
kpeter@647
   215
    check(countStronglyConnectedComponents(d) == 3,
kpeter@647
   216
          "This digraph has 3 strongly connected components");
kpeter@647
   217
    Digraph::NodeMap<int> scomp2(d);
kpeter@647
   218
    check(stronglyConnectedComponents(d, scomp2) == 3,
kpeter@647
   219
          "This digraph has 3 strongly connected components");
kpeter@647
   220
    check(scomp2[n3] == 0, "Wrong stronglyConnectedComponents()");
kpeter@647
   221
    check(scomp2[n1] == 1 && scomp2[n2] == 1 && scomp2[n5] == 1 &&
kpeter@647
   222
          scomp2[n8] == 1, "Wrong stronglyConnectedComponents()");
kpeter@647
   223
    check(scomp2[n4] == 2 && scomp2[n6] == 2 && scomp2[n7] == 2,
kpeter@647
   224
          "Wrong stronglyConnectedComponents()");
kpeter@647
   225
    Digraph::ArcMap<bool> scut2(d, false);
kpeter@647
   226
    check(stronglyConnectedCutArcs(d, scut2) == 5,
kpeter@647
   227
          "This digraph has 5 strongly connected cut arcs.");
kpeter@647
   228
    for (Digraph::ArcIt a(d); a != INVALID; ++a) {
kpeter@647
   229
      check(scut2[a] == cutarcs[a], "Wrong stronglyConnectedCutArcs()");
kpeter@647
   230
    }
kpeter@647
   231
  }
kpeter@647
   232
kpeter@647
   233
  {
kpeter@647
   234
    // DAG example for topological sort from the book New Algorithms
kpeter@647
   235
    // (T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein)
kpeter@647
   236
    Digraph d;
kpeter@647
   237
    Digraph::NodeMap<int> order(d);
kpeter@647
   238
    
kpeter@647
   239
    Digraph::Node belt = d.addNode();
kpeter@647
   240
    Digraph::Node trousers = d.addNode();
kpeter@647
   241
    Digraph::Node necktie = d.addNode();
kpeter@647
   242
    Digraph::Node coat = d.addNode();
kpeter@647
   243
    Digraph::Node socks = d.addNode();
kpeter@647
   244
    Digraph::Node shirt = d.addNode();
kpeter@647
   245
    Digraph::Node shoe = d.addNode();
kpeter@647
   246
    Digraph::Node watch = d.addNode();
kpeter@647
   247
    Digraph::Node pants = d.addNode();
kpeter@647
   248
kpeter@647
   249
    d.addArc(socks, shoe);
kpeter@647
   250
    d.addArc(pants, shoe);
kpeter@647
   251
    d.addArc(pants, trousers);
kpeter@647
   252
    d.addArc(trousers, shoe);
kpeter@647
   253
    d.addArc(trousers, belt);
kpeter@647
   254
    d.addArc(belt, coat);
kpeter@647
   255
    d.addArc(shirt, belt);
kpeter@647
   256
    d.addArc(shirt, necktie);
kpeter@647
   257
    d.addArc(necktie, coat);
kpeter@647
   258
    
kpeter@647
   259
    check(dag(d), "This digraph is DAG.");
kpeter@647
   260
    topologicalSort(d, order);
kpeter@647
   261
    for (Digraph::ArcIt a(d); a != INVALID; ++a) {
kpeter@647
   262
      check(order[d.source(a)] < order[d.target(a)],
kpeter@647
   263
            "Wrong topologicalSort()");
kpeter@647
   264
    }
kpeter@647
   265
  }
kpeter@647
   266
kpeter@647
   267
  {
kpeter@647
   268
    ListGraph g;
kpeter@647
   269
    ListGraph::NodeMap<bool> map(g);
kpeter@647
   270
    
kpeter@647
   271
    ListGraph::Node n1 = g.addNode();
kpeter@647
   272
    ListGraph::Node n2 = g.addNode();
kpeter@647
   273
    ListGraph::Node n3 = g.addNode();
kpeter@647
   274
    ListGraph::Node n4 = g.addNode();
kpeter@647
   275
    ListGraph::Node n5 = g.addNode();
kpeter@647
   276
    ListGraph::Node n6 = g.addNode();
kpeter@647
   277
    ListGraph::Node n7 = g.addNode();
kpeter@647
   278
kpeter@647
   279
    g.addEdge(n1, n3);
kpeter@647
   280
    g.addEdge(n1, n4);
kpeter@647
   281
    g.addEdge(n2, n5);
kpeter@647
   282
    g.addEdge(n3, n6);
kpeter@647
   283
    g.addEdge(n4, n6);
kpeter@647
   284
    g.addEdge(n4, n7);
kpeter@647
   285
    g.addEdge(n5, n7);
kpeter@647
   286
   
kpeter@647
   287
    check(bipartite(g), "This graph is bipartite");
kpeter@647
   288
    check(bipartitePartitions(g, map), "This graph is bipartite");
kpeter@647
   289
    
kpeter@647
   290
    check(map[n1] == map[n2] && map[n1] == map[n6] && map[n1] == map[n7],
kpeter@647
   291
          "Wrong bipartitePartitions()");
kpeter@647
   292
    check(map[n3] == map[n4] && map[n3] == map[n5],
kpeter@647
   293
          "Wrong bipartitePartitions()");
kpeter@647
   294
  }
kpeter@647
   295
kpeter@647
   296
  return 0;
kpeter@647
   297
}