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