lemon/connectivity.h
author Akos Ladanyi <ladanyi@tmit.bme.hu>
Tue, 28 Apr 2009 16:49:57 +0100
changeset 677 936355864d6e
parent 606 c5fd2d996909
child 694 dcba640438c7
permissions -rw-r--r--
Add cbc.h to lemon/Makefile.am (#279)
deba@433
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@433
     2
 *
deba@433
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@433
     4
 *
alpar@463
     5
 * Copyright (C) 2003-2009
deba@433
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@433
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@433
     8
 *
deba@433
     9
 * Permission to use, modify and distribute this software is granted
deba@433
    10
 * provided that this copyright notice appears in all copies. For
deba@433
    11
 * precise terms see the accompanying LICENSE file.
deba@433
    12
 *
deba@433
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@433
    14
 * express or implied, and with no claim as to its suitability for any
deba@433
    15
 * purpose.
deba@433
    16
 *
deba@433
    17
 */
deba@433
    18
deba@435
    19
#ifndef LEMON_CONNECTIVITY_H
deba@435
    20
#define LEMON_CONNECTIVITY_H
deba@433
    21
deba@433
    22
#include <lemon/dfs.h>
deba@433
    23
#include <lemon/bfs.h>
deba@433
    24
#include <lemon/core.h>
deba@433
    25
#include <lemon/maps.h>
deba@433
    26
#include <lemon/adaptors.h>
deba@433
    27
deba@433
    28
#include <lemon/concepts/digraph.h>
deba@433
    29
#include <lemon/concepts/graph.h>
deba@433
    30
#include <lemon/concept_check.h>
deba@433
    31
deba@433
    32
#include <stack>
deba@433
    33
#include <functional>
deba@433
    34
kpeter@633
    35
/// \ingroup graph_properties
deba@433
    36
/// \file
deba@433
    37
/// \brief Connectivity algorithms
deba@433
    38
///
deba@433
    39
/// Connectivity algorithms
deba@433
    40
deba@433
    41
namespace lemon {
deba@433
    42
kpeter@633
    43
  /// \ingroup graph_properties
deba@433
    44
  ///
deba@433
    45
  /// \brief Check whether the given undirected graph is connected.
deba@433
    46
  ///
deba@433
    47
  /// Check whether the given undirected graph is connected.
deba@433
    48
  /// \param graph The undirected graph.
kpeter@606
    49
  /// \return \c true when there is path between any two nodes in the graph.
deba@433
    50
  /// \note By definition, the empty graph is connected.
deba@433
    51
  template <typename Graph>
deba@433
    52
  bool connected(const Graph& graph) {
deba@433
    53
    checkConcept<concepts::Graph, Graph>();
deba@433
    54
    typedef typename Graph::NodeIt NodeIt;
deba@433
    55
    if (NodeIt(graph) == INVALID) return true;
deba@433
    56
    Dfs<Graph> dfs(graph);
deba@433
    57
    dfs.run(NodeIt(graph));
deba@433
    58
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
    59
      if (!dfs.reached(it)) {
deba@433
    60
        return false;
deba@433
    61
      }
deba@433
    62
    }
deba@433
    63
    return true;
deba@433
    64
  }
deba@433
    65
kpeter@633
    66
  /// \ingroup graph_properties
deba@433
    67
  ///
deba@433
    68
  /// \brief Count the number of connected components of an undirected graph
deba@433
    69
  ///
deba@433
    70
  /// Count the number of connected components of an undirected graph
deba@433
    71
  ///
deba@433
    72
  /// \param graph The graph. It must be undirected.
deba@433
    73
  /// \return The number of components
deba@433
    74
  /// \note By definition, the empty graph consists
deba@433
    75
  /// of zero connected components.
deba@433
    76
  template <typename Graph>
deba@433
    77
  int countConnectedComponents(const Graph &graph) {
deba@433
    78
    checkConcept<concepts::Graph, Graph>();
deba@433
    79
    typedef typename Graph::Node Node;
deba@433
    80
    typedef typename Graph::Arc Arc;
deba@433
    81
deba@433
    82
    typedef NullMap<Node, Arc> PredMap;
deba@433
    83
    typedef NullMap<Node, int> DistMap;
deba@433
    84
deba@433
    85
    int compNum = 0;
deba@433
    86
    typename Bfs<Graph>::
deba@433
    87
      template SetPredMap<PredMap>::
deba@433
    88
      template SetDistMap<DistMap>::
deba@433
    89
      Create bfs(graph);
deba@433
    90
deba@433
    91
    PredMap predMap;
deba@433
    92
    bfs.predMap(predMap);
deba@433
    93
deba@433
    94
    DistMap distMap;
deba@433
    95
    bfs.distMap(distMap);
deba@433
    96
deba@433
    97
    bfs.init();
deba@433
    98
    for(typename Graph::NodeIt n(graph); n != INVALID; ++n) {
deba@433
    99
      if (!bfs.reached(n)) {
deba@433
   100
        bfs.addSource(n);
deba@433
   101
        bfs.start();
deba@433
   102
        ++compNum;
deba@433
   103
      }
deba@433
   104
    }
deba@433
   105
    return compNum;
deba@433
   106
  }
deba@433
   107
kpeter@633
   108
  /// \ingroup graph_properties
deba@433
   109
  ///
deba@433
   110
  /// \brief Find the connected components of an undirected graph
deba@433
   111
  ///
deba@433
   112
  /// Find the connected components of an undirected graph.
deba@433
   113
  ///
kpeter@633
   114
  /// \image html connected_components.png
kpeter@633
   115
  /// \image latex connected_components.eps "Connected components" width=\textwidth
kpeter@633
   116
  ///
deba@433
   117
  /// \param graph The graph. It must be undirected.
deba@433
   118
  /// \retval compMap A writable node map. The values will be set from 0 to
deba@433
   119
  /// the number of the connected components minus one. Each values of the map
deba@433
   120
  /// will be set exactly once, the values of a certain component will be
deba@433
   121
  /// set continuously.
deba@433
   122
  /// \return The number of components
deba@433
   123
  template <class Graph, class NodeMap>
deba@433
   124
  int connectedComponents(const Graph &graph, NodeMap &compMap) {
deba@433
   125
    checkConcept<concepts::Graph, Graph>();
deba@433
   126
    typedef typename Graph::Node Node;
deba@433
   127
    typedef typename Graph::Arc Arc;
deba@433
   128
    checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
deba@433
   129
deba@433
   130
    typedef NullMap<Node, Arc> PredMap;
deba@433
   131
    typedef NullMap<Node, int> DistMap;
deba@433
   132
deba@433
   133
    int compNum = 0;
deba@433
   134
    typename Bfs<Graph>::
deba@433
   135
      template SetPredMap<PredMap>::
deba@433
   136
      template SetDistMap<DistMap>::
deba@433
   137
      Create bfs(graph);
deba@433
   138
deba@433
   139
    PredMap predMap;
deba@433
   140
    bfs.predMap(predMap);
deba@433
   141
deba@433
   142
    DistMap distMap;
deba@433
   143
    bfs.distMap(distMap);
deba@433
   144
deba@433
   145
    bfs.init();
deba@433
   146
    for(typename Graph::NodeIt n(graph); n != INVALID; ++n) {
deba@433
   147
      if(!bfs.reached(n)) {
deba@433
   148
        bfs.addSource(n);
deba@433
   149
        while (!bfs.emptyQueue()) {
deba@433
   150
          compMap.set(bfs.nextNode(), compNum);
deba@433
   151
          bfs.processNextNode();
deba@433
   152
        }
deba@433
   153
        ++compNum;
deba@433
   154
      }
deba@433
   155
    }
deba@433
   156
    return compNum;
deba@433
   157
  }
deba@433
   158
deba@435
   159
  namespace _connectivity_bits {
deba@433
   160
deba@433
   161
    template <typename Digraph, typename Iterator >
deba@433
   162
    struct LeaveOrderVisitor : public DfsVisitor<Digraph> {
deba@433
   163
    public:
deba@433
   164
      typedef typename Digraph::Node Node;
deba@433
   165
      LeaveOrderVisitor(Iterator it) : _it(it) {}
deba@433
   166
deba@433
   167
      void leave(const Node& node) {
deba@433
   168
        *(_it++) = node;
deba@433
   169
      }
deba@433
   170
deba@433
   171
    private:
deba@433
   172
      Iterator _it;
deba@433
   173
    };
deba@433
   174
deba@433
   175
    template <typename Digraph, typename Map>
deba@433
   176
    struct FillMapVisitor : public DfsVisitor<Digraph> {
deba@433
   177
    public:
deba@433
   178
      typedef typename Digraph::Node Node;
deba@433
   179
      typedef typename Map::Value Value;
deba@433
   180
deba@433
   181
      FillMapVisitor(Map& map, Value& value)
deba@433
   182
        : _map(map), _value(value) {}
deba@433
   183
deba@433
   184
      void reach(const Node& node) {
deba@433
   185
        _map.set(node, _value);
deba@433
   186
      }
deba@433
   187
    private:
deba@433
   188
      Map& _map;
deba@433
   189
      Value& _value;
deba@433
   190
    };
deba@433
   191
deba@433
   192
    template <typename Digraph, typename ArcMap>
deba@435
   193
    struct StronglyConnectedCutArcsVisitor : public DfsVisitor<Digraph> {
deba@433
   194
    public:
deba@433
   195
      typedef typename Digraph::Node Node;
deba@433
   196
      typedef typename Digraph::Arc Arc;
deba@433
   197
deba@435
   198
      StronglyConnectedCutArcsVisitor(const Digraph& digraph,
deba@435
   199
                                      ArcMap& cutMap,
deba@435
   200
                                      int& cutNum)
deba@433
   201
        : _digraph(digraph), _cutMap(cutMap), _cutNum(cutNum),
deba@435
   202
          _compMap(digraph, -1), _num(-1) {
deba@433
   203
      }
deba@433
   204
deba@435
   205
      void start(const Node&) {
deba@433
   206
        ++_num;
deba@433
   207
      }
deba@433
   208
deba@433
   209
      void reach(const Node& node) {
deba@433
   210
        _compMap.set(node, _num);
deba@433
   211
      }
deba@433
   212
deba@433
   213
      void examine(const Arc& arc) {
deba@433
   214
         if (_compMap[_digraph.source(arc)] !=
deba@433
   215
             _compMap[_digraph.target(arc)]) {
deba@433
   216
           _cutMap.set(arc, true);
deba@433
   217
           ++_cutNum;
deba@433
   218
         }
deba@433
   219
      }
deba@433
   220
    private:
deba@433
   221
      const Digraph& _digraph;
deba@433
   222
      ArcMap& _cutMap;
deba@433
   223
      int& _cutNum;
deba@433
   224
deba@433
   225
      typename Digraph::template NodeMap<int> _compMap;
deba@433
   226
      int _num;
deba@433
   227
    };
deba@433
   228
deba@433
   229
  }
deba@433
   230
deba@433
   231
kpeter@633
   232
  /// \ingroup graph_properties
deba@433
   233
  ///
deba@433
   234
  /// \brief Check whether the given directed graph is strongly connected.
deba@433
   235
  ///
deba@433
   236
  /// Check whether the given directed graph is strongly connected. The
deba@433
   237
  /// graph is strongly connected when any two nodes of the graph are
deba@433
   238
  /// connected with directed paths in both direction.
kpeter@606
   239
  /// \return \c false when the graph is not strongly connected.
deba@433
   240
  /// \see connected
deba@433
   241
  ///
deba@433
   242
  /// \note By definition, the empty graph is strongly connected.
deba@433
   243
  template <typename Digraph>
deba@433
   244
  bool stronglyConnected(const Digraph& digraph) {
deba@433
   245
    checkConcept<concepts::Digraph, Digraph>();
deba@433
   246
deba@433
   247
    typedef typename Digraph::Node Node;
deba@433
   248
    typedef typename Digraph::NodeIt NodeIt;
deba@433
   249
deba@433
   250
    typename Digraph::Node source = NodeIt(digraph);
deba@433
   251
    if (source == INVALID) return true;
deba@433
   252
deba@435
   253
    using namespace _connectivity_bits;
deba@433
   254
deba@433
   255
    typedef DfsVisitor<Digraph> Visitor;
deba@433
   256
    Visitor visitor;
deba@433
   257
deba@433
   258
    DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
deba@433
   259
    dfs.init();
deba@433
   260
    dfs.addSource(source);
deba@433
   261
    dfs.start();
deba@433
   262
deba@433
   263
    for (NodeIt it(digraph); it != INVALID; ++it) {
deba@433
   264
      if (!dfs.reached(it)) {
deba@433
   265
        return false;
deba@433
   266
      }
deba@433
   267
    }
deba@433
   268
deba@433
   269
    typedef ReverseDigraph<const Digraph> RDigraph;
deba@435
   270
    typedef typename RDigraph::NodeIt RNodeIt;
deba@433
   271
    RDigraph rdigraph(digraph);
deba@433
   272
deba@433
   273
    typedef DfsVisitor<Digraph> RVisitor;
deba@433
   274
    RVisitor rvisitor;
deba@433
   275
deba@433
   276
    DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
deba@433
   277
    rdfs.init();
deba@433
   278
    rdfs.addSource(source);
deba@433
   279
    rdfs.start();
deba@433
   280
deba@435
   281
    for (RNodeIt it(rdigraph); it != INVALID; ++it) {
deba@433
   282
      if (!rdfs.reached(it)) {
deba@433
   283
        return false;
deba@433
   284
      }
deba@433
   285
    }
deba@433
   286
deba@433
   287
    return true;
deba@433
   288
  }
deba@433
   289
kpeter@633
   290
  /// \ingroup graph_properties
deba@433
   291
  ///
deba@433
   292
  /// \brief Count the strongly connected components of a directed graph
deba@433
   293
  ///
deba@433
   294
  /// Count the strongly connected components of a directed graph.
deba@433
   295
  /// The strongly connected components are the classes of an
deba@433
   296
  /// equivalence relation on the nodes of the graph. Two nodes are in
deba@433
   297
  /// the same class if they are connected with directed paths in both
deba@433
   298
  /// direction.
deba@433
   299
  ///
deba@444
   300
  /// \param digraph The graph.
deba@433
   301
  /// \return The number of components
deba@433
   302
  /// \note By definition, the empty graph has zero
deba@433
   303
  /// strongly connected components.
deba@433
   304
  template <typename Digraph>
deba@433
   305
  int countStronglyConnectedComponents(const Digraph& digraph) {
deba@433
   306
    checkConcept<concepts::Digraph, Digraph>();
deba@433
   307
deba@435
   308
    using namespace _connectivity_bits;
deba@433
   309
deba@433
   310
    typedef typename Digraph::Node Node;
deba@433
   311
    typedef typename Digraph::Arc Arc;
deba@433
   312
    typedef typename Digraph::NodeIt NodeIt;
deba@433
   313
    typedef typename Digraph::ArcIt ArcIt;
deba@433
   314
deba@433
   315
    typedef std::vector<Node> Container;
deba@433
   316
    typedef typename Container::iterator Iterator;
deba@433
   317
deba@433
   318
    Container nodes(countNodes(digraph));
deba@433
   319
    typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
deba@433
   320
    Visitor visitor(nodes.begin());
deba@433
   321
deba@433
   322
    DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
deba@433
   323
    dfs.init();
deba@433
   324
    for (NodeIt it(digraph); it != INVALID; ++it) {
deba@433
   325
      if (!dfs.reached(it)) {
deba@433
   326
        dfs.addSource(it);
deba@433
   327
        dfs.start();
deba@433
   328
      }
deba@433
   329
    }
deba@433
   330
deba@433
   331
    typedef typename Container::reverse_iterator RIterator;
deba@433
   332
    typedef ReverseDigraph<const Digraph> RDigraph;
deba@433
   333
deba@433
   334
    RDigraph rdigraph(digraph);
deba@433
   335
deba@433
   336
    typedef DfsVisitor<Digraph> RVisitor;
deba@433
   337
    RVisitor rvisitor;
deba@433
   338
deba@433
   339
    DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
deba@433
   340
deba@433
   341
    int compNum = 0;
deba@433
   342
deba@433
   343
    rdfs.init();
deba@433
   344
    for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
deba@433
   345
      if (!rdfs.reached(*it)) {
deba@433
   346
        rdfs.addSource(*it);
deba@433
   347
        rdfs.start();
deba@433
   348
        ++compNum;
deba@433
   349
      }
deba@433
   350
    }
deba@433
   351
    return compNum;
deba@433
   352
  }
deba@433
   353
kpeter@633
   354
  /// \ingroup graph_properties
deba@433
   355
  ///
deba@433
   356
  /// \brief Find the strongly connected components of a directed graph
deba@433
   357
  ///
deba@433
   358
  /// Find the strongly connected components of a directed graph.  The
deba@433
   359
  /// strongly connected components are the classes of an equivalence
deba@433
   360
  /// relation on the nodes of the graph. Two nodes are in
deba@433
   361
  /// relationship when there are directed paths between them in both
deba@433
   362
  /// direction. In addition, the numbering of components will satisfy
deba@433
   363
  /// that there is no arc going from a higher numbered component to
deba@433
   364
  /// a lower.
deba@433
   365
  ///
kpeter@633
   366
  /// \image html strongly_connected_components.png
kpeter@633
   367
  /// \image latex strongly_connected_components.eps "Strongly connected components" width=\textwidth
kpeter@633
   368
  ///
deba@433
   369
  /// \param digraph The digraph.
deba@433
   370
  /// \retval compMap A writable node map. The values will be set from 0 to
deba@433
   371
  /// the number of the strongly connected components minus one. Each value
deba@433
   372
  /// of the map will be set exactly once, the values of a certain component
deba@433
   373
  /// will be set continuously.
deba@433
   374
  /// \return The number of components
deba@433
   375
  template <typename Digraph, typename NodeMap>
deba@433
   376
  int stronglyConnectedComponents(const Digraph& digraph, NodeMap& compMap) {
deba@433
   377
    checkConcept<concepts::Digraph, Digraph>();
deba@433
   378
    typedef typename Digraph::Node Node;
deba@433
   379
    typedef typename Digraph::NodeIt NodeIt;
deba@433
   380
    checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
deba@433
   381
deba@435
   382
    using namespace _connectivity_bits;
deba@433
   383
deba@433
   384
    typedef std::vector<Node> Container;
deba@433
   385
    typedef typename Container::iterator Iterator;
deba@433
   386
deba@433
   387
    Container nodes(countNodes(digraph));
deba@433
   388
    typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
deba@433
   389
    Visitor visitor(nodes.begin());
deba@433
   390
deba@433
   391
    DfsVisit<Digraph, Visitor> dfs(digraph, visitor);
deba@433
   392
    dfs.init();
deba@433
   393
    for (NodeIt it(digraph); it != INVALID; ++it) {
deba@433
   394
      if (!dfs.reached(it)) {
deba@433
   395
        dfs.addSource(it);
deba@433
   396
        dfs.start();
deba@433
   397
      }
deba@433
   398
    }
deba@433
   399
deba@433
   400
    typedef typename Container::reverse_iterator RIterator;
deba@433
   401
    typedef ReverseDigraph<const Digraph> RDigraph;
deba@433
   402
deba@433
   403
    RDigraph rdigraph(digraph);
deba@433
   404
deba@433
   405
    int compNum = 0;
deba@433
   406
deba@433
   407
    typedef FillMapVisitor<RDigraph, NodeMap> RVisitor;
deba@433
   408
    RVisitor rvisitor(compMap, compNum);
deba@433
   409
deba@433
   410
    DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor);
deba@433
   411
deba@433
   412
    rdfs.init();
deba@433
   413
    for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
deba@433
   414
      if (!rdfs.reached(*it)) {
deba@433
   415
        rdfs.addSource(*it);
deba@433
   416
        rdfs.start();
deba@433
   417
        ++compNum;
deba@433
   418
      }
deba@433
   419
    }
deba@433
   420
    return compNum;
deba@433
   421
  }
deba@433
   422
kpeter@633
   423
  /// \ingroup graph_properties
deba@433
   424
  ///
deba@433
   425
  /// \brief Find the cut arcs of the strongly connected components.
deba@433
   426
  ///
deba@433
   427
  /// Find the cut arcs of the strongly connected components.
deba@433
   428
  /// The strongly connected components are the classes of an equivalence
deba@433
   429
  /// relation on the nodes of the graph. Two nodes are in relationship
deba@433
   430
  /// when there are directed paths between them in both direction.
deba@433
   431
  /// The strongly connected components are separated by the cut arcs.
deba@433
   432
  ///
deba@433
   433
  /// \param graph The graph.
deba@433
   434
  /// \retval cutMap A writable node map. The values will be set true when the
deba@433
   435
  /// arc is a cut arc.
deba@433
   436
  ///
deba@433
   437
  /// \return The number of cut arcs
deba@433
   438
  template <typename Digraph, typename ArcMap>
deba@433
   439
  int stronglyConnectedCutArcs(const Digraph& graph, ArcMap& cutMap) {
deba@433
   440
    checkConcept<concepts::Digraph, Digraph>();
deba@433
   441
    typedef typename Digraph::Node Node;
deba@433
   442
    typedef typename Digraph::Arc Arc;
deba@433
   443
    typedef typename Digraph::NodeIt NodeIt;
deba@433
   444
    checkConcept<concepts::WriteMap<Arc, bool>, ArcMap>();
deba@433
   445
deba@435
   446
    using namespace _connectivity_bits;
deba@433
   447
deba@433
   448
    typedef std::vector<Node> Container;
deba@433
   449
    typedef typename Container::iterator Iterator;
deba@433
   450
deba@433
   451
    Container nodes(countNodes(graph));
deba@433
   452
    typedef LeaveOrderVisitor<Digraph, Iterator> Visitor;
deba@433
   453
    Visitor visitor(nodes.begin());
deba@433
   454
deba@433
   455
    DfsVisit<Digraph, Visitor> dfs(graph, visitor);
deba@433
   456
    dfs.init();
deba@433
   457
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
   458
      if (!dfs.reached(it)) {
deba@433
   459
        dfs.addSource(it);
deba@433
   460
        dfs.start();
deba@433
   461
      }
deba@433
   462
    }
deba@433
   463
deba@433
   464
    typedef typename Container::reverse_iterator RIterator;
deba@433
   465
    typedef ReverseDigraph<const Digraph> RDigraph;
deba@433
   466
deba@433
   467
    RDigraph rgraph(graph);
deba@433
   468
deba@433
   469
    int cutNum = 0;
deba@433
   470
deba@435
   471
    typedef StronglyConnectedCutArcsVisitor<RDigraph, ArcMap> RVisitor;
deba@433
   472
    RVisitor rvisitor(rgraph, cutMap, cutNum);
deba@433
   473
deba@433
   474
    DfsVisit<RDigraph, RVisitor> rdfs(rgraph, rvisitor);
deba@433
   475
deba@433
   476
    rdfs.init();
deba@433
   477
    for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) {
deba@433
   478
      if (!rdfs.reached(*it)) {
deba@433
   479
        rdfs.addSource(*it);
deba@433
   480
        rdfs.start();
deba@433
   481
      }
deba@433
   482
    }
deba@433
   483
    return cutNum;
deba@433
   484
  }
deba@433
   485
deba@435
   486
  namespace _connectivity_bits {
deba@433
   487
deba@433
   488
    template <typename Digraph>
deba@433
   489
    class CountBiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
deba@433
   490
    public:
deba@433
   491
      typedef typename Digraph::Node Node;
deba@433
   492
      typedef typename Digraph::Arc Arc;
deba@433
   493
      typedef typename Digraph::Edge Edge;
deba@433
   494
deba@433
   495
      CountBiNodeConnectedComponentsVisitor(const Digraph& graph, int &compNum)
deba@433
   496
        : _graph(graph), _compNum(compNum),
deba@433
   497
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
deba@433
   498
deba@433
   499
      void start(const Node& node) {
deba@433
   500
        _predMap.set(node, INVALID);
deba@433
   501
      }
deba@433
   502
deba@433
   503
      void reach(const Node& node) {
deba@433
   504
        _numMap.set(node, _num);
deba@433
   505
        _retMap.set(node, _num);
deba@433
   506
        ++_num;
deba@433
   507
      }
deba@433
   508
deba@433
   509
      void discover(const Arc& edge) {
deba@433
   510
        _predMap.set(_graph.target(edge), _graph.source(edge));
deba@433
   511
      }
deba@433
   512
deba@433
   513
      void examine(const Arc& edge) {
deba@433
   514
        if (_graph.source(edge) == _graph.target(edge) &&
deba@433
   515
            _graph.direction(edge)) {
deba@433
   516
          ++_compNum;
deba@433
   517
          return;
deba@433
   518
        }
deba@433
   519
        if (_predMap[_graph.source(edge)] == _graph.target(edge)) {
deba@433
   520
          return;
deba@433
   521
        }
deba@433
   522
        if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) {
deba@433
   523
          _retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]);
deba@433
   524
        }
deba@433
   525
      }
deba@433
   526
deba@433
   527
      void backtrack(const Arc& edge) {
deba@433
   528
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
deba@433
   529
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
deba@433
   530
        }
deba@433
   531
        if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) {
deba@433
   532
          ++_compNum;
deba@433
   533
        }
deba@433
   534
      }
deba@433
   535
deba@433
   536
    private:
deba@433
   537
      const Digraph& _graph;
deba@433
   538
      int& _compNum;
deba@433
   539
deba@433
   540
      typename Digraph::template NodeMap<int> _numMap;
deba@433
   541
      typename Digraph::template NodeMap<int> _retMap;
deba@433
   542
      typename Digraph::template NodeMap<Node> _predMap;
deba@433
   543
      int _num;
deba@433
   544
    };
deba@433
   545
deba@433
   546
    template <typename Digraph, typename ArcMap>
deba@433
   547
    class BiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
deba@433
   548
    public:
deba@433
   549
      typedef typename Digraph::Node Node;
deba@433
   550
      typedef typename Digraph::Arc Arc;
deba@433
   551
      typedef typename Digraph::Edge Edge;
deba@433
   552
deba@433
   553
      BiNodeConnectedComponentsVisitor(const Digraph& graph,
deba@433
   554
                                       ArcMap& compMap, int &compNum)
deba@433
   555
        : _graph(graph), _compMap(compMap), _compNum(compNum),
deba@433
   556
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
deba@433
   557
deba@433
   558
      void start(const Node& node) {
deba@433
   559
        _predMap.set(node, INVALID);
deba@433
   560
      }
deba@433
   561
deba@433
   562
      void reach(const Node& node) {
deba@433
   563
        _numMap.set(node, _num);
deba@433
   564
        _retMap.set(node, _num);
deba@433
   565
        ++_num;
deba@433
   566
      }
deba@433
   567
deba@433
   568
      void discover(const Arc& edge) {
deba@433
   569
        Node target = _graph.target(edge);
deba@433
   570
        _predMap.set(target, edge);
deba@433
   571
        _edgeStack.push(edge);
deba@433
   572
      }
deba@433
   573
deba@433
   574
      void examine(const Arc& edge) {
deba@433
   575
        Node source = _graph.source(edge);
deba@433
   576
        Node target = _graph.target(edge);
deba@433
   577
        if (source == target && _graph.direction(edge)) {
deba@433
   578
          _compMap.set(edge, _compNum);
deba@433
   579
          ++_compNum;
deba@433
   580
          return;
deba@433
   581
        }
deba@433
   582
        if (_numMap[target] < _numMap[source]) {
deba@433
   583
          if (_predMap[source] != _graph.oppositeArc(edge)) {
deba@433
   584
            _edgeStack.push(edge);
deba@433
   585
          }
deba@433
   586
        }
deba@433
   587
        if (_predMap[source] != INVALID &&
deba@433
   588
            target == _graph.source(_predMap[source])) {
deba@433
   589
          return;
deba@433
   590
        }
deba@433
   591
        if (_retMap[source] > _numMap[target]) {
deba@433
   592
          _retMap.set(source, _numMap[target]);
deba@433
   593
        }
deba@433
   594
      }
deba@433
   595
deba@433
   596
      void backtrack(const Arc& edge) {
deba@433
   597
        Node source = _graph.source(edge);
deba@433
   598
        Node target = _graph.target(edge);
deba@433
   599
        if (_retMap[source] > _retMap[target]) {
deba@433
   600
          _retMap.set(source, _retMap[target]);
deba@433
   601
        }
deba@433
   602
        if (_numMap[source] <= _retMap[target]) {
deba@433
   603
          while (_edgeStack.top() != edge) {
deba@433
   604
            _compMap.set(_edgeStack.top(), _compNum);
deba@433
   605
            _edgeStack.pop();
deba@433
   606
          }
deba@433
   607
          _compMap.set(edge, _compNum);
deba@433
   608
          _edgeStack.pop();
deba@433
   609
          ++_compNum;
deba@433
   610
        }
deba@433
   611
      }
deba@433
   612
deba@433
   613
    private:
deba@433
   614
      const Digraph& _graph;
deba@433
   615
      ArcMap& _compMap;
deba@433
   616
      int& _compNum;
deba@433
   617
deba@433
   618
      typename Digraph::template NodeMap<int> _numMap;
deba@433
   619
      typename Digraph::template NodeMap<int> _retMap;
deba@433
   620
      typename Digraph::template NodeMap<Arc> _predMap;
deba@433
   621
      std::stack<Edge> _edgeStack;
deba@433
   622
      int _num;
deba@433
   623
    };
deba@433
   624
deba@433
   625
deba@433
   626
    template <typename Digraph, typename NodeMap>
deba@433
   627
    class BiNodeConnectedCutNodesVisitor : public DfsVisitor<Digraph> {
deba@433
   628
    public:
deba@433
   629
      typedef typename Digraph::Node Node;
deba@433
   630
      typedef typename Digraph::Arc Arc;
deba@433
   631
      typedef typename Digraph::Edge Edge;
deba@433
   632
deba@433
   633
      BiNodeConnectedCutNodesVisitor(const Digraph& graph, NodeMap& cutMap,
deba@433
   634
                                     int& cutNum)
deba@433
   635
        : _graph(graph), _cutMap(cutMap), _cutNum(cutNum),
deba@433
   636
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
deba@433
   637
deba@433
   638
      void start(const Node& node) {
deba@433
   639
        _predMap.set(node, INVALID);
deba@433
   640
        rootCut = false;
deba@433
   641
      }
deba@433
   642
deba@433
   643
      void reach(const Node& node) {
deba@433
   644
        _numMap.set(node, _num);
deba@433
   645
        _retMap.set(node, _num);
deba@433
   646
        ++_num;
deba@433
   647
      }
deba@433
   648
deba@433
   649
      void discover(const Arc& edge) {
deba@433
   650
        _predMap.set(_graph.target(edge), _graph.source(edge));
deba@433
   651
      }
deba@433
   652
deba@433
   653
      void examine(const Arc& edge) {
deba@433
   654
        if (_graph.source(edge) == _graph.target(edge) &&
deba@433
   655
            _graph.direction(edge)) {
deba@433
   656
          if (!_cutMap[_graph.source(edge)]) {
deba@433
   657
            _cutMap.set(_graph.source(edge), true);
deba@433
   658
            ++_cutNum;
deba@433
   659
          }
deba@433
   660
          return;
deba@433
   661
        }
deba@433
   662
        if (_predMap[_graph.source(edge)] == _graph.target(edge)) return;
deba@433
   663
        if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) {
deba@433
   664
          _retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]);
deba@433
   665
        }
deba@433
   666
      }
deba@433
   667
deba@433
   668
      void backtrack(const Arc& edge) {
deba@433
   669
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
deba@433
   670
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
deba@433
   671
        }
deba@433
   672
        if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) {
deba@433
   673
          if (_predMap[_graph.source(edge)] != INVALID) {
deba@433
   674
            if (!_cutMap[_graph.source(edge)]) {
deba@433
   675
              _cutMap.set(_graph.source(edge), true);
deba@433
   676
              ++_cutNum;
deba@433
   677
            }
deba@433
   678
          } else if (rootCut) {
deba@433
   679
            if (!_cutMap[_graph.source(edge)]) {
deba@433
   680
              _cutMap.set(_graph.source(edge), true);
deba@433
   681
              ++_cutNum;
deba@433
   682
            }
deba@433
   683
          } else {
deba@433
   684
            rootCut = true;
deba@433
   685
          }
deba@433
   686
        }
deba@433
   687
      }
deba@433
   688
deba@433
   689
    private:
deba@433
   690
      const Digraph& _graph;
deba@433
   691
      NodeMap& _cutMap;
deba@433
   692
      int& _cutNum;
deba@433
   693
deba@433
   694
      typename Digraph::template NodeMap<int> _numMap;
deba@433
   695
      typename Digraph::template NodeMap<int> _retMap;
deba@433
   696
      typename Digraph::template NodeMap<Node> _predMap;
deba@433
   697
      std::stack<Edge> _edgeStack;
deba@433
   698
      int _num;
deba@433
   699
      bool rootCut;
deba@433
   700
    };
deba@433
   701
deba@433
   702
  }
deba@433
   703
deba@433
   704
  template <typename Graph>
deba@433
   705
  int countBiNodeConnectedComponents(const Graph& graph);
deba@433
   706
kpeter@633
   707
  /// \ingroup graph_properties
deba@433
   708
  ///
deba@433
   709
  /// \brief Checks the graph is bi-node-connected.
deba@433
   710
  ///
deba@433
   711
  /// This function checks that the undirected graph is bi-node-connected
deba@433
   712
  /// graph. The graph is bi-node-connected if any two undirected edge is
deba@433
   713
  /// on same circle.
deba@433
   714
  ///
deba@433
   715
  /// \param graph The graph.
kpeter@606
   716
  /// \return \c true when the graph bi-node-connected.
deba@433
   717
  template <typename Graph>
deba@433
   718
  bool biNodeConnected(const Graph& graph) {
deba@433
   719
    return countBiNodeConnectedComponents(graph) <= 1;
deba@433
   720
  }
deba@433
   721
kpeter@633
   722
  /// \ingroup graph_properties
deba@433
   723
  ///
deba@433
   724
  /// \brief Count the biconnected components.
deba@433
   725
  ///
deba@433
   726
  /// This function finds the bi-node-connected components in an undirected
deba@433
   727
  /// graph. The biconnected components are the classes of an equivalence
deba@433
   728
  /// relation on the undirected edges. Two undirected edge is in relationship
deba@433
   729
  /// when they are on same circle.
deba@433
   730
  ///
deba@433
   731
  /// \param graph The graph.
deba@433
   732
  /// \return The number of components.
deba@433
   733
  template <typename Graph>
deba@433
   734
  int countBiNodeConnectedComponents(const Graph& graph) {
deba@433
   735
    checkConcept<concepts::Graph, Graph>();
deba@433
   736
    typedef typename Graph::NodeIt NodeIt;
deba@433
   737
deba@435
   738
    using namespace _connectivity_bits;
deba@433
   739
deba@433
   740
    typedef CountBiNodeConnectedComponentsVisitor<Graph> Visitor;
deba@433
   741
deba@433
   742
    int compNum = 0;
deba@433
   743
    Visitor visitor(graph, compNum);
deba@433
   744
deba@433
   745
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
deba@433
   746
    dfs.init();
deba@433
   747
deba@433
   748
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
   749
      if (!dfs.reached(it)) {
deba@433
   750
        dfs.addSource(it);
deba@433
   751
        dfs.start();
deba@433
   752
      }
deba@433
   753
    }
deba@433
   754
    return compNum;
deba@433
   755
  }
deba@433
   756
kpeter@633
   757
  /// \ingroup graph_properties
deba@433
   758
  ///
deba@433
   759
  /// \brief Find the bi-node-connected components.
deba@433
   760
  ///
deba@433
   761
  /// This function finds the bi-node-connected components in an undirected
deba@433
   762
  /// graph. The bi-node-connected components are the classes of an equivalence
deba@433
   763
  /// relation on the undirected edges. Two undirected edge are in relationship
deba@433
   764
  /// when they are on same circle.
deba@433
   765
  ///
kpeter@633
   766
  /// \image html node_biconnected_components.png
kpeter@633
   767
  /// \image latex node_biconnected_components.eps "bi-node-connected components" width=\textwidth
kpeter@633
   768
  ///
deba@433
   769
  /// \param graph The graph.
deba@433
   770
  /// \retval compMap A writable uedge map. The values will be set from 0
deba@433
   771
  /// to the number of the biconnected components minus one. Each values
deba@433
   772
  /// of the map will be set exactly once, the values of a certain component
deba@433
   773
  /// will be set continuously.
deba@433
   774
  /// \return The number of components.
deba@433
   775
  template <typename Graph, typename EdgeMap>
deba@433
   776
  int biNodeConnectedComponents(const Graph& graph,
deba@433
   777
                                EdgeMap& compMap) {
deba@433
   778
    checkConcept<concepts::Graph, Graph>();
deba@433
   779
    typedef typename Graph::NodeIt NodeIt;
deba@433
   780
    typedef typename Graph::Edge Edge;
deba@433
   781
    checkConcept<concepts::WriteMap<Edge, int>, EdgeMap>();
deba@433
   782
deba@435
   783
    using namespace _connectivity_bits;
deba@433
   784
deba@433
   785
    typedef BiNodeConnectedComponentsVisitor<Graph, EdgeMap> Visitor;
deba@433
   786
deba@433
   787
    int compNum = 0;
deba@433
   788
    Visitor visitor(graph, compMap, compNum);
deba@433
   789
deba@433
   790
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
deba@433
   791
    dfs.init();
deba@433
   792
deba@433
   793
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
   794
      if (!dfs.reached(it)) {
deba@433
   795
        dfs.addSource(it);
deba@433
   796
        dfs.start();
deba@433
   797
      }
deba@433
   798
    }
deba@433
   799
    return compNum;
deba@433
   800
  }
deba@433
   801
kpeter@633
   802
  /// \ingroup graph_properties
deba@433
   803
  ///
deba@433
   804
  /// \brief Find the bi-node-connected cut nodes.
deba@433
   805
  ///
deba@433
   806
  /// This function finds the bi-node-connected cut nodes in an undirected
deba@433
   807
  /// graph. The bi-node-connected components are the classes of an equivalence
deba@433
   808
  /// relation on the undirected edges. Two undirected edges are in
deba@433
   809
  /// relationship when they are on same circle. The biconnected components
deba@433
   810
  /// are separted by nodes which are the cut nodes of the components.
deba@433
   811
  ///
deba@433
   812
  /// \param graph The graph.
deba@433
   813
  /// \retval cutMap A writable edge map. The values will be set true when
deba@433
   814
  /// the node separate two or more components.
deba@433
   815
  /// \return The number of the cut nodes.
deba@433
   816
  template <typename Graph, typename NodeMap>
deba@433
   817
  int biNodeConnectedCutNodes(const Graph& graph, NodeMap& cutMap) {
deba@433
   818
    checkConcept<concepts::Graph, Graph>();
deba@433
   819
    typedef typename Graph::Node Node;
deba@433
   820
    typedef typename Graph::NodeIt NodeIt;
deba@433
   821
    checkConcept<concepts::WriteMap<Node, bool>, NodeMap>();
deba@433
   822
deba@435
   823
    using namespace _connectivity_bits;
deba@433
   824
deba@433
   825
    typedef BiNodeConnectedCutNodesVisitor<Graph, NodeMap> Visitor;
deba@433
   826
deba@433
   827
    int cutNum = 0;
deba@433
   828
    Visitor visitor(graph, cutMap, cutNum);
deba@433
   829
deba@433
   830
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
deba@433
   831
    dfs.init();
deba@433
   832
deba@433
   833
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
   834
      if (!dfs.reached(it)) {
deba@433
   835
        dfs.addSource(it);
deba@433
   836
        dfs.start();
deba@433
   837
      }
deba@433
   838
    }
deba@433
   839
    return cutNum;
deba@433
   840
  }
deba@433
   841
deba@435
   842
  namespace _connectivity_bits {
deba@433
   843
deba@433
   844
    template <typename Digraph>
deba@433
   845
    class CountBiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
deba@433
   846
    public:
deba@433
   847
      typedef typename Digraph::Node Node;
deba@433
   848
      typedef typename Digraph::Arc Arc;
deba@433
   849
      typedef typename Digraph::Edge Edge;
deba@433
   850
deba@433
   851
      CountBiEdgeConnectedComponentsVisitor(const Digraph& graph, int &compNum)
deba@433
   852
        : _graph(graph), _compNum(compNum),
deba@433
   853
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
deba@433
   854
deba@433
   855
      void start(const Node& node) {
deba@433
   856
        _predMap.set(node, INVALID);
deba@433
   857
      }
deba@433
   858
deba@433
   859
      void reach(const Node& node) {
deba@433
   860
        _numMap.set(node, _num);
deba@433
   861
        _retMap.set(node, _num);
deba@433
   862
        ++_num;
deba@433
   863
      }
deba@433
   864
deba@433
   865
      void leave(const Node& node) {
deba@433
   866
        if (_numMap[node] <= _retMap[node]) {
deba@433
   867
          ++_compNum;
deba@433
   868
        }
deba@433
   869
      }
deba@433
   870
deba@433
   871
      void discover(const Arc& edge) {
deba@433
   872
        _predMap.set(_graph.target(edge), edge);
deba@433
   873
      }
deba@433
   874
deba@433
   875
      void examine(const Arc& edge) {
deba@433
   876
        if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) {
deba@433
   877
          return;
deba@433
   878
        }
deba@433
   879
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
deba@433
   880
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
deba@433
   881
        }
deba@433
   882
      }
deba@433
   883
deba@433
   884
      void backtrack(const Arc& edge) {
deba@433
   885
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
deba@433
   886
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
deba@433
   887
        }
deba@433
   888
      }
deba@433
   889
deba@433
   890
    private:
deba@433
   891
      const Digraph& _graph;
deba@433
   892
      int& _compNum;
deba@433
   893
deba@433
   894
      typename Digraph::template NodeMap<int> _numMap;
deba@433
   895
      typename Digraph::template NodeMap<int> _retMap;
deba@433
   896
      typename Digraph::template NodeMap<Arc> _predMap;
deba@433
   897
      int _num;
deba@433
   898
    };
deba@433
   899
deba@433
   900
    template <typename Digraph, typename NodeMap>
deba@433
   901
    class BiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> {
deba@433
   902
    public:
deba@433
   903
      typedef typename Digraph::Node Node;
deba@433
   904
      typedef typename Digraph::Arc Arc;
deba@433
   905
      typedef typename Digraph::Edge Edge;
deba@433
   906
deba@433
   907
      BiEdgeConnectedComponentsVisitor(const Digraph& graph,
deba@433
   908
                                       NodeMap& compMap, int &compNum)
deba@433
   909
        : _graph(graph), _compMap(compMap), _compNum(compNum),
deba@433
   910
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
deba@433
   911
deba@433
   912
      void start(const Node& node) {
deba@433
   913
        _predMap.set(node, INVALID);
deba@433
   914
      }
deba@433
   915
deba@433
   916
      void reach(const Node& node) {
deba@433
   917
        _numMap.set(node, _num);
deba@433
   918
        _retMap.set(node, _num);
deba@433
   919
        _nodeStack.push(node);
deba@433
   920
        ++_num;
deba@433
   921
      }
deba@433
   922
deba@433
   923
      void leave(const Node& node) {
deba@433
   924
        if (_numMap[node] <= _retMap[node]) {
deba@433
   925
          while (_nodeStack.top() != node) {
deba@433
   926
            _compMap.set(_nodeStack.top(), _compNum);
deba@433
   927
            _nodeStack.pop();
deba@433
   928
          }
deba@433
   929
          _compMap.set(node, _compNum);
deba@433
   930
          _nodeStack.pop();
deba@433
   931
          ++_compNum;
deba@433
   932
        }
deba@433
   933
      }
deba@433
   934
deba@433
   935
      void discover(const Arc& edge) {
deba@433
   936
        _predMap.set(_graph.target(edge), edge);
deba@433
   937
      }
deba@433
   938
deba@433
   939
      void examine(const Arc& edge) {
deba@433
   940
        if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) {
deba@433
   941
          return;
deba@433
   942
        }
deba@433
   943
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
deba@433
   944
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
deba@433
   945
        }
deba@433
   946
      }
deba@433
   947
deba@433
   948
      void backtrack(const Arc& edge) {
deba@433
   949
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
deba@433
   950
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
deba@433
   951
        }
deba@433
   952
      }
deba@433
   953
deba@433
   954
    private:
deba@433
   955
      const Digraph& _graph;
deba@433
   956
      NodeMap& _compMap;
deba@433
   957
      int& _compNum;
deba@433
   958
deba@433
   959
      typename Digraph::template NodeMap<int> _numMap;
deba@433
   960
      typename Digraph::template NodeMap<int> _retMap;
deba@433
   961
      typename Digraph::template NodeMap<Arc> _predMap;
deba@433
   962
      std::stack<Node> _nodeStack;
deba@433
   963
      int _num;
deba@433
   964
    };
deba@433
   965
deba@433
   966
deba@433
   967
    template <typename Digraph, typename ArcMap>
deba@433
   968
    class BiEdgeConnectedCutEdgesVisitor : public DfsVisitor<Digraph> {
deba@433
   969
    public:
deba@433
   970
      typedef typename Digraph::Node Node;
deba@433
   971
      typedef typename Digraph::Arc Arc;
deba@433
   972
      typedef typename Digraph::Edge Edge;
deba@433
   973
deba@433
   974
      BiEdgeConnectedCutEdgesVisitor(const Digraph& graph,
deba@433
   975
                                     ArcMap& cutMap, int &cutNum)
deba@433
   976
        : _graph(graph), _cutMap(cutMap), _cutNum(cutNum),
deba@433
   977
          _numMap(graph), _retMap(graph), _predMap(graph), _num(0) {}
deba@433
   978
deba@433
   979
      void start(const Node& node) {
deba@433
   980
        _predMap[node] = INVALID;
deba@433
   981
      }
deba@433
   982
deba@433
   983
      void reach(const Node& node) {
deba@433
   984
        _numMap.set(node, _num);
deba@433
   985
        _retMap.set(node, _num);
deba@433
   986
        ++_num;
deba@433
   987
      }
deba@433
   988
deba@433
   989
      void leave(const Node& node) {
deba@433
   990
        if (_numMap[node] <= _retMap[node]) {
deba@433
   991
          if (_predMap[node] != INVALID) {
deba@433
   992
            _cutMap.set(_predMap[node], true);
deba@433
   993
            ++_cutNum;
deba@433
   994
          }
deba@433
   995
        }
deba@433
   996
      }
deba@433
   997
deba@433
   998
      void discover(const Arc& edge) {
deba@433
   999
        _predMap.set(_graph.target(edge), edge);
deba@433
  1000
      }
deba@433
  1001
deba@433
  1002
      void examine(const Arc& edge) {
deba@433
  1003
        if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) {
deba@433
  1004
          return;
deba@433
  1005
        }
deba@433
  1006
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
deba@433
  1007
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
deba@433
  1008
        }
deba@433
  1009
      }
deba@433
  1010
deba@433
  1011
      void backtrack(const Arc& edge) {
deba@433
  1012
        if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
deba@433
  1013
          _retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]);
deba@433
  1014
        }
deba@433
  1015
      }
deba@433
  1016
deba@433
  1017
    private:
deba@433
  1018
      const Digraph& _graph;
deba@433
  1019
      ArcMap& _cutMap;
deba@433
  1020
      int& _cutNum;
deba@433
  1021
deba@433
  1022
      typename Digraph::template NodeMap<int> _numMap;
deba@433
  1023
      typename Digraph::template NodeMap<int> _retMap;
deba@433
  1024
      typename Digraph::template NodeMap<Arc> _predMap;
deba@433
  1025
      int _num;
deba@433
  1026
    };
deba@433
  1027
  }
deba@433
  1028
deba@433
  1029
  template <typename Graph>
deba@433
  1030
  int countBiEdgeConnectedComponents(const Graph& graph);
deba@433
  1031
kpeter@633
  1032
  /// \ingroup graph_properties
deba@433
  1033
  ///
deba@433
  1034
  /// \brief Checks that the graph is bi-edge-connected.
deba@433
  1035
  ///
deba@433
  1036
  /// This function checks that the graph is bi-edge-connected. The undirected
deba@433
  1037
  /// graph is bi-edge-connected when any two nodes are connected with two
deba@433
  1038
  /// edge-disjoint paths.
deba@433
  1039
  ///
deba@433
  1040
  /// \param graph The undirected graph.
deba@433
  1041
  /// \return The number of components.
deba@433
  1042
  template <typename Graph>
deba@433
  1043
  bool biEdgeConnected(const Graph& graph) {
deba@433
  1044
    return countBiEdgeConnectedComponents(graph) <= 1;
deba@433
  1045
  }
deba@433
  1046
kpeter@633
  1047
  /// \ingroup graph_properties
deba@433
  1048
  ///
deba@433
  1049
  /// \brief Count the bi-edge-connected components.
deba@433
  1050
  ///
deba@433
  1051
  /// This function count the bi-edge-connected components in an undirected
deba@433
  1052
  /// graph. The bi-edge-connected components are the classes of an equivalence
deba@433
  1053
  /// relation on the nodes. Two nodes are in relationship when they are
deba@433
  1054
  /// connected with at least two edge-disjoint paths.
deba@433
  1055
  ///
deba@433
  1056
  /// \param graph The undirected graph.
deba@433
  1057
  /// \return The number of components.
deba@433
  1058
  template <typename Graph>
deba@433
  1059
  int countBiEdgeConnectedComponents(const Graph& graph) {
deba@433
  1060
    checkConcept<concepts::Graph, Graph>();
deba@433
  1061
    typedef typename Graph::NodeIt NodeIt;
deba@433
  1062
deba@435
  1063
    using namespace _connectivity_bits;
deba@433
  1064
deba@433
  1065
    typedef CountBiEdgeConnectedComponentsVisitor<Graph> Visitor;
deba@433
  1066
deba@433
  1067
    int compNum = 0;
deba@433
  1068
    Visitor visitor(graph, compNum);
deba@433
  1069
deba@433
  1070
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
deba@433
  1071
    dfs.init();
deba@433
  1072
deba@433
  1073
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
  1074
      if (!dfs.reached(it)) {
deba@433
  1075
        dfs.addSource(it);
deba@433
  1076
        dfs.start();
deba@433
  1077
      }
deba@433
  1078
    }
deba@433
  1079
    return compNum;
deba@433
  1080
  }
deba@433
  1081
kpeter@633
  1082
  /// \ingroup graph_properties
deba@433
  1083
  ///
deba@433
  1084
  /// \brief Find the bi-edge-connected components.
deba@433
  1085
  ///
deba@433
  1086
  /// This function finds the bi-edge-connected components in an undirected
deba@433
  1087
  /// graph. The bi-edge-connected components are the classes of an equivalence
deba@433
  1088
  /// relation on the nodes. Two nodes are in relationship when they are
deba@433
  1089
  /// connected at least two edge-disjoint paths.
deba@433
  1090
  ///
kpeter@633
  1091
  /// \image html edge_biconnected_components.png
kpeter@633
  1092
  /// \image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth
kpeter@633
  1093
  ///
deba@433
  1094
  /// \param graph The graph.
deba@433
  1095
  /// \retval compMap A writable node map. The values will be set from 0 to
deba@433
  1096
  /// the number of the biconnected components minus one. Each values
deba@433
  1097
  /// of the map will be set exactly once, the values of a certain component
deba@433
  1098
  /// will be set continuously.
deba@433
  1099
  /// \return The number of components.
deba@433
  1100
  template <typename Graph, typename NodeMap>
deba@433
  1101
  int biEdgeConnectedComponents(const Graph& graph, NodeMap& compMap) {
deba@433
  1102
    checkConcept<concepts::Graph, Graph>();
deba@433
  1103
    typedef typename Graph::NodeIt NodeIt;
deba@433
  1104
    typedef typename Graph::Node Node;
deba@433
  1105
    checkConcept<concepts::WriteMap<Node, int>, NodeMap>();
deba@433
  1106
deba@435
  1107
    using namespace _connectivity_bits;
deba@433
  1108
deba@433
  1109
    typedef BiEdgeConnectedComponentsVisitor<Graph, NodeMap> Visitor;
deba@433
  1110
deba@433
  1111
    int compNum = 0;
deba@433
  1112
    Visitor visitor(graph, compMap, compNum);
deba@433
  1113
deba@433
  1114
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
deba@433
  1115
    dfs.init();
deba@433
  1116
deba@433
  1117
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
  1118
      if (!dfs.reached(it)) {
deba@433
  1119
        dfs.addSource(it);
deba@433
  1120
        dfs.start();
deba@433
  1121
      }
deba@433
  1122
    }
deba@433
  1123
    return compNum;
deba@433
  1124
  }
deba@433
  1125
kpeter@633
  1126
  /// \ingroup graph_properties
deba@433
  1127
  ///
deba@433
  1128
  /// \brief Find the bi-edge-connected cut edges.
deba@433
  1129
  ///
deba@433
  1130
  /// This function finds the bi-edge-connected components in an undirected
deba@433
  1131
  /// graph. The bi-edge-connected components are the classes of an equivalence
deba@433
  1132
  /// relation on the nodes. Two nodes are in relationship when they are
deba@433
  1133
  /// connected with at least two edge-disjoint paths. The bi-edge-connected
deba@433
  1134
  /// components are separted by edges which are the cut edges of the
deba@433
  1135
  /// components.
deba@433
  1136
  ///
deba@433
  1137
  /// \param graph The graph.
deba@433
  1138
  /// \retval cutMap A writable node map. The values will be set true when the
deba@433
  1139
  /// edge is a cut edge.
deba@433
  1140
  /// \return The number of cut edges.
deba@433
  1141
  template <typename Graph, typename EdgeMap>
deba@433
  1142
  int biEdgeConnectedCutEdges(const Graph& graph, EdgeMap& cutMap) {
deba@433
  1143
    checkConcept<concepts::Graph, Graph>();
deba@433
  1144
    typedef typename Graph::NodeIt NodeIt;
deba@433
  1145
    typedef typename Graph::Edge Edge;
deba@433
  1146
    checkConcept<concepts::WriteMap<Edge, bool>, EdgeMap>();
deba@433
  1147
deba@435
  1148
    using namespace _connectivity_bits;
deba@433
  1149
deba@433
  1150
    typedef BiEdgeConnectedCutEdgesVisitor<Graph, EdgeMap> Visitor;
deba@433
  1151
deba@433
  1152
    int cutNum = 0;
deba@433
  1153
    Visitor visitor(graph, cutMap, cutNum);
deba@433
  1154
deba@433
  1155
    DfsVisit<Graph, Visitor> dfs(graph, visitor);
deba@433
  1156
    dfs.init();
deba@433
  1157
deba@433
  1158
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
  1159
      if (!dfs.reached(it)) {
deba@433
  1160
        dfs.addSource(it);
deba@433
  1161
        dfs.start();
deba@433
  1162
      }
deba@433
  1163
    }
deba@433
  1164
    return cutNum;
deba@433
  1165
  }
deba@433
  1166
deba@433
  1167
deba@435
  1168
  namespace _connectivity_bits {
deba@433
  1169
deba@433
  1170
    template <typename Digraph, typename IntNodeMap>
deba@433
  1171
    class TopologicalSortVisitor : public DfsVisitor<Digraph> {
deba@433
  1172
    public:
deba@433
  1173
      typedef typename Digraph::Node Node;
deba@433
  1174
      typedef typename Digraph::Arc edge;
deba@433
  1175
deba@433
  1176
      TopologicalSortVisitor(IntNodeMap& order, int num)
deba@433
  1177
        : _order(order), _num(num) {}
deba@433
  1178
deba@433
  1179
      void leave(const Node& node) {
deba@433
  1180
        _order.set(node, --_num);
deba@433
  1181
      }
deba@433
  1182
deba@433
  1183
    private:
deba@433
  1184
      IntNodeMap& _order;
deba@433
  1185
      int _num;
deba@433
  1186
    };
deba@433
  1187
deba@433
  1188
  }
deba@433
  1189
kpeter@633
  1190
  /// \ingroup graph_properties
deba@433
  1191
  ///
deba@433
  1192
  /// \brief Sort the nodes of a DAG into topolgical order.
deba@433
  1193
  ///
deba@433
  1194
  /// Sort the nodes of a DAG into topolgical order.
deba@433
  1195
  ///
deba@433
  1196
  /// \param graph The graph. It must be directed and acyclic.
deba@433
  1197
  /// \retval order A writable node map. The values will be set from 0 to
deba@433
  1198
  /// the number of the nodes in the graph minus one. Each values of the map
deba@433
  1199
  /// will be set exactly once, the values  will be set descending order.
deba@433
  1200
  ///
deba@433
  1201
  /// \see checkedTopologicalSort
deba@433
  1202
  /// \see dag
deba@433
  1203
  template <typename Digraph, typename NodeMap>
deba@433
  1204
  void topologicalSort(const Digraph& graph, NodeMap& order) {
deba@435
  1205
    using namespace _connectivity_bits;
deba@433
  1206
deba@433
  1207
    checkConcept<concepts::Digraph, Digraph>();
deba@433
  1208
    checkConcept<concepts::WriteMap<typename Digraph::Node, int>, NodeMap>();
deba@433
  1209
deba@433
  1210
    typedef typename Digraph::Node Node;
deba@433
  1211
    typedef typename Digraph::NodeIt NodeIt;
deba@433
  1212
    typedef typename Digraph::Arc Arc;
deba@433
  1213
deba@433
  1214
    TopologicalSortVisitor<Digraph, NodeMap>
deba@433
  1215
      visitor(order, countNodes(graph));
deba@433
  1216
deba@433
  1217
    DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> >
deba@433
  1218
      dfs(graph, visitor);
deba@433
  1219
deba@433
  1220
    dfs.init();
deba@433
  1221
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
  1222
      if (!dfs.reached(it)) {
deba@433
  1223
        dfs.addSource(it);
deba@433
  1224
        dfs.start();
deba@433
  1225
      }
deba@433
  1226
    }
deba@433
  1227
  }
deba@433
  1228
kpeter@633
  1229
  /// \ingroup graph_properties
deba@433
  1230
  ///
deba@433
  1231
  /// \brief Sort the nodes of a DAG into topolgical order.
deba@433
  1232
  ///
deba@433
  1233
  /// Sort the nodes of a DAG into topolgical order. It also checks
deba@433
  1234
  /// that the given graph is DAG.
deba@433
  1235
  ///
deba@444
  1236
  /// \param digraph The graph. It must be directed and acyclic.
deba@433
  1237
  /// \retval order A readable - writable node map. The values will be set
deba@433
  1238
  /// from 0 to the number of the nodes in the graph minus one. Each values
deba@433
  1239
  /// of the map will be set exactly once, the values will be set descending
deba@433
  1240
  /// order.
kpeter@606
  1241
  /// \return \c false when the graph is not DAG.
deba@433
  1242
  ///
deba@433
  1243
  /// \see topologicalSort
deba@433
  1244
  /// \see dag
deba@433
  1245
  template <typename Digraph, typename NodeMap>
deba@435
  1246
  bool checkedTopologicalSort(const Digraph& digraph, NodeMap& order) {
deba@435
  1247
    using namespace _connectivity_bits;
deba@433
  1248
deba@433
  1249
    checkConcept<concepts::Digraph, Digraph>();
deba@433
  1250
    checkConcept<concepts::ReadWriteMap<typename Digraph::Node, int>,
deba@433
  1251
      NodeMap>();
deba@433
  1252
deba@433
  1253
    typedef typename Digraph::Node Node;
deba@433
  1254
    typedef typename Digraph::NodeIt NodeIt;
deba@433
  1255
    typedef typename Digraph::Arc Arc;
deba@433
  1256
deba@435
  1257
    for (NodeIt it(digraph); it != INVALID; ++it) {
deba@435
  1258
      order.set(it, -1);
deba@435
  1259
    }
deba@433
  1260
deba@433
  1261
    TopologicalSortVisitor<Digraph, NodeMap>
deba@435
  1262
      visitor(order, countNodes(digraph));
deba@433
  1263
deba@433
  1264
    DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> >
deba@435
  1265
      dfs(digraph, visitor);
deba@433
  1266
deba@433
  1267
    dfs.init();
deba@435
  1268
    for (NodeIt it(digraph); it != INVALID; ++it) {
deba@433
  1269
      if (!dfs.reached(it)) {
deba@433
  1270
        dfs.addSource(it);
deba@433
  1271
        while (!dfs.emptyQueue()) {
deba@435
  1272
           Arc arc = dfs.nextArc();
deba@435
  1273
           Node target = digraph.target(arc);
deba@433
  1274
           if (dfs.reached(target) && order[target] == -1) {
deba@433
  1275
             return false;
deba@433
  1276
           }
deba@433
  1277
           dfs.processNextArc();
deba@433
  1278
         }
deba@433
  1279
      }
deba@433
  1280
    }
deba@433
  1281
    return true;
deba@433
  1282
  }
deba@433
  1283
kpeter@633
  1284
  /// \ingroup graph_properties
deba@433
  1285
  ///
deba@433
  1286
  /// \brief Check that the given directed graph is a DAG.
deba@433
  1287
  ///
deba@433
  1288
  /// Check that the given directed graph is a DAG. The DAG is
deba@433
  1289
  /// an Directed Acyclic Digraph.
kpeter@606
  1290
  /// \return \c false when the graph is not DAG.
deba@433
  1291
  /// \see acyclic
deba@433
  1292
  template <typename Digraph>
deba@435
  1293
  bool dag(const Digraph& digraph) {
deba@433
  1294
deba@433
  1295
    checkConcept<concepts::Digraph, Digraph>();
deba@433
  1296
deba@433
  1297
    typedef typename Digraph::Node Node;
deba@433
  1298
    typedef typename Digraph::NodeIt NodeIt;
deba@433
  1299
    typedef typename Digraph::Arc Arc;
deba@433
  1300
deba@433
  1301
    typedef typename Digraph::template NodeMap<bool> ProcessedMap;
deba@433
  1302
deba@433
  1303
    typename Dfs<Digraph>::template SetProcessedMap<ProcessedMap>::
deba@435
  1304
      Create dfs(digraph);
deba@433
  1305
deba@435
  1306
    ProcessedMap processed(digraph);
deba@433
  1307
    dfs.processedMap(processed);
deba@433
  1308
deba@433
  1309
    dfs.init();
deba@435
  1310
    for (NodeIt it(digraph); it != INVALID; ++it) {
deba@433
  1311
      if (!dfs.reached(it)) {
deba@433
  1312
        dfs.addSource(it);
deba@433
  1313
        while (!dfs.emptyQueue()) {
deba@433
  1314
          Arc edge = dfs.nextArc();
deba@435
  1315
          Node target = digraph.target(edge);
deba@433
  1316
          if (dfs.reached(target) && !processed[target]) {
deba@433
  1317
            return false;
deba@433
  1318
          }
deba@433
  1319
          dfs.processNextArc();
deba@433
  1320
        }
deba@433
  1321
      }
deba@433
  1322
    }
deba@433
  1323
    return true;
deba@433
  1324
  }
deba@433
  1325
kpeter@633
  1326
  /// \ingroup graph_properties
deba@433
  1327
  ///
deba@433
  1328
  /// \brief Check that the given undirected graph is acyclic.
deba@433
  1329
  ///
deba@433
  1330
  /// Check that the given undirected graph acyclic.
deba@433
  1331
  /// \param graph The undirected graph.
kpeter@606
  1332
  /// \return \c true when there is no circle in the graph.
deba@433
  1333
  /// \see dag
deba@433
  1334
  template <typename Graph>
deba@433
  1335
  bool acyclic(const Graph& graph) {
deba@433
  1336
    checkConcept<concepts::Graph, Graph>();
deba@433
  1337
    typedef typename Graph::Node Node;
deba@433
  1338
    typedef typename Graph::NodeIt NodeIt;
deba@433
  1339
    typedef typename Graph::Arc Arc;
deba@433
  1340
    Dfs<Graph> dfs(graph);
deba@433
  1341
    dfs.init();
deba@433
  1342
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
  1343
      if (!dfs.reached(it)) {
deba@433
  1344
        dfs.addSource(it);
deba@433
  1345
        while (!dfs.emptyQueue()) {
deba@433
  1346
          Arc edge = dfs.nextArc();
deba@433
  1347
          Node source = graph.source(edge);
deba@433
  1348
          Node target = graph.target(edge);
deba@433
  1349
          if (dfs.reached(target) &&
deba@433
  1350
              dfs.predArc(source) != graph.oppositeArc(edge)) {
deba@433
  1351
            return false;
deba@433
  1352
          }
deba@433
  1353
          dfs.processNextArc();
deba@433
  1354
        }
deba@433
  1355
      }
deba@433
  1356
    }
deba@433
  1357
    return true;
deba@433
  1358
  }
deba@433
  1359
kpeter@633
  1360
  /// \ingroup graph_properties
deba@433
  1361
  ///
deba@433
  1362
  /// \brief Check that the given undirected graph is tree.
deba@433
  1363
  ///
deba@433
  1364
  /// Check that the given undirected graph is tree.
deba@433
  1365
  /// \param graph The undirected graph.
kpeter@606
  1366
  /// \return \c true when the graph is acyclic and connected.
deba@433
  1367
  template <typename Graph>
deba@433
  1368
  bool tree(const Graph& graph) {
deba@433
  1369
    checkConcept<concepts::Graph, Graph>();
deba@433
  1370
    typedef typename Graph::Node Node;
deba@433
  1371
    typedef typename Graph::NodeIt NodeIt;
deba@433
  1372
    typedef typename Graph::Arc Arc;
deba@433
  1373
    Dfs<Graph> dfs(graph);
deba@433
  1374
    dfs.init();
deba@433
  1375
    dfs.addSource(NodeIt(graph));
deba@433
  1376
    while (!dfs.emptyQueue()) {
deba@433
  1377
      Arc edge = dfs.nextArc();
deba@433
  1378
      Node source = graph.source(edge);
deba@433
  1379
      Node target = graph.target(edge);
deba@433
  1380
      if (dfs.reached(target) &&
deba@433
  1381
          dfs.predArc(source) != graph.oppositeArc(edge)) {
deba@433
  1382
        return false;
deba@433
  1383
      }
deba@433
  1384
      dfs.processNextArc();
deba@433
  1385
    }
deba@433
  1386
    for (NodeIt it(graph); it != INVALID; ++it) {
deba@433
  1387
      if (!dfs.reached(it)) {
deba@433
  1388
        return false;
deba@433
  1389
      }
deba@433
  1390
    }
deba@433
  1391
    return true;
deba@433
  1392
  }
deba@433
  1393
deba@435
  1394
  namespace _connectivity_bits {
deba@433
  1395
deba@433
  1396
    template <typename Digraph>
deba@433
  1397
    class BipartiteVisitor : public BfsVisitor<Digraph> {
deba@433
  1398
    public:
deba@433
  1399
      typedef typename Digraph::Arc Arc;
deba@433
  1400
      typedef typename Digraph::Node Node;
deba@433
  1401
deba@433
  1402
      BipartiteVisitor(const Digraph& graph, bool& bipartite)
deba@433
  1403
        : _graph(graph), _part(graph), _bipartite(bipartite) {}
deba@433
  1404
deba@433
  1405
      void start(const Node& node) {
deba@433
  1406
        _part[node] = true;
deba@433
  1407
      }
deba@433
  1408
      void discover(const Arc& edge) {
deba@433
  1409
        _part.set(_graph.target(edge), !_part[_graph.source(edge)]);
deba@433
  1410
      }
deba@433
  1411
      void examine(const Arc& edge) {
deba@433
  1412
        _bipartite = _bipartite &&
deba@433
  1413
          _part[_graph.target(edge)] != _part[_graph.source(edge)];
deba@433
  1414
      }
deba@433
  1415
deba@433
  1416
    private:
deba@433
  1417
deba@433
  1418
      const Digraph& _graph;
deba@433
  1419
      typename Digraph::template NodeMap<bool> _part;
deba@433
  1420
      bool& _bipartite;
deba@433
  1421
    };
deba@433
  1422
deba@433
  1423
    template <typename Digraph, typename PartMap>
deba@433
  1424
    class BipartitePartitionsVisitor : public BfsVisitor<Digraph> {
deba@433
  1425
    public:
deba@433
  1426
      typedef typename Digraph::Arc Arc;
deba@433
  1427
      typedef typename Digraph::Node Node;
deba@433
  1428
deba@433
  1429
      BipartitePartitionsVisitor(const Digraph& graph,
deba@433
  1430
                                 PartMap& part, bool& bipartite)
deba@433
  1431
        : _graph(graph), _part(part), _bipartite(bipartite) {}
deba@433
  1432
deba@433
  1433
      void start(const Node& node) {
deba@433
  1434
        _part.set(node, true);
deba@433
  1435
      }
deba@433
  1436
      void discover(const Arc& edge) {
deba@433
  1437
        _part.set(_graph.target(edge), !_part[_graph.source(edge)]);
deba@433
  1438
      }
deba@433
  1439
      void examine(const Arc& edge) {
deba@433
  1440
        _bipartite = _bipartite &&
deba@433
  1441
          _part[_graph.target(edge)] != _part[_graph.source(edge)];
deba@433
  1442
      }
deba@433
  1443
deba@433
  1444
    private:
deba@433
  1445
deba@433
  1446
      const Digraph& _graph;
deba@433
  1447
      PartMap& _part;
deba@433
  1448
      bool& _bipartite;
deba@433
  1449
    };
deba@433
  1450
  }
deba@433
  1451
kpeter@633
  1452
  /// \ingroup graph_properties
deba@433
  1453
  ///
deba@433
  1454
  /// \brief Check if the given undirected graph is bipartite or not
deba@433
  1455
  ///
deba@433
  1456
  /// The function checks if the given undirected \c graph graph is bipartite
deba@433
  1457
  /// or not. The \ref Bfs algorithm is used to calculate the result.
deba@433
  1458
  /// \param graph The undirected graph.
kpeter@606
  1459
  /// \return \c true if \c graph is bipartite, \c false otherwise.
deba@433
  1460
  /// \sa bipartitePartitions
deba@433
  1461
  template<typename Graph>
deba@433
  1462
  inline bool bipartite(const Graph &graph){
deba@435
  1463
    using namespace _connectivity_bits;
deba@433
  1464
deba@433
  1465
    checkConcept<concepts::Graph, Graph>();
deba@433
  1466
deba@433
  1467
    typedef typename Graph::NodeIt NodeIt;
deba@433
  1468
    typedef typename Graph::ArcIt ArcIt;
deba@433
  1469
deba@433
  1470
    bool bipartite = true;
deba@433
  1471
deba@433
  1472
    BipartiteVisitor<Graph>
deba@433
  1473
      visitor(graph, bipartite);
deba@433
  1474
    BfsVisit<Graph, BipartiteVisitor<Graph> >
deba@433
  1475
      bfs(graph, visitor);
deba@433
  1476
    bfs.init();
deba@433
  1477
    for(NodeIt it(graph); it != INVALID; ++it) {
deba@433
  1478
      if(!bfs.reached(it)){
deba@433
  1479
        bfs.addSource(it);
deba@433
  1480
        while (!bfs.emptyQueue()) {
deba@433
  1481
          bfs.processNextNode();
deba@433
  1482
          if (!bipartite) return false;
deba@433
  1483
        }
deba@433
  1484
      }
deba@433
  1485
    }
deba@433
  1486
    return true;
deba@433
  1487
  }
deba@433
  1488
kpeter@633
  1489
  /// \ingroup graph_properties
deba@433
  1490
  ///
deba@433
  1491
  /// \brief Check if the given undirected graph is bipartite or not
deba@433
  1492
  ///
deba@433
  1493
  /// The function checks if the given undirected graph is bipartite
deba@433
  1494
  /// or not. The  \ref  Bfs  algorithm  is   used  to  calculate the result.
deba@433
  1495
  /// During the execution, the \c partMap will be set as the two
deba@433
  1496
  /// partitions of the graph.
kpeter@633
  1497
  ///
kpeter@633
  1498
  /// \image html bipartite_partitions.png
kpeter@633
  1499
  /// \image latex bipartite_partitions.eps "Bipartite partititions" width=\textwidth
kpeter@633
  1500
  ///
deba@433
  1501
  /// \param graph The undirected graph.
deba@433
  1502
  /// \retval partMap A writable bool map of nodes. It will be set as the
deba@433
  1503
  /// two partitions of the graph.
kpeter@606
  1504
  /// \return \c true if \c graph is bipartite, \c false otherwise.
deba@433
  1505
  template<typename Graph, typename NodeMap>
deba@433
  1506
  inline bool bipartitePartitions(const Graph &graph, NodeMap &partMap){
deba@435
  1507
    using namespace _connectivity_bits;
deba@433
  1508
deba@433
  1509
    checkConcept<concepts::Graph, Graph>();
deba@433
  1510
deba@433
  1511
    typedef typename Graph::Node Node;
deba@433
  1512
    typedef typename Graph::NodeIt NodeIt;
deba@433
  1513
    typedef typename Graph::ArcIt ArcIt;
deba@433
  1514
deba@433
  1515
    bool bipartite = true;
deba@433
  1516
deba@433
  1517
    BipartitePartitionsVisitor<Graph, NodeMap>
deba@433
  1518
      visitor(graph, partMap, bipartite);
deba@433
  1519
    BfsVisit<Graph, BipartitePartitionsVisitor<Graph, NodeMap> >
deba@433
  1520
      bfs(graph, visitor);
deba@433
  1521
    bfs.init();
deba@433
  1522
    for(NodeIt it(graph); it != INVALID; ++it) {
deba@433
  1523
      if(!bfs.reached(it)){
deba@433
  1524
        bfs.addSource(it);
deba@433
  1525
        while (!bfs.emptyQueue()) {
deba@433
  1526
          bfs.processNextNode();
deba@433
  1527
          if (!bipartite) return false;
deba@433
  1528
        }
deba@433
  1529
      }
deba@433
  1530
    }
deba@433
  1531
    return true;
deba@433
  1532
  }
deba@433
  1533
deba@433
  1534
  /// \brief Returns true when there are not loop edges in the graph.
deba@433
  1535
  ///
deba@433
  1536
  /// Returns true when there are not loop edges in the graph.
deba@433
  1537
  template <typename Digraph>
deba@435
  1538
  bool loopFree(const Digraph& digraph) {
deba@435
  1539
    for (typename Digraph::ArcIt it(digraph); it != INVALID; ++it) {
deba@435
  1540
      if (digraph.source(it) == digraph.target(it)) return false;
deba@433
  1541
    }
deba@433
  1542
    return true;
deba@433
  1543
  }
deba@433
  1544
deba@433
  1545
  /// \brief Returns true when there are not parallel edges in the graph.
deba@433
  1546
  ///
deba@433
  1547
  /// Returns true when there are not parallel edges in the graph.
deba@433
  1548
  template <typename Digraph>
deba@435
  1549
  bool parallelFree(const Digraph& digraph) {
deba@435
  1550
    typename Digraph::template NodeMap<bool> reached(digraph, false);
deba@435
  1551
    for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
deba@435
  1552
      for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
deba@435
  1553
        if (reached[digraph.target(a)]) return false;
deba@435
  1554
        reached.set(digraph.target(a), true);
deba@433
  1555
      }
deba@435
  1556
      for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
deba@435
  1557
        reached.set(digraph.target(a), false);
deba@433
  1558
      }
deba@433
  1559
    }
deba@433
  1560
    return true;
deba@433
  1561
  }
deba@433
  1562
deba@433
  1563
  /// \brief Returns true when there are not loop edges and parallel
deba@433
  1564
  /// edges in the graph.
deba@433
  1565
  ///
deba@433
  1566
  /// Returns true when there are not loop edges and parallel edges in
deba@433
  1567
  /// the graph.
deba@433
  1568
  template <typename Digraph>
deba@435
  1569
  bool simpleDigraph(const Digraph& digraph) {
deba@435
  1570
    typename Digraph::template NodeMap<bool> reached(digraph, false);
deba@435
  1571
    for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
deba@433
  1572
      reached.set(n, true);
deba@435
  1573
      for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
deba@435
  1574
        if (reached[digraph.target(a)]) return false;
deba@435
  1575
        reached.set(digraph.target(a), true);
deba@433
  1576
      }
deba@435
  1577
      for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) {
deba@435
  1578
        reached.set(digraph.target(a), false);
deba@433
  1579
      }
deba@433
  1580
      reached.set(n, false);
deba@433
  1581
    }
deba@433
  1582
    return true;
deba@433
  1583
  }
deba@433
  1584
deba@433
  1585
} //namespace lemon
deba@433
  1586
deba@435
  1587
#endif //LEMON_CONNECTIVITY_H