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