lemon/graph_utils.h
author deba
Mon, 03 Apr 2006 08:41:52 +0000
changeset 2029 e00114f165f5
parent 2022 0f3367da6104
child 2031 080d51024ac5
permissions -rw-r--r--
Count ANodes-BNodes
klao@946
     1
/* -*- C++ -*-
klao@946
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
klao@946
     8
 *
klao@946
     9
 * Permission to use, modify and distribute this software is granted
klao@946
    10
 * provided that this copyright notice appears in all copies. For
klao@946
    11
 * precise terms see the accompanying LICENSE file.
klao@946
    12
 *
klao@946
    13
 * This software is provided "AS IS" with no warranty of any kind,
klao@946
    14
 * express or implied, and with no claim as to its suitability for any
klao@946
    15
 * purpose.
klao@946
    16
 *
klao@946
    17
 */
klao@946
    18
klao@946
    19
#ifndef LEMON_GRAPH_UTILS_H
klao@946
    20
#define LEMON_GRAPH_UTILS_H
klao@946
    21
klao@946
    22
#include <iterator>
deba@1419
    23
#include <vector>
alpar@1402
    24
#include <map>
deba@1695
    25
#include <cmath>
klao@946
    26
deba@1993
    27
#include <lemon/bits/invalid.h>
deba@1993
    28
#include <lemon/bits/utility.h>
deba@1413
    29
#include <lemon/maps.h>
deba@1993
    30
#include <lemon/bits/traits.h>
deba@1990
    31
alpar@1459
    32
#include <lemon/bits/alteration_notifier.h>
deba@1990
    33
#include <lemon/bits/default_map.h>
klao@946
    34
alpar@947
    35
///\ingroup gutils
klao@946
    36
///\file
alpar@947
    37
///\brief Graph utilities.
klao@946
    38
///
alpar@964
    39
///
klao@946
    40
klao@946
    41
klao@946
    42
namespace lemon {
klao@946
    43
deba@1267
    44
  /// \addtogroup gutils
deba@1267
    45
  /// @{
alpar@947
    46
alpar@1756
    47
  ///Creates convenience typedefs for the graph types and iterators
alpar@1756
    48
alpar@1756
    49
  ///This \c \#define creates convenience typedefs for the following types
alpar@1756
    50
  ///of \c Graph: \c Node,  \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
alpar@1804
    51
  ///\c OutEdgeIt,  \c BoolNodeMap,  \c IntNodeMap,  \c DoubleNodeMap,
alpar@1804
    52
  ///\c BoolEdgeMap, \c IntEdgeMap,  \c DoubleEdgeMap.  
alpar@1756
    53
  ///\note If \c G it a template parameter, it should be used in this way.
alpar@1756
    54
  ///\code
alpar@1756
    55
  ///  GRAPH_TYPEDEFS(typename G)
alpar@1756
    56
  ///\endcode
alpar@1756
    57
  ///
alpar@1756
    58
  ///\warning There are no typedefs for the graph maps because of the lack of
alpar@1756
    59
  ///template typedefs in C++.
alpar@1804
    60
#define GRAPH_TYPEDEFS(Graph)				\
alpar@1804
    61
  typedef Graph::     Node      Node;			\
alpar@1804
    62
    typedef Graph::   NodeIt    NodeIt;			\
alpar@1804
    63
    typedef Graph::   Edge      Edge;			\
alpar@1804
    64
    typedef Graph::   EdgeIt    EdgeIt;			\
alpar@1804
    65
    typedef Graph:: InEdgeIt  InEdgeIt;			\
alpar@1811
    66
    typedef Graph::OutEdgeIt OutEdgeIt;			
alpar@1811
    67
//     typedef Graph::template NodeMap<bool> BoolNodeMap;	       
alpar@1811
    68
//     typedef Graph::template NodeMap<int> IntNodeMap;	       
alpar@1811
    69
//     typedef Graph::template NodeMap<double> DoubleNodeMap;  
alpar@1811
    70
//     typedef Graph::template EdgeMap<bool> BoolEdgeMap;	       
alpar@1811
    71
//     typedef Graph::template EdgeMap<int> IntEdgeMap;	       
alpar@1811
    72
//     typedef Graph::template EdgeMap<double> DoubleEdgeMap;
alpar@1756
    73
  
alpar@1756
    74
  ///Creates convenience typedefs for the undirected graph types and iterators
alpar@1756
    75
alpar@1756
    76
  ///This \c \#define creates the same convenience typedefs as defined by
alpar@1756
    77
  ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
klao@1909
    78
  ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
klao@1909
    79
  ///\c BoolUEdgeMap, \c IntUEdgeMap,  \c DoubleUEdgeMap.  
alpar@1756
    80
  ///
alpar@1756
    81
  ///\note If \c G it a template parameter, it should be used in this way.
alpar@1756
    82
  ///\code
deba@1992
    83
  ///  UGRAPH_TYPEDEFS(typename G)
alpar@1756
    84
  ///\endcode
alpar@1756
    85
  ///
alpar@1756
    86
  ///\warning There are no typedefs for the graph maps because of the lack of
alpar@1756
    87
  ///template typedefs in C++.
deba@1992
    88
#define UGRAPH_TYPEDEFS(Graph)				\
alpar@1804
    89
  GRAPH_TYPEDEFS(Graph)						\
klao@1909
    90
    typedef Graph:: UEdge   UEdge;			\
klao@1909
    91
    typedef Graph:: UEdgeIt UEdgeIt;			\
alpar@1811
    92
    typedef Graph:: IncEdgeIt   IncEdgeIt;		       
klao@1909
    93
//     typedef Graph::template UEdgeMap<bool> BoolUEdgeMap;	 
klao@1909
    94
//     typedef Graph::template UEdgeMap<int> IntUEdgeMap;
klao@1909
    95
//     typedef Graph::template UEdgeMap<double> DoubleUEdgeMap;
alpar@1804
    96
  
alpar@1756
    97
alpar@1756
    98
klao@946
    99
  /// \brief Function to count the items in the graph.
klao@946
   100
  ///
athos@1540
   101
  /// This function counts the items (nodes, edges etc) in the graph.
klao@946
   102
  /// The complexity of the function is O(n) because
klao@946
   103
  /// it iterates on all of the items.
klao@946
   104
deba@2020
   105
  template <typename Graph, typename Item>
klao@977
   106
  inline int countItems(const Graph& g) {
deba@2020
   107
    typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
klao@946
   108
    int num = 0;
klao@977
   109
    for (ItemIt it(g); it != INVALID; ++it) {
klao@946
   110
      ++num;
klao@946
   111
    }
klao@946
   112
    return num;
klao@946
   113
  }
klao@946
   114
klao@977
   115
  // Node counting:
klao@977
   116
deba@2020
   117
  namespace _graph_utils_bits {
deba@2020
   118
    
deba@2020
   119
    template <typename Graph, typename Enable = void>
deba@2020
   120
    struct CountNodesSelector {
deba@2020
   121
      static int count(const Graph &g) {
deba@2020
   122
        return countItems<Graph, typename Graph::Node>(g);
deba@2020
   123
      }
deba@2020
   124
    };
klao@977
   125
deba@2020
   126
    template <typename Graph>
deba@2020
   127
    struct CountNodesSelector<
deba@2020
   128
      Graph, typename 
deba@2020
   129
      enable_if<typename Graph::NodeNumTag, void>::type> 
deba@2020
   130
    {
deba@2020
   131
      static int count(const Graph &g) {
deba@2020
   132
        return g.nodeNum();
deba@2020
   133
      }
deba@2020
   134
    };    
klao@977
   135
  }
klao@977
   136
klao@946
   137
  /// \brief Function to count the nodes in the graph.
klao@946
   138
  ///
klao@946
   139
  /// This function counts the nodes in the graph.
klao@946
   140
  /// The complexity of the function is O(n) but for some
athos@1526
   141
  /// graph structures it is specialized to run in O(1).
klao@977
   142
  ///
klao@977
   143
  /// \todo refer how to specialize it
klao@946
   144
klao@946
   145
  template <typename Graph>
klao@977
   146
  inline int countNodes(const Graph& g) {
deba@2020
   147
    return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
klao@977
   148
  }
klao@977
   149
deba@2029
   150
  namespace _graph_utils_bits {
deba@2029
   151
    
deba@2029
   152
    template <typename Graph, typename Enable = void>
deba@2029
   153
    struct CountANodesSelector {
deba@2029
   154
      static int count(const Graph &g) {
deba@2029
   155
        return countItems<Graph, typename Graph::ANode>(g);
deba@2029
   156
      }
deba@2029
   157
    };
deba@2029
   158
deba@2029
   159
    template <typename Graph>
deba@2029
   160
    struct CountANodesSelector<
deba@2029
   161
      Graph, typename 
deba@2029
   162
      enable_if<typename Graph::NodeNumTag, void>::type> 
deba@2029
   163
    {
deba@2029
   164
      static int count(const Graph &g) {
deba@2029
   165
        return g.nodeNum();
deba@2029
   166
      }
deba@2029
   167
    };    
deba@2029
   168
  }
deba@2029
   169
deba@2029
   170
  /// \brief Function to count the anodes in the graph.
deba@2029
   171
  ///
deba@2029
   172
  /// This function counts the anodes in the graph.
deba@2029
   173
  /// The complexity of the function is O(an) but for some
deba@2029
   174
  /// graph structures it is specialized to run in O(1).
deba@2029
   175
  ///
deba@2029
   176
  /// \todo refer how to specialize it
deba@2029
   177
deba@2029
   178
  template <typename Graph>
deba@2029
   179
  inline int countANodes(const Graph& g) {
deba@2029
   180
    return _graph_utils_bits::CountANodesSelector<Graph>::count(g);
deba@2029
   181
  }
deba@2029
   182
deba@2029
   183
  namespace _graph_utils_bits {
deba@2029
   184
    
deba@2029
   185
    template <typename Graph, typename Enable = void>
deba@2029
   186
    struct CountBNodesSelector {
deba@2029
   187
      static int count(const Graph &g) {
deba@2029
   188
        return countItems<Graph, typename Graph::BNode>(g);
deba@2029
   189
      }
deba@2029
   190
    };
deba@2029
   191
deba@2029
   192
    template <typename Graph>
deba@2029
   193
    struct CountBNodesSelector<
deba@2029
   194
      Graph, typename 
deba@2029
   195
      enable_if<typename Graph::NodeNumTag, void>::type> 
deba@2029
   196
    {
deba@2029
   197
      static int count(const Graph &g) {
deba@2029
   198
        return g.nodeNum();
deba@2029
   199
      }
deba@2029
   200
    };    
deba@2029
   201
  }
deba@2029
   202
deba@2029
   203
  /// \brief Function to count the bnodes in the graph.
deba@2029
   204
  ///
deba@2029
   205
  /// This function counts the bnodes in the graph.
deba@2029
   206
  /// The complexity of the function is O(bn) but for some
deba@2029
   207
  /// graph structures it is specialized to run in O(1).
deba@2029
   208
  ///
deba@2029
   209
  /// \todo refer how to specialize it
deba@2029
   210
deba@2029
   211
  template <typename Graph>
deba@2029
   212
  inline int countBNodes(const Graph& g) {
deba@2029
   213
    return _graph_utils_bits::CountBNodesSelector<Graph>::count(g);
deba@2029
   214
  }
deba@2029
   215
deba@2020
   216
klao@977
   217
  // Edge counting:
klao@977
   218
deba@2020
   219
  namespace _graph_utils_bits {
deba@2020
   220
    
deba@2020
   221
    template <typename Graph, typename Enable = void>
deba@2020
   222
    struct CountEdgesSelector {
deba@2020
   223
      static int count(const Graph &g) {
deba@2020
   224
        return countItems<Graph, typename Graph::Edge>(g);
deba@2020
   225
      }
deba@2020
   226
    };
klao@977
   227
deba@2020
   228
    template <typename Graph>
deba@2020
   229
    struct CountEdgesSelector<
deba@2020
   230
      Graph, 
deba@2020
   231
      typename enable_if<typename Graph::EdgeNumTag, void>::type> 
deba@2020
   232
    {
deba@2020
   233
      static int count(const Graph &g) {
deba@2020
   234
        return g.edgeNum();
deba@2020
   235
      }
deba@2020
   236
    };    
klao@946
   237
  }
klao@946
   238
klao@946
   239
  /// \brief Function to count the edges in the graph.
klao@946
   240
  ///
klao@946
   241
  /// This function counts the edges in the graph.
klao@946
   242
  /// The complexity of the function is O(e) but for some
athos@1526
   243
  /// graph structures it is specialized to run in O(1).
klao@977
   244
klao@946
   245
  template <typename Graph>
klao@977
   246
  inline int countEdges(const Graph& g) {
deba@2020
   247
    return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
klao@946
   248
  }
klao@946
   249
klao@1053
   250
  // Undirected edge counting:
deba@2020
   251
  namespace _graph_utils_bits {
deba@2020
   252
    
deba@2020
   253
    template <typename Graph, typename Enable = void>
deba@2020
   254
    struct CountUEdgesSelector {
deba@2020
   255
      static int count(const Graph &g) {
deba@2020
   256
        return countItems<Graph, typename Graph::UEdge>(g);
deba@2020
   257
      }
deba@2020
   258
    };
klao@1053
   259
deba@2020
   260
    template <typename Graph>
deba@2020
   261
    struct CountUEdgesSelector<
deba@2020
   262
      Graph, 
deba@2020
   263
      typename enable_if<typename Graph::EdgeNumTag, void>::type> 
deba@2020
   264
    {
deba@2020
   265
      static int count(const Graph &g) {
deba@2020
   266
        return g.uEdgeNum();
deba@2020
   267
      }
deba@2020
   268
    };    
klao@1053
   269
  }
klao@1053
   270
athos@1526
   271
  /// \brief Function to count the undirected edges in the graph.
klao@946
   272
  ///
athos@1526
   273
  /// This function counts the undirected edges in the graph.
klao@946
   274
  /// The complexity of the function is O(e) but for some
athos@1540
   275
  /// graph structures it is specialized to run in O(1).
klao@1053
   276
klao@946
   277
  template <typename Graph>
klao@1909
   278
  inline int countUEdges(const Graph& g) {
deba@2020
   279
    return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
deba@2020
   280
klao@946
   281
  }
klao@946
   282
klao@977
   283
klao@946
   284
  template <typename Graph, typename DegIt>
klao@946
   285
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
klao@946
   286
    int num = 0;
klao@946
   287
    for (DegIt it(_g, _n); it != INVALID; ++it) {
klao@946
   288
      ++num;
klao@946
   289
    }
klao@946
   290
    return num;
klao@946
   291
  }
alpar@967
   292
deba@1531
   293
  /// \brief Function to count the number of the out-edges from node \c n.
deba@1531
   294
  ///
deba@1531
   295
  /// This function counts the number of the out-edges from node \c n
deba@1531
   296
  /// in the graph.  
deba@1531
   297
  template <typename Graph>
deba@1531
   298
  inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1531
   299
    return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
deba@1531
   300
  }
deba@1531
   301
deba@1531
   302
  /// \brief Function to count the number of the in-edges to node \c n.
deba@1531
   303
  ///
deba@1531
   304
  /// This function counts the number of the in-edges to node \c n
deba@1531
   305
  /// in the graph.  
deba@1531
   306
  template <typename Graph>
deba@1531
   307
  inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1531
   308
    return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
deba@1531
   309
  }
deba@1531
   310
deba@1704
   311
  /// \brief Function to count the number of the inc-edges to node \c n.
deba@1679
   312
  ///
deba@1704
   313
  /// This function counts the number of the inc-edges to node \c n
deba@1679
   314
  /// in the graph.  
deba@1679
   315
  template <typename Graph>
deba@1679
   316
  inline int countIncEdges(const Graph& _g,  const typename Graph::Node& _n) {
deba@1679
   317
    return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
deba@1679
   318
  }
deba@1679
   319
deba@2020
   320
  namespace _graph_utils_bits {
deba@2020
   321
    
deba@2020
   322
    template <typename Graph, typename Enable = void>
deba@2020
   323
    struct FindEdgeSelector {
deba@2020
   324
      typedef typename Graph::Node Node;
deba@2020
   325
      typedef typename Graph::Edge Edge;
deba@2020
   326
      static Edge find(const Graph &g, Node u, Node v, Edge e) {
deba@2020
   327
        if (e == INVALID) {
deba@2020
   328
          g.firstOut(e, u);
deba@2020
   329
        } else {
deba@2020
   330
          g.nextOut(e);
deba@2020
   331
        }
deba@2020
   332
        while (e != INVALID && g.target(e) != v) {
deba@2020
   333
          g.nextOut(e);
deba@2020
   334
        }
deba@2020
   335
        return e;
deba@2020
   336
      }
deba@2020
   337
    };
deba@1531
   338
deba@2020
   339
    template <typename Graph>
deba@2020
   340
    struct FindEdgeSelector<
deba@2020
   341
      Graph, 
deba@2020
   342
      typename enable_if<typename Graph::FindEdgeTag, void>::type> 
deba@2020
   343
    {
deba@2020
   344
      typedef typename Graph::Node Node;
deba@2020
   345
      typedef typename Graph::Edge Edge;
deba@2020
   346
      static Edge find(const Graph &g, Node u, Node v, Edge prev) {
deba@2020
   347
        return g.findEdge(u, v, prev);
deba@2020
   348
      }
deba@2020
   349
    };    
deba@1565
   350
  }
deba@1565
   351
deba@1565
   352
  /// \brief Finds an edge between two nodes of a graph.
deba@1565
   353
  ///
alpar@967
   354
  /// Finds an edge from node \c u to node \c v in graph \c g.
alpar@967
   355
  ///
alpar@967
   356
  /// If \c prev is \ref INVALID (this is the default value), then
alpar@967
   357
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
alpar@967
   358
  /// the next edge from \c u to \c v after \c prev.
alpar@967
   359
  /// \return The found edge or \ref INVALID if there is no such an edge.
alpar@967
   360
  ///
alpar@967
   361
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@1946
   362
  ///\code
alpar@967
   363
  /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
alpar@967
   364
  ///   ...
alpar@967
   365
  /// }
alpar@1946
   366
  ///\endcode
alpar@967
   367
  template <typename Graph>
deba@1565
   368
  inline typename Graph::Edge findEdge(const Graph &g,
deba@1565
   369
				       typename Graph::Node u, 
deba@1565
   370
				       typename Graph::Node v,
deba@1565
   371
				       typename Graph::Edge prev = INVALID) {
deba@2020
   372
    return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
alpar@967
   373
  }
deba@1531
   374
deba@1565
   375
  /// \brief Iterator for iterating on edges connected the same nodes.
deba@1565
   376
  ///
deba@1565
   377
  /// Iterator for iterating on edges connected the same nodes. It is 
deba@1565
   378
  /// higher level interface for the findEdge() function. You can
alpar@1591
   379
  /// use it the following way:
alpar@1946
   380
  ///\code
deba@1565
   381
  /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1565
   382
  ///   ...
deba@1565
   383
  /// }
alpar@1946
   384
  ///\endcode
deba@1565
   385
  ///
deba@1565
   386
  /// \author Balazs Dezso 
deba@1565
   387
  template <typename _Graph>
deba@1565
   388
  class ConEdgeIt : public _Graph::Edge {
deba@1565
   389
  public:
deba@1565
   390
deba@1565
   391
    typedef _Graph Graph;
deba@1565
   392
    typedef typename Graph::Edge Parent;
deba@1565
   393
deba@1565
   394
    typedef typename Graph::Edge Edge;
deba@1565
   395
    typedef typename Graph::Node Node;
deba@1565
   396
deba@1565
   397
    /// \brief Constructor.
deba@1565
   398
    ///
deba@1565
   399
    /// Construct a new ConEdgeIt iterating on the edges which
deba@1565
   400
    /// connects the \c u and \c v node.
deba@1565
   401
    ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
deba@1565
   402
      Parent::operator=(findEdge(graph, u, v));
deba@1565
   403
    }
deba@1565
   404
deba@1565
   405
    /// \brief Constructor.
deba@1565
   406
    ///
deba@1565
   407
    /// Construct a new ConEdgeIt which continues the iterating from 
deba@1565
   408
    /// the \c e edge.
deba@1565
   409
    ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
deba@1565
   410
    
deba@1565
   411
    /// \brief Increment operator.
deba@1565
   412
    ///
deba@1565
   413
    /// It increments the iterator and gives back the next edge.
deba@1565
   414
    ConEdgeIt& operator++() {
deba@1565
   415
      Parent::operator=(findEdge(graph, graph.source(*this), 
deba@1565
   416
				 graph.target(*this), *this));
deba@1565
   417
      return *this;
deba@1565
   418
    }
deba@1565
   419
  private:
deba@1565
   420
    const Graph& graph;
deba@1565
   421
  };
deba@1565
   422
deba@2020
   423
  namespace _graph_utils_bits {
deba@2020
   424
    
deba@2020
   425
    template <typename Graph, typename Enable = void>
deba@2020
   426
    struct FindUEdgeSelector {
deba@2020
   427
      typedef typename Graph::Node Node;
deba@2020
   428
      typedef typename Graph::UEdge UEdge;
deba@2020
   429
      static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
deba@2020
   430
        bool b;
deba@2020
   431
        if (u != v) {
deba@2020
   432
          if (e == INVALID) {
deba@2020
   433
            g.firstInc(e, u, b);
deba@2020
   434
          } else {
deba@2020
   435
            b = g.source(e) == u;
deba@2020
   436
            g.nextInc(e, b);
deba@2020
   437
          }
deba@2020
   438
          while (e != INVALID && g.target(e) != v) {
deba@2020
   439
            g.nextInc(e, b);
deba@2020
   440
          }
deba@2020
   441
        } else {
deba@2020
   442
          if (e == INVALID) {
deba@2020
   443
            g.firstInc(e, u, b);
deba@2020
   444
          } else {
deba@2020
   445
            b = true;
deba@2020
   446
            g.nextInc(e, b);
deba@2020
   447
          }
deba@2020
   448
          while (e != INVALID && (!b || g.target(e) != v)) {
deba@2020
   449
            g.nextInc(e, b);
deba@2020
   450
          }
deba@2020
   451
        }
deba@2020
   452
        return e;
deba@2020
   453
      }
deba@2020
   454
    };
deba@1704
   455
deba@2020
   456
    template <typename Graph>
deba@2020
   457
    struct FindUEdgeSelector<
deba@2020
   458
      Graph, 
deba@2020
   459
      typename enable_if<typename Graph::FindEdgeTag, void>::type> 
deba@2020
   460
    {
deba@2020
   461
      typedef typename Graph::Node Node;
deba@2020
   462
      typedef typename Graph::UEdge UEdge;
deba@2020
   463
      static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
deba@2020
   464
        return g.findUEdge(u, v, prev);
deba@2020
   465
      }
deba@2020
   466
    };    
deba@1704
   467
  }
deba@1704
   468
klao@1909
   469
  /// \brief Finds an uedge between two nodes of a graph.
deba@1704
   470
  ///
klao@1909
   471
  /// Finds an uedge from node \c u to node \c v in graph \c g.
deba@2020
   472
  /// If the node \c u and node \c v is equal then each loop edge
deba@2020
   473
  /// will be enumerated.
deba@1704
   474
  ///
deba@1704
   475
  /// If \c prev is \ref INVALID (this is the default value), then
deba@1704
   476
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
deba@1704
   477
  /// the next edge from \c u to \c v after \c prev.
deba@1704
   478
  /// \return The found edge or \ref INVALID if there is no such an edge.
deba@1704
   479
  ///
deba@1704
   480
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@1946
   481
  ///\code
klao@1909
   482
  /// for(UEdge e = findUEdge(g,u,v); e != INVALID; 
klao@1909
   483
  ///     e = findUEdge(g,u,v,e)) {
deba@1704
   484
  ///   ...
deba@1704
   485
  /// }
alpar@1946
   486
  ///\endcode
deba@1704
   487
  template <typename Graph>
deba@2020
   488
  inline typename Graph::UEdge findEdge(const Graph &g,
deba@2020
   489
                                        typename Graph::Node u, 
deba@2020
   490
                                        typename Graph::Node v,
deba@2020
   491
                                        typename Graph::UEdge prev = INVALID) {
deba@2020
   492
    return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, prev);
deba@1704
   493
  }
deba@1704
   494
klao@1909
   495
  /// \brief Iterator for iterating on uedges connected the same nodes.
deba@1704
   496
  ///
klao@1909
   497
  /// Iterator for iterating on uedges connected the same nodes. It is 
klao@1909
   498
  /// higher level interface for the findUEdge() function. You can
deba@1704
   499
  /// use it the following way:
alpar@1946
   500
  ///\code
klao@1909
   501
  /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
deba@1704
   502
  ///   ...
deba@1704
   503
  /// }
alpar@1946
   504
  ///\endcode
deba@1704
   505
  ///
deba@1704
   506
  /// \author Balazs Dezso 
deba@1704
   507
  template <typename _Graph>
klao@1909
   508
  class ConUEdgeIt : public _Graph::UEdge {
deba@1704
   509
  public:
deba@1704
   510
deba@1704
   511
    typedef _Graph Graph;
klao@1909
   512
    typedef typename Graph::UEdge Parent;
deba@1704
   513
klao@1909
   514
    typedef typename Graph::UEdge UEdge;
deba@1704
   515
    typedef typename Graph::Node Node;
deba@1704
   516
deba@1704
   517
    /// \brief Constructor.
deba@1704
   518
    ///
klao@1909
   519
    /// Construct a new ConUEdgeIt iterating on the edges which
deba@1704
   520
    /// connects the \c u and \c v node.
klao@1909
   521
    ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
klao@1909
   522
      Parent::operator=(findUEdge(graph, u, v));
deba@1704
   523
    }
deba@1704
   524
deba@1704
   525
    /// \brief Constructor.
deba@1704
   526
    ///
klao@1909
   527
    /// Construct a new ConUEdgeIt which continues the iterating from 
deba@1704
   528
    /// the \c e edge.
klao@1909
   529
    ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
deba@1704
   530
    
deba@1704
   531
    /// \brief Increment operator.
deba@1704
   532
    ///
deba@1704
   533
    /// It increments the iterator and gives back the next edge.
klao@1909
   534
    ConUEdgeIt& operator++() {
klao@1909
   535
      Parent::operator=(findUEdge(graph, graph.source(*this), 
deba@1829
   536
				      graph.target(*this), *this));
deba@1704
   537
      return *this;
deba@1704
   538
    }
deba@1704
   539
  private:
deba@1704
   540
    const Graph& graph;
deba@1704
   541
  };
deba@1704
   542
athos@1540
   543
  /// \brief Copy a map.
alpar@964
   544
  ///
alpar@1547
   545
  /// This function copies the \c source map to the \c target map. It uses the
athos@1540
   546
  /// given iterator to iterate on the data structure and it uses the \c ref
athos@1540
   547
  /// mapping to convert the source's keys to the target's keys.
deba@1531
   548
  template <typename Target, typename Source, 
deba@1531
   549
	    typename ItemIt, typename Ref>	    
deba@1531
   550
  void copyMap(Target& target, const Source& source, 
deba@1531
   551
	       ItemIt it, const Ref& ref) {
deba@1531
   552
    for (; it != INVALID; ++it) {
deba@1531
   553
      target[ref[it]] = source[it];
klao@946
   554
    }
klao@946
   555
  }
klao@946
   556
deba@1531
   557
  /// \brief Copy the source map to the target map.
deba@1531
   558
  ///
deba@1531
   559
  /// Copy the \c source map to the \c target map. It uses the given iterator
deba@1531
   560
  /// to iterate on the data structure.
deba@1830
   561
  template <typename Target, typename Source, typename ItemIt>	    
deba@1531
   562
  void copyMap(Target& target, const Source& source, ItemIt it) {
deba@1531
   563
    for (; it != INVALID; ++it) {
deba@1531
   564
      target[it] = source[it];
klao@946
   565
    }
klao@946
   566
  }
klao@946
   567
athos@1540
   568
  /// \brief Class to copy a graph.
deba@1531
   569
  ///
alpar@2006
   570
  /// Class to copy a graph to another graph (duplicate a graph). The
athos@1540
   571
  /// simplest way of using it is through the \c copyGraph() function.
deba@1531
   572
  template <typename Target, typename Source>
deba@1267
   573
  class GraphCopy {
deba@1531
   574
  public: 
deba@1531
   575
    typedef typename Source::Node Node;
deba@1531
   576
    typedef typename Source::NodeIt NodeIt;
deba@1531
   577
    typedef typename Source::Edge Edge;
deba@1531
   578
    typedef typename Source::EdgeIt EdgeIt;
klao@946
   579
deba@1531
   580
    typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
deba@1531
   581
    typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
klao@946
   582
deba@1531
   583
    /// \brief Constructor for the GraphCopy.
deba@1531
   584
    ///
deba@1531
   585
    /// It copies the content of the \c _source graph into the
deba@1531
   586
    /// \c _target graph. It creates also two references, one beetween
deba@1531
   587
    /// the two nodeset and one beetween the two edgesets.
deba@1531
   588
    GraphCopy(Target& _target, const Source& _source) 
deba@1531
   589
      : source(_source), target(_target), 
deba@1531
   590
	nodeRefMap(_source), edgeRefMap(_source) {
deba@1531
   591
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   592
	nodeRefMap[it] = target.addNode();
deba@1531
   593
      }
deba@1531
   594
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   595
	edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
deba@1531
   596
					nodeRefMap[source.target(it)]);
deba@1531
   597
      }
deba@1267
   598
    }
klao@946
   599
deba@1531
   600
    /// \brief Copies the node references into the given map.
deba@1531
   601
    ///
deba@1531
   602
    /// Copies the node references into the given map.
deba@1531
   603
    template <typename NodeRef>
deba@1531
   604
    const GraphCopy& nodeRef(NodeRef& map) const {
deba@1531
   605
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   606
	map.set(it, nodeRefMap[it]);
deba@1531
   607
      }
deba@1531
   608
      return *this;
deba@1267
   609
    }
deba@1531
   610
deba@1531
   611
    /// \brief Reverse and copies the node references into the given map.
deba@1531
   612
    ///
deba@1531
   613
    /// Reverse and copies the node references into the given map.
deba@1531
   614
    template <typename NodeRef>
deba@1531
   615
    const GraphCopy& nodeCrossRef(NodeRef& map) const {
deba@1531
   616
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1531
   617
	map.set(nodeRefMap[it], it);
deba@1531
   618
      }
deba@1531
   619
      return *this;
deba@1531
   620
    }
deba@1531
   621
deba@1531
   622
    /// \brief Copies the edge references into the given map.
deba@1531
   623
    ///
deba@1531
   624
    /// Copies the edge references into the given map.
deba@1531
   625
    template <typename EdgeRef>
deba@1531
   626
    const GraphCopy& edgeRef(EdgeRef& map) const {
deba@1531
   627
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   628
	map.set(it, edgeRefMap[it]);
deba@1531
   629
      }
deba@1531
   630
      return *this;
deba@1531
   631
    }
deba@1531
   632
deba@1531
   633
    /// \brief Reverse and copies the edge references into the given map.
deba@1531
   634
    ///
deba@1531
   635
    /// Reverse and copies the edge references into the given map.
deba@1531
   636
    template <typename EdgeRef>
deba@1531
   637
    const GraphCopy& edgeCrossRef(EdgeRef& map) const {
deba@1531
   638
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1531
   639
	map.set(edgeRefMap[it], it);
deba@1531
   640
      }
deba@1531
   641
      return *this;
deba@1531
   642
    }
deba@1531
   643
deba@1531
   644
    /// \brief Make copy of the given map.
deba@1531
   645
    ///
deba@1531
   646
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   647
    /// The new map's key type is the target graph's node type,
deba@1531
   648
    /// and the copied map's key type is the source graph's node
deba@1531
   649
    /// type.  
deba@1531
   650
    template <typename TargetMap, typename SourceMap>
deba@1531
   651
    const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
deba@1531
   652
      copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
deba@1531
   653
      return *this;
deba@1531
   654
    }
deba@1531
   655
deba@1531
   656
    /// \brief Make copy of the given map.
deba@1531
   657
    ///
deba@1531
   658
    /// Makes copy of the given map for the newly created graph. 
deba@1531
   659
    /// The new map's key type is the target graph's edge type,
deba@1531
   660
    /// and the copied map's key type is the source graph's edge
deba@1531
   661
    /// type.  
deba@1531
   662
    template <typename TargetMap, typename SourceMap>
deba@1531
   663
    const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
deba@1531
   664
      copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
deba@1531
   665
      return *this;
deba@1531
   666
    }
deba@1531
   667
deba@1531
   668
    /// \brief Gives back the stored node references.
deba@1531
   669
    ///
deba@1531
   670
    /// Gives back the stored node references.
deba@1531
   671
    const NodeRefMap& nodeRef() const {
deba@1531
   672
      return nodeRefMap;
deba@1531
   673
    }
deba@1531
   674
deba@1531
   675
    /// \brief Gives back the stored edge references.
deba@1531
   676
    ///
deba@1531
   677
    /// Gives back the stored edge references.
deba@1531
   678
    const EdgeRefMap& edgeRef() const {
deba@1531
   679
      return edgeRefMap;
deba@1531
   680
    }
deba@1531
   681
deba@1981
   682
    void run() const {}
deba@1720
   683
deba@1531
   684
  private:
deba@1531
   685
    
deba@1531
   686
    const Source& source;
deba@1531
   687
    Target& target;
deba@1531
   688
deba@1531
   689
    NodeRefMap nodeRefMap;
deba@1531
   690
    EdgeRefMap edgeRefMap;
deba@1267
   691
  };
klao@946
   692
alpar@2006
   693
  /// \brief Copy a graph to another graph.
deba@1531
   694
  ///
alpar@2006
   695
  /// Copy a graph to another graph.
deba@1531
   696
  /// The usage of the function:
deba@1531
   697
  /// 
alpar@1946
   698
  ///\code
deba@1531
   699
  /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
alpar@1946
   700
  ///\endcode
deba@1531
   701
  /// 
deba@1531
   702
  /// After the copy the \c nr map will contain the mapping from the
deba@1531
   703
  /// source graph's nodes to the target graph's nodes and the \c ecr will
athos@1540
   704
  /// contain the mapping from the target graph's edges to the source's
deba@1531
   705
  /// edges.
deba@1531
   706
  template <typename Target, typename Source>
deba@1531
   707
  GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
deba@1531
   708
    return GraphCopy<Target, Source>(target, source);
deba@1531
   709
  }
klao@946
   710
deba@1720
   711
  /// \brief Class to copy an undirected graph.
deba@1720
   712
  ///
alpar@2006
   713
  /// Class to copy an undirected graph to another graph (duplicate a graph).
klao@1909
   714
  /// The simplest way of using it is through the \c copyUGraph() function.
deba@1720
   715
  template <typename Target, typename Source>
klao@1909
   716
  class UGraphCopy {
deba@1720
   717
  public: 
deba@1720
   718
    typedef typename Source::Node Node;
deba@1720
   719
    typedef typename Source::NodeIt NodeIt;
deba@1720
   720
    typedef typename Source::Edge Edge;
deba@1720
   721
    typedef typename Source::EdgeIt EdgeIt;
klao@1909
   722
    typedef typename Source::UEdge UEdge;
klao@1909
   723
    typedef typename Source::UEdgeIt UEdgeIt;
deba@1720
   724
deba@1720
   725
    typedef typename Source::
deba@1720
   726
    template NodeMap<typename Target::Node> NodeRefMap;
deba@1720
   727
    
deba@1720
   728
    typedef typename Source::
klao@1909
   729
    template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
deba@1720
   730
deba@1720
   731
  private:
deba@1720
   732
deba@1720
   733
    struct EdgeRefMap {
klao@1909
   734
      EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
deba@1720
   735
      typedef typename Source::Edge Key;
deba@1720
   736
      typedef typename Target::Edge Value;
deba@1720
   737
deba@1720
   738
      Value operator[](const Key& key) {
klao@1909
   739
	return gc.target.direct(gc.uEdgeRef[key], 
deba@1720
   740
				gc.target.direction(key));
deba@1720
   741
      }
deba@1720
   742
      
klao@1909
   743
      UGraphCopy& gc;
deba@1720
   744
    };
deba@1720
   745
    
deba@1192
   746
  public:
deba@1720
   747
klao@1909
   748
    /// \brief Constructor for the UGraphCopy.
deba@1720
   749
    ///
deba@1720
   750
    /// It copies the content of the \c _source graph into the
deba@1720
   751
    /// \c _target graph. It creates also two references, one beetween
deba@1720
   752
    /// the two nodeset and one beetween the two edgesets.
klao@1909
   753
    UGraphCopy(Target& _target, const Source& _source) 
deba@1720
   754
      : source(_source), target(_target), 
klao@1909
   755
	nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
deba@1720
   756
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1720
   757
	nodeRefMap[it] = target.addNode();
deba@1720
   758
      }
klao@1909
   759
      for (UEdgeIt it(source); it != INVALID; ++it) {
klao@1909
   760
	uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)], 
deba@1720
   761
					nodeRefMap[source.target(it)]);
deba@1720
   762
      }
deba@1720
   763
    }
deba@1720
   764
deba@1720
   765
    /// \brief Copies the node references into the given map.
deba@1720
   766
    ///
deba@1720
   767
    /// Copies the node references into the given map.
deba@1720
   768
    template <typename NodeRef>
klao@1909
   769
    const UGraphCopy& nodeRef(NodeRef& map) const {
deba@1720
   770
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1720
   771
	map.set(it, nodeRefMap[it]);
deba@1720
   772
      }
deba@1720
   773
      return *this;
deba@1720
   774
    }
deba@1720
   775
deba@1720
   776
    /// \brief Reverse and copies the node references into the given map.
deba@1720
   777
    ///
deba@1720
   778
    /// Reverse and copies the node references into the given map.
deba@1720
   779
    template <typename NodeRef>
klao@1909
   780
    const UGraphCopy& nodeCrossRef(NodeRef& map) const {
deba@1720
   781
      for (NodeIt it(source); it != INVALID; ++it) {
deba@1720
   782
	map.set(nodeRefMap[it], it);
deba@1720
   783
      }
deba@1720
   784
      return *this;
deba@1720
   785
    }
deba@1720
   786
deba@1720
   787
    /// \brief Copies the edge references into the given map.
deba@1720
   788
    ///
deba@1720
   789
    /// Copies the edge references into the given map.
deba@1720
   790
    template <typename EdgeRef>
klao@1909
   791
    const UGraphCopy& edgeRef(EdgeRef& map) const {
deba@1720
   792
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1720
   793
	map.set(edgeRefMap[it], it);
deba@1720
   794
      }
deba@1720
   795
      return *this;
deba@1720
   796
    }
deba@1720
   797
deba@1720
   798
    /// \brief Reverse and copies the undirected edge references into the 
deba@1720
   799
    /// given map.
deba@1720
   800
    ///
deba@1720
   801
    /// Reverse and copies the undirected edge references into the given map.
deba@1720
   802
    template <typename EdgeRef>
klao@1909
   803
    const UGraphCopy& edgeCrossRef(EdgeRef& map) const {
deba@1720
   804
      for (EdgeIt it(source); it != INVALID; ++it) {
deba@1720
   805
	map.set(it, edgeRefMap[it]);
deba@1720
   806
      }
deba@1720
   807
      return *this;
deba@1720
   808
    }
deba@1720
   809
deba@1720
   810
    /// \brief Copies the undirected edge references into the given map.
deba@1720
   811
    ///
deba@1720
   812
    /// Copies the undirected edge references into the given map.
deba@1720
   813
    template <typename EdgeRef>
klao@1909
   814
    const UGraphCopy& uEdgeRef(EdgeRef& map) const {
klao@1909
   815
      for (UEdgeIt it(source); it != INVALID; ++it) {
klao@1909
   816
	map.set(it, uEdgeRefMap[it]);
deba@1720
   817
      }
deba@1720
   818
      return *this;
deba@1720
   819
    }
deba@1720
   820
deba@1720
   821
    /// \brief Reverse and copies the undirected edge references into the 
deba@1720
   822
    /// given map.
deba@1720
   823
    ///
deba@1720
   824
    /// Reverse and copies the undirected edge references into the given map.
deba@1720
   825
    template <typename EdgeRef>
klao@1909
   826
    const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
klao@1909
   827
      for (UEdgeIt it(source); it != INVALID; ++it) {
klao@1909
   828
	map.set(uEdgeRefMap[it], it);
deba@1720
   829
      }
deba@1720
   830
      return *this;
deba@1720
   831
    }
deba@1720
   832
deba@1720
   833
    /// \brief Make copy of the given map.
deba@1720
   834
    ///
deba@1720
   835
    /// Makes copy of the given map for the newly created graph. 
deba@1720
   836
    /// The new map's key type is the target graph's node type,
deba@1720
   837
    /// and the copied map's key type is the source graph's node
deba@1720
   838
    /// type.  
deba@1720
   839
    template <typename TargetMap, typename SourceMap>
klao@1909
   840
    const UGraphCopy& nodeMap(TargetMap& tMap, 
deba@1720
   841
				  const SourceMap& sMap) const {
deba@1720
   842
      copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
deba@1720
   843
      return *this;
deba@1720
   844
    }
deba@1720
   845
deba@1720
   846
    /// \brief Make copy of the given map.
deba@1720
   847
    ///
deba@1720
   848
    /// Makes copy of the given map for the newly created graph. 
deba@1720
   849
    /// The new map's key type is the target graph's edge type,
deba@1720
   850
    /// and the copied map's key type is the source graph's edge
deba@1720
   851
    /// type.  
deba@1720
   852
    template <typename TargetMap, typename SourceMap>
klao@1909
   853
    const UGraphCopy& edgeMap(TargetMap& tMap, 
deba@1720
   854
				  const SourceMap& sMap) const {
deba@1720
   855
      copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
deba@1720
   856
      return *this;
deba@1720
   857
    }
deba@1720
   858
deba@1720
   859
    /// \brief Make copy of the given map.
deba@1720
   860
    ///
deba@1720
   861
    /// Makes copy of the given map for the newly created graph. 
deba@1720
   862
    /// The new map's key type is the target graph's edge type,
deba@1720
   863
    /// and the copied map's key type is the source graph's edge
deba@1720
   864
    /// type.  
deba@1720
   865
    template <typename TargetMap, typename SourceMap>
klao@1909
   866
    const UGraphCopy& uEdgeMap(TargetMap& tMap, 
deba@1720
   867
				  const SourceMap& sMap) const {
klao@1909
   868
      copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
deba@1720
   869
      return *this;
deba@1720
   870
    }
deba@1720
   871
deba@1720
   872
    /// \brief Gives back the stored node references.
deba@1720
   873
    ///
deba@1720
   874
    /// Gives back the stored node references.
deba@1720
   875
    const NodeRefMap& nodeRef() const {
deba@1720
   876
      return nodeRefMap;
deba@1720
   877
    }
deba@1720
   878
deba@1720
   879
    /// \brief Gives back the stored edge references.
deba@1720
   880
    ///
deba@1720
   881
    /// Gives back the stored edge references.
deba@1720
   882
    const EdgeRefMap& edgeRef() const {
deba@1720
   883
      return edgeRefMap;
deba@1720
   884
    }
deba@1720
   885
klao@1909
   886
    /// \brief Gives back the stored uedge references.
deba@1720
   887
    ///
klao@1909
   888
    /// Gives back the stored uedge references.
klao@1909
   889
    const UEdgeRefMap& uEdgeRef() const {
klao@1909
   890
      return uEdgeRefMap;
deba@1720
   891
    }
deba@1720
   892
deba@1981
   893
    void run() const {}
deba@1720
   894
deba@1720
   895
  private:
deba@1192
   896
    
deba@1720
   897
    const Source& source;
deba@1720
   898
    Target& target;
alpar@947
   899
deba@1720
   900
    NodeRefMap nodeRefMap;
deba@1720
   901
    EdgeRefMap edgeRefMap;
klao@1909
   902
    UEdgeRefMap uEdgeRefMap;
deba@1192
   903
  };
deba@1192
   904
alpar@2006
   905
  /// \brief Copy a graph to another graph.
deba@1720
   906
  ///
alpar@2006
   907
  /// Copy a graph to another graph.
deba@1720
   908
  /// The usage of the function:
deba@1720
   909
  /// 
alpar@1946
   910
  ///\code
alpar@2022
   911
  /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
alpar@1946
   912
  ///\endcode
deba@1720
   913
  /// 
deba@1720
   914
  /// After the copy the \c nr map will contain the mapping from the
deba@1720
   915
  /// source graph's nodes to the target graph's nodes and the \c ecr will
deba@1720
   916
  /// contain the mapping from the target graph's edges to the source's
deba@1720
   917
  /// edges.
deba@1720
   918
  template <typename Target, typename Source>
klao@1909
   919
  UGraphCopy<Target, Source> 
klao@1909
   920
  copyUGraph(Target& target, const Source& source) {
klao@1909
   921
    return UGraphCopy<Target, Source>(target, source);
deba@1720
   922
  }
deba@1192
   923
deba@1192
   924
deba@1192
   925
  /// @}
alpar@1402
   926
alpar@1402
   927
  /// \addtogroup graph_maps
alpar@1402
   928
  /// @{
alpar@1402
   929
deba@1413
   930
  /// Provides an immutable and unique id for each item in the graph.
deba@1413
   931
athos@1540
   932
  /// The IdMap class provides a unique and immutable id for each item of the
athos@1540
   933
  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
athos@1540
   934
  /// different items (nodes) get different ids <li>\b immutable: the id of an
athos@1540
   935
  /// item (node) does not change (even if you delete other nodes).  </ul>
athos@1540
   936
  /// Through this map you get access (i.e. can read) the inner id values of
athos@1540
   937
  /// the items stored in the graph. This map can be inverted with its member
athos@1540
   938
  /// class \c InverseMap.
deba@1413
   939
  ///
deba@1413
   940
  template <typename _Graph, typename _Item>
deba@1413
   941
  class IdMap {
deba@1413
   942
  public:
deba@1413
   943
    typedef _Graph Graph;
deba@1413
   944
    typedef int Value;
deba@1413
   945
    typedef _Item Item;
deba@1413
   946
    typedef _Item Key;
deba@1413
   947
deba@1413
   948
    /// \brief Constructor.
deba@1413
   949
    ///
deba@1413
   950
    /// Constructor for creating id map.
deba@1413
   951
    IdMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   952
deba@1413
   953
    /// \brief Gives back the \e id of the item.
deba@1413
   954
    ///
deba@1413
   955
    /// Gives back the immutable and unique \e id of the map.
deba@1413
   956
    int operator[](const Item& item) const { return graph->id(item);}
deba@1413
   957
deba@1413
   958
deba@1413
   959
  private:
deba@1413
   960
    const Graph* graph;
deba@1413
   961
deba@1413
   962
  public:
deba@1413
   963
athos@1540
   964
    /// \brief The class represents the inverse of its owner (IdMap).
deba@1413
   965
    ///
athos@1540
   966
    /// The class represents the inverse of its owner (IdMap).
deba@1413
   967
    /// \see inverse()
deba@1413
   968
    class InverseMap {
deba@1413
   969
    public:
deba@1419
   970
deba@1413
   971
      /// \brief Constructor.
deba@1413
   972
      ///
deba@1413
   973
      /// Constructor for creating an id-to-item map.
deba@1413
   974
      InverseMap(const Graph& _graph) : graph(&_graph) {}
deba@1413
   975
deba@1413
   976
      /// \brief Constructor.
deba@1413
   977
      ///
deba@1413
   978
      /// Constructor for creating an id-to-item map.
deba@1413
   979
      InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
deba@1413
   980
deba@1413
   981
      /// \brief Gives back the given item from its id.
deba@1413
   982
      ///
deba@1413
   983
      /// Gives back the given item from its id.
deba@1413
   984
      /// 
deba@1413
   985
      Item operator[](int id) const { return graph->fromId(id, Item());}
deba@1413
   986
    private:
deba@1413
   987
      const Graph* graph;
deba@1413
   988
    };
deba@1413
   989
deba@1413
   990
    /// \brief Gives back the inverse of the map.
deba@1413
   991
    ///
athos@1540
   992
    /// Gives back the inverse of the IdMap.
deba@1413
   993
    InverseMap inverse() const { return InverseMap(*graph);} 
deba@1413
   994
deba@1413
   995
  };
deba@1413
   996
deba@1413
   997
  
athos@1526
   998
  /// \brief General invertable graph-map type.
alpar@1402
   999
athos@1540
  1000
  /// This type provides simple invertable graph-maps. 
athos@1526
  1001
  /// The InvertableMap wraps an arbitrary ReadWriteMap 
athos@1526
  1002
  /// and if a key is set to a new value then store it
alpar@1402
  1003
  /// in the inverse map.
deba@1931
  1004
  ///
deba@1931
  1005
  /// The values of the map can be accessed
deba@1931
  1006
  /// with stl compatible forward iterator.
deba@1931
  1007
  ///
alpar@1402
  1008
  /// \param _Graph The graph type.
deba@1830
  1009
  /// \param _Item The item type of the graph.
deba@1830
  1010
  /// \param _Value The value type of the map.
deba@1931
  1011
  ///
deba@1931
  1012
  /// \see IterableValueMap
deba@1830
  1013
#ifndef DOXYGEN
deba@1830
  1014
  /// \param _Map A ReadWriteMap mapping from the item type to integer.
alpar@1402
  1015
  template <
deba@1990
  1016
    typename _Graph, typename _Item, typename _Value, 
deba@1990
  1017
    typename _Map = DefaultMap<_Graph, _Item, _Value>
alpar@1402
  1018
  >
deba@1830
  1019
#else
deba@1830
  1020
  template <typename _Graph, typename _Item, typename _Value>
deba@1830
  1021
#endif
deba@1413
  1022
  class InvertableMap : protected _Map {
alpar@1402
  1023
  public:
deba@1413
  1024
klao@1909
  1025
    /// The key type of InvertableMap (Node, Edge, UEdge).
alpar@1402
  1026
    typedef typename _Map::Key Key;
deba@1413
  1027
    /// The value type of the InvertableMap.
alpar@1402
  1028
    typedef typename _Map::Value Value;
alpar@1402
  1029
deba@1931
  1030
  private:
deba@1931
  1031
    
deba@1931
  1032
    typedef _Map Map;
deba@1931
  1033
    typedef _Graph Graph;
deba@1931
  1034
deba@1931
  1035
    typedef std::map<Value, Key> Container;
deba@1931
  1036
    Container invMap;    
deba@1931
  1037
deba@1931
  1038
  public:
deba@1931
  1039
 
deba@1931
  1040
deba@1931
  1041
alpar@1402
  1042
    /// \brief Constructor.
alpar@1402
  1043
    ///
deba@1413
  1044
    /// Construct a new InvertableMap for the graph.
alpar@1402
  1045
    ///
deba@1413
  1046
    InvertableMap(const Graph& graph) : Map(graph) {} 
deba@1931
  1047
deba@1931
  1048
    /// \brief Forward iterator for values.
deba@1931
  1049
    ///
deba@1931
  1050
    /// This iterator is an stl compatible forward
deba@1931
  1051
    /// iterator on the values of the map. The values can
deba@1931
  1052
    /// be accessed in the [beginValue, endValue) range.
deba@1931
  1053
    ///
deba@1931
  1054
    class ValueIterator 
deba@1931
  1055
      : public std::iterator<std::forward_iterator_tag, Value> {
deba@1931
  1056
      friend class InvertableMap;
deba@1931
  1057
    private:
deba@1931
  1058
      ValueIterator(typename Container::const_iterator _it) 
deba@1931
  1059
        : it(_it) {}
deba@1931
  1060
    public:
deba@1931
  1061
      
deba@1931
  1062
      ValueIterator() {}
deba@1931
  1063
deba@1931
  1064
      ValueIterator& operator++() { ++it; return *this; }
deba@1931
  1065
      ValueIterator operator++(int) { 
deba@1931
  1066
        ValueIterator tmp(*this); 
deba@1931
  1067
        operator++();
deba@1931
  1068
        return tmp; 
deba@1931
  1069
      }
deba@1931
  1070
deba@1931
  1071
      const Value& operator*() const { return it->first; }
deba@1931
  1072
      const Value* operator->() const { return &(it->first); }
deba@1931
  1073
deba@1931
  1074
      bool operator==(ValueIterator jt) const { return it == jt.it; }
deba@1931
  1075
      bool operator!=(ValueIterator jt) const { return it != jt.it; }
deba@1931
  1076
      
deba@1931
  1077
    private:
deba@1931
  1078
      typename Container::const_iterator it;
deba@1931
  1079
    };
deba@1931
  1080
deba@1931
  1081
    /// \brief Returns an iterator to the first value.
deba@1931
  1082
    ///
deba@1931
  1083
    /// Returns an stl compatible iterator to the 
deba@1931
  1084
    /// first value of the map. The values of the
deba@1931
  1085
    /// map can be accessed in the [beginValue, endValue)
deba@1931
  1086
    /// range.
deba@1931
  1087
    ValueIterator beginValue() const {
deba@1931
  1088
      return ValueIterator(invMap.begin());
deba@1931
  1089
    }
deba@1931
  1090
deba@1931
  1091
    /// \brief Returns an iterator after the last value.
deba@1931
  1092
    ///
deba@1931
  1093
    /// Returns an stl compatible iterator after the 
deba@1931
  1094
    /// last value of the map. The values of the
deba@1931
  1095
    /// map can be accessed in the [beginValue, endValue)
deba@1931
  1096
    /// range.
deba@1931
  1097
    ValueIterator endValue() const {
deba@1931
  1098
      return ValueIterator(invMap.end());
deba@1931
  1099
    }
alpar@1402
  1100
    
alpar@1402
  1101
    /// \brief The setter function of the map.
alpar@1402
  1102
    ///
deba@1413
  1103
    /// Sets the mapped value.
alpar@1402
  1104
    void set(const Key& key, const Value& val) {
alpar@1402
  1105
      Value oldval = Map::operator[](key);
deba@1413
  1106
      typename Container::iterator it = invMap.find(oldval);
alpar@1402
  1107
      if (it != invMap.end() && it->second == key) {
alpar@1402
  1108
	invMap.erase(it);
alpar@1402
  1109
      }      
alpar@1402
  1110
      invMap.insert(make_pair(val, key));
alpar@1402
  1111
      Map::set(key, val);
alpar@1402
  1112
    }
alpar@1402
  1113
alpar@1402
  1114
    /// \brief The getter function of the map.
alpar@1402
  1115
    ///
alpar@1402
  1116
    /// It gives back the value associated with the key.
deba@1931
  1117
    typename MapTraits<Map>::ConstReturnValue 
deba@1931
  1118
    operator[](const Key& key) const {
alpar@1402
  1119
      return Map::operator[](key);
alpar@1402
  1120
    }
alpar@1402
  1121
deba@1515
  1122
  protected:
deba@1515
  1123
alpar@1402
  1124
    /// \brief Erase the key from the map.
alpar@1402
  1125
    ///
alpar@1402
  1126
    /// Erase the key to the map. It is called by the
alpar@1402
  1127
    /// \c AlterationNotifier.
alpar@1402
  1128
    virtual void erase(const Key& key) {
alpar@1402
  1129
      Value val = Map::operator[](key);
deba@1413
  1130
      typename Container::iterator it = invMap.find(val);
alpar@1402
  1131
      if (it != invMap.end() && it->second == key) {
alpar@1402
  1132
	invMap.erase(it);
alpar@1402
  1133
      }
alpar@1402
  1134
      Map::erase(key);
alpar@1402
  1135
    }
alpar@1402
  1136
deba@1829
  1137
    /// \brief Erase more keys from the map.
deba@1829
  1138
    ///
deba@1829
  1139
    /// Erase more keys from the map. It is called by the
deba@1829
  1140
    /// \c AlterationNotifier.
deba@1829
  1141
    virtual void erase(const std::vector<Key>& keys) {
deba@1829
  1142
      for (int i = 0; i < (int)keys.size(); ++i) {
deba@1829
  1143
	Value val = Map::operator[](keys[i]);
deba@1829
  1144
	typename Container::iterator it = invMap.find(val);
deba@1829
  1145
	if (it != invMap.end() && it->second == keys[i]) {
deba@1829
  1146
	  invMap.erase(it);
deba@1829
  1147
	}
deba@1829
  1148
      }
deba@1829
  1149
      Map::erase(keys);
deba@1829
  1150
    }
deba@1829
  1151
alpar@1402
  1152
    /// \brief Clear the keys from the map and inverse map.
alpar@1402
  1153
    ///
alpar@1402
  1154
    /// Clear the keys from the map and inverse map. It is called by the
alpar@1402
  1155
    /// \c AlterationNotifier.
alpar@1402
  1156
    virtual void clear() {
alpar@1402
  1157
      invMap.clear();
alpar@1402
  1158
      Map::clear();
alpar@1402
  1159
    }
alpar@1402
  1160
deba@1413
  1161
  public:
deba@1413
  1162
deba@1413
  1163
    /// \brief The inverse map type.
deba@1413
  1164
    ///
deba@1413
  1165
    /// The inverse of this map. The subscript operator of the map
deba@1413
  1166
    /// gives back always the item what was last assigned to the value. 
deba@1413
  1167
    class InverseMap {
deba@1413
  1168
    public:
deba@1413
  1169
      /// \brief Constructor of the InverseMap.
deba@1413
  1170
      ///
deba@1413
  1171
      /// Constructor of the InverseMap.
deba@1413
  1172
      InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
deba@1413
  1173
deba@1413
  1174
      /// The value type of the InverseMap.
deba@1413
  1175
      typedef typename InvertableMap::Key Value;
deba@1413
  1176
      /// The key type of the InverseMap.
deba@1413
  1177
      typedef typename InvertableMap::Value Key; 
deba@1413
  1178
deba@1413
  1179
      /// \brief Subscript operator. 
deba@1413
  1180
      ///
deba@1413
  1181
      /// Subscript operator. It gives back always the item 
deba@1413
  1182
      /// what was last assigned to the value.
deba@1413
  1183
      Value operator[](const Key& key) const {
deba@1413
  1184
	typename Container::const_iterator it = inverted.invMap.find(key);
deba@1413
  1185
	return it->second;
deba@1413
  1186
      }
deba@1413
  1187
      
deba@1413
  1188
    private:
deba@1413
  1189
      const InvertableMap& inverted;
deba@1413
  1190
    };
deba@1413
  1191
alpar@1402
  1192
    /// \brief It gives back the just readeable inverse map.
alpar@1402
  1193
    ///
alpar@1402
  1194
    /// It gives back the just readeable inverse map.
deba@1413
  1195
    InverseMap inverse() const {
deba@1413
  1196
      return InverseMap(*this);
alpar@1402
  1197
    } 
alpar@1402
  1198
alpar@1402
  1199
deba@1413
  1200
    
alpar@1402
  1201
  };
alpar@1402
  1202
alpar@1402
  1203
  /// \brief Provides a mutable, continuous and unique descriptor for each 
alpar@1402
  1204
  /// item in the graph.
alpar@1402
  1205
  ///
athos@1540
  1206
  /// The DescriptorMap class provides a unique and continuous (but mutable)
athos@1540
  1207
  /// descriptor (id) for each item of the same type (e.g. node) in the
athos@1540
  1208
  /// graph. This id is <ul><li>\b unique: different items (nodes) get
athos@1540
  1209
  /// different ids <li>\b continuous: the range of the ids is the set of
athos@1540
  1210
  /// integers between 0 and \c n-1, where \c n is the number of the items of
athos@1540
  1211
  /// this type (e.g. nodes) (so the id of a node can change if you delete an
athos@1540
  1212
  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
athos@1540
  1213
  /// with its member class \c InverseMap.
alpar@1402
  1214
  ///
alpar@1402
  1215
  /// \param _Graph The graph class the \c DescriptorMap belongs to.
alpar@1402
  1216
  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or 
klao@1909
  1217
  /// UEdge.
deba@1830
  1218
#ifndef DOXYGEN
alpar@1402
  1219
  /// \param _Map A ReadWriteMap mapping from the item type to integer.
alpar@1402
  1220
  template <
deba@1990
  1221
    typename _Graph, typename _Item,
deba@1990
  1222
    typename _Map = DefaultMap<_Graph, _Item, int>
alpar@1402
  1223
  >
deba@1830
  1224
#else
deba@1830
  1225
  template <typename _Graph, typename _Item>
deba@1830
  1226
#endif
alpar@1402
  1227
  class DescriptorMap : protected _Map {
alpar@1402
  1228
alpar@1402
  1229
    typedef _Item Item;
alpar@1402
  1230
    typedef _Map Map;
alpar@1402
  1231
alpar@1402
  1232
  public:
alpar@1402
  1233
    /// The graph class of DescriptorMap.
alpar@1402
  1234
    typedef _Graph Graph;
alpar@1402
  1235
klao@1909
  1236
    /// The key type of DescriptorMap (Node, Edge, UEdge).
alpar@1402
  1237
    typedef typename _Map::Key Key;
alpar@1402
  1238
    /// The value type of DescriptorMap.
alpar@1402
  1239
    typedef typename _Map::Value Value;
alpar@1402
  1240
alpar@1402
  1241
    /// \brief Constructor.
alpar@1402
  1242
    ///
deba@1413
  1243
    /// Constructor for descriptor map.
alpar@1402
  1244
    DescriptorMap(const Graph& _graph) : Map(_graph) {
alpar@1402
  1245
      build();
alpar@1402
  1246
    }
alpar@1402
  1247
deba@1515
  1248
  protected:
deba@1515
  1249
alpar@1402
  1250
    /// \brief Add a new key to the map.
alpar@1402
  1251
    ///
alpar@1402
  1252
    /// Add a new key to the map. It is called by the
alpar@1402
  1253
    /// \c AlterationNotifier.
alpar@1402
  1254
    virtual void add(const Item& item) {
alpar@1402
  1255
      Map::add(item);
alpar@1402
  1256
      Map::set(item, invMap.size());
alpar@1402
  1257
      invMap.push_back(item);
alpar@1402
  1258
    }
alpar@1402
  1259
deba@1829
  1260
    /// \brief Add more new keys to the map.
deba@1829
  1261
    ///
deba@1829
  1262
    /// Add more new keys to the map. It is called by the
deba@1829
  1263
    /// \c AlterationNotifier.
deba@1829
  1264
    virtual void add(const std::vector<Item>& items) {
deba@1829
  1265
      Map::add(items);
deba@1829
  1266
      for (int i = 0; i < (int)items.size(); ++i) {
deba@1829
  1267
	Map::set(items[i], invMap.size());
deba@1829
  1268
	invMap.push_back(items[i]);
deba@1829
  1269
      }
deba@1829
  1270
    }
deba@1829
  1271
alpar@1402
  1272
    /// \brief Erase the key from the map.
alpar@1402
  1273
    ///
deba@1829
  1274
    /// Erase the key from the map. It is called by the
alpar@1402
  1275
    /// \c AlterationNotifier.
alpar@1402
  1276
    virtual void erase(const Item& item) {
alpar@1402
  1277
      Map::set(invMap.back(), Map::operator[](item));
alpar@1402
  1278
      invMap[Map::operator[](item)] = invMap.back();
deba@1413
  1279
      invMap.pop_back();
alpar@1402
  1280
      Map::erase(item);
alpar@1402
  1281
    }
alpar@1402
  1282
deba@1829
  1283
    /// \brief Erase more keys from the map.
deba@1829
  1284
    ///
deba@1829
  1285
    /// Erase more keys from the map. It is called by the
deba@1829
  1286
    /// \c AlterationNotifier.
deba@1829
  1287
    virtual void erase(const std::vector<Item>& items) {
deba@1829
  1288
      for (int i = 0; i < (int)items.size(); ++i) {
deba@1829
  1289
	Map::set(invMap.back(), Map::operator[](items[i]));
deba@1829
  1290
	invMap[Map::operator[](items[i])] = invMap.back();
deba@1829
  1291
	invMap.pop_back();
deba@1829
  1292
      }
deba@1829
  1293
      Map::erase(items);
deba@1829
  1294
    }
deba@1829
  1295
alpar@1402
  1296
    /// \brief Build the unique map.
alpar@1402
  1297
    ///
alpar@1402
  1298
    /// Build the unique map. It is called by the
alpar@1402
  1299
    /// \c AlterationNotifier.
alpar@1402
  1300
    virtual void build() {
alpar@1402
  1301
      Map::build();
alpar@1402
  1302
      Item it;
deba@1999
  1303
      const typename Map::Notifier* notifier = Map::getNotifier(); 
deba@1999
  1304
      for (notifier->first(it); it != INVALID; notifier->next(it)) {
alpar@1402
  1305
	Map::set(it, invMap.size());
alpar@1402
  1306
	invMap.push_back(it);	
alpar@1402
  1307
      }      
alpar@1402
  1308
    }
alpar@1402
  1309
    
alpar@1402
  1310
    /// \brief Clear the keys from the map.
alpar@1402
  1311
    ///
alpar@1402
  1312
    /// Clear the keys from the map. It is called by the
alpar@1402
  1313
    /// \c AlterationNotifier.
alpar@1402
  1314
    virtual void clear() {
alpar@1402
  1315
      invMap.clear();
alpar@1402
  1316
      Map::clear();
alpar@1402
  1317
    }
alpar@1402
  1318
deba@1538
  1319
  public:
deba@1538
  1320
deba@1931
  1321
    /// \brief Returns the maximal value plus one.
deba@1931
  1322
    ///
deba@1931
  1323
    /// Returns the maximal value plus one in the map.
deba@1931
  1324
    unsigned int size() const {
deba@1931
  1325
      return invMap.size();
deba@1931
  1326
    }
deba@1931
  1327
deba@1552
  1328
    /// \brief Swaps the position of the two items in the map.
deba@1552
  1329
    ///
deba@1552
  1330
    /// Swaps the position of the two items in the map.
deba@1552
  1331
    void swap(const Item& p, const Item& q) {
deba@1552
  1332
      int pi = Map::operator[](p);
deba@1552
  1333
      int qi = Map::operator[](q);
deba@1552
  1334
      Map::set(p, qi);
deba@1552
  1335
      invMap[qi] = p;
deba@1552
  1336
      Map::set(q, pi);
deba@1552
  1337
      invMap[pi] = q;
deba@1552
  1338
    }
deba@1552
  1339
alpar@1402
  1340
    /// \brief Gives back the \e descriptor of the item.
alpar@1402
  1341
    ///
alpar@1402
  1342
    /// Gives back the mutable and unique \e descriptor of the map.
alpar@1402
  1343
    int operator[](const Item& item) const {
alpar@1402
  1344
      return Map::operator[](item);
alpar@1402
  1345
    }
alpar@1402
  1346
    
deba@1413
  1347
  private:
deba@1413
  1348
deba@1413
  1349
    typedef std::vector<Item> Container;
deba@1413
  1350
    Container invMap;
deba@1413
  1351
deba@1413
  1352
  public:
athos@1540
  1353
    /// \brief The inverse map type of DescriptorMap.
deba@1413
  1354
    ///
athos@1540
  1355
    /// The inverse map type of DescriptorMap.
deba@1413
  1356
    class InverseMap {
deba@1413
  1357
    public:
deba@1413
  1358
      /// \brief Constructor of the InverseMap.
deba@1413
  1359
      ///
deba@1413
  1360
      /// Constructor of the InverseMap.
deba@1413
  1361
      InverseMap(const DescriptorMap& _inverted) 
deba@1413
  1362
	: inverted(_inverted) {}
deba@1413
  1363
deba@1413
  1364
deba@1413
  1365
      /// The value type of the InverseMap.
deba@1413
  1366
      typedef typename DescriptorMap::Key Value;
deba@1413
  1367
      /// The key type of the InverseMap.
deba@1413
  1368
      typedef typename DescriptorMap::Value Key; 
deba@1413
  1369
deba@1413
  1370
      /// \brief Subscript operator. 
deba@1413
  1371
      ///
deba@1413
  1372
      /// Subscript operator. It gives back the item 
deba@1413
  1373
      /// that the descriptor belongs to currently.
deba@1413
  1374
      Value operator[](const Key& key) const {
deba@1413
  1375
	return inverted.invMap[key];
deba@1413
  1376
      }
deba@1470
  1377
deba@1470
  1378
      /// \brief Size of the map.
deba@1470
  1379
      ///
deba@1470
  1380
      /// Returns the size of the map.
deba@1931
  1381
      unsigned int size() const {
deba@1470
  1382
	return inverted.invMap.size();
deba@1470
  1383
      }
deba@1413
  1384
      
deba@1413
  1385
    private:
deba@1413
  1386
      const DescriptorMap& inverted;
deba@1413
  1387
    };
deba@1413
  1388
alpar@1402
  1389
    /// \brief Gives back the inverse of the map.
alpar@1402
  1390
    ///
alpar@1402
  1391
    /// Gives back the inverse of the map.
alpar@1402
  1392
    const InverseMap inverse() const {
deba@1413
  1393
      return InverseMap(*this);
alpar@1402
  1394
    }
alpar@1402
  1395
  };
alpar@1402
  1396
alpar@1402
  1397
  /// \brief Returns the source of the given edge.
alpar@1402
  1398
  ///
alpar@1402
  1399
  /// The SourceMap gives back the source Node of the given edge. 
alpar@1402
  1400
  /// \author Balazs Dezso
alpar@1402
  1401
  template <typename Graph>
alpar@1402
  1402
  class SourceMap {
alpar@1402
  1403
  public:
deba@1419
  1404
alpar@1402
  1405
    typedef typename Graph::Node Value;
alpar@1402
  1406
    typedef typename Graph::Edge Key;
alpar@1402
  1407
alpar@1402
  1408
    /// \brief Constructor
alpar@1402
  1409
    ///
alpar@1402
  1410
    /// Constructor
alpar@1402
  1411
    /// \param _graph The graph that the map belongs to.
alpar@1402
  1412
    SourceMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  1413
alpar@1402
  1414
    /// \brief The subscript operator.
alpar@1402
  1415
    ///
alpar@1402
  1416
    /// The subscript operator.
alpar@1402
  1417
    /// \param edge The edge 
alpar@1402
  1418
    /// \return The source of the edge 
deba@1679
  1419
    Value operator[](const Key& edge) const {
alpar@1402
  1420
      return graph.source(edge);
alpar@1402
  1421
    }
alpar@1402
  1422
alpar@1402
  1423
  private:
alpar@1402
  1424
    const Graph& graph;
alpar@1402
  1425
  };
alpar@1402
  1426
alpar@1402
  1427
  /// \brief Returns a \ref SourceMap class
alpar@1402
  1428
  ///
alpar@1402
  1429
  /// This function just returns an \ref SourceMap class.
alpar@1402
  1430
  /// \relates SourceMap
alpar@1402
  1431
  template <typename Graph>
alpar@1402
  1432
  inline SourceMap<Graph> sourceMap(const Graph& graph) {
alpar@1402
  1433
    return SourceMap<Graph>(graph);
alpar@1402
  1434
  } 
alpar@1402
  1435
alpar@1402
  1436
  /// \brief Returns the target of the given edge.
alpar@1402
  1437
  ///
alpar@1402
  1438
  /// The TargetMap gives back the target Node of the given edge. 
alpar@1402
  1439
  /// \author Balazs Dezso
alpar@1402
  1440
  template <typename Graph>
alpar@1402
  1441
  class TargetMap {
alpar@1402
  1442
  public:
deba@1419
  1443
alpar@1402
  1444
    typedef typename Graph::Node Value;
alpar@1402
  1445
    typedef typename Graph::Edge Key;
alpar@1402
  1446
alpar@1402
  1447
    /// \brief Constructor
alpar@1402
  1448
    ///
alpar@1402
  1449
    /// Constructor
alpar@1402
  1450
    /// \param _graph The graph that the map belongs to.
alpar@1402
  1451
    TargetMap(const Graph& _graph) : graph(_graph) {}
alpar@1402
  1452
alpar@1402
  1453
    /// \brief The subscript operator.
alpar@1402
  1454
    ///
alpar@1402
  1455
    /// The subscript operator.
alpar@1536
  1456
    /// \param e The edge 
alpar@1402
  1457
    /// \return The target of the edge 
deba@1679
  1458
    Value operator[](const Key& e) const {
alpar@1536
  1459
      return graph.target(e);
alpar@1402
  1460
    }
alpar@1402
  1461
alpar@1402
  1462
  private:
alpar@1402
  1463
    const Graph& graph;
alpar@1402
  1464
  };
alpar@1402
  1465
alpar@1402
  1466
  /// \brief Returns a \ref TargetMap class
deba@1515
  1467
  ///
athos@1540
  1468
  /// This function just returns a \ref TargetMap class.
alpar@1402
  1469
  /// \relates TargetMap
alpar@1402
  1470
  template <typename Graph>
alpar@1402
  1471
  inline TargetMap<Graph> targetMap(const Graph& graph) {
alpar@1402
  1472
    return TargetMap<Graph>(graph);
alpar@1402
  1473
  }
alpar@1402
  1474
athos@1540
  1475
  /// \brief Returns the "forward" directed edge view of an undirected edge.
deba@1419
  1476
  ///
athos@1540
  1477
  /// Returns the "forward" directed edge view of an undirected edge.
deba@1419
  1478
  /// \author Balazs Dezso
deba@1419
  1479
  template <typename Graph>
deba@1419
  1480
  class ForwardMap {
deba@1419
  1481
  public:
deba@1419
  1482
deba@1419
  1483
    typedef typename Graph::Edge Value;
klao@1909
  1484
    typedef typename Graph::UEdge Key;
deba@1419
  1485
deba@1419
  1486
    /// \brief Constructor
deba@1419
  1487
    ///
deba@1419
  1488
    /// Constructor
deba@1419
  1489
    /// \param _graph The graph that the map belongs to.
deba@1419
  1490
    ForwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  1491
deba@1419
  1492
    /// \brief The subscript operator.
deba@1419
  1493
    ///
deba@1419
  1494
    /// The subscript operator.
deba@1419
  1495
    /// \param key An undirected edge 
deba@1419
  1496
    /// \return The "forward" directed edge view of undirected edge 
deba@1419
  1497
    Value operator[](const Key& key) const {
deba@1627
  1498
      return graph.direct(key, true);
deba@1419
  1499
    }
deba@1419
  1500
deba@1419
  1501
  private:
deba@1419
  1502
    const Graph& graph;
deba@1419
  1503
  };
deba@1419
  1504
deba@1419
  1505
  /// \brief Returns a \ref ForwardMap class
deba@1515
  1506
  ///
deba@1419
  1507
  /// This function just returns an \ref ForwardMap class.
deba@1419
  1508
  /// \relates ForwardMap
deba@1419
  1509
  template <typename Graph>
deba@1419
  1510
  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
deba@1419
  1511
    return ForwardMap<Graph>(graph);
deba@1419
  1512
  }
deba@1419
  1513
athos@1540
  1514
  /// \brief Returns the "backward" directed edge view of an undirected edge.
deba@1419
  1515
  ///
athos@1540
  1516
  /// Returns the "backward" directed edge view of an undirected edge.
deba@1419
  1517
  /// \author Balazs Dezso
deba@1419
  1518
  template <typename Graph>
deba@1419
  1519
  class BackwardMap {
deba@1419
  1520
  public:
deba@1419
  1521
deba@1419
  1522
    typedef typename Graph::Edge Value;
klao@1909
  1523
    typedef typename Graph::UEdge Key;
deba@1419
  1524
deba@1419
  1525
    /// \brief Constructor
deba@1419
  1526
    ///
deba@1419
  1527
    /// Constructor
deba@1419
  1528
    /// \param _graph The graph that the map belongs to.
deba@1419
  1529
    BackwardMap(const Graph& _graph) : graph(_graph) {}
deba@1419
  1530
deba@1419
  1531
    /// \brief The subscript operator.
deba@1419
  1532
    ///
deba@1419
  1533
    /// The subscript operator.
deba@1419
  1534
    /// \param key An undirected edge 
deba@1419
  1535
    /// \return The "backward" directed edge view of undirected edge 
deba@1419
  1536
    Value operator[](const Key& key) const {
deba@1627
  1537
      return graph.direct(key, false);
deba@1419
  1538
    }
deba@1419
  1539
deba@1419
  1540
  private:
deba@1419
  1541
    const Graph& graph;
deba@1419
  1542
  };
deba@1419
  1543
deba@1419
  1544
  /// \brief Returns a \ref BackwardMap class
deba@1419
  1545
athos@1540
  1546
  /// This function just returns a \ref BackwardMap class.
deba@1419
  1547
  /// \relates BackwardMap
deba@1419
  1548
  template <typename Graph>
deba@1419
  1549
  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
deba@1419
  1550
    return BackwardMap<Graph>(graph);
deba@1419
  1551
  }
deba@1419
  1552
deba@1695
  1553
  /// \brief Potential difference map
deba@1695
  1554
  ///
deba@1695
  1555
  /// If there is an potential map on the nodes then we
deba@1695
  1556
  /// can get an edge map as we get the substraction of the
deba@1695
  1557
  /// values of the target and source.
deba@1695
  1558
  template <typename Graph, typename NodeMap>
deba@1695
  1559
  class PotentialDifferenceMap {
deba@1515
  1560
  public:
deba@1695
  1561
    typedef typename Graph::Edge Key;
deba@1695
  1562
    typedef typename NodeMap::Value Value;
deba@1695
  1563
deba@1695
  1564
    /// \brief Constructor
deba@1695
  1565
    ///
deba@1695
  1566
    /// Contructor of the map
deba@1695
  1567
    PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential) 
deba@1695
  1568
      : graph(_graph), potential(_potential) {}
deba@1695
  1569
deba@1695
  1570
    /// \brief Const subscription operator
deba@1695
  1571
    ///
deba@1695
  1572
    /// Const subscription operator
deba@1695
  1573
    Value operator[](const Key& edge) const {
deba@1695
  1574
      return potential[graph.target(edge)] - potential[graph.source(edge)];
deba@1695
  1575
    }
deba@1695
  1576
deba@1695
  1577
  private:
deba@1695
  1578
    const Graph& graph;
deba@1695
  1579
    const NodeMap& potential;
deba@1695
  1580
  };
deba@1695
  1581
deba@1695
  1582
  /// \brief Just returns a PotentialDifferenceMap
deba@1695
  1583
  ///
deba@1695
  1584
  /// Just returns a PotentialDifferenceMap
deba@1695
  1585
  /// \relates PotentialDifferenceMap
deba@1695
  1586
  template <typename Graph, typename NodeMap>
deba@1695
  1587
  PotentialDifferenceMap<Graph, NodeMap> 
deba@1695
  1588
  potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
deba@1695
  1589
    return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
deba@1695
  1590
  }
deba@1695
  1591
deba@1515
  1592
  /// \brief Map of the node in-degrees.
alpar@1453
  1593
  ///
athos@1540
  1594
  /// This map returns the in-degree of a node. Once it is constructed,
deba@1515
  1595
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1596
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1597
  /// whenever the graph changes.
deba@1515
  1598
  ///
deba@1729
  1599
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  1600
  /// alternative ways to modify the graph. The correct behavior of InDegMap
deba@1829
  1601
  /// is not guarantied if these additional features are used. For example
deba@1829
  1602
  /// the functions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  1603
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  1604
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  1605
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  1606
  ///
deba@1515
  1607
  /// \sa OutDegMap
deba@1515
  1608
alpar@1453
  1609
  template <typename _Graph>
deba@1515
  1610
  class InDegMap  
deba@1999
  1611
    : protected ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  1612
      ::ItemNotifier::ObserverBase {
deba@1515
  1613
alpar@1453
  1614
  public:
deba@1515
  1615
    
deba@1515
  1616
    typedef _Graph Graph;
alpar@1453
  1617
    typedef int Value;
deba@1515
  1618
    typedef typename Graph::Node Key;
deba@1515
  1619
deba@1999
  1620
    typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  1621
    ::ItemNotifier::ObserverBase Parent;
deba@1999
  1622
deba@1515
  1623
  private:
deba@1515
  1624
deba@1990
  1625
    class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
deba@1515
  1626
    public:
deba@1515
  1627
deba@1990
  1628
      typedef DefaultMap<_Graph, Key, int> Parent;
deba@2002
  1629
      typedef typename Parent::Graph Graph;
deba@1515
  1630
deba@1515
  1631
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1632
      
deba@1829
  1633
      virtual void add(const Key& key) {
deba@1515
  1634
	Parent::add(key);
deba@1515
  1635
	Parent::set(key, 0);
deba@1515
  1636
      }
deba@1931
  1637
deba@1829
  1638
      virtual void add(const std::vector<Key>& keys) {
deba@1829
  1639
	Parent::add(keys);
deba@1829
  1640
	for (int i = 0; i < (int)keys.size(); ++i) {
deba@1829
  1641
	  Parent::set(keys[i], 0);
deba@1829
  1642
	}
deba@1829
  1643
      }
deba@1515
  1644
    };
deba@1515
  1645
deba@1515
  1646
  public:
alpar@1453
  1647
alpar@1453
  1648
    /// \brief Constructor.
alpar@1453
  1649
    ///
alpar@1453
  1650
    /// Constructor for creating in-degree map.
deba@1515
  1651
    InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
deba@1999
  1652
      Parent::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1653
      
deba@1515
  1654
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1655
	deg[it] = countInEdges(graph, it);
deba@1515
  1656
      }
alpar@1453
  1657
    }
alpar@1453
  1658
    
alpar@1459
  1659
    /// Gives back the in-degree of a Node.
deba@1515
  1660
    int operator[](const Key& key) const {
deba@1515
  1661
      return deg[key];
alpar@1459
  1662
    }
alpar@1453
  1663
alpar@1453
  1664
  protected:
deba@1515
  1665
    
deba@1515
  1666
    typedef typename Graph::Edge Edge;
deba@1515
  1667
deba@1515
  1668
    virtual void add(const Edge& edge) {
deba@1515
  1669
      ++deg[graph.target(edge)];
alpar@1453
  1670
    }
alpar@1453
  1671
deba@1931
  1672
    virtual void add(const std::vector<Edge>& edges) {
deba@1931
  1673
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  1674
        ++deg[graph.target(edges[i])];
deba@1931
  1675
      }
deba@1931
  1676
    }
deba@1931
  1677
deba@1515
  1678
    virtual void erase(const Edge& edge) {
deba@1515
  1679
      --deg[graph.target(edge)];
deba@1515
  1680
    }
deba@1515
  1681
deba@1931
  1682
    virtual void erase(const std::vector<Edge>& edges) {
deba@1931
  1683
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  1684
        --deg[graph.target(edges[i])];
deba@1931
  1685
      }
deba@1931
  1686
    }
deba@1931
  1687
deba@1515
  1688
    virtual void build() {
deba@1515
  1689
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1690
	deg[it] = countInEdges(graph, it);
deba@1515
  1691
      }      
deba@1515
  1692
    }
deba@1515
  1693
deba@1515
  1694
    virtual void clear() {
deba@1515
  1695
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1696
	deg[it] = 0;
deba@1515
  1697
      }
deba@1515
  1698
    }
deba@1515
  1699
  private:
alpar@1506
  1700
    
deba@1515
  1701
    const _Graph& graph;
deba@1515
  1702
    AutoNodeMap deg;
alpar@1459
  1703
  };
alpar@1459
  1704
deba@1515
  1705
  /// \brief Map of the node out-degrees.
deba@1515
  1706
  ///
athos@1540
  1707
  /// This map returns the out-degree of a node. Once it is constructed,
deba@1515
  1708
  /// the degrees are stored in a standard NodeMap, so each query is done
athos@1540
  1709
  /// in constant time. On the other hand, the values are updated automatically
deba@1515
  1710
  /// whenever the graph changes.
deba@1515
  1711
  ///
deba@1729
  1712
  /// \warning Besides addNode() and addEdge(), a graph structure may provide
deba@1730
  1713
  /// alternative ways to modify the graph. The correct behavior of OutDegMap
deba@1829
  1714
  /// is not guarantied if these additional features are used. For example
deba@1829
  1715
  /// the functions \ref ListGraph::changeSource() "changeSource()",
deba@1729
  1716
  /// \ref ListGraph::changeTarget() "changeTarget()" and
deba@1729
  1717
  /// \ref ListGraph::reverseEdge() "reverseEdge()"
deba@1729
  1718
  /// of \ref ListGraph will \e not update the degree values correctly.
deba@1729
  1719
  ///
alpar@1555
  1720
  /// \sa InDegMap
alpar@1459
  1721
alpar@1459
  1722
  template <typename _Graph>
deba@1515
  1723
  class OutDegMap  
deba@1999
  1724
    : protected ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  1725
      ::ItemNotifier::ObserverBase {
deba@1515
  1726
alpar@1459
  1727
  public:
deba@1999
  1728
deba@1999
  1729
    typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
deba@1999
  1730
    ::ItemNotifier::ObserverBase Parent;
deba@1515
  1731
    
deba@1515
  1732
    typedef _Graph Graph;
alpar@1459
  1733
    typedef int Value;
deba@1515
  1734
    typedef typename Graph::Node Key;
deba@1515
  1735
deba@1515
  1736
  private:
deba@1515
  1737
deba@1990
  1738
    class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
deba@1515
  1739
    public:
deba@1515
  1740
deba@1990
  1741
      typedef DefaultMap<_Graph, Key, int> Parent;
deba@2002
  1742
      typedef typename Parent::Graph Graph;
deba@1515
  1743
deba@1515
  1744
      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
deba@1515
  1745
      
deba@1829
  1746
      virtual void add(const Key& key) {
deba@1515
  1747
	Parent::add(key);
deba@1515
  1748
	Parent::set(key, 0);
deba@1515
  1749
      }
deba@1829
  1750
      virtual void add(const std::vector<Key>& keys) {
deba@1829
  1751
	Parent::add(keys);
deba@1829
  1752
	for (int i = 0; i < (int)keys.size(); ++i) {
deba@1829
  1753
	  Parent::set(keys[i], 0);
deba@1829
  1754
	}
deba@1829
  1755
      }
deba@1515
  1756
    };
deba@1515
  1757
deba@1515
  1758
  public:
alpar@1459
  1759
alpar@1459
  1760
    /// \brief Constructor.
alpar@1459
  1761
    ///
alpar@1459
  1762
    /// Constructor for creating out-degree map.
deba@1515
  1763
    OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
deba@1999
  1764
      Parent::attach(graph.getNotifier(typename _Graph::Edge()));
deba@1515
  1765
      
deba@1515
  1766
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1767
	deg[it] = countOutEdges(graph, it);
deba@1515
  1768
      }
alpar@1459
  1769
    }
alpar@1459
  1770
deba@1990
  1771
    /// Gives back the out-degree of a Node.
deba@1515
  1772
    int operator[](const Key& key) const {
deba@1515
  1773
      return deg[key];
alpar@1459
  1774
    }
alpar@1459
  1775
alpar@1459
  1776
  protected:
deba@1515
  1777
    
deba@1515
  1778
    typedef typename Graph::Edge Edge;
deba@1515
  1779
deba@1515
  1780
    virtual void add(const Edge& edge) {
deba@1515
  1781
      ++deg[graph.source(edge)];
alpar@1459
  1782
    }
alpar@1459
  1783
deba@1931
  1784
    virtual void add(const std::vector<Edge>& edges) {
deba@1931
  1785
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  1786
        ++deg[graph.source(edges[i])];
deba@1931
  1787
      }
deba@1931
  1788
    }
deba@1931
  1789
deba@1515
  1790
    virtual void erase(const Edge& edge) {
deba@1515
  1791
      --deg[graph.source(edge)];
deba@1515
  1792
    }
deba@1515
  1793
deba@1931
  1794
    virtual void erase(const std::vector<Edge>& edges) {
deba@1931
  1795
      for (int i = 0; i < (int)edges.size(); ++i) {
deba@1931
  1796
        --deg[graph.source(edges[i])];
deba@1931
  1797
      }
deba@1931
  1798
    }
deba@1931
  1799
deba@1515
  1800
    virtual void build() {
deba@1515
  1801
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1802
	deg[it] = countOutEdges(graph, it);
deba@1515
  1803
      }      
deba@1515
  1804
    }
deba@1515
  1805
deba@1515
  1806
    virtual void clear() {
deba@1515
  1807
      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
deba@1515
  1808
	deg[it] = 0;
deba@1515
  1809
      }
deba@1515
  1810
    }
deba@1515
  1811
  private:
alpar@1506
  1812
    
deba@1515
  1813
    const _Graph& graph;
deba@1515
  1814
    AutoNodeMap deg;
alpar@1453
  1815
  };
alpar@1453
  1816
deba@1695
  1817
alpar@1402
  1818
  /// @}
alpar@1402
  1819
alpar@947
  1820
} //END OF NAMESPACE LEMON
klao@946
  1821
klao@946
  1822
#endif