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