lemon/graph_adaptor.h
author deba
Tue, 04 Apr 2006 17:45:35 +0000
changeset 2038 33db14058543
parent 2034 b71f8ff62046
child 2042 bdc953f2a449
permissions -rw-r--r--
LinearHeap is renamed to BucketHeap which is more conform
and widely used name for this data structure
alpar@906
     1
/* -*- C++ -*-
alpar@906
     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).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
alpar@1401
    19
#ifndef LEMON_GRAPH_ADAPTOR_H
alpar@1401
    20
#define LEMON_GRAPH_ADAPTOR_H
marci@556
    21
deba@2037
    22
///\ingroup graph_adaptors
deba@2037
    23
///\file
deba@2037
    24
///\brief Several graph adaptors.
marci@556
    25
///
deba@2037
    26
///This file contains several useful graph adaptor functions.
marci@556
    27
///
deba@2037
    28
///\author Marton Makai and Balazs Dezso
marci@556
    29
deba@1993
    30
#include <lemon/bits/invalid.h>
alpar@921
    31
#include <lemon/maps.h>
deba@1979
    32
deba@1999
    33
#include <lemon/bits/base_extender.h>
deba@1979
    34
#include <lemon/bits/graph_adaptor_extender.h>
deba@1791
    35
#include <lemon/bits/graph_extender.h>
deba@1979
    36
deba@2034
    37
#include <lemon/tolerance.h>
deba@2034
    38
alpar@774
    39
#include <iostream>
marci@556
    40
alpar@921
    41
namespace lemon {
marci@556
    42
klao@1951
    43
  ///\brief Base type for the Graph Adaptors
klao@1951
    44
  ///\ingroup graph_adaptors
klao@1951
    45
  ///
klao@1951
    46
  ///Base type for the Graph Adaptors
klao@1951
    47
  ///
klao@1951
    48
  ///\warning Graph adaptors are in even
klao@1951
    49
  ///more experimental state than the other
klao@1951
    50
  ///parts of the lib. Use them at you own risk.
klao@1951
    51
  ///
klao@1951
    52
  ///This is the base type for most of LEMON graph adaptors. 
klao@1951
    53
  ///This class implements a trivial graph adaptor i.e. it only wraps the 
klao@1951
    54
  ///functions and types of the graph. The purpose of this class is to 
klao@1951
    55
  ///make easier implementing graph adaptors. E.g. if an adaptor is 
klao@1951
    56
  ///considered which differs from the wrapped graph only in some of its 
klao@1951
    57
  ///functions or types, then it can be derived from GraphAdaptor,
klao@1951
    58
  ///and only the 
klao@1951
    59
  ///differences should be implemented.
klao@1951
    60
  ///
klao@1951
    61
  ///author Marton Makai 
marci@970
    62
  template<typename _Graph>
alpar@1401
    63
  class GraphAdaptorBase {
marci@970
    64
  public:
marci@970
    65
    typedef _Graph Graph;
deba@2031
    66
    typedef GraphAdaptorBase Adaptor;
marci@970
    67
    typedef Graph ParentGraph;
marci@970
    68
marci@556
    69
  protected:
marci@556
    70
    Graph* graph;
alpar@1401
    71
    GraphAdaptorBase() : graph(0) { }
marci@556
    72
    void setGraph(Graph& _graph) { graph=&_graph; }
marci@556
    73
marci@556
    74
  public:
alpar@1401
    75
    GraphAdaptorBase(Graph& _graph) : graph(&_graph) { }
deba@2034
    76
alpar@774
    77
    typedef typename Graph::Node Node;
alpar@774
    78
    typedef typename Graph::Edge Edge;
marci@556
    79
   
marci@970
    80
    void first(Node& i) const { graph->first(i); }
marci@970
    81
    void first(Edge& i) const { graph->first(i); }
marci@970
    82
    void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
marci@970
    83
    void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
marci@556
    84
marci@970
    85
    void next(Node& i) const { graph->next(i); }
marci@970
    86
    void next(Edge& i) const { graph->next(i); }
marci@970
    87
    void nextIn(Edge& i) const { graph->nextIn(i); }
marci@970
    88
    void nextOut(Edge& i) const { graph->nextOut(i); }
marci@970
    89
alpar@986
    90
    Node source(const Edge& e) const { return graph->source(e); }
alpar@986
    91
    Node target(const Edge& e) const { return graph->target(e); }
marci@556
    92
deba@1697
    93
    typedef NodeNumTagIndicator<Graph> NodeNumTag;
marci@556
    94
    int nodeNum() const { return graph->nodeNum(); }
deba@1697
    95
    
deba@1697
    96
    typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
marci@556
    97
    int edgeNum() const { return graph->edgeNum(); }
deba@1697
    98
deba@1697
    99
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@1697
   100
    Edge findEdge(const Node& source, const Node& target, 
deba@1697
   101
		  const Edge& prev = INVALID) {
deba@1697
   102
      return graph->findEdge(source, target, prev);
deba@1697
   103
    }
marci@556
   104
  
deba@1697
   105
    Node addNode() const { 
deba@1697
   106
      return Node(graph->addNode()); 
deba@1697
   107
    }
deba@1697
   108
alpar@986
   109
    Edge addEdge(const Node& source, const Node& target) const { 
deba@1697
   110
      return Edge(graph->addEdge(source, target)); 
deba@1697
   111
    }
marci@556
   112
marci@556
   113
    void erase(const Node& i) const { graph->erase(i); }
marci@556
   114
    void erase(const Edge& i) const { graph->erase(i); }
marci@556
   115
  
marci@556
   116
    void clear() const { graph->clear(); }
marci@556
   117
    
marci@739
   118
    int id(const Node& v) const { return graph->id(v); }
marci@739
   119
    int id(const Edge& e) const { return graph->id(e); }
deba@1991
   120
deba@2031
   121
    Node fromNodeId(int id) const {
deba@2031
   122
      return graph->fromNodeId(id);
deba@2031
   123
    }
deba@2031
   124
deba@2031
   125
    Edge fromEdgeId(int id) const {
deba@2031
   126
      return graph->fromEdgeId(id);
deba@2031
   127
    }
deba@2031
   128
deba@1991
   129
    int maxNodeId() const {
deba@1991
   130
      return graph->maxNodeId();
deba@1991
   131
    }
deba@1991
   132
deba@1991
   133
    int maxEdgeId() const {
deba@1991
   134
      return graph->maxEdgeId();
deba@1991
   135
    }
deba@1991
   136
deba@1991
   137
    typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
deba@1991
   138
deba@1991
   139
    NodeNotifier& getNotifier(Node) const {
deba@1991
   140
      return graph->getNotifier(Node());
deba@1991
   141
    } 
deba@1991
   142
deba@1991
   143
    typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier;
deba@1991
   144
deba@1991
   145
    EdgeNotifier& getNotifier(Edge) const {
deba@1991
   146
      return graph->getNotifier(Edge());
deba@1991
   147
    } 
marci@650
   148
    
marci@970
   149
    template <typename _Value>
deba@2031
   150
    class NodeMap : public Graph::template NodeMap<_Value> {
marci@970
   151
    public:
deba@2031
   152
deba@2031
   153
      typedef typename Graph::template NodeMap<_Value> Parent;
deba@2031
   154
deba@2031
   155
      explicit NodeMap(const Adaptor& ga) 
deba@2031
   156
	: Parent(*ga.graph) {}
deba@2031
   157
deba@2031
   158
      NodeMap(const Adaptor& ga, const _Value& value)
deba@1991
   159
	: Parent(*ga.graph, value) { }
deba@2031
   160
deba@2031
   161
      NodeMap& operator=(const NodeMap& cmap) {
deba@2031
   162
        return operator=<NodeMap>(cmap);
deba@2031
   163
      }
deba@2031
   164
deba@2031
   165
      template <typename CMap>
deba@2031
   166
      NodeMap& operator=(const CMap& cmap) {
deba@2031
   167
        Parent::operator=(cmap);
deba@2031
   168
        return *this;
deba@2031
   169
      }
deba@2031
   170
      
marci@970
   171
    };
marci@556
   172
marci@970
   173
    template <typename _Value>
deba@2031
   174
    class EdgeMap : public Graph::template EdgeMap<_Value> {
marci@970
   175
    public:
deba@2031
   176
      
deba@2031
   177
      typedef typename Graph::template EdgeMap<_Value> Parent;
deba@2031
   178
      
deba@2031
   179
      explicit EdgeMap(const Adaptor& ga) 
deba@2031
   180
	: Parent(*ga.graph) {}
deba@2031
   181
deba@2031
   182
      EdgeMap(const Adaptor& ga, const _Value& value)
deba@2031
   183
	: Parent(*ga.graph, value) {}
deba@2031
   184
deba@2031
   185
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2031
   186
        return operator=<EdgeMap>(cmap);
deba@2031
   187
      }
deba@2031
   188
deba@2031
   189
      template <typename CMap>
deba@2031
   190
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
   191
        Parent::operator=(cmap);
deba@2031
   192
        return *this;
deba@2031
   193
      }
deba@2031
   194
marci@970
   195
    };
deba@877
   196
marci@556
   197
  };
marci@556
   198
marci@970
   199
  template <typename _Graph>
alpar@1401
   200
  class GraphAdaptor :
deba@1979
   201
    public GraphAdaptorExtender<GraphAdaptorBase<_Graph> > { 
marci@970
   202
  public:
marci@970
   203
    typedef _Graph Graph;
deba@1979
   204
    typedef GraphAdaptorExtender<GraphAdaptorBase<_Graph> > Parent;
marci@970
   205
  protected:
alpar@1401
   206
    GraphAdaptor() : Parent() { }
marci@569
   207
marci@970
   208
  public:
deba@1755
   209
    explicit GraphAdaptor(Graph& _graph) { setGraph(_graph); }
marci@970
   210
  };
marci@569
   211
deba@1991
   212
  /// \brief Just gives back a graph adaptor
deba@1991
   213
  ///
deba@1991
   214
  /// Just gives back a graph adaptor which 
deba@1991
   215
  /// should be provide original graph
deba@1991
   216
  template<typename Graph>
deba@1991
   217
  GraphAdaptor<const Graph>
deba@1991
   218
  graphAdaptor(const Graph& graph) {
deba@1991
   219
    return GraphAdaptor<const Graph>(graph);
deba@1991
   220
  }
deba@1991
   221
deba@1991
   222
marci@997
   223
  template <typename _Graph>
alpar@1401
   224
  class RevGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
marci@997
   225
  public:
marci@997
   226
    typedef _Graph Graph;
alpar@1401
   227
    typedef GraphAdaptorBase<_Graph> Parent;
marci@997
   228
  protected:
alpar@1401
   229
    RevGraphAdaptorBase() : Parent() { }
marci@997
   230
  public:
marci@997
   231
    typedef typename Parent::Node Node;
marci@997
   232
    typedef typename Parent::Edge Edge;
marci@997
   233
marci@997
   234
    void firstIn(Edge& i, const Node& n) const { Parent::firstOut(i, n); }
marci@997
   235
    void firstOut(Edge& i, const Node& n ) const { Parent::firstIn(i, n); }
marci@997
   236
marci@997
   237
    void nextIn(Edge& i) const { Parent::nextOut(i); }
marci@997
   238
    void nextOut(Edge& i) const { Parent::nextIn(i); }
marci@997
   239
marci@997
   240
    Node source(const Edge& e) const { return Parent::target(e); }
marci@997
   241
    Node target(const Edge& e) const { return Parent::source(e); }
deba@1991
   242
deba@1991
   243
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@1991
   244
    Edge findEdge(const Node& source, const Node& target, 
deba@1991
   245
		  const Edge& prev = INVALID) {
deba@1991
   246
      return Parent::findEdge(target, source, prev);
deba@1991
   247
    }
deba@1991
   248
marci@997
   249
  };
marci@997
   250
    
marci@997
   251
alpar@1949
   252
  ///\brief A graph adaptor which reverses the orientation of the edges.
alpar@1949
   253
  ///\ingroup graph_adaptors
alpar@1949
   254
  ///
alpar@1949
   255
  ///\warning Graph adaptors are in even more experimental 
alpar@1949
   256
  ///state than the other
alpar@879
   257
  ///parts of the lib. Use them at you own risk.
alpar@879
   258
  ///
alpar@1949
   259
  /// If \c g is defined as
alpar@1946
   260
  ///\code
marci@923
   261
  /// ListGraph g;
alpar@1946
   262
  ///\endcode
alpar@1949
   263
  /// then
alpar@1946
   264
  ///\code
deba@1991
   265
  /// RevGraphAdaptor<ListGraph> ga(g);
alpar@1946
   266
  ///\endcode
alpar@1949
   267
  ///implements the graph obtained from \c g by 
alpar@1949
   268
  /// reversing the orientation of its edges.
alpar@1946
   269
marci@997
   270
  template<typename _Graph>
alpar@1401
   271
  class RevGraphAdaptor : 
deba@1979
   272
    public GraphAdaptorExtender<RevGraphAdaptorBase<_Graph> > {
marci@650
   273
  public:
marci@997
   274
    typedef _Graph Graph;
deba@1979
   275
    typedef GraphAdaptorExtender<
alpar@1401
   276
      RevGraphAdaptorBase<_Graph> > Parent;
marci@556
   277
  protected:
alpar@1401
   278
    RevGraphAdaptor() { }
marci@556
   279
  public:
deba@1755
   280
    explicit RevGraphAdaptor(_Graph& _graph) { setGraph(_graph); }
marci@997
   281
  };
marci@556
   282
deba@1991
   283
  /// \brief Just gives back a reverse graph adaptor
deba@1991
   284
  ///
deba@1991
   285
  /// Just gives back a reverse graph adaptor
deba@1991
   286
  template<typename Graph>
deba@1991
   287
  RevGraphAdaptor<const Graph>
deba@1991
   288
  revGraphAdaptor(const Graph& graph) {
deba@1991
   289
    return RevGraphAdaptor<const Graph>(graph);
deba@1991
   290
  }
deba@1991
   291
deba@1681
   292
  template <typename _Graph, typename NodeFilterMap, 
deba@1681
   293
	    typename EdgeFilterMap, bool checked = true>
alpar@1401
   294
  class SubGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
marci@992
   295
  public:
marci@992
   296
    typedef _Graph Graph;
deba@2031
   297
    typedef SubGraphAdaptorBase Adaptor;
alpar@1401
   298
    typedef GraphAdaptorBase<_Graph> Parent;
marci@992
   299
  protected:
marci@992
   300
    NodeFilterMap* node_filter_map;
marci@992
   301
    EdgeFilterMap* edge_filter_map;
alpar@1401
   302
    SubGraphAdaptorBase() : Parent(), 
marci@992
   303
			    node_filter_map(0), edge_filter_map(0) { }
marci@775
   304
marci@992
   305
    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
marci@992
   306
      node_filter_map=&_node_filter_map;
marci@992
   307
    }
marci@992
   308
    void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
marci@992
   309
      edge_filter_map=&_edge_filter_map;
marci@992
   310
    }
marci@992
   311
marci@992
   312
  public:
marci@992
   313
marci@992
   314
    typedef typename Parent::Node Node;
marci@992
   315
    typedef typename Parent::Edge Edge;
marci@992
   316
marci@992
   317
    void first(Node& i) const { 
marci@992
   318
      Parent::first(i); 
marci@992
   319
      while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i); 
marci@992
   320
    }
deba@1681
   321
deba@1681
   322
    void first(Edge& i) const { 
deba@1681
   323
      Parent::first(i); 
deba@1681
   324
      while (i!=INVALID && (!(*edge_filter_map)[i] 
deba@1681
   325
	     || !(*node_filter_map)[Parent::source(i)]
deba@1681
   326
	     || !(*node_filter_map)[Parent::target(i)])) Parent::next(i); 
deba@1681
   327
    }
deba@1681
   328
deba@1681
   329
    void firstIn(Edge& i, const Node& n) const { 
deba@1681
   330
      Parent::firstIn(i, n); 
deba@1681
   331
      while (i!=INVALID && (!(*edge_filter_map)[i] 
deba@1681
   332
	     || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i); 
deba@1681
   333
    }
deba@1681
   334
deba@1681
   335
    void firstOut(Edge& i, const Node& n) const { 
deba@1681
   336
      Parent::firstOut(i, n); 
deba@1681
   337
      while (i!=INVALID && (!(*edge_filter_map)[i] 
deba@1681
   338
	     || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i); 
deba@1681
   339
    }
deba@1681
   340
deba@1681
   341
    void next(Node& i) const { 
deba@1681
   342
      Parent::next(i); 
deba@1681
   343
      while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i); 
deba@1681
   344
    }
deba@1681
   345
deba@1681
   346
    void next(Edge& i) const { 
deba@1681
   347
      Parent::next(i); 
deba@1681
   348
      while (i!=INVALID && (!(*edge_filter_map)[i] 
deba@1681
   349
	     || !(*node_filter_map)[Parent::source(i)]
deba@1681
   350
	     || !(*node_filter_map)[Parent::target(i)])) Parent::next(i); 
deba@1681
   351
    }
deba@1681
   352
deba@1681
   353
    void nextIn(Edge& i) const { 
deba@1681
   354
      Parent::nextIn(i); 
deba@1681
   355
      while (i!=INVALID && (!(*edge_filter_map)[i] 
deba@1681
   356
	     || !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i); 
deba@1681
   357
    }
deba@1681
   358
deba@1681
   359
    void nextOut(Edge& i) const { 
deba@1681
   360
      Parent::nextOut(i); 
deba@1681
   361
      while (i!=INVALID && (!(*edge_filter_map)[i] 
deba@1681
   362
	     || !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i); 
deba@1681
   363
    }
deba@1681
   364
klao@1951
   365
    ///\e
alpar@1949
   366
klao@1951
   367
    /// This function hides \c n in the graph, i.e. the iteration 
klao@1951
   368
    /// jumps over it. This is done by simply setting the value of \c n  
klao@1951
   369
    /// to be false in the corresponding node-map.
deba@1681
   370
    void hide(const Node& n) const { node_filter_map->set(n, false); }
deba@1681
   371
klao@1951
   372
    ///\e
alpar@1949
   373
klao@1951
   374
    /// This function hides \c e in the graph, i.e. the iteration 
klao@1951
   375
    /// jumps over it. This is done by simply setting the value of \c e  
klao@1951
   376
    /// to be false in the corresponding edge-map.
deba@1681
   377
    void hide(const Edge& e) const { edge_filter_map->set(e, false); }
deba@1681
   378
klao@1951
   379
    ///\e
alpar@1949
   380
klao@1951
   381
    /// The value of \c n is set to be true in the node-map which stores 
klao@1951
   382
    /// hide information. If \c n was hidden previuosly, then it is shown 
klao@1951
   383
    /// again
deba@1681
   384
     void unHide(const Node& n) const { node_filter_map->set(n, true); }
deba@1681
   385
klao@1951
   386
    ///\e
alpar@1949
   387
klao@1951
   388
    /// The value of \c e is set to be true in the edge-map which stores 
klao@1951
   389
    /// hide information. If \c e was hidden previuosly, then it is shown 
klao@1951
   390
    /// again
deba@1681
   391
    void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
deba@1681
   392
klao@1951
   393
    /// Returns true if \c n is hidden.
alpar@1949
   394
    
klao@1951
   395
    ///\e
klao@1951
   396
    ///
deba@1681
   397
    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
deba@1681
   398
klao@1951
   399
    /// Returns true if \c n is hidden.
alpar@1949
   400
    
klao@1951
   401
    ///\e
klao@1951
   402
    ///
deba@1681
   403
    bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
deba@1681
   404
deba@1697
   405
    typedef False NodeNumTag;
deba@1697
   406
    typedef False EdgeNumTag;
deba@1991
   407
deba@1991
   408
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@1991
   409
    Edge findEdge(const Node& source, const Node& target, 
deba@1991
   410
		  const Edge& prev = INVALID) {
deba@1991
   411
      if (!(*node_filter_map)[source] || !(*node_filter_map)[target]) {
deba@1991
   412
        return INVALID;
deba@1991
   413
      }
deba@1991
   414
      Edge edge = Parent::findEdge(source, target, prev);
deba@1991
   415
      while (edge != INVALID && !(*edge_filter_map)[edge]) {
deba@1991
   416
        edge = Parent::findEdge(source, target, edge);
deba@1991
   417
      }
deba@1991
   418
      return edge;
deba@1991
   419
    }
deba@2031
   420
deba@2031
   421
    template <typename _Value>
deba@2031
   422
    class NodeMap 
deba@2031
   423
      : public SubMapExtender<Adaptor, 
deba@2031
   424
                              typename Parent::template NodeMap<_Value> > 
deba@2031
   425
    {
deba@2031
   426
    public:
deba@2031
   427
      typedef Adaptor Graph;
deba@2031
   428
      typedef SubMapExtender<Adaptor, typename Parent::
deba@2031
   429
                             template NodeMap<_Value> > Parent;
deba@2031
   430
    
deba@2031
   431
      NodeMap(const Graph& graph) 
deba@2031
   432
	: Parent(graph) {}
deba@2031
   433
      NodeMap(const Graph& graph, const _Value& value) 
deba@2031
   434
	: Parent(graph, value) {}
deba@2031
   435
    
deba@2031
   436
      NodeMap& operator=(const NodeMap& cmap) {
deba@2031
   437
	return operator=<NodeMap>(cmap);
deba@2031
   438
      }
deba@2031
   439
    
deba@2031
   440
      template <typename CMap>
deba@2031
   441
      NodeMap& operator=(const CMap& cmap) {
deba@2031
   442
        Parent::operator=(cmap);
deba@2031
   443
	return *this;
deba@2031
   444
      }
deba@2031
   445
    };
deba@2031
   446
deba@2031
   447
    template <typename _Value>
deba@2031
   448
    class EdgeMap 
deba@2031
   449
      : public SubMapExtender<Adaptor, 
deba@2031
   450
                              typename Parent::template EdgeMap<_Value> > 
deba@2031
   451
    {
deba@2031
   452
    public:
deba@2031
   453
      typedef Adaptor Graph;
deba@2031
   454
      typedef SubMapExtender<Adaptor, typename Parent::
deba@2031
   455
                             template EdgeMap<_Value> > Parent;
deba@2031
   456
    
deba@2031
   457
      EdgeMap(const Graph& graph) 
deba@2031
   458
	: Parent(graph) {}
deba@2031
   459
      EdgeMap(const Graph& graph, const _Value& value) 
deba@2031
   460
	: Parent(graph, value) {}
deba@2031
   461
    
deba@2031
   462
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2031
   463
	return operator=<EdgeMap>(cmap);
deba@2031
   464
      }
deba@2031
   465
    
deba@2031
   466
      template <typename CMap>
deba@2031
   467
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
   468
        Parent::operator=(cmap);
deba@2031
   469
	return *this;
deba@2031
   470
      }
deba@2031
   471
    };
deba@2031
   472
deba@1681
   473
  };
deba@1681
   474
deba@1681
   475
  template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
deba@1681
   476
  class SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, false> 
deba@1681
   477
    : public GraphAdaptorBase<_Graph> {
deba@1681
   478
  public:
deba@1681
   479
    typedef _Graph Graph;
deba@2031
   480
    typedef SubGraphAdaptorBase Adaptor;
deba@1681
   481
    typedef GraphAdaptorBase<_Graph> Parent;
deba@1681
   482
  protected:
deba@1681
   483
    NodeFilterMap* node_filter_map;
deba@1681
   484
    EdgeFilterMap* edge_filter_map;
deba@1681
   485
    SubGraphAdaptorBase() : Parent(), 
deba@1681
   486
			    node_filter_map(0), edge_filter_map(0) { }
deba@1681
   487
deba@1681
   488
    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
deba@1681
   489
      node_filter_map=&_node_filter_map;
deba@1681
   490
    }
deba@1681
   491
    void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
deba@1681
   492
      edge_filter_map=&_edge_filter_map;
deba@1681
   493
    }
deba@1681
   494
deba@1681
   495
  public:
deba@1681
   496
deba@1681
   497
    typedef typename Parent::Node Node;
deba@1681
   498
    typedef typename Parent::Edge Edge;
deba@1681
   499
deba@1681
   500
    void first(Node& i) const { 
deba@1681
   501
      Parent::first(i); 
deba@1681
   502
      while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i); 
deba@1681
   503
    }
deba@1681
   504
marci@992
   505
    void first(Edge& i) const { 
marci@992
   506
      Parent::first(i); 
marci@992
   507
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i); 
marci@992
   508
    }
deba@1681
   509
marci@992
   510
    void firstIn(Edge& i, const Node& n) const { 
marci@992
   511
      Parent::firstIn(i, n); 
marci@992
   512
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i); 
marci@992
   513
    }
deba@1681
   514
marci@992
   515
    void firstOut(Edge& i, const Node& n) const { 
marci@992
   516
      Parent::firstOut(i, n); 
marci@992
   517
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i); 
marci@992
   518
    }
marci@992
   519
marci@992
   520
    void next(Node& i) const { 
marci@992
   521
      Parent::next(i); 
marci@992
   522
      while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i); 
marci@992
   523
    }
marci@992
   524
    void next(Edge& i) const { 
marci@992
   525
      Parent::next(i); 
marci@992
   526
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i); 
marci@992
   527
    }
marci@992
   528
    void nextIn(Edge& i) const { 
marci@992
   529
      Parent::nextIn(i); 
marci@992
   530
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i); 
marci@992
   531
    }
deba@1681
   532
marci@992
   533
    void nextOut(Edge& i) const { 
marci@992
   534
      Parent::nextOut(i); 
marci@992
   535
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i); 
marci@992
   536
    }
marci@992
   537
klao@1951
   538
    ///\e
alpar@1949
   539
klao@1951
   540
    /// This function hides \c n in the graph, i.e. the iteration 
klao@1951
   541
    /// jumps over it. This is done by simply setting the value of \c n  
klao@1951
   542
    /// to be false in the corresponding node-map.
marci@992
   543
    void hide(const Node& n) const { node_filter_map->set(n, false); }
marci@992
   544
klao@1951
   545
    ///\e
alpar@1949
   546
klao@1951
   547
    /// This function hides \c e in the graph, i.e. the iteration 
klao@1951
   548
    /// jumps over it. This is done by simply setting the value of \c e  
klao@1951
   549
    /// to be false in the corresponding edge-map.
marci@992
   550
    void hide(const Edge& e) const { edge_filter_map->set(e, false); }
marci@992
   551
klao@1951
   552
    ///\e
alpar@1949
   553
klao@1951
   554
    /// The value of \c n is set to be true in the node-map which stores 
klao@1951
   555
    /// hide information. If \c n was hidden previuosly, then it is shown 
klao@1951
   556
    /// again
marci@992
   557
     void unHide(const Node& n) const { node_filter_map->set(n, true); }
marci@992
   558
klao@1951
   559
    ///\e
alpar@1949
   560
klao@1951
   561
    /// The value of \c e is set to be true in the edge-map which stores 
klao@1951
   562
    /// hide information. If \c e was hidden previuosly, then it is shown 
klao@1951
   563
    /// again
marci@992
   564
    void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
marci@992
   565
klao@1951
   566
    /// Returns true if \c n is hidden.
alpar@1949
   567
    
klao@1951
   568
    ///\e
klao@1951
   569
    ///
marci@992
   570
    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
marci@992
   571
klao@1951
   572
    /// Returns true if \c n is hidden.
alpar@1949
   573
    
klao@1951
   574
    ///\e
klao@1951
   575
    ///
marci@992
   576
    bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
marci@992
   577
deba@1697
   578
    typedef False NodeNumTag;
deba@1697
   579
    typedef False EdgeNumTag;
deba@1991
   580
deba@1991
   581
    typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
deba@1991
   582
    Edge findEdge(const Node& source, const Node& target, 
deba@1991
   583
		  const Edge& prev = INVALID) {
deba@1991
   584
      if (!(*node_filter_map)[source] || !(*node_filter_map)[target]) {
deba@1991
   585
        return INVALID;
deba@1991
   586
      }
deba@1991
   587
      Edge edge = Parent::findEdge(source, target, prev);
deba@1991
   588
      while (edge != INVALID && !(*edge_filter_map)[edge]) {
deba@1991
   589
        edge = Parent::findEdge(source, target, edge);
deba@1991
   590
      }
deba@1991
   591
      return edge;
deba@1991
   592
    }
deba@2031
   593
deba@2031
   594
    template <typename _Value>
deba@2031
   595
    class NodeMap 
deba@2031
   596
      : public SubMapExtender<Adaptor, 
deba@2031
   597
                              typename Parent::template NodeMap<_Value> > 
deba@2031
   598
    {
deba@2031
   599
    public:
deba@2031
   600
      typedef Adaptor Graph;
deba@2031
   601
      typedef SubMapExtender<Adaptor, typename Parent::
deba@2031
   602
                             template NodeMap<_Value> > Parent;
deba@2031
   603
    
deba@2031
   604
      NodeMap(const Graph& graph) 
deba@2031
   605
	: Parent(graph) {}
deba@2031
   606
      NodeMap(const Graph& graph, const _Value& value) 
deba@2031
   607
	: Parent(graph, value) {}
deba@2031
   608
    
deba@2031
   609
      NodeMap& operator=(const NodeMap& cmap) {
deba@2031
   610
	return operator=<NodeMap>(cmap);
deba@2031
   611
      }
deba@2031
   612
    
deba@2031
   613
      template <typename CMap>
deba@2031
   614
      NodeMap& operator=(const CMap& cmap) {
deba@2031
   615
        Parent::operator=(cmap);
deba@2031
   616
	return *this;
deba@2031
   617
      }
deba@2031
   618
    };
deba@2031
   619
deba@2031
   620
    template <typename _Value>
deba@2031
   621
    class EdgeMap 
deba@2031
   622
      : public SubMapExtender<Adaptor, 
deba@2031
   623
                              typename Parent::template EdgeMap<_Value> > 
deba@2031
   624
    {
deba@2031
   625
    public:
deba@2031
   626
      typedef Adaptor Graph;
deba@2031
   627
      typedef SubMapExtender<Adaptor, typename Parent::
deba@2031
   628
                             template EdgeMap<_Value> > Parent;
deba@2031
   629
    
deba@2031
   630
      EdgeMap(const Graph& graph) 
deba@2031
   631
	: Parent(graph) {}
deba@2031
   632
      EdgeMap(const Graph& graph, const _Value& value) 
deba@2031
   633
	: Parent(graph, value) {}
deba@2031
   634
    
deba@2031
   635
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2031
   636
	return operator=<EdgeMap>(cmap);
deba@2031
   637
      }
deba@2031
   638
    
deba@2031
   639
      template <typename CMap>
deba@2031
   640
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
   641
        Parent::operator=(cmap);
deba@2031
   642
	return *this;
deba@2031
   643
      }
deba@2031
   644
    };
deba@2031
   645
marci@992
   646
  };
marci@775
   647
klao@1951
   648
  /// \brief A graph adaptor for hiding nodes and edges from a graph.
klao@1951
   649
  /// \ingroup graph_adaptors
klao@1951
   650
  /// 
klao@1951
   651
  /// \warning Graph adaptors are in even more experimental state than the
klao@1951
   652
  /// other parts of the lib. Use them at you own risk.
klao@1951
   653
  /// 
klao@1951
   654
  /// SubGraphAdaptor shows the graph with filtered node-set and 
klao@1951
   655
  /// edge-set. If the \c checked parameter is true then it filters the edgeset
klao@1951
   656
  /// to do not get invalid edges without source or target.
klao@1952
   657
  /// Let \f$ G=(V, A) \f$ be a directed graph
klao@1951
   658
  /// and suppose that the graph instance \c g of type ListGraph
klao@1952
   659
  /// implements \f$ G \f$.
klao@1952
   660
  /// Let moreover \f$ b_V \f$ and \f$ b_A \f$ be bool-valued functions resp.
klao@1951
   661
  /// on the node-set and edge-set.
klao@1951
   662
  /// SubGraphAdaptor<...>::NodeIt iterates 
klao@1952
   663
  /// on the node-set \f$ \{v\in V : b_V(v)=true\} \f$ and 
klao@1951
   664
  /// SubGraphAdaptor<...>::EdgeIt iterates 
klao@1952
   665
  /// on the edge-set \f$ \{e\in A : b_A(e)=true\} \f$. Similarly, 
klao@1951
   666
  /// SubGraphAdaptor<...>::OutEdgeIt and
klao@1951
   667
  /// SubGraphAdaptor<...>::InEdgeIt iterates 
klao@1951
   668
  /// only on edges leaving and entering a specific node which have true value.
klao@1951
   669
  /// 
klao@1951
   670
  /// If the \c checked template parameter is false then we have to note that 
klao@1951
   671
  /// the node-iterator cares only the filter on the node-set, and the 
klao@1951
   672
  /// edge-iterator cares only the filter on the edge-set.
klao@1951
   673
  /// This way the edge-map
klao@1951
   674
  /// should filter all edges which's source or target is filtered by the 
klao@1951
   675
  /// node-filter.
alpar@1957
   676
  ///\code
klao@1951
   677
  /// typedef ListGraph Graph;
klao@1951
   678
  /// Graph g;
klao@1951
   679
  /// typedef Graph::Node Node;
klao@1951
   680
  /// typedef Graph::Edge Edge;
klao@1951
   681
  /// Node u=g.addNode(); //node of id 0
klao@1951
   682
  /// Node v=g.addNode(); //node of id 1
klao@1951
   683
  /// Node e=g.addEdge(u, v); //edge of id 0
klao@1951
   684
  /// Node f=g.addEdge(v, u); //edge of id 1
klao@1951
   685
  /// Graph::NodeMap<bool> nm(g, true);
klao@1951
   686
  /// nm.set(u, false);
klao@1951
   687
  /// Graph::EdgeMap<bool> em(g, true);
klao@1951
   688
  /// em.set(e, false);
deba@1991
   689
  /// typedef SubGraphAdaptor<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGA;
deba@1991
   690
  /// SubGA ga(g, nm, em);
deba@1991
   691
  /// for (SubGA::NodeIt n(ga); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
klao@1951
   692
  /// std::cout << ":-)" << std::endl;
deba@1991
   693
  /// for (SubGA::EdgeIt e(ga); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
alpar@1957
   694
  ///\endcode
klao@1951
   695
  /// The output of the above code is the following.
alpar@1957
   696
  ///\code
klao@1951
   697
  /// 1
klao@1951
   698
  /// :-)
klao@1951
   699
  /// 1
alpar@1957
   700
  ///\endcode
deba@1991
   701
  /// Note that \c n is of type \c SubGA::NodeIt, but it can be converted to
klao@1951
   702
  /// \c Graph::Node that is why \c g.id(n) can be applied.
klao@1951
   703
  /// 
klao@1951
   704
  /// For other examples see also the documentation of NodeSubGraphAdaptor and 
klao@1951
   705
  /// EdgeSubGraphAdaptor.
klao@1951
   706
  /// 
klao@1951
   707
  /// \author Marton Makai
marci@1242
   708
marci@992
   709
  template<typename _Graph, typename NodeFilterMap, 
deba@1681
   710
	   typename EdgeFilterMap, bool checked = true>
alpar@1401
   711
  class SubGraphAdaptor : 
deba@1979
   712
    public GraphAdaptorExtender<
deba@1681
   713
    SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, checked> > {
marci@650
   714
  public:
marci@992
   715
    typedef _Graph Graph;
deba@2031
   716
    typedef GraphAdaptorExtender< SubGraphAdaptorBase<_Graph, NodeFilterMap, 
deba@2031
   717
                                                      EdgeFilterMap, checked> >
deba@2031
   718
    Parent;
deba@2031
   719
marci@556
   720
  protected:
alpar@1401
   721
    SubGraphAdaptor() { }
marci@992
   722
  public:
deba@2031
   723
alpar@1401
   724
    SubGraphAdaptor(_Graph& _graph, NodeFilterMap& _node_filter_map, 
marci@992
   725
		    EdgeFilterMap& _edge_filter_map) { 
marci@992
   726
      setGraph(_graph);
marci@992
   727
      setNodeFilterMap(_node_filter_map);
marci@992
   728
      setEdgeFilterMap(_edge_filter_map);
marci@992
   729
    }
deba@2031
   730
marci@992
   731
  };
marci@556
   732
deba@1991
   733
  /// \brief Just gives back a sub graph adaptor
deba@1991
   734
  ///
deba@1991
   735
  /// Just gives back a sub graph adaptor
deba@1991
   736
  template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
deba@1991
   737
  SubGraphAdaptor<const Graph, NodeFilterMap, EdgeFilterMap>
deba@1991
   738
  subGraphAdaptor(const Graph& graph, 
deba@1991
   739
                   NodeFilterMap& nfm, EdgeFilterMap& efm) {
deba@1991
   740
    return SubGraphAdaptor<const Graph, NodeFilterMap, EdgeFilterMap>
deba@1991
   741
      (graph, nfm, efm);
deba@1991
   742
  }
deba@1991
   743
deba@1991
   744
  template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
deba@1991
   745
  SubGraphAdaptor<const Graph, const NodeFilterMap, EdgeFilterMap>
deba@1991
   746
  subGraphAdaptor(const Graph& graph, 
deba@1991
   747
                   NodeFilterMap& nfm, EdgeFilterMap& efm) {
deba@1991
   748
    return SubGraphAdaptor<const Graph, const NodeFilterMap, EdgeFilterMap>
deba@1991
   749
      (graph, nfm, efm);
deba@1991
   750
  }
deba@1991
   751
deba@1991
   752
  template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
deba@1991
   753
  SubGraphAdaptor<const Graph, NodeFilterMap, const EdgeFilterMap>
deba@1991
   754
  subGraphAdaptor(const Graph& graph, 
deba@1991
   755
                   NodeFilterMap& nfm, EdgeFilterMap& efm) {
deba@1991
   756
    return SubGraphAdaptor<const Graph, NodeFilterMap, const EdgeFilterMap>
deba@1991
   757
      (graph, nfm, efm);
deba@1991
   758
  }
deba@1991
   759
deba@1991
   760
  template<typename Graph, typename NodeFilterMap, typename EdgeFilterMap>
deba@1991
   761
  SubGraphAdaptor<const Graph, const NodeFilterMap, const EdgeFilterMap>
deba@1991
   762
  subGraphAdaptor(const Graph& graph, 
deba@1991
   763
                   NodeFilterMap& nfm, EdgeFilterMap& efm) {
deba@1991
   764
    return SubGraphAdaptor<const Graph, const NodeFilterMap, 
deba@1991
   765
      const EdgeFilterMap>(graph, nfm, efm);
deba@1991
   766
  }
deba@1991
   767
marci@556
   768
marci@569
   769
klao@1951
   770
  ///\brief An adaptor for hiding nodes from a graph.
klao@1951
   771
  ///\ingroup graph_adaptors
klao@1951
   772
  ///
klao@1951
   773
  ///\warning Graph adaptors are in even more experimental state
klao@1951
   774
  ///than the other
klao@1951
   775
  ///parts of the lib. Use them at you own risk.
klao@1951
   776
  ///
klao@1951
   777
  ///An adaptor for hiding nodes from a graph.
klao@1951
   778
  ///This adaptor specializes SubGraphAdaptor in the way that only
klao@1951
   779
  ///the node-set 
klao@1951
   780
  ///can be filtered. In usual case the checked parameter is true, we get the
klao@1951
   781
  ///induced subgraph. But if the checked parameter is false then we can only
klao@1951
   782
  ///filter only isolated nodes.
klao@1951
   783
  ///\author Marton Makai
deba@1681
   784
  template<typename Graph, typename NodeFilterMap, bool checked = true>
alpar@1401
   785
  class NodeSubGraphAdaptor : 
alpar@1401
   786
    public SubGraphAdaptor<Graph, NodeFilterMap, 
deba@1681
   787
			   ConstMap<typename Graph::Edge,bool>, checked> {
marci@933
   788
  public:
deba@2031
   789
alpar@1401
   790
    typedef SubGraphAdaptor<Graph, NodeFilterMap, 
deba@2031
   791
			    ConstMap<typename Graph::Edge,bool>, checked > 
deba@2031
   792
    Parent;
deba@2031
   793
marci@933
   794
  protected:
marci@933
   795
    ConstMap<typename Graph::Edge, bool> const_true_map;
deba@1991
   796
deba@1991
   797
    NodeSubGraphAdaptor() : const_true_map(true) {
deba@1991
   798
      Parent::setEdgeFilterMap(const_true_map);
deba@1991
   799
    }
deba@1991
   800
marci@933
   801
  public:
deba@2031
   802
alpar@1401
   803
    NodeSubGraphAdaptor(Graph& _graph, NodeFilterMap& _node_filter_map) : 
marci@933
   804
      Parent(), const_true_map(true) { 
marci@933
   805
      Parent::setGraph(_graph);
marci@933
   806
      Parent::setNodeFilterMap(_node_filter_map);
marci@933
   807
      Parent::setEdgeFilterMap(const_true_map);
marci@933
   808
    }
deba@2031
   809
marci@933
   810
  };
marci@933
   811
marci@933
   812
deba@1991
   813
  /// \brief Just gives back a node sub graph adaptor
deba@1991
   814
  ///
deba@1991
   815
  /// Just gives back a node sub graph adaptor
deba@1991
   816
  template<typename Graph, typename NodeFilterMap>
deba@1991
   817
  NodeSubGraphAdaptor<const Graph, NodeFilterMap>
deba@1991
   818
  nodeSubGraphAdaptor(const Graph& graph, NodeFilterMap& nfm) {
deba@1991
   819
    return NodeSubGraphAdaptor<const Graph, NodeFilterMap>(graph, nfm);
deba@1991
   820
  }
deba@1991
   821
deba@1991
   822
  template<typename Graph, typename NodeFilterMap>
deba@1991
   823
  NodeSubGraphAdaptor<const Graph, const NodeFilterMap>
deba@1991
   824
  nodeSubGraphAdaptor(const Graph& graph, const NodeFilterMap& nfm) {
deba@1991
   825
    return NodeSubGraphAdaptor<const Graph, const NodeFilterMap>(graph, nfm);
deba@1991
   826
  }
deba@1991
   827
klao@1951
   828
  ///\brief An adaptor for hiding edges from a graph.
klao@1951
   829
  ///
klao@1951
   830
  ///\warning Graph adaptors are in even more experimental state
klao@1951
   831
  ///than the other parts of the lib. Use them at you own risk.
klao@1951
   832
  ///
klao@1951
   833
  ///An adaptor for hiding edges from a graph.
klao@1951
   834
  ///This adaptor specializes SubGraphAdaptor in the way that
klao@1951
   835
  ///only the edge-set 
klao@1951
   836
  ///can be filtered. The usefulness of this adaptor is demonstrated in the 
klao@1951
   837
  ///problem of searching a maximum number of edge-disjoint shortest paths 
klao@1951
   838
  ///between 
klao@1951
   839
  ///two nodes \c s and \c t. Shortest here means being shortest w.r.t. 
klao@1951
   840
  ///non-negative edge-lengths. Note that 
klao@1951
   841
  ///the comprehension of the presented solution 
klao@1951
   842
  ///need's some elementary knowledge from combinatorial optimization. 
klao@1951
   843
  ///
klao@1951
   844
  ///If a single shortest path is to be 
klao@1951
   845
  ///searched between \c s and \c t, then this can be done easily by 
klao@1951
   846
  ///applying the Dijkstra algorithm. What happens, if a maximum number of 
klao@1951
   847
  ///edge-disjoint shortest paths is to be computed. It can be proved that an 
klao@1951
   848
  ///edge can be in a shortest path if and only
klao@1951
   849
  ///if it is tight with respect to 
klao@1951
   850
  ///the potential function computed by Dijkstra.
klao@1951
   851
  ///Moreover, any path containing 
klao@1951
   852
  ///only such edges is a shortest one.
klao@1951
   853
  ///Thus we have to compute a maximum number 
klao@1951
   854
  ///of edge-disjoint paths between \c s and \c t in
klao@1951
   855
  ///the graph which has edge-set 
klao@1951
   856
  ///all the tight edges. The computation will be demonstrated
klao@1951
   857
  ///on the following 
klao@1951
   858
  ///graph, which is read from the dimacs file \c sub_graph_adaptor_demo.dim. 
klao@1951
   859
  ///The full source code is available in \ref sub_graph_adaptor_demo.cc. 
klao@1951
   860
  ///If you are interested in more demo programs, you can use 
klao@1951
   861
  ///\ref dim_to_dot.cc to generate .dot files from dimacs files. 
klao@1951
   862
  ///The .dot file of the following figure was generated by  
klao@1951
   863
  ///the demo program \ref dim_to_dot.cc.
klao@1951
   864
  ///
klao@1951
   865
  ///\dot
klao@1951
   866
  ///digraph lemon_dot_example {
klao@1951
   867
  ///node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
klao@1951
   868
  ///n0 [ label="0 (s)" ];
klao@1951
   869
  ///n1 [ label="1" ];
klao@1951
   870
  ///n2 [ label="2" ];
klao@1951
   871
  ///n3 [ label="3" ];
klao@1951
   872
  ///n4 [ label="4" ];
klao@1951
   873
  ///n5 [ label="5" ];
klao@1951
   874
  ///n6 [ label="6 (t)" ];
klao@1951
   875
  ///edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
klao@1951
   876
  ///n5 ->  n6 [ label="9, length:4" ];
klao@1951
   877
  ///n4 ->  n6 [ label="8, length:2" ];
klao@1951
   878
  ///n3 ->  n5 [ label="7, length:1" ];
klao@1951
   879
  ///n2 ->  n5 [ label="6, length:3" ];
klao@1951
   880
  ///n2 ->  n6 [ label="5, length:5" ];
klao@1951
   881
  ///n2 ->  n4 [ label="4, length:2" ];
klao@1951
   882
  ///n1 ->  n4 [ label="3, length:3" ];
klao@1951
   883
  ///n0 ->  n3 [ label="2, length:1" ];
klao@1951
   884
  ///n0 ->  n2 [ label="1, length:2" ];
klao@1951
   885
  ///n0 ->  n1 [ label="0, length:3" ];
klao@1951
   886
  ///}
klao@1951
   887
  ///\enddot
klao@1951
   888
  ///
klao@1951
   889
  ///\code
klao@1951
   890
  ///Graph g;
klao@1951
   891
  ///Node s, t;
klao@1951
   892
  ///LengthMap length(g);
klao@1951
   893
  ///
klao@1951
   894
  ///readDimacs(std::cin, g, length, s, t);
klao@1951
   895
  ///
klao@1951
   896
  ///cout << "edges with lengths (of form id, source--length->target): " << endl;
klao@1951
   897
  ///for(EdgeIt e(g); e!=INVALID; ++e) 
klao@1951
   898
  ///  cout << g.id(e) << ", " << g.id(g.source(e)) << "--" 
klao@1951
   899
  ///       << length[e] << "->" << g.id(g.target(e)) << endl;
klao@1951
   900
  ///
klao@1951
   901
  ///cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
klao@1951
   902
  ///\endcode
klao@1951
   903
  ///Next, the potential function is computed with Dijkstra.
klao@1951
   904
  ///\code
klao@1951
   905
  ///typedef Dijkstra<Graph, LengthMap> Dijkstra;
klao@1951
   906
  ///Dijkstra dijkstra(g, length);
klao@1951
   907
  ///dijkstra.run(s);
klao@1951
   908
  ///\endcode
klao@1951
   909
  ///Next, we consrtruct a map which filters the edge-set to the tight edges.
klao@1951
   910
  ///\code
klao@1951
   911
  ///typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap> 
klao@1951
   912
  ///  TightEdgeFilter;
klao@1951
   913
  ///TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
klao@1951
   914
  ///
deba@1991
   915
  ///typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGA;
deba@1991
   916
  ///SubGA ga(g, tight_edge_filter);
klao@1951
   917
  ///\endcode
klao@1951
   918
  ///Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed 
klao@1951
   919
  ///with a max flow algorithm Preflow.
klao@1951
   920
  ///\code
klao@1951
   921
  ///ConstMap<Edge, int> const_1_map(1);
klao@1951
   922
  ///Graph::EdgeMap<int> flow(g, 0);
klao@1951
   923
  ///
deba@1991
   924
  ///Preflow<SubGA, int, ConstMap<Edge, int>, Graph::EdgeMap<int> > 
deba@1991
   925
  ///  preflow(ga, s, t, const_1_map, flow);
klao@1951
   926
  ///preflow.run();
klao@1951
   927
  ///\endcode
klao@1951
   928
  ///Last, the output is:
klao@1951
   929
  ///\code  
klao@1951
   930
  ///cout << "maximum number of edge-disjoint shortest path: " 
klao@1951
   931
  ///     << preflow.flowValue() << endl;
klao@1951
   932
  ///cout << "edges of the maximum number of edge-disjoint shortest s-t paths: " 
klao@1951
   933
  ///     << endl;
klao@1951
   934
  ///for(EdgeIt e(g); e!=INVALID; ++e) 
klao@1951
   935
  ///  if (flow[e])
klao@1951
   936
  ///    cout << " " << g.id(g.source(e)) << "--"
klao@1951
   937
  ///         << length[e] << "->" << g.id(g.target(e)) << endl;
klao@1951
   938
  ///\endcode
klao@1951
   939
  ///The program has the following (expected :-)) output:
klao@1951
   940
  ///\code
klao@1951
   941
  ///edges with lengths (of form id, source--length->target):
klao@1951
   942
  /// 9, 5--4->6
klao@1951
   943
  /// 8, 4--2->6
klao@1951
   944
  /// 7, 3--1->5
klao@1951
   945
  /// 6, 2--3->5
klao@1951
   946
  /// 5, 2--5->6
klao@1951
   947
  /// 4, 2--2->4
klao@1951
   948
  /// 3, 1--3->4
klao@1951
   949
  /// 2, 0--1->3
klao@1951
   950
  /// 1, 0--2->2
klao@1951
   951
  /// 0, 0--3->1
klao@1951
   952
  ///s: 0 t: 6
klao@1951
   953
  ///maximum number of edge-disjoint shortest path: 2
klao@1951
   954
  ///edges of the maximum number of edge-disjoint shortest s-t paths:
klao@1951
   955
  /// 9, 5--4->6
klao@1951
   956
  /// 8, 4--2->6
klao@1951
   957
  /// 7, 3--1->5
klao@1951
   958
  /// 4, 2--2->4
klao@1951
   959
  /// 2, 0--1->3
klao@1951
   960
  /// 1, 0--2->2
klao@1951
   961
  ///\endcode
klao@1951
   962
  ///
klao@1951
   963
  ///\author Marton Makai
marci@932
   964
  template<typename Graph, typename EdgeFilterMap>
alpar@1401
   965
  class EdgeSubGraphAdaptor : 
alpar@1401
   966
    public SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>, 
deba@1681
   967
			   EdgeFilterMap, false> {
marci@932
   968
  public:
alpar@1401
   969
    typedef SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>, 
deba@1685
   970
			    EdgeFilterMap, false> Parent;
marci@932
   971
  protected:
marci@932
   972
    ConstMap<typename Graph::Node, bool> const_true_map;
deba@1991
   973
deba@1991
   974
    EdgeSubGraphAdaptor() : const_true_map(true) {
deba@1991
   975
      Parent::setNodeFilterMap(const_true_map);
deba@1991
   976
    }
deba@1991
   977
marci@932
   978
  public:
deba@2031
   979
alpar@1401
   980
    EdgeSubGraphAdaptor(Graph& _graph, EdgeFilterMap& _edge_filter_map) : 
marci@932
   981
      Parent(), const_true_map(true) { 
marci@932
   982
      Parent::setGraph(_graph);
marci@932
   983
      Parent::setNodeFilterMap(const_true_map);
marci@932
   984
      Parent::setEdgeFilterMap(_edge_filter_map);
marci@932
   985
    }
deba@2031
   986
marci@932
   987
  };
marci@932
   988
deba@1991
   989
  /// \brief Just gives back an edge sub graph adaptor
deba@1991
   990
  ///
deba@1991
   991
  /// Just gives back an edge sub graph adaptor
deba@1991
   992
  template<typename Graph, typename EdgeFilterMap>
deba@1991
   993
  EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>
deba@1991
   994
  edgeSubGraphAdaptor(const Graph& graph, EdgeFilterMap& efm) {
deba@1991
   995
    return EdgeSubGraphAdaptor<const Graph, EdgeFilterMap>(graph, efm);
deba@1991
   996
  }
deba@1991
   997
deba@1991
   998
  template<typename Graph, typename EdgeFilterMap>
deba@1991
   999
  EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>
deba@1991
  1000
  edgeSubGraphAdaptor(const Graph& graph, const EdgeFilterMap& efm) {
deba@1991
  1001
    return EdgeSubGraphAdaptor<const Graph, const EdgeFilterMap>(graph, efm);
deba@1991
  1002
  }
deba@1991
  1003
deba@1991
  1004
  template <typename _Graph, typename Enable = void>
deba@1980
  1005
  class UndirGraphAdaptorBase : 
deba@1979
  1006
    public UGraphBaseExtender<GraphAdaptorBase<_Graph> > {
marci@1383
  1007
  public:
marci@1383
  1008
    typedef _Graph Graph;
deba@2031
  1009
    typedef UndirGraphAdaptorBase Adaptor;
deba@1979
  1010
    typedef UGraphBaseExtender<GraphAdaptorBase<_Graph> > Parent;
deba@1991
  1011
marci@1383
  1012
  protected:
deba@1991
  1013
deba@1991
  1014
    UndirGraphAdaptorBase() : Parent() {}
deba@1991
  1015
marci@1383
  1016
  public:
deba@1991
  1017
klao@1909
  1018
    typedef typename Parent::UEdge UEdge;
marci@1383
  1019
    typedef typename Parent::Edge Edge;
deba@1991
  1020
deba@2031
  1021
  private:
marci@1383
  1022
    
deba@1991
  1023
    template <typename _Value>
deba@2031
  1024
    class EdgeMapBase {
deba@1991
  1025
    private:
deba@1991
  1026
      
deba@1991
  1027
      typedef typename _Graph::template EdgeMap<_Value> MapImpl;
deba@1991
  1028
      
marci@1383
  1029
    public:
deba@1991
  1030
deba@1991
  1031
      typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
deba@1991
  1032
deba@1991
  1033
      typedef _Value Value;
marci@1383
  1034
      typedef Edge Key;
marci@1383
  1035
      
deba@2031
  1036
      EdgeMapBase(const Adaptor& adaptor) :
deba@2031
  1037
	forward_map(*adaptor.graph), backward_map(*adaptor.graph) {}
marci@569
  1038
deba@2031
  1039
      EdgeMapBase(const Adaptor& adaptor, const Value& v) 
deba@2031
  1040
        : forward_map(*adaptor.graph, v), backward_map(*adaptor.graph, v) {}
marci@1383
  1041
      
deba@1991
  1042
      void set(const Edge& e, const Value& a) { 
deba@1991
  1043
	if (Parent::direction(e)) {
marci@1383
  1044
	  forward_map.set(e, a); 
deba@1991
  1045
        } else { 
deba@1991
  1046
	  backward_map.set(e, a);
deba@1991
  1047
        } 
marci@1383
  1048
      }
marci@556
  1049
deba@1991
  1050
      typename MapTraits<MapImpl>::ConstReturnValue operator[](Edge e) const { 
deba@1991
  1051
	if (Parent::direction(e)) {
marci@1383
  1052
	  return forward_map[e]; 
deba@1991
  1053
	} else { 
marci@1383
  1054
	  return backward_map[e]; 
deba@1991
  1055
        }
marci@556
  1056
      }
deba@1991
  1057
deba@1991
  1058
      typename MapTraits<MapImpl>::ReturnValue operator[](Edge e) { 
deba@1991
  1059
	if (Parent::direction(e)) {
deba@1991
  1060
	  return forward_map[e]; 
deba@1991
  1061
	} else { 
deba@1991
  1062
	  return backward_map[e]; 
deba@1991
  1063
        }
deba@1991
  1064
      }
deba@1991
  1065
deba@1991
  1066
    protected:
deba@1991
  1067
deba@1991
  1068
      MapImpl forward_map, backward_map; 
deba@1991
  1069
marci@556
  1070
    };
deba@2031
  1071
deba@2031
  1072
  public:
deba@2031
  1073
deba@2031
  1074
    template <typename _Value>
deba@2031
  1075
    class EdgeMap 
deba@2031
  1076
      : public SubMapExtender<Adaptor, EdgeMapBase<_Value> > 
deba@2031
  1077
    {
deba@2031
  1078
    public:
deba@2031
  1079
      typedef Adaptor Graph;
deba@2031
  1080
      typedef SubMapExtender<Adaptor, EdgeMapBase<_Value> > Parent;
deba@2031
  1081
    
deba@2031
  1082
      EdgeMap(const Graph& graph) 
deba@2031
  1083
	: Parent(graph) {}
deba@2031
  1084
      EdgeMap(const Graph& graph, const _Value& value) 
deba@2031
  1085
	: Parent(graph, value) {}
deba@2031
  1086
    
deba@2031
  1087
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2031
  1088
	return operator=<EdgeMap>(cmap);
deba@2031
  1089
      }
deba@2031
  1090
    
deba@2031
  1091
      template <typename CMap>
deba@2031
  1092
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
  1093
        Parent::operator=(cmap);
deba@2031
  1094
	return *this;
deba@2031
  1095
      }
deba@2031
  1096
    };
marci@1383
  1097
        
deba@1991
  1098
    template <typename _Value>
deba@2031
  1099
    class UEdgeMap : public Graph::template EdgeMap<_Value> {
marci@1383
  1100
    public:
deba@2031
  1101
      
deba@2031
  1102
      typedef typename Graph::template EdgeMap<_Value> Parent;
deba@2031
  1103
      
deba@2031
  1104
      explicit UEdgeMap(const Adaptor& ga) 
deba@2031
  1105
	: Parent(*ga.graph) {}
deba@1991
  1106
deba@2031
  1107
      UEdgeMap(const Adaptor& ga, const _Value& value)
deba@2031
  1108
	: Parent(*ga.graph, value) {}
deba@1991
  1109
deba@2031
  1110
      UEdgeMap& operator=(const UEdgeMap& cmap) {
deba@2031
  1111
        return operator=<UEdgeMap>(cmap);
deba@2031
  1112
      }
deba@1991
  1113
deba@2031
  1114
      template <typename CMap>
deba@2031
  1115
      UEdgeMap& operator=(const CMap& cmap) {
deba@2031
  1116
        Parent::operator=(cmap);
deba@2031
  1117
        return *this;
deba@2031
  1118
      }
deba@2031
  1119
deba@1991
  1120
    };
deba@1991
  1121
      
deba@1991
  1122
  };
marci@556
  1123
deba@1991
  1124
  template <typename _Graph>
deba@1991
  1125
  class UndirGraphAdaptorBase<
deba@1991
  1126
    _Graph, typename enable_if<
deba@1991
  1127
    typename _Graph::EdgeNotifier::Notifier>::type > 
deba@1991
  1128
      : public UGraphBaseExtender<GraphAdaptorBase<_Graph> > {
deba@1991
  1129
  public:
deba@1991
  1130
deba@1991
  1131
    typedef _Graph Graph;
deba@2031
  1132
    typedef UndirGraphAdaptorBase Adaptor;
deba@1991
  1133
    typedef UGraphBaseExtender<GraphAdaptorBase<_Graph> > Parent;
deba@1991
  1134
deba@1991
  1135
  protected:
deba@1991
  1136
deba@1991
  1137
    UndirGraphAdaptorBase() 
deba@1999
  1138
      : edge_notifier(*this), edge_notifier_proxy(edge_notifier) {}
deba@1991
  1139
deba@1991
  1140
    void setGraph(_Graph& graph) {
deba@1991
  1141
      Parent::setGraph(graph);
deba@1991
  1142
      edge_notifier_proxy.setUEdgeNotifier(graph.getNotifier(UEdge()));
deba@1991
  1143
    }
deba@1991
  1144
deba@1991
  1145
  public:
deba@1991
  1146
deba@1991
  1147
    ~UndirGraphAdaptorBase() {
deba@1999
  1148
      edge_notifier.clear();
deba@1999
  1149
    }
deba@1999
  1150
deba@1999
  1151
    int maxId(typename Parent::Edge) const {
deba@1999
  1152
      return Parent::maxEdgeId();
deba@1991
  1153
    }
deba@1991
  1154
deba@1991
  1155
deba@1991
  1156
    typedef typename Parent::UEdge UEdge;
deba@1991
  1157
    typedef typename Parent::Edge Edge;
deba@1991
  1158
deba@1991
  1159
    typedef typename Parent::EdgeNotifier UEdgeNotifier;
deba@1991
  1160
deba@1991
  1161
    using Parent::getNotifier;
deba@1991
  1162
deba@1999
  1163
    typedef AlterationNotifier<UndirGraphAdaptorBase, Edge> EdgeNotifier;
deba@1991
  1164
    EdgeNotifier& getNotifier(Edge) const { return edge_notifier; }
deba@1991
  1165
deba@1991
  1166
  protected:
deba@1991
  1167
deba@1991
  1168
    class NotifierProxy : public UEdgeNotifier::ObserverBase {
deba@1991
  1169
    public:
deba@1991
  1170
deba@1991
  1171
      typedef typename UEdgeNotifier::ObserverBase Parent;
deba@1991
  1172
      typedef UndirGraphAdaptorBase AdaptorBase;
marci@1383
  1173
      
deba@1991
  1174
      NotifierProxy(EdgeNotifier& _edge_notifier)
deba@1991
  1175
        : Parent(), edge_notifier(_edge_notifier) {
marci@1383
  1176
      }
marci@556
  1177
deba@1991
  1178
      virtual ~NotifierProxy() {
deba@1991
  1179
        if (Parent::attached()) {
deba@1991
  1180
          Parent::detach();
deba@1991
  1181
        }
marci@1383
  1182
      }
deba@1991
  1183
deba@1991
  1184
      void setUEdgeNotifier(UEdgeNotifier& _uedge_notifier) {
deba@1991
  1185
        Parent::attach(_uedge_notifier);
deba@1991
  1186
      }
deba@1991
  1187
deba@1991
  1188
      
deba@1991
  1189
    protected:
deba@1991
  1190
deba@1991
  1191
      virtual void add(const UEdge& uedge) {
deba@1991
  1192
        std::vector<Edge> edges;
deba@1991
  1193
        edges.push_back(AdaptorBase::Parent::direct(uedge, true));
deba@1991
  1194
        edges.push_back(AdaptorBase::Parent::direct(uedge, false));
deba@1991
  1195
        edge_notifier.add(edges);
deba@1991
  1196
      }
deba@1991
  1197
      virtual void add(const std::vector<UEdge>& uedges) {
deba@1991
  1198
        std::vector<Edge> edges;
deba@1991
  1199
        for (int i = 0; i < (int)uedges.size(); ++i) { 
deba@1991
  1200
          edges.push_back(AdaptorBase::Parent::direct(uedges[i], true));
deba@1991
  1201
          edges.push_back(AdaptorBase::Parent::direct(uedges[i], false));
deba@1991
  1202
        }
deba@1991
  1203
        edge_notifier.add(edges);
deba@1991
  1204
      }
deba@1991
  1205
      virtual void erase(const UEdge& uedge) {
deba@1991
  1206
        std::vector<Edge> edges;
deba@1991
  1207
        edges.push_back(AdaptorBase::Parent::direct(uedge, true));
deba@1991
  1208
        edges.push_back(AdaptorBase::Parent::direct(uedge, false));
deba@1991
  1209
        edge_notifier.erase(edges);
deba@1991
  1210
      }
deba@1991
  1211
      virtual void erase(const std::vector<UEdge>& uedges) {
deba@1991
  1212
        std::vector<Edge> edges;
deba@1991
  1213
        for (int i = 0; i < (int)uedges.size(); ++i) { 
deba@1991
  1214
          edges.push_back(AdaptorBase::Parent::direct(uedges[i], true));
deba@1991
  1215
          edges.push_back(AdaptorBase::Parent::direct(uedges[i], false));
deba@1991
  1216
        }
deba@1991
  1217
        edge_notifier.erase(edges);
deba@1991
  1218
      }
deba@1991
  1219
      virtual void build() {
deba@1991
  1220
        edge_notifier.build();
deba@1991
  1221
      }
deba@1991
  1222
      virtual void clear() {
deba@1991
  1223
        edge_notifier.clear();
deba@1991
  1224
      }
deba@1991
  1225
deba@1991
  1226
      EdgeNotifier& edge_notifier;
deba@1991
  1227
    };
deba@1991
  1228
deba@1991
  1229
deba@1991
  1230
    mutable EdgeNotifier edge_notifier;
deba@1991
  1231
    NotifierProxy edge_notifier_proxy;
deba@1991
  1232
deba@2031
  1233
  private:
deba@1991
  1234
    
deba@1991
  1235
    template <typename _Value>
deba@2031
  1236
    class EdgeMapBase {
deba@1991
  1237
    private:
deba@1991
  1238
      
deba@1991
  1239
      typedef typename _Graph::template EdgeMap<_Value> MapImpl;
deba@1991
  1240
      
deba@1991
  1241
    public:
deba@1991
  1242
deba@1991
  1243
      typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag;
deba@1991
  1244
deba@1991
  1245
      typedef _Value Value;
deba@1991
  1246
      typedef Edge Key;
deba@1991
  1247
      
deba@2031
  1248
      EdgeMapBase(const Adaptor& adaptor) :
deba@2031
  1249
	forward_map(*adaptor.graph), backward_map(*adaptor.graph) {}
deba@1991
  1250
deba@2031
  1251
      EdgeMapBase(const Adaptor& adaptor, const Value& v) 
deba@2031
  1252
        : forward_map(*adaptor.graph, v), backward_map(*adaptor.graph, v) {}
deba@1991
  1253
      
deba@1991
  1254
      void set(const Edge& e, const Value& a) { 
deba@1991
  1255
	if (Parent::direction(e)) {
deba@1991
  1256
	  forward_map.set(e, a); 
deba@1991
  1257
        } else { 
deba@1991
  1258
	  backward_map.set(e, a);
deba@1991
  1259
        } 
deba@1991
  1260
      }
deba@1991
  1261
deba@2031
  1262
      typename MapTraits<MapImpl>::ConstReturnValue operator[](Edge e) const { 
deba@1991
  1263
	if (Parent::direction(e)) {
deba@1991
  1264
	  return forward_map[e]; 
deba@1991
  1265
	} else { 
deba@1991
  1266
	  return backward_map[e]; 
deba@1991
  1267
        }
deba@1991
  1268
      }
deba@1991
  1269
deba@2031
  1270
      typename MapTraits<MapImpl>::ReturnValue operator[](Edge e) { 
deba@1991
  1271
	if (Parent::direction(e)) {
deba@1991
  1272
	  return forward_map[e]; 
deba@1991
  1273
	} else { 
deba@1991
  1274
	  return backward_map[e]; 
deba@1991
  1275
        }
deba@1991
  1276
      }
deba@1991
  1277
deba@1991
  1278
    protected:
deba@1991
  1279
deba@1991
  1280
      MapImpl forward_map, backward_map; 
deba@1991
  1281
deba@1991
  1282
    };
deba@2031
  1283
deba@2031
  1284
  public:
deba@2031
  1285
deba@2031
  1286
    template <typename _Value>
deba@2031
  1287
    class EdgeMap 
deba@2031
  1288
      : public SubMapExtender<Adaptor, EdgeMapBase<_Value> > 
deba@2031
  1289
    {
deba@2031
  1290
    public:
deba@2031
  1291
      typedef Adaptor Graph;
deba@2031
  1292
      typedef SubMapExtender<Adaptor, EdgeMapBase<_Value> > Parent;
deba@2031
  1293
    
deba@2031
  1294
      EdgeMap(const Graph& graph) 
deba@2031
  1295
	: Parent(graph) {}
deba@2031
  1296
      EdgeMap(const Graph& graph, const _Value& value) 
deba@2031
  1297
	: Parent(graph, value) {}
deba@2031
  1298
    
deba@2031
  1299
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2031
  1300
	return operator=<EdgeMap>(cmap);
deba@2031
  1301
      }
deba@2031
  1302
    
deba@2031
  1303
      template <typename CMap>
deba@2031
  1304
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
  1305
        Parent::operator=(cmap);
deba@2031
  1306
	return *this;
deba@2031
  1307
      }
deba@2031
  1308
    };
deba@1991
  1309
        
deba@1991
  1310
    template <typename _Value>
deba@2031
  1311
    class UEdgeMap : public Graph::template EdgeMap<_Value> {
deba@1991
  1312
    public:
deba@2031
  1313
      
deba@2031
  1314
      typedef typename Graph::template EdgeMap<_Value> Parent;
deba@2031
  1315
      
deba@2031
  1316
      explicit UEdgeMap(const Adaptor& ga) 
deba@2031
  1317
	: Parent(*ga.graph) {}
deba@1991
  1318
deba@2031
  1319
      UEdgeMap(const Adaptor& ga, const _Value& value)
deba@2031
  1320
	: Parent(*ga.graph, value) {}
deba@1991
  1321
deba@2031
  1322
      UEdgeMap& operator=(const UEdgeMap& cmap) {
deba@2031
  1323
        return operator=<UEdgeMap>(cmap);
deba@2031
  1324
      }
deba@1991
  1325
deba@2031
  1326
      template <typename CMap>
deba@2031
  1327
      UEdgeMap& operator=(const CMap& cmap) {
deba@2031
  1328
        Parent::operator=(cmap);
deba@2031
  1329
        return *this;
deba@2031
  1330
      }
deba@2031
  1331
marci@1383
  1332
    };
marci@1383
  1333
      
marci@1383
  1334
  };
marci@1383
  1335
klao@1951
  1336
  ///\brief An undirected graph is made from a directed graph by an adaptor
klao@1951
  1337
  ///\ingroup graph_adaptors
klao@1951
  1338
  ///
klao@1951
  1339
  /// Undocumented, untested!!!
klao@1951
  1340
  /// If somebody knows nice demo application, let's polulate it.
klao@1951
  1341
  /// 
klao@1951
  1342
  /// \author Marton Makai
marci@1383
  1343
  template<typename _Graph>
deba@1980
  1344
  class UndirGraphAdaptor : 
deba@1979
  1345
    public UGraphAdaptorExtender<
deba@1980
  1346
    UndirGraphAdaptorBase<_Graph> > {
marci@1383
  1347
  public:
marci@1383
  1348
    typedef _Graph Graph;
deba@1979
  1349
    typedef UGraphAdaptorExtender<
deba@1980
  1350
      UndirGraphAdaptorBase<_Graph> > Parent;
marci@1383
  1351
  protected:
deba@1980
  1352
    UndirGraphAdaptor() { }
marci@1383
  1353
  public:
deba@1980
  1354
    UndirGraphAdaptor(_Graph& _graph) { 
marci@1383
  1355
      setGraph(_graph);
marci@556
  1356
    }
marci@556
  1357
deba@1991
  1358
    template <typename _ForwardMap, typename _BackwardMap>
deba@1991
  1359
    class CombinedEdgeMap {
deba@1991
  1360
    public:
deba@1991
  1361
      
deba@1991
  1362
      typedef _ForwardMap ForwardMap;
deba@1991
  1363
      typedef _BackwardMap BackwardMap;
marci@992
  1364
deba@1991
  1365
      typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag;
marci@992
  1366
deba@1991
  1367
      typedef typename ForwardMap::Value Value;
deba@1991
  1368
      typedef typename Parent::Edge Key;
deba@1991
  1369
      
deba@1991
  1370
      CombinedEdgeMap() : forward_map(0), backward_map(0) {}
marci@992
  1371
deba@1991
  1372
      CombinedEdgeMap(ForwardMap& _forward_map, BackwardMap& _backward_map) 
deba@1991
  1373
        : forward_map(&_forward_map), backward_map(&_backward_map) {}
marci@992
  1374
      
deba@1991
  1375
      void set(const Key& e, const Value& a) { 
deba@1991
  1376
	if (Parent::direction(e)) {
deba@1991
  1377
	  forward_map->set(e, a); 
deba@1991
  1378
        } else { 
deba@1991
  1379
	  backward_map->set(e, a);
deba@1991
  1380
        } 
marci@992
  1381
      }
marci@992
  1382
deba@1991
  1383
      typename MapTraits<ForwardMap>::ConstReturnValue 
deba@1991
  1384
      operator[](const Key& e) const { 
deba@1991
  1385
	if (Parent::direction(e)) {
deba@1991
  1386
	  return (*forward_map)[e]; 
deba@1991
  1387
	} else { 
deba@1991
  1388
	  return (*backward_map)[e]; 
deba@1991
  1389
        }
marci@992
  1390
      }
marci@992
  1391
deba@1991
  1392
      typename MapTraits<ForwardMap>::ReturnValue 
deba@1991
  1393
      operator[](const Key& e) { 
deba@1991
  1394
	if (Parent::direction(e)) {
deba@1991
  1395
	  return (*forward_map)[e]; 
deba@1991
  1396
	} else { 
deba@1991
  1397
	  return (*backward_map)[e]; 
deba@1991
  1398
        }
marci@992
  1399
      }
deba@1991
  1400
deba@1991
  1401
      void setForwardMap(ForwardMap& _forward_map) {
deba@1991
  1402
        forward_map = &_forward_map;
deba@1991
  1403
      }
deba@1991
  1404
deba@1991
  1405
      void setBackwardMap(BackwardMap& _backward_map) {
deba@1991
  1406
        backward_map = &_backward_map;
deba@1991
  1407
      }
deba@1991
  1408
deba@1991
  1409
    protected:
deba@1991
  1410
deba@1991
  1411
      ForwardMap* forward_map;
deba@1991
  1412
      BackwardMap* backward_map; 
deba@1991
  1413
marci@992
  1414
    };
marci@992
  1415
marci@992
  1416
  };
marci@569
  1417
deba@1991
  1418
  /// \brief Just gives back an undir graph adaptor
klao@1951
  1419
  ///
deba@1991
  1420
  /// Just gives back an undir graph adaptor
marci@650
  1421
  template<typename Graph>
deba@1991
  1422
  UndirGraphAdaptor<const Graph>
deba@1991
  1423
  undirGraphAdaptor(const Graph& graph) {
deba@1991
  1424
    return UndirGraphAdaptor<const Graph>(graph);
deba@1991
  1425
  }
marci@650
  1426
deba@2034
  1427
  template<typename Graph, typename Number,  
deba@2034
  1428
           typename CapacityMap, typename FlowMap, 
deba@2034
  1429
           typename Tolerance = Tolerance<Number> >
marci@658
  1430
  class ResForwardFilter {
marci@650
  1431
    const CapacityMap* capacity;
marci@650
  1432
    const FlowMap* flow;
deba@2034
  1433
    Tolerance tolerance;
marci@650
  1434
  public:
deba@1991
  1435
    typedef typename Graph::Edge Key;
deba@1991
  1436
    typedef bool Value;
deba@1991
  1437
deba@2034
  1438
    ResForwardFilter(const CapacityMap& _capacity, const FlowMap& _flow,
deba@2034
  1439
                     const Tolerance& _tolerance = Tolerance()) 
deba@2034
  1440
      : capacity(&_capacity), flow(&_flow), tolerance(_tolerance) { }
deba@2034
  1441
deba@2034
  1442
    ResForwardFilter(const Tolerance& _tolerance) 
deba@2034
  1443
      : capacity(0), flow(0), tolerance(_tolerance)  { }
deba@2034
  1444
deba@1991
  1445
    void setCapacity(const CapacityMap& _capacity) { capacity = &_capacity; }
deba@1991
  1446
    void setFlow(const FlowMap& _flow) { flow = &_flow; }
deba@2034
  1447
marci@650
  1448
    bool operator[](const typename Graph::Edge& e) const {
deba@2034
  1449
      return tolerance.less((*flow)[e], (*capacity)[e]);
marci@650
  1450
    }
marci@650
  1451
  };
marci@650
  1452
marci@650
  1453
  template<typename Graph, typename Number,
deba@2034
  1454
	   typename CapacityMap, typename FlowMap,
deba@2034
  1455
           typename Tolerance = Tolerance<Number> >
marci@658
  1456
  class ResBackwardFilter {
marci@650
  1457
    const CapacityMap* capacity;
marci@650
  1458
    const FlowMap* flow;
deba@2034
  1459
    Tolerance tolerance;
marci@650
  1460
  public:
deba@1991
  1461
    typedef typename Graph::Edge Key;
deba@1991
  1462
    typedef bool Value;
deba@1991
  1463
deba@2034
  1464
    ResBackwardFilter(const CapacityMap& _capacity, const FlowMap& _flow,
deba@2034
  1465
                      const Tolerance& _tolerance = Tolerance())
deba@2034
  1466
      : capacity(&_capacity), flow(&_flow), tolerance(_tolerance) { }
deba@2034
  1467
    ResBackwardFilter(const Tolerance& _tolerance = Tolerance())
deba@2034
  1468
      : capacity(0), flow(0), tolerance(_tolerance) { }
deba@1991
  1469
    void setCapacity(const CapacityMap& _capacity) { capacity = &_capacity; }
deba@1991
  1470
    void setFlow(const FlowMap& _flow) { flow = &_flow; }
marci@650
  1471
    bool operator[](const typename Graph::Edge& e) const {
deba@2034
  1472
      return tolerance.less(0, Number((*flow)[e]));
marci@650
  1473
    }
marci@650
  1474
  };
marci@650
  1475
marci@653
  1476
  
klao@1951
  1477
  ///\brief An adaptor for composing the residual
klao@1951
  1478
  ///graph for directed flow and circulation problems.
deba@2037
  1479
  ///
klao@1951
  1480
  ///\ingroup graph_adaptors
klao@1951
  1481
  ///
klao@1951
  1482
  ///An adaptor for composing the residual graph for
klao@1951
  1483
  ///directed flow and circulation problems. 
deba@2037
  1484
//   ///Let \f$ G=(V, A) \f$ be a directed graph and let \f$ F \f$ be a 
deba@2037
  1485
//   ///number type. Let moreover 
deba@2037
  1486
//   ///\f$ f,c:A\to F \f$, be functions on the edge-set. 
deba@2037
  1487
//   ///In the appications of ResGraphAdaptor, \f$ f \f$ usually stands for a 
deba@2037
  1488
//   ///flow and \f$ c \f$ for a capacity function.   
deba@2037
  1489
//   ///Suppose that a graph instange \c g of type 
deba@2037
  1490
//   ///\c ListGraph implements \f$ G \f$.
deba@2037
  1491
//   ///\code
deba@2037
  1492
//   /// ListGraph g;
deba@2037
  1493
//   ///\endcode
deba@2037
  1494
//   ///Then RevGraphAdaptor implements the graph structure with node-set 
deba@2037
  1495
//   ///\f$ V \f$ and edge-set \f$ A_{forward}\cup A_{backward} \f$, where 
deba@2037
  1496
//   ///\f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and 
deba@2037
  1497
//   ///\f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$, 
deba@2037
  1498
//   ///i.e. the so called residual graph. 
deba@2037
  1499
//   ///When we take the union \f$ A_{forward}\cup A_{backward} \f$, 
deba@2037
  1500
//   ///multilicities are counted, i.e. if an edge is in both 
deba@2037
  1501
//   ///\f$ A_{forward} \f$ and \f$ A_{backward} \f$, then in the adaptor it 
deba@2037
  1502
//   ///appears twice. The following code shows how such an instance can be 
deba@2037
  1503
//   ///constructed.
deba@2037
  1504
//   ///\code
deba@2037
  1505
//   /// typedef ListGraph Graph;
deba@2037
  1506
//   /// Graph::EdgeMap<int> f(g);
deba@2037
  1507
//   /// Graph::EdgeMap<int> c(g);
deba@2037
  1508
//   /// ResGraphAdaptor<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > ga(g);
deba@2037
  1509
//   ///\endcode
deba@2037
  1510
//   ///\author Marton Makai
deba@2037
  1511
//   ///
marci@650
  1512
  template<typename Graph, typename Number, 
deba@2034
  1513
	   typename CapacityMap, typename FlowMap,
deba@2034
  1514
           typename Tolerance = Tolerance<Number> >
alpar@1401
  1515
  class ResGraphAdaptor : 
deba@1991
  1516
    public EdgeSubGraphAdaptor< 
deba@2034
  1517
    UndirGraphAdaptor<const Graph>, 
deba@2034
  1518
    typename UndirGraphAdaptor<const Graph>::template CombinedEdgeMap<
deba@2034
  1519
    ResForwardFilter<const Graph, Number, CapacityMap, FlowMap>,  
deba@2034
  1520
    ResBackwardFilter<const Graph, Number, CapacityMap, FlowMap> > > {
marci@650
  1521
  public:
deba@1991
  1522
deba@2034
  1523
    typedef UndirGraphAdaptor<const Graph> UGraph;
deba@1991
  1524
deba@2034
  1525
    typedef ResForwardFilter<const Graph, Number, CapacityMap, FlowMap> 
deba@1991
  1526
    ForwardFilter;
deba@1991
  1527
deba@2034
  1528
    typedef ResBackwardFilter<const Graph, Number, CapacityMap, FlowMap> 
deba@1991
  1529
    BackwardFilter;
deba@1991
  1530
deba@1991
  1531
    typedef typename UGraph::
deba@1991
  1532
    template CombinedEdgeMap<ForwardFilter, BackwardFilter>
deba@1991
  1533
    EdgeFilter;
deba@1991
  1534
deba@1991
  1535
    typedef EdgeSubGraphAdaptor<UGraph, EdgeFilter> Parent;
deba@1991
  1536
marci@650
  1537
  protected:
deba@1991
  1538
marci@650
  1539
    const CapacityMap* capacity;
marci@650
  1540
    FlowMap* flow;
deba@1991
  1541
deba@1991
  1542
    UGraph ugraph;
deba@1991
  1543
    ForwardFilter forward_filter;
deba@1991
  1544
    BackwardFilter backward_filter;
deba@1991
  1545
    EdgeFilter edge_filter;
deba@1991
  1546
marci@658
  1547
    void setCapacityMap(const CapacityMap& _capacity) {
marci@658
  1548
      capacity=&_capacity;
marci@658
  1549
      forward_filter.setCapacity(_capacity);
marci@658
  1550
      backward_filter.setCapacity(_capacity);
marci@658
  1551
    }
deba@1991
  1552
marci@658
  1553
    void setFlowMap(FlowMap& _flow) {
marci@658
  1554
      flow=&_flow;
marci@658
  1555
      forward_filter.setFlow(_flow);
marci@658
  1556
      backward_filter.setFlow(_flow);
marci@658
  1557
    }
deba@1991
  1558
marci@650
  1559
  public:
deba@1991
  1560
deba@2034
  1561
    /// \brief Constructor of the residual graph.
deba@2034
  1562
    ///
deba@2034
  1563
    /// Constructor of the residual graph. The parameters are the graph type,
deba@2034
  1564
    /// the flow map, the capacity map and a tolerance object.
deba@2034
  1565
    ResGraphAdaptor(const Graph& _graph, const CapacityMap& _capacity, 
deba@2034
  1566
                    FlowMap& _flow, const Tolerance& _tolerance = Tolerance()) 
deba@2034
  1567
      : Parent(), capacity(&_capacity), flow(&_flow), ugraph(_graph),
deba@2034
  1568
        forward_filter(_capacity, _flow, _tolerance), 
deba@2034
  1569
        backward_filter(_capacity, _flow, _tolerance),
deba@2034
  1570
        edge_filter(forward_filter, backward_filter)
deba@2034
  1571
    {
deba@1991
  1572
      Parent::setGraph(ugraph);
deba@1991
  1573
      Parent::setEdgeFilterMap(edge_filter);
marci@650
  1574
    }
marci@650
  1575
marci@660
  1576
    typedef typename Parent::Edge Edge;
marci@660
  1577
deba@2034
  1578
    /// \brief Gives back the residual capacity of the edge.
deba@2034
  1579
    ///
deba@2034
  1580
    /// Gives back the residual capacity of the edge.
deba@2034
  1581
    Number rescap(const Edge& edge) const {
deba@2034
  1582
      if (UGraph::direction(edge)) {
deba@2034
  1583
        return (*capacity)[edge]-(*flow)[edge]; 
deba@2034
  1584
      } else {
deba@2034
  1585
        return (*flow)[edge];
deba@2034
  1586
      }
deba@2034
  1587
    } 
deba@2034
  1588
deba@2034
  1589
    /// \brief Augment on the given edge in the residual graph.
deba@2034
  1590
    ///
deba@2034
  1591
    /// Augment on the given edge in the residual graph. It increase
deba@2034
  1592
    /// or decrease the flow on the original edge depend on the direction
deba@2034
  1593
    /// of the residual edge.
marci@660
  1594
    void augment(const Edge& e, Number a) const {
deba@1991
  1595
      if (UGraph::direction(e)) {
deba@2034
  1596
        flow->set(e, (*flow)[e] + a);
deba@1991
  1597
      } else {  
deba@2034
  1598
        flow->set(e, (*flow)[e] - a);
deba@1991
  1599
      }
marci@650
  1600
    }
marci@650
  1601
deba@2034
  1602
    /// \brief Returns the direction of the edge.
deba@2034
  1603
    ///
deba@2034
  1604
    /// Returns true when the edge is same oriented as the original edge.
deba@1991
  1605
    static bool forward(const Edge& e) {
deba@1991
  1606
      return UGraph::direction(e);
deba@1991
  1607
    }
deba@1991
  1608
deba@2034
  1609
    /// \brief Returns the direction of the edge.
deba@2034
  1610
    ///
deba@2034
  1611
    /// Returns true when the edge is opposite oriented as the original edge.
deba@1991
  1612
    static bool backward(const Edge& e) {
deba@1991
  1613
      return !UGraph::direction(e);
deba@1991
  1614
    }
deba@1991
  1615
deba@2034
  1616
    /// \brief Gives back the forward oriented residual edge.
deba@2034
  1617
    ///
deba@2034
  1618
    /// Gives back the forward oriented residual edge.
deba@1991
  1619
    static Edge forward(const typename Graph::Edge& e) {
deba@1991
  1620
      return UGraph::direct(e, true);
deba@1991
  1621
    }
deba@1991
  1622
deba@2034
  1623
    /// \brief Gives back the backward oriented residual edge.
deba@2034
  1624
    ///
deba@2034
  1625
    /// Gives back the backward oriented residual edge.
deba@1991
  1626
    static Edge backward(const typename Graph::Edge& e) {
deba@1991
  1627
      return UGraph::direct(e, false);
deba@1991
  1628
    }
deba@1991
  1629
klao@1951
  1630
    /// \brief Residual capacity map.
klao@1951
  1631
    ///
klao@1951
  1632
    /// In generic residual graphs the residual capacity can be obtained 
klao@1951
  1633
    /// as a map. 
marci@660
  1634
    class ResCap {
marci@660
  1635
    protected:
deba@1991
  1636
      const ResGraphAdaptor* res_graph;
marci@660
  1637
    public:
alpar@987
  1638
      typedef Number Value;
alpar@987
  1639
      typedef Edge Key;
deba@1991
  1640
      ResCap(const ResGraphAdaptor& _res_graph) 
deba@1991
  1641
        : res_graph(&_res_graph) {}
deba@1991
  1642
      
deba@2034
  1643
      Number operator[](const Edge& e) const {
deba@2034
  1644
        return res_graph->rescap(e);
marci@660
  1645
      }
deba@1991
  1646
      
marci@660
  1647
    };
marci@660
  1648
marci@650
  1649
  };
marci@650
  1650
marci@650
  1651
marci@998
  1652
marci@998
  1653
  template <typename _Graph, typename FirstOutEdgesMap>
alpar@1401
  1654
  class ErasingFirstGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
marci@998
  1655
  public:
marci@998
  1656
    typedef _Graph Graph;
alpar@1401
  1657
    typedef GraphAdaptorBase<_Graph> Parent;
marci@998
  1658
  protected:
marci@998
  1659
    FirstOutEdgesMap* first_out_edges;
alpar@1401
  1660
    ErasingFirstGraphAdaptorBase() : Parent(), 
marci@998
  1661
				     first_out_edges(0) { }
marci@998
  1662
marci@998
  1663
    void setFirstOutEdgesMap(FirstOutEdgesMap& _first_out_edges) {
marci@998
  1664
      first_out_edges=&_first_out_edges;
marci@998
  1665
    }
marci@998
  1666
marci@998
  1667
  public:
marci@998
  1668
marci@998
  1669
    typedef typename Parent::Node Node;
marci@998
  1670
    typedef typename Parent::Edge Edge;
marci@998
  1671
marci@998
  1672
    void firstOut(Edge& i, const Node& n) const { 
marci@998
  1673
      i=(*first_out_edges)[n];
marci@998
  1674
    }
marci@998
  1675
marci@998
  1676
    void erase(const Edge& e) const {
marci@998
  1677
      Node n=source(e);
marci@998
  1678
      Edge f=e;
marci@998
  1679
      Parent::nextOut(f);
marci@998
  1680
      first_out_edges->set(n, f);
marci@998
  1681
    }    
marci@998
  1682
  };
marci@998
  1683
marci@998
  1684
klao@1951
  1685
  ///\brief For blocking flows.
klao@1951
  1686
  ///\ingroup graph_adaptors
klao@1951
  1687
  ///
klao@1951
  1688
  ///\warning Graph adaptors are in even more
klao@1951
  1689
  ///experimental state than the other
klao@1951
  1690
  ///parts of the lib. Use them at you own risk.
klao@1951
  1691
  ///
klao@1951
  1692
  ///This graph adaptor is used for on-the-fly 
klao@1951
  1693
  ///Dinits blocking flow computations.
klao@1951
  1694
  ///For each node, an out-edge is stored which is used when the 
klao@1951
  1695
  ///\code
klao@1951
  1696
  ///OutEdgeIt& first(OutEdgeIt&, const Node&)
klao@1951
  1697
  ///\endcode
klao@1951
  1698
  ///is called. 
klao@1951
  1699
  ///
klao@1951
  1700
  ///\author Marton Makai
klao@1951
  1701
  ///
marci@998
  1702
  template <typename _Graph, typename FirstOutEdgesMap>
alpar@1401
  1703
  class ErasingFirstGraphAdaptor : 
deba@1979
  1704
    public GraphAdaptorExtender<
alpar@1401
  1705
    ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > {
marci@650
  1706
  public:
marci@998
  1707
    typedef _Graph Graph;
deba@1979
  1708
    typedef GraphAdaptorExtender<
alpar@1401
  1709
      ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > Parent;
alpar@1401
  1710
    ErasingFirstGraphAdaptor(Graph& _graph, 
marci@998
  1711
			     FirstOutEdgesMap& _first_out_edges) { 
marci@998
  1712
      setGraph(_graph);
marci@998
  1713
      setFirstOutEdgesMap(_first_out_edges);
marci@998
  1714
    } 
marci@1019
  1715
marci@998
  1716
  };
marci@556
  1717
deba@1989
  1718
//   template <typename _Graph>
deba@1989
  1719
//   class SplitGraphAdaptorBase 
deba@1989
  1720
//     : public GraphAdaptorBase<_Graph> {
deba@1989
  1721
//   public:
deba@1989
  1722
//     typedef GraphAdaptorBase<_Graph> Parent;
deba@1697
  1723
deba@1989
  1724
//     class Node;
deba@1989
  1725
//     class Edge;
deba@1989
  1726
//     template <typename T> class NodeMap;
deba@1989
  1727
//     template <typename T> class EdgeMap;
deba@1697
  1728
    
deba@1697
  1729
deba@1989
  1730
//     class Node : public Parent::Node {
deba@1989
  1731
//       friend class SplitGraphAdaptorBase;
deba@1989
  1732
//       template <typename T> friend class NodeMap;
deba@1989
  1733
//       typedef typename Parent::Node NodeParent;
deba@1989
  1734
//     private:
deba@1697
  1735
deba@1989
  1736
//       bool entry;
deba@1989
  1737
//       Node(typename Parent::Node _node, bool _entry)
deba@1989
  1738
// 	: Parent::Node(_node), entry(_entry) {}
deba@1697
  1739
      
deba@1989
  1740
//     public:
deba@1989
  1741
//       Node() {}
deba@1989
  1742
//       Node(Invalid) : NodeParent(INVALID), entry(true) {}
deba@1697
  1743
deba@1989
  1744
//       bool operator==(const Node& node) const {
deba@1989
  1745
// 	return NodeParent::operator==(node) && entry == node.entry;
deba@1989
  1746
//       }
deba@1697
  1747
      
deba@1989
  1748
//       bool operator!=(const Node& node) const {
deba@1989
  1749
// 	return !(*this == node);
deba@1989
  1750
//       }
deba@1697
  1751
      
deba@1989
  1752
//       bool operator<(const Node& node) const {
deba@1989
  1753
// 	return NodeParent::operator<(node) || 
deba@1989
  1754
// 	  (NodeParent::operator==(node) && entry < node.entry);
deba@1989
  1755
//       }
deba@1989
  1756
//     };
deba@1697
  1757
deba@1989
  1758
//     /// \todo May we want VARIANT/union type
deba@1989
  1759
//     class Edge : public Parent::Edge {
deba@1989
  1760
//       friend class SplitGraphAdaptorBase;
deba@1989
  1761
//       template <typename T> friend class EdgeMap;
deba@1989
  1762
//     private:
deba@1989
  1763
//       typedef typename Parent::Edge EdgeParent;
deba@1989
  1764
//       typedef typename Parent::Node NodeParent;
deba@1989
  1765
//       NodeParent bind;
deba@1697
  1766
deba@1989
  1767
//       Edge(const EdgeParent& edge, const NodeParent& node)
deba@1989
  1768
// 	: EdgeParent(edge), bind(node) {}
deba@1989
  1769
//     public:
deba@1989
  1770
//       Edge() {}
deba@1989
  1771
//       Edge(Invalid) : EdgeParent(INVALID), bind(INVALID) {}
deba@1697
  1772
deba@1989
  1773
//       bool operator==(const Edge& edge) const {
deba@1989
  1774
// 	return EdgeParent::operator==(edge) && bind == edge.bind;
deba@1989
  1775
//       }
deba@1697
  1776
      
deba@1989
  1777
//       bool operator!=(const Edge& edge) const {
deba@1989
  1778
// 	return !(*this == edge);
deba@1989
  1779
//       }
deba@1697
  1780
      
deba@1989
  1781
//       bool operator<(const Edge& edge) const {
deba@1989
  1782
// 	return EdgeParent::operator<(edge) || 
deba@1989
  1783
// 	  (EdgeParent::operator==(edge) && bind < edge.bind);
deba@1989
  1784
//       }
deba@1989
  1785
//     };
deba@1697
  1786
deba@1989
  1787
//     void first(Node& node) const {
deba@1989
  1788
//       Parent::first(node);
deba@1989
  1789
//       node.entry = true;
deba@1989
  1790
//     } 
deba@1697
  1791
deba@1989
  1792
//     void next(Node& node) const {
deba@1989
  1793
//       if (node.entry) {
deba@1989
  1794
// 	node.entry = false;
deba@1989
  1795
//       } else {
deba@1989
  1796
// 	node.entry = true;
deba@1989
  1797
// 	Parent::next(node);
deba@1989
  1798
//       }
deba@1989
  1799
//     }
deba@1697
  1800
deba@1989
  1801
//     void first(Edge& edge) const {
deba@1989
  1802
//       Parent::first(edge);
deba@1989
  1803
//       if ((typename Parent::Edge&)edge == INVALID) {
deba@1989
  1804
// 	Parent::first(edge.bind);
deba@1989
  1805
//       } else {
deba@1989
  1806
// 	edge.bind = INVALID;
deba@1989
  1807
//       }
deba@1989
  1808
//     }
deba@1697
  1809
deba@1989
  1810
//     void next(Edge& edge) const {
deba@1989
  1811
//       if ((typename Parent::Edge&)edge != INVALID) {
deba@1989
  1812
// 	Parent::next(edge);
deba@1989
  1813
// 	if ((typename Parent::Edge&)edge == INVALID) {
deba@1989
  1814
// 	  Parent::first(edge.bind);
deba@1989
  1815
// 	}
deba@1989
  1816
//       } else {
deba@1989
  1817
// 	Parent::next(edge.bind);
deba@1989
  1818
//       }      
deba@1989
  1819
//     }
deba@1697
  1820
deba@1989
  1821
//     void firstIn(Edge& edge, const Node& node) const {
deba@1989
  1822
//       if (node.entry) {
deba@1989
  1823
// 	Parent::firstIn(edge, node);
deba@1989
  1824
// 	edge.bind = INVALID;
deba@1989
  1825
//       } else {
deba@1989
  1826
// 	(typename Parent::Edge&)edge = INVALID;
deba@1989
  1827
// 	edge.bind = node;
deba@1989
  1828
//       }
deba@1989
  1829
//     }
deba@1697
  1830
deba@1989
  1831
//     void nextIn(Edge& edge) const {
deba@1989
  1832
//       if ((typename Parent::Edge&)edge != INVALID) {
deba@1989
  1833
// 	Parent::nextIn(edge);
deba@1989
  1834
//       } else {
deba@1989
  1835
// 	edge.bind = INVALID;
deba@1989
  1836
//       }      
deba@1989
  1837
//     }
deba@1697
  1838
deba@1989
  1839
//     void firstOut(Edge& edge, const Node& node) const {
deba@1989
  1840
//       if (!node.entry) {
deba@1989
  1841
// 	Parent::firstOut(edge, node);
deba@1989
  1842
// 	edge.bind = INVALID;
deba@1989
  1843
//       } else {
deba@1989
  1844
// 	(typename Parent::Edge&)edge = INVALID;
deba@1989
  1845
// 	edge.bind = node;
deba@1989
  1846
//       }
deba@1989
  1847
//     }
deba@1697
  1848
deba@1989
  1849
//     void nextOut(Edge& edge) const {
deba@1989
  1850
//       if ((typename Parent::Edge&)edge != INVALID) {
deba@1989
  1851
// 	Parent::nextOut(edge);
deba@1989
  1852
//       } else {
deba@1989
  1853
// 	edge.bind = INVALID;
deba@1989
  1854
//       }
deba@1989
  1855
//     }
deba@1697
  1856
deba@1989
  1857
//     Node source(const Edge& edge) const {
deba@1989
  1858
//       if ((typename Parent::Edge&)edge != INVALID) {
deba@1989
  1859
// 	return Node(Parent::source(edge), false);
deba@1989
  1860
//       } else {
deba@1989
  1861
// 	return Node(edge.bind, true);
deba@1989
  1862
//       }
deba@1989
  1863
//     }
deba@1697
  1864
deba@1989
  1865
//     Node target(const Edge& edge) const {
deba@1989
  1866
//       if ((typename Parent::Edge&)edge != INVALID) {
deba@1989
  1867
// 	return Node(Parent::target(edge), true);
deba@1989
  1868
//       } else {
deba@1989
  1869
// 	return Node(edge.bind, false);
deba@1989
  1870
//       }
deba@1989
  1871
//     }
deba@1697
  1872
deba@1989
  1873
//     static bool entryNode(const Node& node) {
deba@1989
  1874
//       return node.entry;
deba@1989
  1875
//     }
deba@1697
  1876
deba@1989
  1877
//     static bool exitNode(const Node& node) {
deba@1989
  1878
//       return !node.entry;
deba@1989
  1879
//     }
deba@1697
  1880
deba@1989
  1881
//     static Node getEntry(const typename Parent::Node& node) {
deba@1989
  1882
//       return Node(node, true);
deba@1989
  1883
//     }
deba@1697
  1884
deba@1989
  1885
//     static Node getExit(const typename Parent::Node& node) {
deba@1989
  1886
//       return Node(node, false);
deba@1989
  1887
//     }
deba@1697
  1888
deba@1989
  1889
//     static bool originalEdge(const Edge& edge) {
deba@1989
  1890
//       return (typename Parent::Edge&)edge != INVALID;
deba@1989
  1891
//     }
deba@1697
  1892
deba@1989
  1893
//     static bool bindingEdge(const Edge& edge) {
deba@1989
  1894
//       return edge.bind != INVALID;
deba@1989
  1895
//     }
deba@1697
  1896
deba@1989
  1897
//     static Node getBindedNode(const Edge& edge) {
deba@1989
  1898
//       return edge.bind;
deba@1989
  1899
//     }
deba@1697
  1900
deba@1989
  1901
//     int nodeNum() const {
deba@1989
  1902
//       return Parent::nodeNum() * 2;
deba@1989
  1903
//     }
deba@1697
  1904
deba@1989
  1905
//     typedef True EdgeNumTag;
deba@1697
  1906
    
deba@1989
  1907
//     int edgeNum() const {
deba@1989
  1908
//       return countEdges() + Parent::nodeNum();
deba@1989
  1909
//     }
deba@1697
  1910
deba@1989
  1911
//     Edge findEdge(const Node& source, const Node& target, 
deba@1989
  1912
// 		  const Edge& prev = INVALID) const {
deba@1989
  1913
//       if (exitNode(source) && entryNode(target)) {
deba@1989
  1914
// 	return Parent::findEdge(source, target, prev);
deba@1989
  1915
//       } else {
deba@1989
  1916
// 	if (prev == INVALID && entryNode(source) && exitNode(target) && 
deba@1989
  1917
// 	    (typename Parent::Node&)source == (typename Parent::Node&)target) {
deba@1989
  1918
// 	  return Edge(INVALID, source);
deba@1989
  1919
// 	} else {
deba@1989
  1920
// 	  return INVALID;
deba@1989
  1921
// 	}
deba@1989
  1922
//       }
deba@1989
  1923
//     }
deba@1697
  1924
    
deba@1989
  1925
//     template <typename T>
deba@1989
  1926
//     class NodeMap : public MapBase<Node, T> {
deba@1989
  1927
//       typedef typename Parent::template NodeMap<T> NodeImpl;
deba@1989
  1928
//     public:
deba@1989
  1929
//       NodeMap(const SplitGraphAdaptorBase& _graph) 
deba@1989
  1930
// 	: entry(_graph), exit(_graph) {}
deba@1989
  1931
//       NodeMap(const SplitGraphAdaptorBase& _graph, const T& t) 
deba@1989
  1932
// 	: entry(_graph, t), exit(_graph, t) {}
deba@1697
  1933
      
deba@1989
  1934
//       void set(const Node& key, const T& val) {
deba@1989
  1935
// 	if (key.entry) { entry.set(key, val); }
deba@1989
  1936
// 	else {exit.set(key, val); }
deba@1989
  1937
//       }
deba@1697
  1938
      
deba@1989
  1939
//       typename MapTraits<NodeImpl>::ReturnValue 
deba@1989
  1940
//       operator[](const Node& key) {
deba@1989
  1941
// 	if (key.entry) { return entry[key]; }
deba@1989
  1942
// 	else { return exit[key]; }
deba@1989
  1943
//       }
deba@1697
  1944
deba@1989
  1945
//       typename MapTraits<NodeImpl>::ConstReturnValue
deba@1989
  1946
//       operator[](const Node& key) const {
deba@1989
  1947
// 	if (key.entry) { return entry[key]; }
deba@1989
  1948
// 	else { return exit[key]; }
deba@1989
  1949
//       }
deba@1697
  1950
deba@1989
  1951
//     private:
deba@1989
  1952
//       NodeImpl entry, exit;
deba@1989
  1953
//     };
deba@1697
  1954
deba@1989
  1955
//     template <typename T>
deba@1989
  1956
//     class EdgeMap : public MapBase<Edge, T> {
deba@1989
  1957
//       typedef typename Parent::template NodeMap<T> NodeImpl;
deba@1989
  1958
//       typedef typename Parent::template EdgeMap<T> EdgeImpl;
deba@1989
  1959
//     public:
deba@1989
  1960
//       EdgeMap(const SplitGraphAdaptorBase& _graph) 
deba@1989
  1961
// 	: bind(_graph), orig(_graph) {}
deba@1989
  1962
//       EdgeMap(const SplitGraphAdaptorBase& _graph, const T& t) 
deba@1989
  1963
// 	: bind(_graph, t), orig(_graph, t) {}
deba@1697
  1964
      
deba@1989
  1965
//       void set(const Edge& key, const T& val) {
deba@1989
  1966
// 	if ((typename Parent::Edge&)key != INVALID) { orig.set(key, val); }
deba@1989
  1967
// 	else {bind.set(key.bind, val); }
deba@1989
  1968
//       }
deba@1697
  1969
      
deba@1989
  1970
//       typename MapTraits<EdgeImpl>::ReturnValue
deba@1989
  1971
//       operator[](const Edge& key) {
deba@1989
  1972
// 	if ((typename Parent::Edge&)key != INVALID) { return orig[key]; }
deba@1989
  1973
// 	else {return bind[key.bind]; }
deba@1989
  1974
//       }
deba@1697
  1975
deba@1989
  1976
//       typename MapTraits<EdgeImpl>::ConstReturnValue
deba@1989
  1977
//       operator[](const Edge& key) const {
deba@1989
  1978
// 	if ((typename Parent::Edge&)key != INVALID) { return orig[key]; }
deba@1989
  1979
// 	else {return bind[key.bind]; }
deba@1989
  1980
//       }
deba@1697
  1981
deba@1989
  1982
//     private:
deba@1989
  1983
//       typename Parent::template NodeMap<T> bind;
deba@1989
  1984
//       typename Parent::template EdgeMap<T> orig;
deba@1989
  1985
//     };
deba@1697
  1986
deba@1989
  1987
//     template <typename EntryMap, typename ExitMap>
deba@1989
  1988
//     class CombinedNodeMap : public MapBase<Node, typename EntryMap::Value> {
deba@1989
  1989
//     public:
deba@1989
  1990
//       typedef MapBase<Node, typename EntryMap::Value> Parent;
deba@1697
  1991
deba@1989
  1992
//       typedef typename Parent::Key Key;
deba@1989
  1993
//       typedef typename Parent::Value Value;
deba@1697
  1994
deba@1989
  1995
//       CombinedNodeMap(EntryMap& _entryMap, ExitMap& _exitMap) 
deba@1989
  1996
// 	: entryMap(_entryMap), exitMap(_exitMap) {}
deba@1697
  1997
deba@1989
  1998
//       Value& operator[](const Key& key) {
deba@1989
  1999
// 	if (key.entry) {
deba@1989
  2000
// 	  return entryMap[key];
deba@1989
  2001
// 	} else {
deba@1989
  2002
// 	  return exitMap[key];
deba@1989
  2003
// 	}
deba@1989
  2004
//       }
deba@1697
  2005
deba@1989
  2006
//       Value operator[](const Key& key) const {
deba@1989
  2007
// 	if (key.entry) {
deba@1989
  2008
// 	  return entryMap[key];
deba@1989
  2009
// 	} else {
deba@1989
  2010
// 	  return exitMap[key];
deba@1989
  2011
// 	}
deba@1989
  2012
//       }
deba@1697
  2013
deba@1989
  2014
//       void set(const Key& key, const Value& value) {
deba@1989
  2015
// 	if (key.entry) {
deba@1989
  2016
// 	  entryMap.set(key, value);
deba@1989
  2017
// 	} else {
deba@1989
  2018
// 	  exitMap.set(key, value);
deba@1989
  2019
// 	}
deba@1989
  2020
//       }
deba@1697
  2021
      
deba@1989
  2022
//     private:
deba@1697
  2023
      
deba@1989
  2024
//       EntryMap& entryMap;
deba@1989
  2025
//       ExitMap& exitMap;
deba@1697
  2026
      
deba@1989
  2027
//     };
deba@1697
  2028
deba@1989
  2029
//     template <typename EdgeMap, typename NodeMap>
deba@1989
  2030
//     class CombinedEdgeMap : public MapBase<Edge, typename EdgeMap::Value> {
deba@1989
  2031
//     public:
deba@1989
  2032
//       typedef MapBase<Edge, typename EdgeMap::Value> Parent;
deba@1697
  2033
deba@1989
  2034
//       typedef typename Parent::Key Key;
deba@1989
  2035
//       typedef typename Parent::Value Value;
deba@1697
  2036
deba@1989
  2037
//       CombinedEdgeMap(EdgeMap& _edgeMap, NodeMap& _nodeMap) 
deba@1989
  2038
// 	: edgeMap(_edgeMap), nodeMap(_nodeMap) {}
deba@1697
  2039
deba@1989
  2040
//       void set(const Edge& edge, const Value& val) {
deba@1989
  2041
// 	if (SplitGraphAdaptorBase::originalEdge(edge)) {
deba@1989
  2042
// 	  edgeMap.set(edge, val);
deba@1989
  2043
// 	} else {
deba@1989
  2044
// 	  nodeMap.set(SplitGraphAdaptorBase::bindedNode(edge), val);
deba@1989
  2045
// 	}
deba@1989
  2046
//       }
deba@1697
  2047
deba@1989
  2048
//       Value operator[](const Key& edge) const {
deba@1989
  2049
// 	if (SplitGraphAdaptorBase::originalEdge(edge)) {
deba@1989
  2050
// 	  return edgeMap[edge];
deba@1989
  2051
// 	} else {
deba@1989
  2052
// 	  return nodeMap[SplitGraphAdaptorBase::bindedNode(edge)];
deba@1989
  2053
// 	}
deba@1989
  2054
//       }      
deba@1697
  2055
deba@1989
  2056
//       Value& operator[](const Key& edge) {
deba@1989
  2057
// 	if (SplitGraphAdaptorBase::originalEdge(edge)) {
deba@1989
  2058
// 	  return edgeMap[edge];
deba@1989
  2059
// 	} else {
deba@1989
  2060
// 	  return nodeMap[SplitGraphAdaptorBase::bindedNode(edge)];
deba@1989
  2061
// 	}
deba@1989
  2062
//       }      
deba@1697
  2063
      
deba@1989
  2064
//     private:
deba@1989
  2065
//       EdgeMap& edgeMap;
deba@1989
  2066
//       NodeMap& nodeMap;
deba@1989
  2067
//     };
deba@1697
  2068
deba@1989
  2069
//   };
deba@1697
  2070
deba@1989
  2071
//   template <typename _Graph>
deba@1989
  2072
//   class SplitGraphAdaptor 
deba@1989
  2073
//     : public GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > {
deba@1989
  2074
//   public:
deba@1989
  2075
//     typedef GraphAdaptorExtender<SplitGraphAdaptorBase<_Graph> > Parent;
deba@1697
  2076
deba@1989
  2077
//     SplitGraphAdaptor(_Graph& graph) {
deba@1989
  2078
//       Parent::setGraph(graph);
deba@1989
  2079
//     }
deba@1697
  2080
deba@1697
  2081
deba@1989
  2082
//   };
deba@1697
  2083
alpar@921
  2084
} //namespace lemon
marci@556
  2085
alpar@1401
  2086
#endif //LEMON_GRAPH_ADAPTOR_H
marci@556
  2087