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