src/hugo/graph_wrapper.h
author alpar
Thu, 22 Jul 2004 20:07:49 +0000
changeset 734 329832ac02b7
parent 658 b3564d0e9c60
child 736 ba76a7f56b23
permissions -rw-r--r--
A remark added.
marci@556
     1
// -*- c++ -*-
marci@556
     2
#ifndef HUGO_GRAPH_WRAPPER_H
marci@556
     3
#define HUGO_GRAPH_WRAPPER_H
marci@556
     4
marci@556
     5
///\ingroup gwrappers
marci@556
     6
///\file
marci@556
     7
///\brief Several graph wrappers.
marci@556
     8
///
marci@556
     9
///This file contains several useful graph wrapper functions.
marci@556
    10
///
marci@556
    11
///\author Marton Makai
marci@556
    12
marci@556
    13
#include <hugo/invalid.h>
marci@650
    14
#include <hugo/maps.h>
marci@556
    15
//#include <iter_map.h>
marci@556
    16
marci@556
    17
namespace hugo {
marci@556
    18
marci@556
    19
  // Graph wrappers
marci@556
    20
marci@556
    21
  /// \addtogroup gwrappers
marci@556
    22
  /// A main parts of HUGOlib are the different graph structures, 
marci@556
    23
  /// generic graph algorithms, graph concepts which couple these, and 
marci@556
    24
  /// graph wrappers. While the previous ones are more or less clear, the 
marci@556
    25
  /// latter notion needs further explanation.
marci@556
    26
  /// Graph wrappers are graph classes which serve for considering graph 
marci@556
    27
  /// structures in different ways. A short example makes the notion much 
marci@556
    28
  /// clearer. 
marci@556
    29
  /// Suppose that we have an instance \c g of a directed graph
marci@556
    30
  /// type say \c ListGraph and an algorithm 
marci@556
    31
  /// \code template<typename Graph> int algorithm(const Graph&); \endcode 
marci@556
    32
  /// is needed to run on the reversely oriented graph. 
marci@556
    33
  /// It may be expensive (in time or in memory usage) to copy 
marci@556
    34
  /// \c g with the reverse orientation. 
marci@556
    35
  /// Thus, a wrapper class
marci@556
    36
  /// \code template<typename Graph> class RevGraphWrapper; \endcode is used. 
marci@556
    37
  /// The code looks as follows
marci@556
    38
  /// \code
marci@556
    39
  /// ListGraph g;
marci@556
    40
  /// RevGraphWrapper<ListGraph> rgw(g);
marci@556
    41
  /// int result=algorithm(rgw);
marci@556
    42
  /// \endcode
marci@556
    43
  /// After running the algorithm, the original graph \c g 
marci@556
    44
  /// remains untouched. Thus the graph wrapper used above is to consider the 
marci@556
    45
  /// original graph with reverse orientation. 
marci@556
    46
  /// This techniques gives rise to an elegant code, and 
marci@556
    47
  /// based on stable graph wrappers, complex algorithms can be 
marci@556
    48
  /// implemented easily. 
marci@556
    49
  /// In flow, circulation and bipartite matching problems, the residual 
marci@556
    50
  /// graph is of particular importance. Combining a wrapper implementing 
marci@556
    51
  /// this, shortest path algorithms and minimum mean cycle algorithms, 
marci@556
    52
  /// a range of weighted and cardinality optimization algorithms can be 
marci@556
    53
  /// obtained. For lack of space, for other examples, 
marci@556
    54
  /// the interested user is referred to the detailed documentation of graph 
marci@556
    55
  /// wrappers. 
marci@556
    56
  /// The behavior of graph wrappers can be very different. Some of them keep 
marci@556
    57
  /// capabilities of the original graph while in other cases this would be 
marci@556
    58
  /// meaningless. This means that the concepts that they are a model of depend 
marci@556
    59
  /// on the graph wrapper, and the wrapped graph(s). 
marci@556
    60
  /// If an edge of \c rgw is deleted, this is carried out by 
marci@556
    61
  /// deleting the corresponding edge of \c g. But for a residual 
marci@556
    62
  /// graph, this operation has no sense. 
marci@556
    63
  /// Let we stand one more example here to simplify your work. 
marci@556
    64
  /// wrapper class
marci@556
    65
  /// \code template<typename Graph> class RevGraphWrapper; \endcode 
marci@556
    66
  /// has constructor 
marci@556
    67
  /// <tt> RevGraphWrapper(Graph& _g)</tt>. 
marci@556
    68
  /// This means that in a situation, 
marci@556
    69
  /// when a <tt> const ListGraph& </tt> reference to a graph is given, 
marci@556
    70
  /// then it have to be instantiated with <tt>Graph=const ListGraph</tt>.
marci@556
    71
  /// \code
marci@556
    72
  /// int algorithm1(const ListGraph& g) {
marci@556
    73
  ///   RevGraphWrapper<const ListGraph> rgw(g);
marci@556
    74
  ///   return algorithm2(rgw);
marci@556
    75
  /// }
marci@556
    76
  /// \endcode
marci@556
    77
marci@556
    78
  /// \addtogroup gwrappers
marci@556
    79
  /// @{
marci@556
    80
marci@556
    81
  ///Base type for the Graph Wrappers
marci@556
    82
marci@556
    83
  ///This is the base type for the Graph Wrappers.
marci@556
    84
  ///\todo Some more docs... 
marci@556
    85
  ///
marci@612
    86
  ///\author Marton Makai 
marci@556
    87
  template<typename Graph>
marci@556
    88
  class GraphWrapper {
marci@556
    89
  protected:
marci@556
    90
    Graph* graph;
marci@556
    91
    GraphWrapper() : graph(0) { }
marci@556
    92
    void setGraph(Graph& _graph) { graph=&_graph; }
marci@556
    93
marci@556
    94
  public:
marci@556
    95
    typedef Graph BaseGraph;
marci@556
    96
    typedef Graph ParentGraph;
marci@556
    97
marci@556
    98
    GraphWrapper(Graph& _graph) : graph(&_graph) { }
marci@556
    99
//     Graph& getGraph() const { return *graph; }
marci@556
   100
 
marci@556
   101
//    typedef typename Graph::Node Node;
marci@556
   102
    class Node : public Graph::Node {
marci@556
   103
      friend class GraphWrapper<Graph>;
marci@556
   104
    public:
marci@556
   105
      Node() { }
marci@556
   106
      Node(const typename Graph::Node& _n) : Graph::Node(_n) { }
marci@650
   107
      // /// \bug construction throughrthr multiple levels should be 
marci@650
   108
      // /// handled better
marci@650
   109
      // Node(const typename ParentGraph::ParentGraph::Node& _n) : 
marci@650
   110
      // Graph::Node(_n) { }
marci@556
   111
      Node(const Invalid& i) : Graph::Node(i) { }
marci@556
   112
    };
marci@556
   113
    class NodeIt { 
marci@556
   114
      friend class GraphWrapper<Graph>;
marci@556
   115
      typename Graph::NodeIt n;
marci@556
   116
     public:
marci@556
   117
      NodeIt() { }
marci@556
   118
      NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
marci@556
   119
      NodeIt(const Invalid& i) : n(i) { }
marci@556
   120
      NodeIt(const GraphWrapper<Graph>& _G) : n(*(_G.graph)) { }
marci@556
   121
      operator Node() const { return Node(typename Graph::Node(n)); }
marci@556
   122
    };
marci@556
   123
//    typedef typename Graph::Edge Edge;
marci@556
   124
    class Edge : public Graph::Edge {
marci@556
   125
      friend class GraphWrapper<Graph>;
marci@556
   126
    public:
marci@556
   127
      Edge() { }
marci@556
   128
      Edge(const typename Graph::Edge& _e) : Graph::Edge(_e) { }
marci@556
   129
      Edge(const Invalid& i) : Graph::Edge(i) { }
marci@556
   130
    };
marci@556
   131
    class OutEdgeIt { 
marci@556
   132
      friend class GraphWrapper<Graph>;
marci@556
   133
      typename Graph::OutEdgeIt e;
marci@556
   134
    public:
marci@556
   135
      OutEdgeIt() { }
marci@556
   136
      OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
marci@556
   137
      OutEdgeIt(const Invalid& i) : e(i) { }
marci@556
   138
      OutEdgeIt(const GraphWrapper<Graph>& _G, const Node& _n) : 
marci@556
   139
	e(*(_G.graph), typename Graph::Node(_n)) { }
marci@556
   140
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
   141
    };
marci@556
   142
    class InEdgeIt { 
marci@556
   143
      friend class GraphWrapper<Graph>;
marci@556
   144
      typename Graph::InEdgeIt e;
marci@556
   145
    public:
marci@556
   146
      InEdgeIt() { }
marci@556
   147
      InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
marci@556
   148
      InEdgeIt(const Invalid& i) : e(i) { }
marci@556
   149
      InEdgeIt(const GraphWrapper<Graph>& _G, const Node& _n) : 
marci@556
   150
	e(*(_G.graph), typename Graph::Node(_n)) { }
marci@556
   151
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
   152
    };
marci@556
   153
    //typedef typename Graph::SymEdgeIt SymEdgeIt;
marci@556
   154
    class EdgeIt { 
marci@556
   155
      friend class GraphWrapper<Graph>;
marci@556
   156
      typename Graph::EdgeIt e;
marci@556
   157
    public:
marci@556
   158
      EdgeIt() { }
marci@556
   159
      EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
marci@556
   160
      EdgeIt(const Invalid& i) : e(i) { }
marci@556
   161
      EdgeIt(const GraphWrapper<Graph>& _G) : e(*(_G.graph)) { }
marci@556
   162
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
   163
    };
marci@556
   164
   
marci@556
   165
    NodeIt& first(NodeIt& i) const { 
marci@556
   166
      i=NodeIt(*this); return i;
marci@556
   167
    }
marci@556
   168
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   169
      i=OutEdgeIt(*this, p); return i;
marci@556
   170
    }
marci@556
   171
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   172
      i=InEdgeIt(*this, p); return i;
marci@556
   173
    }
marci@556
   174
    EdgeIt& first(EdgeIt& i) const { 
marci@556
   175
      i=EdgeIt(*this); return i;
marci@556
   176
    }
marci@556
   177
marci@556
   178
    NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
marci@556
   179
    OutEdgeIt& next(OutEdgeIt& i) const { graph->next(i.e); return i; }
marci@556
   180
    InEdgeIt& next(InEdgeIt& i) const { graph->next(i.e); return i; }
marci@556
   181
    EdgeIt& next(EdgeIt& i) const { graph->next(i.e); return i; }    
marci@556
   182
marci@556
   183
    Node tail(const Edge& e) const { 
marci@556
   184
      return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
marci@556
   185
    Node head(const Edge& e) const { 
marci@556
   186
      return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
marci@556
   187
marci@556
   188
    bool valid(const Node& n) const { 
marci@556
   189
      return graph->valid(static_cast<typename Graph::Node>(n)); }
marci@556
   190
    bool valid(const Edge& e) const { 
marci@556
   191
      return graph->valid(static_cast<typename Graph::Edge>(e)); }
marci@556
   192
marci@556
   193
    int nodeNum() const { return graph->nodeNum(); }
marci@556
   194
    int edgeNum() const { return graph->edgeNum(); }
marci@556
   195
  
marci@556
   196
    Node aNode(const OutEdgeIt& e) const { return Node(graph->aNode(e.e)); }
marci@556
   197
    Node aNode(const InEdgeIt& e) const { return Node(graph->aNode(e.e)); }
marci@556
   198
    Node bNode(const OutEdgeIt& e) const { return Node(graph->bNode(e.e)); }
marci@556
   199
    Node bNode(const InEdgeIt& e) const { return Node(graph->bNode(e.e)); }
marci@556
   200
  
marci@556
   201
    Node addNode() const { return Node(graph->addNode()); }
marci@556
   202
    Edge addEdge(const Node& tail, const Node& head) const { 
marci@556
   203
      return Edge(graph->addEdge(tail, head)); }
marci@556
   204
marci@556
   205
    void erase(const Node& i) const { graph->erase(i); }
marci@556
   206
    void erase(const Edge& i) const { graph->erase(i); }
marci@556
   207
  
marci@556
   208
    void clear() const { graph->clear(); }
marci@556
   209
    
marci@650
   210
    bool forward(const Edge& e) const { graph->forward(e); }
marci@650
   211
    bool backward(const Edge& e) const { graph->backward(e); }
marci@650
   212
    
marci@650
   213
    Edge opposite(const Edge& e) const { Edge(graph->opposite(e)); }
marci@650
   214
marci@556
   215
    template<typename T> class NodeMap : public Graph::template NodeMap<T> { 
marci@556
   216
      typedef typename Graph::template NodeMap<T> Parent;
marci@556
   217
    public:
marci@556
   218
      NodeMap(const GraphWrapper<Graph>& _G) :  Parent(*(_G.graph)) { }
marci@556
   219
      NodeMap(const GraphWrapper<Graph>& _G, T a) : Parent(*(_G.graph), a) { }
marci@556
   220
    };
marci@556
   221
marci@556
   222
    template<typename T> class EdgeMap : public Graph::template EdgeMap<T> { 
marci@556
   223
      typedef typename Graph::template EdgeMap<T> Parent;
marci@556
   224
    public:
marci@556
   225
      EdgeMap(const GraphWrapper<Graph>& _G) : Parent(*(_G.graph)) { }
marci@556
   226
      EdgeMap(const GraphWrapper<Graph>& _G, T a) : Parent(*(_G.graph), a) { }
marci@556
   227
    };
marci@556
   228
  };
marci@556
   229
marci@569
   230
marci@569
   231
marci@556
   232
  /// A graph wrapper which reverses the orientation of the edges.
marci@556
   233
marci@556
   234
  /// A graph wrapper which reverses the orientation of the edges.
marci@612
   235
  /// Thus \c Graph have to be a directed graph type.
marci@556
   236
  ///
marci@556
   237
  ///\author Marton Makai
marci@556
   238
  template<typename Graph>
marci@556
   239
  class RevGraphWrapper : public GraphWrapper<Graph> {
marci@650
   240
  public:
marci@650
   241
    typedef GraphWrapper<Graph> Parent; 
marci@556
   242
  protected:
marci@612
   243
    RevGraphWrapper() : GraphWrapper<Graph>() { }
marci@556
   244
  public:
marci@556
   245
    RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }  
marci@556
   246
marci@556
   247
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
   248
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@556
   249
    //If Graph::OutEdgeIt is not defined
marci@556
   250
    //and we do not want to use RevGraphWrapper::InEdgeIt,
marci@556
   251
    //the typdef techinque does not work.
marci@556
   252
    //Unfortunately all the typedefs are instantiated in templates.
marci@556
   253
    //typedef typename GraphWrapper<Graph>::OutEdgeIt InEdgeIt;
marci@556
   254
    //typedef typename GraphWrapper<Graph>::InEdgeIt OutEdgeIt;
marci@556
   255
marci@556
   256
    class OutEdgeIt { 
marci@556
   257
      friend class GraphWrapper<Graph>;
marci@556
   258
      friend class RevGraphWrapper<Graph>;
marci@556
   259
      typename Graph::InEdgeIt e;
marci@556
   260
    public:
marci@556
   261
      OutEdgeIt() { }
marci@556
   262
      OutEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
marci@556
   263
      OutEdgeIt(const Invalid& i) : e(i) { }
marci@556
   264
      OutEdgeIt(const RevGraphWrapper<Graph>& _G, const Node& _n) : 
marci@556
   265
	e(*(_G.graph), typename Graph::Node(_n)) { }
marci@556
   266
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
   267
    };
marci@556
   268
    class InEdgeIt { 
marci@556
   269
      friend class GraphWrapper<Graph>;
marci@556
   270
      friend class RevGraphWrapper<Graph>;
marci@556
   271
      typename Graph::OutEdgeIt e;
marci@556
   272
    public:
marci@556
   273
      InEdgeIt() { }
marci@556
   274
      InEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
marci@556
   275
      InEdgeIt(const Invalid& i) : e(i) { }
marci@556
   276
      InEdgeIt(const RevGraphWrapper<Graph>& _G, const Node& _n) : 
marci@556
   277
	e(*(_G.graph), typename Graph::Node(_n)) { }
marci@556
   278
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
   279
    };
marci@556
   280
marci@556
   281
    using GraphWrapper<Graph>::first;
marci@556
   282
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   283
      i=OutEdgeIt(*this, p); return i;
marci@556
   284
    }
marci@556
   285
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   286
      i=InEdgeIt(*this, p); return i;
marci@556
   287
    }
marci@556
   288
marci@556
   289
    using GraphWrapper<Graph>::next;
marci@556
   290
    OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
marci@556
   291
    InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
marci@556
   292
marci@556
   293
    Node aNode(const OutEdgeIt& e) const { 
marci@556
   294
      return Node(this->graph->aNode(e.e)); }
marci@556
   295
    Node aNode(const InEdgeIt& e) const { 
marci@556
   296
      return Node(this->graph->aNode(e.e)); }
marci@556
   297
    Node bNode(const OutEdgeIt& e) const { 
marci@556
   298
      return Node(this->graph->bNode(e.e)); }
marci@556
   299
    Node bNode(const InEdgeIt& e) const { 
marci@556
   300
      return Node(this->graph->bNode(e.e)); }
marci@556
   301
marci@556
   302
    Node tail(const Edge& e) const { 
marci@556
   303
      return GraphWrapper<Graph>::head(e); }
marci@556
   304
    Node head(const Edge& e) const { 
marci@556
   305
      return GraphWrapper<Graph>::tail(e); }
marci@556
   306
marci@556
   307
  };
marci@556
   308
marci@569
   309
marci@569
   310
marci@612
   311
  /// A graph wrapper for hiding nodes and edges from a graph.
marci@556
   312
  
marci@556
   313
  /// This wrapper shows a graph with filtered node-set and 
marci@556
   314
  /// edge-set. The quick brown fox iterator jumps over 
marci@556
   315
  /// the lazy dog nodes or edges if the values for them are false 
marci@556
   316
  /// in the bool maps. 
marci@556
   317
  ///
marci@556
   318
  ///\author Marton Makai
marci@556
   319
  template<typename Graph, typename NodeFilterMap, 
marci@556
   320
	   typename EdgeFilterMap>
marci@556
   321
  class SubGraphWrapper : public GraphWrapper<Graph> {
marci@650
   322
  public:
marci@650
   323
    typedef GraphWrapper<Graph> Parent;
marci@556
   324
  protected:
marci@556
   325
    NodeFilterMap* node_filter_map;
marci@556
   326
    EdgeFilterMap* edge_filter_map;
marci@556
   327
marci@612
   328
    SubGraphWrapper() : GraphWrapper<Graph>(), 
marci@556
   329
			node_filter_map(0), edge_filter_map(0) { }
marci@556
   330
    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
marci@556
   331
      node_filter_map=&_node_filter_map;
marci@556
   332
    }
marci@556
   333
    void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
marci@556
   334
      edge_filter_map=&_edge_filter_map;
marci@556
   335
    }
marci@556
   336
    
marci@556
   337
  public:
marci@556
   338
    SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map, 
marci@556
   339
		    EdgeFilterMap& _edge_filter_map) : 
marci@556
   340
      GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map), 
marci@556
   341
      edge_filter_map(&_edge_filter_map) { }  
marci@556
   342
marci@556
   343
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
   344
    class NodeIt { 
marci@556
   345
      friend class GraphWrapper<Graph>;
marci@556
   346
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   347
      typename Graph::NodeIt n;
marci@556
   348
     public:
marci@556
   349
      NodeIt() { }
marci@556
   350
      NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
marci@556
   351
      NodeIt(const Invalid& i) : n(i) { }
marci@556
   352
      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) : 
marci@556
   353
	n(*(_G.graph)) { 
marci@556
   354
	while (_G.graph->valid(n) && !(*(_G.node_filter_map))[n]) 
marci@556
   355
	  _G.graph->next(n);
marci@556
   356
      }
marci@556
   357
      operator Node() const { return Node(typename Graph::Node(n)); }
marci@556
   358
    };
marci@556
   359
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@556
   360
    class OutEdgeIt { 
marci@556
   361
      friend class GraphWrapper<Graph>;
marci@556
   362
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   363
      typename Graph::OutEdgeIt e;
marci@556
   364
    public:
marci@556
   365
      OutEdgeIt() { }
marci@556
   366
      OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
marci@556
   367
      OutEdgeIt(const Invalid& i) : e(i) { }
marci@556
   368
      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G, 
marci@556
   369
		const Node& _n) : 
marci@556
   370
	e(*(_G.graph), typename Graph::Node(_n)) { 
marci@556
   371
      	while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e]) 
marci@556
   372
	  _G.graph->next(e);
marci@556
   373
      }
marci@556
   374
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
   375
    };
marci@556
   376
    class InEdgeIt { 
marci@556
   377
      friend class GraphWrapper<Graph>;
marci@556
   378
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   379
      typename Graph::InEdgeIt e;
marci@556
   380
    public:
marci@556
   381
      InEdgeIt() { }
marci@556
   382
      InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
marci@556
   383
      InEdgeIt(const Invalid& i) : e(i) { }
marci@556
   384
      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G, 
marci@556
   385
	       const Node& _n) : 
marci@556
   386
	e(*(_G.graph), typename Graph::Node(_n)) { 
marci@556
   387
      	while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e]) 
marci@556
   388
	  _G.graph->next(e);
marci@556
   389
      }
marci@556
   390
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
   391
    };
marci@556
   392
    //typedef typename Graph::SymEdgeIt SymEdgeIt;
marci@556
   393
    class EdgeIt { 
marci@556
   394
      friend class GraphWrapper<Graph>;
marci@556
   395
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   396
      typename Graph::EdgeIt e;
marci@556
   397
    public:
marci@556
   398
      EdgeIt() { }
marci@556
   399
      EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
marci@556
   400
      EdgeIt(const Invalid& i) : e(i) { }
marci@556
   401
      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _G) : 
marci@556
   402
	e(*(_G.graph)) { 
marci@556
   403
      	while (_G.graph->valid(e) && !(*(_G.edge_filter_map))[e]) 
marci@556
   404
	  _G.graph->next(e);
marci@556
   405
      }
marci@556
   406
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
   407
    };
marci@556
   408
marci@556
   409
    NodeIt& first(NodeIt& i) const { 
marci@556
   410
      i=NodeIt(*this); return i;
marci@556
   411
    }
marci@556
   412
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   413
      i=OutEdgeIt(*this, p); return i;
marci@556
   414
    }
marci@556
   415
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   416
      i=InEdgeIt(*this, p); return i;
marci@556
   417
    }
marci@556
   418
    EdgeIt& first(EdgeIt& i) const { 
marci@556
   419
      i=EdgeIt(*this); return i;
marci@556
   420
    }
marci@556
   421
    
marci@556
   422
    NodeIt& next(NodeIt& i) const {
marci@556
   423
      this->graph->next(i.n); 
marci@556
   424
      while (this->graph->valid(i) && !(*node_filter_map)[i.n]) { 
marci@556
   425
	this->graph->next(i.n); }
marci@556
   426
      return i;
marci@556
   427
    }
marci@556
   428
    OutEdgeIt& next(OutEdgeIt& i) const {
marci@556
   429
      this->graph->next(i.e); 
marci@556
   430
      while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) { 
marci@556
   431
	this->graph->next(i.e); }
marci@556
   432
      return i;
marci@556
   433
    }
marci@556
   434
    InEdgeIt& next(InEdgeIt& i) const {
marci@556
   435
      this->graph->next(i.e); 
marci@556
   436
      while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) { 
marci@556
   437
	this->graph->next(i.e); }
marci@556
   438
      return i;
marci@556
   439
    }
marci@556
   440
    EdgeIt& next(EdgeIt& i) const {
marci@556
   441
      this->graph->next(i.e); 
marci@556
   442
      while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) { 
marci@556
   443
	this->graph->next(i.e); }
marci@556
   444
      return i;
marci@556
   445
    }
marci@556
   446
marci@556
   447
    Node aNode(const OutEdgeIt& e) const { 
marci@556
   448
      return Node(this->graph->aNode(e.e)); }
marci@556
   449
    Node aNode(const InEdgeIt& e) const { 
marci@556
   450
      return Node(this->graph->aNode(e.e)); }
marci@556
   451
    Node bNode(const OutEdgeIt& e) const { 
marci@556
   452
      return Node(this->graph->bNode(e.e)); }
marci@556
   453
    Node bNode(const InEdgeIt& e) const { 
marci@556
   454
      return Node(this->graph->bNode(e.e)); }
marci@556
   455
marci@561
   456
    /// This function hides \c n in the graph, i.e. the iteration 
marci@561
   457
    /// jumps over it. This is done by simply setting the value of \c n  
marci@561
   458
    /// to be false in the corresponding node-map.
marci@556
   459
    void hide(const Node& n) const { node_filter_map->set(n, false); }
marci@561
   460
marci@561
   461
    /// This function hides \c e in the graph, i.e. the iteration 
marci@561
   462
    /// jumps over it. This is done by simply setting the value of \c e  
marci@561
   463
    /// to be false in the corresponding edge-map.
marci@556
   464
    void hide(const Edge& e) const { edge_filter_map->set(e, false); }
marci@556
   465
marci@561
   466
    /// The value of \c n is set to be true in the node-map which stores 
marci@561
   467
    /// hide information. If \c n was hidden previuosly, then it is shown 
marci@561
   468
    /// again
marci@561
   469
     void unHide(const Node& n) const { node_filter_map->set(n, true); }
marci@561
   470
marci@561
   471
    /// The value of \c e is set to be true in the edge-map which stores 
marci@561
   472
    /// hide information. If \c e was hidden previuosly, then it is shown 
marci@561
   473
    /// again
marci@556
   474
    void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
marci@556
   475
marci@561
   476
    /// Returns true if \c n is hidden.
marci@561
   477
    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
marci@561
   478
marci@561
   479
    /// Returns true if \c n is hidden.
marci@561
   480
    bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
marci@593
   481
alpar@594
   482
    /// This is a linear time operation and works only if 
marci@593
   483
    /// NodeIt is defined.
marci@593
   484
    int nodeNum() const { 
marci@593
   485
      int i=0;
marci@593
   486
      NodeIt n;
marci@593
   487
      for (this->first(n); this->valid(n); this->next(n)) ++i;
marci@593
   488
      return i; 
marci@593
   489
    }
marci@593
   490
marci@593
   491
    /// This is a linear time operation and works only if 
marci@593
   492
    /// EdgeIt is defined.
marci@593
   493
    int edgeNum() const { 
marci@593
   494
      int i=0;
marci@593
   495
      EdgeIt e;
marci@593
   496
      for (this->first(e); this->valid(e); this->next(e)) ++i;
marci@593
   497
      return i; 
marci@593
   498
    }
marci@593
   499
marci@556
   500
  };
marci@556
   501
marci@569
   502
marci@569
   503
marci@612
   504
  /// \brief A wrapper for forgetting the orientation of a graph.
marci@612
   505
  ///
marci@556
   506
  /// A wrapper for getting an undirected graph by forgetting
marci@556
   507
  /// the orientation of a directed one.
alpar@589
   508
  ///
marci@612
   509
  /// \author Marton Makai
marci@556
   510
  template<typename Graph>
marci@556
   511
  class UndirGraphWrapper : public GraphWrapper<Graph> {
marci@650
   512
  public:
marci@650
   513
    typedef GraphWrapper<Graph> Parent; 
marci@556
   514
  protected:
marci@556
   515
    UndirGraphWrapper() : GraphWrapper<Graph>() { }
marci@556
   516
    
marci@556
   517
  public:
marci@556
   518
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
   519
    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@556
   520
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@556
   521
    typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
marci@556
   522
marci@556
   523
    UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }  
marci@556
   524
marci@556
   525
    class OutEdgeIt {
marci@556
   526
      friend class UndirGraphWrapper<Graph>;
marci@556
   527
      bool out_or_in; //true iff out
marci@556
   528
      typename Graph::OutEdgeIt out;
marci@556
   529
      typename Graph::InEdgeIt in;
marci@556
   530
    public:
marci@556
   531
      OutEdgeIt() { }
marci@556
   532
      OutEdgeIt(const Invalid& i) : Edge(i) { }
marci@556
   533
      OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
marci@556
   534
	out_or_in=true; _G.graph->first(out, _n);
marci@556
   535
	if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n);	}
marci@556
   536
      } 
marci@556
   537
      operator Edge() const { 
marci@556
   538
	if (out_or_in) return Edge(out); else return Edge(in); 
marci@556
   539
      }
marci@556
   540
    };
marci@556
   541
marci@556
   542
//FIXME InEdgeIt
marci@556
   543
    typedef OutEdgeIt InEdgeIt; 
marci@556
   544
marci@556
   545
    using GraphWrapper<Graph>::first;
marci@556
   546
//     NodeIt& first(NodeIt& i) const { 
marci@556
   547
//       i=NodeIt(*this); return i;
marci@556
   548
//     }
marci@556
   549
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   550
      i=OutEdgeIt(*this, p); return i;
marci@556
   551
    }
marci@556
   552
//FIXME
marci@556
   553
//     InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   554
//       i=InEdgeIt(*this, p); return i;
marci@556
   555
//     }
marci@556
   556
//     EdgeIt& first(EdgeIt& i) const { 
marci@556
   557
//       i=EdgeIt(*this); return i;
marci@556
   558
//     }
marci@556
   559
marci@556
   560
    using GraphWrapper<Graph>::next;
marci@556
   561
//     NodeIt& next(NodeIt& n) const {
marci@556
   562
//       GraphWrapper<Graph>::next(n);
marci@556
   563
//       return n;
marci@556
   564
//     }
marci@556
   565
    OutEdgeIt& next(OutEdgeIt& e) const {
marci@556
   566
      if (e.out_or_in) {
marci@556
   567
	typename Graph::Node n=this->graph->tail(e.out);
marci@556
   568
	this->graph->next(e.out);
marci@556
   569
	if (!this->graph->valid(e.out)) { 
marci@556
   570
	  e.out_or_in=false; this->graph->first(e.in, n); }
marci@556
   571
      } else {
marci@556
   572
	this->graph->next(e.in);
marci@556
   573
      }
marci@556
   574
      return e;
marci@556
   575
    }
marci@556
   576
    //FIXME InEdgeIt
marci@556
   577
//     EdgeIt& next(EdgeIt& e) const {
marci@556
   578
//       GraphWrapper<Graph>::next(n);
marci@556
   579
// //      graph->next(e.e);
marci@556
   580
//       return e;
marci@556
   581
//     }
marci@556
   582
marci@556
   583
    Node aNode(const OutEdgeIt& e) const { 
marci@556
   584
      if (e.out_or_in) return this->graph->tail(e); else 
marci@556
   585
	return this->graph->head(e); }
marci@556
   586
    Node bNode(const OutEdgeIt& e) const { 
marci@556
   587
      if (e.out_or_in) return this->graph->head(e); else 
marci@556
   588
	return this->graph->tail(e); }
marci@556
   589
  };
marci@556
   590
  
marci@612
   591
  /// \brief An undirected graph template.
marci@612
   592
  ///
marci@612
   593
  /// An undirected graph template.
marci@612
   594
  /// This class works as an undirected graph and a directed graph of 
marci@612
   595
  /// class \c Graph is used for the physical storage.
marci@612
   596
  /// \ingroup graphs
marci@556
   597
  template<typename Graph>
marci@556
   598
  class UndirGraph : public UndirGraphWrapper<Graph> {
marci@556
   599
    typedef UndirGraphWrapper<Graph> Parent;
marci@556
   600
  protected:
marci@556
   601
    Graph gr;
marci@556
   602
  public:
marci@556
   603
    UndirGraph() : UndirGraphWrapper<Graph>() { 
marci@556
   604
      Parent::setGraph(gr); 
marci@556
   605
    }
marci@556
   606
  };
marci@556
   607
marci@569
   608
marci@650
   609
marci@650
   610
  ///\brief A wrapper for composing a subgraph of a 
marci@650
   611
  /// bidirected graph composed from a directed one. 
marci@612
   612
  /// experimental, for fezso's sake.
marci@612
   613
  ///
marci@650
   614
  /// A wrapper for composing a subgraps of a 
marci@650
   615
  /// bidirected graph composed from a directed one. 
marci@576
   616
  /// experimental, for fezso's sake.
marci@612
   617
  /// A bidirected graph is composed over the directed one without physical 
marci@612
   618
  /// storage. As the oppositely directed edges are logically different ones 
marci@612
   619
  /// the maps are able to attach different values for them.
marci@650
   620
  template<typename Graph, 
marci@650
   621
	   typename ForwardFilterMap, typename BackwardFilterMap>
marci@650
   622
  class SubBidirGraphWrapper : public GraphWrapper<Graph> {
marci@650
   623
  public:
marci@650
   624
    typedef GraphWrapper<Graph> Parent; 
marci@569
   625
  protected:
marci@569
   626
    //const CapacityMap* capacity;
marci@569
   627
    //FlowMap* flow;
marci@569
   628
marci@650
   629
    ForwardFilterMap* forward_filter;
marci@650
   630
    BackwardFilterMap* backward_filter;
marci@650
   631
marci@650
   632
    SubBidirGraphWrapper() : GraphWrapper<Graph>()/*, 
marci@569
   633
						 capacity(0), flow(0)*/ { }
marci@650
   634
    void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
marci@650
   635
      forward_filter=&_forward_filter;
marci@650
   636
    }
marci@650
   637
    void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
marci@650
   638
      backward_filter=&_backward_filter;
marci@650
   639
    }
marci@569
   640
marci@569
   641
  public:
marci@569
   642
marci@650
   643
    SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter, 
marci@650
   644
			 BackwardFilterMap& _backward_filter) : 
marci@650
   645
      GraphWrapper<Graph>(_graph), 
marci@650
   646
      forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
marci@569
   647
marci@569
   648
    class Edge; 
marci@569
   649
    class OutEdgeIt; 
marci@569
   650
    friend class Edge; 
marci@569
   651
    friend class OutEdgeIt; 
marci@569
   652
marci@621
   653
    //template<typename T> class NodeMap;    
marci@621
   654
    template<typename T> class EdgeMap;
marci@621
   655
marci@569
   656
    typedef typename GraphWrapper<Graph>::Node Node;
marci@569
   657
    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@621
   658
marci@569
   659
    class Edge : public Graph::Edge {
marci@650
   660
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   661
					ForwardFilterMap, BackwardFilterMap>;
marci@621
   662
      ///\bug ez nem is kell
marci@621
   663
      //template<typename T> friend class NodeMap;
marci@621
   664
      template<typename T> friend class EdgeMap;
marci@569
   665
    protected:
marci@569
   666
      bool backward; //true, iff backward
marci@569
   667
//      typename Graph::Edge e;
marci@569
   668
    public:
marci@569
   669
      Edge() { }
marci@626
   670
      ///\bug =false kell-e? zsoltnak kell az addEdge miatt
marci@626
   671
      Edge(const typename Graph::Edge& _e, bool _backward=false) : 
marci@569
   672
	Graph::Edge(_e), backward(_backward) { }
marci@569
   673
      Edge(const Invalid& i) : Graph::Edge(i), backward(true) { }
marci@569
   674
//the unique invalid iterator
marci@569
   675
      friend bool operator==(const Edge& u, const Edge& v) { 
marci@569
   676
	return (v.backward==u.backward && 
marci@569
   677
		static_cast<typename Graph::Edge>(u)==
marci@569
   678
		static_cast<typename Graph::Edge>(v));
marci@569
   679
      } 
marci@569
   680
      friend bool operator!=(const Edge& u, const Edge& v) { 
marci@569
   681
	return (v.backward!=u.backward || 
marci@569
   682
		static_cast<typename Graph::Edge>(u)!=
marci@569
   683
		static_cast<typename Graph::Edge>(v));
marci@569
   684
      } 
marci@569
   685
    };
marci@569
   686
marci@569
   687
    class OutEdgeIt {
marci@650
   688
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   689
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   690
    protected:
marci@569
   691
      typename Graph::OutEdgeIt out;
marci@569
   692
      typename Graph::InEdgeIt in;
marci@569
   693
      bool backward;
marci@569
   694
    public:
marci@569
   695
      OutEdgeIt() { }
marci@569
   696
      //FIXME
marci@569
   697
//      OutEdgeIt(const Edge& e) : Edge(e) { }
marci@569
   698
      OutEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
marci@569
   699
//the unique invalid iterator
marci@650
   700
      OutEdgeIt(const SubBidirGraphWrapper<Graph, 
marci@650
   701
		ForwardFilterMap, BackwardFilterMap>& _G, Node v) { 
marci@569
   702
	backward=false;
marci@569
   703
	_G.graph->first(out, v);
marci@650
   704
	while(_G.graph->valid(out) && !(*_G.forward_filter)[*this]) { _G.graph->next(out); }
marci@569
   705
	if (!_G.graph->valid(out)) {
marci@569
   706
	  backward=true;
marci@569
   707
	  _G.graph->first(in, v);
marci@650
   708
	  while(_G.graph->valid(in) && !(*_G.backward_filter)[*this]) { _G.graph->next(in); }
marci@569
   709
	}
marci@569
   710
      }
marci@569
   711
      operator Edge() const { 
marci@569
   712
//	Edge e;
marci@569
   713
//	e.forward=this->forward;
marci@569
   714
//	if (this->forward) e=out; else e=in;
marci@569
   715
//	return e;
marci@569
   716
	if (this->backward) 
marci@569
   717
	  return Edge(in, this->backward); 
marci@569
   718
	else 
marci@569
   719
	  return Edge(out, this->backward);
marci@569
   720
      }
marci@569
   721
    };
marci@569
   722
marci@569
   723
    class InEdgeIt {
marci@650
   724
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   725
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   726
    protected:
marci@569
   727
      typename Graph::OutEdgeIt out;
marci@569
   728
      typename Graph::InEdgeIt in;
marci@569
   729
      bool backward;
marci@569
   730
    public:
marci@569
   731
      InEdgeIt() { }
marci@569
   732
      //FIXME
marci@569
   733
//      OutEdgeIt(const Edge& e) : Edge(e) { }
marci@569
   734
      InEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
marci@569
   735
//the unique invalid iterator
marci@650
   736
      InEdgeIt(const SubBidirGraphWrapper<Graph, 
marci@650
   737
	       ForwardFilterMap, BackwardFilterMap>& _G, Node v) { 
marci@569
   738
	backward=false;
marci@569
   739
	_G.graph->first(in, v);
marci@650
   740
	while(_G.graph->valid(in) && !(*_G.forward_filter)[*this]) { _G.graph->next(in); }
marci@569
   741
	if (!_G.graph->valid(in)) {
marci@569
   742
	  backward=true;
marci@569
   743
	  _G.graph->first(out, v);
marci@650
   744
	  while(_G.graph->valid(out) && !(*_G.backward_filter)[*this]) { _G.graph->next(out); }
marci@569
   745
	}
marci@569
   746
      }
marci@569
   747
      operator Edge() const { 
marci@569
   748
//	Edge e;
marci@569
   749
//	e.forward=this->forward;
marci@569
   750
//	if (this->forward) e=out; else e=in;
marci@569
   751
//	return e;
marci@569
   752
	if (this->backward) 
marci@569
   753
	  return Edge(out, this->backward); 
marci@569
   754
	else 
marci@569
   755
	  return Edge(in, this->backward);
marci@569
   756
      }
marci@569
   757
    };
marci@569
   758
marci@569
   759
    class EdgeIt {
marci@650
   760
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   761
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   762
    protected:
marci@569
   763
      typename Graph::EdgeIt e;
marci@569
   764
      bool backward;
marci@569
   765
    public:
marci@569
   766
      EdgeIt() { }
marci@569
   767
      EdgeIt(const Invalid& i) : e(i), backward(true) { }
marci@650
   768
      EdgeIt(const SubBidirGraphWrapper<Graph, 
marci@650
   769
	     ForwardFilterMap, BackwardFilterMap>& _G) { 
marci@569
   770
	backward=false;
marci@569
   771
	_G.graph->first(e);
marci@650
   772
	while (_G.graph->valid(e) && !(*_G.forward_filter)[*this]) _G.graph->next(e);
marci@569
   773
	if (!_G.graph->valid(e)) {
marci@569
   774
	  backward=true;
marci@569
   775
	  _G.graph->first(e);
marci@650
   776
	  while (_G.graph->valid(e) && !(*_G.backward_filter)[*this]) _G.graph->next(e);
marci@569
   777
	}
marci@569
   778
      }
marci@569
   779
      operator Edge() const { 
marci@569
   780
	return Edge(e, this->backward);
marci@569
   781
      }
marci@569
   782
    };
marci@569
   783
marci@569
   784
    using GraphWrapper<Graph>::first;
marci@569
   785
//     NodeIt& first(NodeIt& i) const { 
marci@569
   786
//       i=NodeIt(*this); return i;
marci@569
   787
//     }
marci@569
   788
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@569
   789
      i=OutEdgeIt(*this, p); return i;
marci@569
   790
    }
marci@569
   791
//    FIXME not tested
marci@569
   792
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@569
   793
      i=InEdgeIt(*this, p); return i;
marci@569
   794
    }
marci@569
   795
    EdgeIt& first(EdgeIt& i) const { 
marci@569
   796
      i=EdgeIt(*this); return i;
marci@569
   797
    }
marci@556
   798
  
marci@569
   799
    using GraphWrapper<Graph>::next;
marci@569
   800
//    NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
marci@569
   801
    OutEdgeIt& next(OutEdgeIt& e) const { 
marci@569
   802
      if (!e.backward) {
marci@569
   803
	Node v=this->graph->aNode(e.out);
marci@569
   804
	this->graph->next(e.out);
marci@650
   805
	while(this->graph->valid(e.out) && !(*forward_filter)[e]) { 
marci@569
   806
	  this->graph->next(e.out); }
marci@569
   807
	if (!this->graph->valid(e.out)) {
marci@569
   808
	  e.backward=true;
marci@569
   809
	  this->graph->first(e.in, v); 
marci@650
   810
	  while(this->graph->valid(e.in) && !(*backward_filter)[e]) { 
marci@569
   811
	    this->graph->next(e.in); }
marci@569
   812
	}
marci@569
   813
      } else {
marci@569
   814
	this->graph->next(e.in);
marci@650
   815
	while(this->graph->valid(e.in) && !(*backward_filter)[e]) { 
marci@569
   816
	  this->graph->next(e.in); } 
marci@569
   817
      }
marci@569
   818
      return e;
marci@569
   819
    }
marci@569
   820
//     FIXME Not tested
marci@569
   821
    InEdgeIt& next(InEdgeIt& e) const { 
marci@569
   822
      if (!e.backward) {
marci@569
   823
	Node v=this->graph->aNode(e.in);
marci@569
   824
	this->graph->next(e.in);
marci@650
   825
	while(this->graph->valid(e.in) && !(*forward_filter)[e]) { 
marci@569
   826
	  this->graph->next(e.in); }
marci@569
   827
	if (!this->graph->valid(e.in)) {
marci@569
   828
	  e.backward=true;
marci@569
   829
	  this->graph->first(e.out, v); 
marci@650
   830
	  while(this->graph->valid(e.out) && !(*backward_filter)[e]) { 
marci@569
   831
	    this->graph->next(e.out); }
marci@569
   832
	}
marci@569
   833
      } else {
marci@569
   834
	this->graph->next(e.out);
marci@650
   835
	while(this->graph->valid(e.out) && !(*backward_filter)[e]) { 
marci@569
   836
	  this->graph->next(e.out); } 
marci@569
   837
      }
marci@569
   838
      return e;
marci@569
   839
    }
marci@569
   840
    EdgeIt& next(EdgeIt& e) const {
marci@569
   841
      if (!e.backward) {
marci@569
   842
	this->graph->next(e.e);
marci@650
   843
	while(this->graph->valid(e.e) && !(*forward_filter)[e]) { 
marci@569
   844
	  this->graph->next(e.e); }
marci@569
   845
	if (!this->graph->valid(e.e)) {
marci@569
   846
	  e.backward=true;
marci@569
   847
	  this->graph->first(e.e); 
marci@650
   848
	  while(this->graph->valid(e.e) && !(*backward_filter)[e]) { 
marci@569
   849
	    this->graph->next(e.e); }
marci@569
   850
	}
marci@569
   851
      } else {
marci@569
   852
	this->graph->next(e.e);
marci@650
   853
	while(this->graph->valid(e.e) && !(*backward_filter)[e]) { 
marci@569
   854
	  this->graph->next(e.e); } 
marci@569
   855
      }
marci@569
   856
      return e;
marci@569
   857
    }
marci@569
   858
marci@569
   859
    Node tail(Edge e) const { 
marci@569
   860
      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
marci@569
   861
    Node head(Edge e) const { 
marci@569
   862
      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
marci@569
   863
marci@569
   864
    Node aNode(OutEdgeIt e) const { 
marci@569
   865
      return ((!e.backward) ? this->graph->aNode(e.out) : 
marci@569
   866
	      this->graph->aNode(e.in)); }
marci@569
   867
    Node bNode(OutEdgeIt e) const { 
marci@569
   868
      return ((!e.backward) ? this->graph->bNode(e.out) : 
marci@569
   869
	      this->graph->bNode(e.in)); }
marci@569
   870
marci@569
   871
    Node aNode(InEdgeIt e) const { 
marci@569
   872
      return ((!e.backward) ? this->graph->aNode(e.in) : 
marci@569
   873
	      this->graph->aNode(e.out)); }
marci@569
   874
    Node bNode(InEdgeIt e) const { 
marci@569
   875
      return ((!e.backward) ? this->graph->bNode(e.in) : 
marci@569
   876
	      this->graph->bNode(e.out)); }
marci@569
   877
marci@572
   878
    /// Gives back the opposite edge.
marci@572
   879
    Edge opposite(const Edge& e) const { 
marci@572
   880
      Edge f=e;
marci@572
   881
      f.backward=!f.backward;
marci@572
   882
      return f;
marci@572
   883
    }
marci@572
   884
marci@569
   885
//    int nodeNum() const { return graph->nodeNum(); }
marci@569
   886
    //FIXME
marci@569
   887
    void edgeNum() const { }
marci@569
   888
    //int edgeNum() const { return graph->edgeNum(); }
marci@569
   889
marci@569
   890
marci@569
   891
//    int id(Node v) const { return graph->id(v); }
marci@569
   892
marci@569
   893
    bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
marci@569
   894
    bool valid(Edge e) const { 
marci@569
   895
      return this->graph->valid(e);
marci@569
   896
	//return e.forward ? graph->valid(e.out) : graph->valid(e.in); 
marci@569
   897
    }
marci@569
   898
marci@569
   899
    bool forward(const Edge& e) const { return !e.backward; }
marci@569
   900
    bool backward(const Edge& e) const { return e.backward; }
marci@569
   901
marci@569
   902
//     void augment(const Edge& e, Number a) const {
marci@569
   903
//       if (!e.backward)  
marci@569
   904
// // 	flow->set(e.out, flow->get(e.out)+a);
marci@569
   905
// 	flow->set(e, (*flow)[e]+a);
marci@569
   906
//       else  
marci@569
   907
// // 	flow->set(e.in, flow->get(e.in)-a);
marci@569
   908
// 	flow->set(e, (*flow)[e]-a);
marci@569
   909
//     }
marci@569
   910
marci@650
   911
//     bool enabled(const Edge& e) const { 
marci@650
   912
//       if (!e.backward) 
marci@650
   913
// //	return (capacity->get(e.out)-flow->get(e.out)); 
marci@650
   914
// 	//return ((*capacity)[e]-(*flow)[e]);
marci@650
   915
// 	return true;
marci@650
   916
//       else 
marci@650
   917
// //	return (flow->get(e.in)); 
marci@650
   918
// 	//return ((*flow)[e]); 
marci@650
   919
// 	return true;
marci@650
   920
//     }
marci@569
   921
marci@569
   922
//     Number enabled(typename Graph::OutEdgeIt out) const { 
marci@569
   923
// //      return (capacity->get(out)-flow->get(out)); 
marci@569
   924
//       return ((*capacity)[out]-(*flow)[out]); 
marci@569
   925
//     }
marci@569
   926
    
marci@569
   927
//     Number enabled(typename Graph::InEdgeIt in) const { 
marci@569
   928
// //      return (flow->get(in)); 
marci@569
   929
//       return ((*flow)[in]); 
marci@569
   930
//     }
marci@569
   931
marci@569
   932
    template <typename T>
marci@569
   933
    class EdgeMap {
marci@569
   934
      typename Graph::template EdgeMap<T> forward_map, backward_map; 
marci@569
   935
    public:
marci@623
   936
      typedef T ValueType;
marci@623
   937
      typedef Edge KeyType;
marci@650
   938
      EdgeMap(const SubBidirGraphWrapper<Graph, 
marci@650
   939
	      ForwardFilterMap, BackwardFilterMap>& _G) : 
marci@650
   940
	forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
marci@650
   941
      EdgeMap(const SubBidirGraphWrapper<Graph, 
marci@650
   942
	      ForwardFilterMap, BackwardFilterMap>& _G, T a) : 
marci@650
   943
	forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
marci@569
   944
      void set(Edge e, T a) { 
marci@569
   945
	if (!e.backward) 
marci@622
   946
	  forward_map.set(e/*.out*/, a); 
marci@569
   947
	else 
marci@622
   948
	  backward_map.set(e/*.in*/, a); 
marci@569
   949
      }
marci@569
   950
      T operator[](Edge e) const { 
marci@569
   951
	if (!e.backward) 
marci@622
   952
	  return forward_map[e/*.out*/]; 
marci@569
   953
	else 
marci@622
   954
	  return backward_map[e/*.in*/]; 
marci@569
   955
      }
marci@625
   956
      void update() { 
marci@625
   957
	forward_map.update(); 
marci@625
   958
	backward_map.update();
marci@625
   959
      }
marci@569
   960
//       T get(Edge e) const { 
marci@569
   961
// 	if (e.out_or_in) 
marci@569
   962
// 	  return forward_map.get(e.out); 
marci@569
   963
// 	else 
marci@569
   964
// 	  return backward_map.get(e.in); 
marci@569
   965
//       }
marci@569
   966
    };
marci@569
   967
  };
marci@569
   968
marci@650
   969
marci@650
   970
marci@650
   971
  ///\brief A wrapper for composing bidirected graph from a directed one. 
marci@650
   972
  /// experimental, for fezso's sake.
marci@650
   973
  ///
marci@650
   974
  /// A wrapper for composing bidirected graph from a directed one. 
marci@650
   975
  /// experimental, for fezso's sake.
marci@650
   976
  /// A bidirected graph is composed over the directed one without physical 
marci@650
   977
  /// storage. As the oppositely directed edges are logically different ones 
marci@650
   978
  /// the maps are able to attach different values for them.
marci@650
   979
  template<typename Graph>
marci@650
   980
  class BidirGraphWrapper : 
marci@650
   981
    public SubBidirGraphWrapper<
marci@650
   982
    Graph, 
marci@650
   983
    ConstMap<typename Graph::Edge, bool>, 
marci@650
   984
    ConstMap<typename Graph::Edge, bool> > {
marci@650
   985
  public:
marci@650
   986
    typedef  SubBidirGraphWrapper<
marci@650
   987
      Graph, 
marci@650
   988
      ConstMap<typename Graph::Edge, bool>, 
marci@650
   989
      ConstMap<typename Graph::Edge, bool> > Parent; 
marci@650
   990
  protected:
marci@650
   991
    ConstMap<typename Graph::Edge, bool> cm;
marci@650
   992
    //const CapacityMap* capacity;
marci@650
   993
    //FlowMap* flow;
marci@650
   994
marci@655
   995
    BidirGraphWrapper() : Parent(), cm(true) { 
marci@655
   996
      Parent::setForwardFilterMap(cm);
marci@655
   997
      Parent::setBackwardFilterMap(cm);
marci@655
   998
    }
marci@650
   999
//     void setCapacityMap(const CapacityMap& _capacity) {
marci@650
  1000
//       capacity=&_capacity;
marci@650
  1001
//     }
marci@650
  1002
//     void setFlowMap(FlowMap& _flow) {
marci@650
  1003
//       flow=&_flow;
marci@650
  1004
//     }
marci@650
  1005
marci@650
  1006
  public:
marci@650
  1007
marci@650
  1008
    BidirGraphWrapper(Graph& _graph) : Parent() { 
marci@650
  1009
      Parent::setGraph(_graph);
marci@650
  1010
      Parent::setForwardFilterMap(cm);
marci@650
  1011
      Parent::setBackwardFilterMap(cm);
marci@650
  1012
    }
marci@650
  1013
  };
marci@650
  1014
marci@650
  1015
marci@650
  1016
marci@650
  1017
marci@653
  1018
  template<typename Graph>
marci@653
  1019
  class OldBidirGraphWrapper : public GraphWrapper<Graph> {
marci@653
  1020
  public:
marci@653
  1021
    typedef GraphWrapper<Graph> Parent; 
marci@653
  1022
  protected:
marci@653
  1023
    //const CapacityMap* capacity;
marci@653
  1024
    //FlowMap* flow;
marci@650
  1025
marci@653
  1026
    OldBidirGraphWrapper() : GraphWrapper<Graph>()/*, 
marci@653
  1027
						 capacity(0), flow(0)*/ { }
marci@653
  1028
//     void setCapacityMap(const CapacityMap& _capacity) {
marci@653
  1029
//       capacity=&_capacity;
marci@650
  1030
//     }
marci@653
  1031
//     void setFlowMap(FlowMap& _flow) {
marci@653
  1032
//       flow=&_flow;
marci@650
  1033
//     }
marci@650
  1034
marci@653
  1035
  public:
marci@650
  1036
marci@653
  1037
    OldBidirGraphWrapper(Graph& _graph/*, const CapacityMap& _capacity, 
marci@653
  1038
				     FlowMap& _flow*/) : 
marci@653
  1039
      GraphWrapper<Graph>(_graph)/*, capacity(&_capacity), flow(&_flow)*/ { }
marci@650
  1040
marci@653
  1041
    class Edge; 
marci@653
  1042
    class OutEdgeIt; 
marci@653
  1043
    friend class Edge; 
marci@653
  1044
    friend class OutEdgeIt; 
marci@650
  1045
marci@653
  1046
    //template<typename T> class NodeMap;    
marci@653
  1047
    template<typename T> class EdgeMap;
marci@653
  1048
marci@653
  1049
    typedef typename GraphWrapper<Graph>::Node Node;
marci@653
  1050
    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@653
  1051
marci@653
  1052
    class Edge : public Graph::Edge {
marci@653
  1053
      friend class OldBidirGraphWrapper<Graph>;
marci@653
  1054
      ///\bug ez nem is kell
marci@653
  1055
      //template<typename T> friend class NodeMap;
marci@653
  1056
      template<typename T> friend class EdgeMap;
marci@653
  1057
    protected:
marci@653
  1058
      bool backward; //true, iff backward
marci@653
  1059
//      typename Graph::Edge e;
marci@653
  1060
    public:
marci@653
  1061
      Edge() { }
marci@653
  1062
      ///\bug =false kell-e? zsoltnak kell az addEdge miatt
marci@653
  1063
      Edge(const typename Graph::Edge& _e, bool _backward=false) : 
marci@653
  1064
	Graph::Edge(_e), backward(_backward) { }
marci@653
  1065
      Edge(const Invalid& i) : Graph::Edge(i), backward(true) { }
marci@653
  1066
//the unique invalid iterator
marci@653
  1067
      friend bool operator==(const Edge& u, const Edge& v) { 
marci@653
  1068
	return (v.backward==u.backward && 
marci@653
  1069
		static_cast<typename Graph::Edge>(u)==
marci@653
  1070
		static_cast<typename Graph::Edge>(v));
marci@653
  1071
      } 
marci@653
  1072
      friend bool operator!=(const Edge& u, const Edge& v) { 
marci@653
  1073
	return (v.backward!=u.backward || 
marci@653
  1074
		static_cast<typename Graph::Edge>(u)!=
marci@653
  1075
		static_cast<typename Graph::Edge>(v));
marci@653
  1076
      } 
marci@653
  1077
    };
marci@653
  1078
marci@653
  1079
    class OutEdgeIt {
marci@653
  1080
      friend class OldBidirGraphWrapper<Graph>;
marci@653
  1081
    protected:
marci@653
  1082
      typename Graph::OutEdgeIt out;
marci@653
  1083
      typename Graph::InEdgeIt in;
marci@653
  1084
      bool backward;
marci@653
  1085
    public:
marci@653
  1086
      OutEdgeIt() { }
marci@653
  1087
      //FIXME
marci@653
  1088
//      OutEdgeIt(const Edge& e) : Edge(e) { }
marci@653
  1089
      OutEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
marci@653
  1090
//the unique invalid iterator
marci@653
  1091
      OutEdgeIt(const OldBidirGraphWrapper<Graph>& _G, Node v) { 
marci@653
  1092
	backward=false;
marci@653
  1093
	_G.graph->first(out, v);
marci@653
  1094
	while(_G.graph->valid(out) && !_G.enabled(*this)) { _G.graph->next(out); }
marci@653
  1095
	if (!_G.graph->valid(out)) {
marci@653
  1096
	  backward=true;
marci@653
  1097
	  _G.graph->first(in, v);
marci@653
  1098
	  while(_G.graph->valid(in) && !_G.enabled(*this)) { _G.graph->next(in); }
marci@653
  1099
	}
marci@653
  1100
      }
marci@653
  1101
      operator Edge() const { 
marci@653
  1102
//	Edge e;
marci@653
  1103
//	e.forward=this->forward;
marci@653
  1104
//	if (this->forward) e=out; else e=in;
marci@653
  1105
//	return e;
marci@653
  1106
	if (this->backward) 
marci@653
  1107
	  return Edge(in, this->backward); 
marci@653
  1108
	else 
marci@653
  1109
	  return Edge(out, this->backward);
marci@653
  1110
      }
marci@653
  1111
    };
marci@653
  1112
marci@653
  1113
    class InEdgeIt {
marci@653
  1114
      friend class OldBidirGraphWrapper<Graph>;
marci@653
  1115
    protected:
marci@653
  1116
      typename Graph::OutEdgeIt out;
marci@653
  1117
      typename Graph::InEdgeIt in;
marci@653
  1118
      bool backward;
marci@653
  1119
    public:
marci@653
  1120
      InEdgeIt() { }
marci@653
  1121
      //FIXME
marci@653
  1122
//      OutEdgeIt(const Edge& e) : Edge(e) { }
marci@653
  1123
      InEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
marci@653
  1124
//the unique invalid iterator
marci@653
  1125
      InEdgeIt(const OldBidirGraphWrapper<Graph>& _G, Node v) { 
marci@653
  1126
	backward=false;
marci@653
  1127
	_G.graph->first(in, v);
marci@653
  1128
	while(_G.graph->valid(in) && !_G.enabled(*this)) { _G.graph->next(in); }
marci@653
  1129
	if (!_G.graph->valid(in)) {
marci@653
  1130
	  backward=true;
marci@653
  1131
	  _G.graph->first(out, v);
marci@653
  1132
	  while(_G.graph->valid(out) && !_G.enabled(*this)) { _G.graph->next(out); }
marci@653
  1133
	}
marci@653
  1134
      }
marci@653
  1135
      operator Edge() const { 
marci@653
  1136
//	Edge e;
marci@653
  1137
//	e.forward=this->forward;
marci@653
  1138
//	if (this->forward) e=out; else e=in;
marci@653
  1139
//	return e;
marci@653
  1140
	if (this->backward) 
marci@653
  1141
	  return Edge(out, this->backward); 
marci@653
  1142
	else 
marci@653
  1143
	  return Edge(in, this->backward);
marci@653
  1144
      }
marci@653
  1145
    };
marci@653
  1146
marci@653
  1147
    class EdgeIt {
marci@653
  1148
      friend class OldBidirGraphWrapper<Graph>;
marci@653
  1149
    protected:
marci@653
  1150
      typename Graph::EdgeIt e;
marci@653
  1151
      bool backward;
marci@653
  1152
    public:
marci@653
  1153
      EdgeIt() { }
marci@653
  1154
      EdgeIt(const Invalid& i) : e(i), backward(true) { }
marci@653
  1155
      EdgeIt(const OldBidirGraphWrapper<Graph>& _G) { 
marci@653
  1156
	backward=false;
marci@653
  1157
	_G.graph->first(e);
marci@653
  1158
	while (_G.graph->valid(e) && !_G.enabled(*this)) _G.graph->next(e);
marci@653
  1159
	if (!_G.graph->valid(e)) {
marci@653
  1160
	  backward=true;
marci@653
  1161
	  _G.graph->first(e);
marci@653
  1162
	  while (_G.graph->valid(e) && !_G.enabled(*this)) _G.graph->next(e);
marci@653
  1163
	}
marci@653
  1164
      }
marci@653
  1165
      operator Edge() const { 
marci@653
  1166
	return Edge(e, this->backward);
marci@653
  1167
      }
marci@653
  1168
    };
marci@653
  1169
marci@653
  1170
    using GraphWrapper<Graph>::first;
marci@653
  1171
//     NodeIt& first(NodeIt& i) const { 
marci@653
  1172
//       i=NodeIt(*this); return i;
marci@653
  1173
//     }
marci@653
  1174
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@653
  1175
      i=OutEdgeIt(*this, p); return i;
marci@653
  1176
    }
marci@653
  1177
//    FIXME not tested
marci@653
  1178
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@653
  1179
      i=InEdgeIt(*this, p); return i;
marci@653
  1180
    }
marci@653
  1181
    EdgeIt& first(EdgeIt& i) const { 
marci@653
  1182
      i=EdgeIt(*this); return i;
marci@653
  1183
    }
marci@653
  1184
  
marci@653
  1185
    using GraphWrapper<Graph>::next;
marci@653
  1186
//    NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
marci@653
  1187
    OutEdgeIt& next(OutEdgeIt& e) const { 
marci@653
  1188
      if (!e.backward) {
marci@653
  1189
	Node v=this->graph->aNode(e.out);
marci@653
  1190
	this->graph->next(e.out);
marci@653
  1191
	while(this->graph->valid(e.out) && !enabled(e)) { 
marci@653
  1192
	  this->graph->next(e.out); }
marci@653
  1193
	if (!this->graph->valid(e.out)) {
marci@653
  1194
	  e.backward=true;
marci@653
  1195
	  this->graph->first(e.in, v); 
marci@653
  1196
	  while(this->graph->valid(e.in) && !enabled(e)) { 
marci@653
  1197
	    this->graph->next(e.in); }
marci@653
  1198
	}
marci@653
  1199
      } else {
marci@653
  1200
	this->graph->next(e.in);
marci@653
  1201
	while(this->graph->valid(e.in) && !enabled(e)) { 
marci@653
  1202
	  this->graph->next(e.in); } 
marci@653
  1203
      }
marci@653
  1204
      return e;
marci@653
  1205
    }
marci@653
  1206
//     FIXME Not tested
marci@653
  1207
    InEdgeIt& next(InEdgeIt& e) const { 
marci@653
  1208
      if (!e.backward) {
marci@653
  1209
	Node v=this->graph->aNode(e.in);
marci@653
  1210
	this->graph->next(e.in);
marci@653
  1211
	while(this->graph->valid(e.in) && !enabled(e)) { 
marci@653
  1212
	  this->graph->next(e.in); }
marci@653
  1213
	if (!this->graph->valid(e.in)) {
marci@653
  1214
	  e.backward=true;
marci@653
  1215
	  this->graph->first(e.out, v); 
marci@653
  1216
	  while(this->graph->valid(e.out) && !enabled(e)) { 
marci@653
  1217
	    this->graph->next(e.out); }
marci@653
  1218
	}
marci@653
  1219
      } else {
marci@653
  1220
	this->graph->next(e.out);
marci@653
  1221
	while(this->graph->valid(e.out) && !enabled(e)) { 
marci@653
  1222
	  this->graph->next(e.out); } 
marci@653
  1223
      }
marci@653
  1224
      return e;
marci@653
  1225
    }
marci@653
  1226
    EdgeIt& next(EdgeIt& e) const {
marci@653
  1227
      if (!e.backward) {
marci@653
  1228
	this->graph->next(e.e);
marci@653
  1229
	while(this->graph->valid(e.e) && !enabled(e)) { 
marci@653
  1230
	  this->graph->next(e.e); }
marci@653
  1231
	if (!this->graph->valid(e.e)) {
marci@653
  1232
	  e.backward=true;
marci@653
  1233
	  this->graph->first(e.e); 
marci@653
  1234
	  while(this->graph->valid(e.e) && !enabled(e)) { 
marci@653
  1235
	    this->graph->next(e.e); }
marci@653
  1236
	}
marci@653
  1237
      } else {
marci@653
  1238
	this->graph->next(e.e);
marci@653
  1239
	while(this->graph->valid(e.e) && !enabled(e)) { 
marci@653
  1240
	  this->graph->next(e.e); } 
marci@653
  1241
      }
marci@653
  1242
      return e;
marci@653
  1243
    }
marci@653
  1244
marci@653
  1245
    Node tail(Edge e) const { 
marci@653
  1246
      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
marci@653
  1247
    Node head(Edge e) const { 
marci@653
  1248
      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
marci@653
  1249
marci@653
  1250
    Node aNode(OutEdgeIt e) const { 
marci@653
  1251
      return ((!e.backward) ? this->graph->aNode(e.out) : 
marci@653
  1252
	      this->graph->aNode(e.in)); }
marci@653
  1253
    Node bNode(OutEdgeIt e) const { 
marci@653
  1254
      return ((!e.backward) ? this->graph->bNode(e.out) : 
marci@653
  1255
	      this->graph->bNode(e.in)); }
marci@653
  1256
marci@653
  1257
    Node aNode(InEdgeIt e) const { 
marci@653
  1258
      return ((!e.backward) ? this->graph->aNode(e.in) : 
marci@653
  1259
	      this->graph->aNode(e.out)); }
marci@653
  1260
    Node bNode(InEdgeIt e) const { 
marci@653
  1261
      return ((!e.backward) ? this->graph->bNode(e.in) : 
marci@653
  1262
	      this->graph->bNode(e.out)); }
marci@653
  1263
marci@653
  1264
    /// Gives back the opposite edge.
marci@653
  1265
    Edge opposite(const Edge& e) const { 
marci@653
  1266
      Edge f=e;
marci@653
  1267
      f.backward=!f.backward;
marci@653
  1268
      return f;
marci@653
  1269
    }
marci@653
  1270
marci@653
  1271
//    int nodeNum() const { return graph->nodeNum(); }
marci@653
  1272
    //FIXME
marci@653
  1273
    void edgeNum() const { }
marci@653
  1274
    //int edgeNum() const { return graph->edgeNum(); }
marci@653
  1275
marci@653
  1276
marci@653
  1277
//    int id(Node v) const { return graph->id(v); }
marci@653
  1278
marci@653
  1279
    bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
marci@653
  1280
    bool valid(Edge e) const { 
marci@653
  1281
      return this->graph->valid(e);
marci@653
  1282
	//return e.forward ? graph->valid(e.out) : graph->valid(e.in); 
marci@653
  1283
    }
marci@653
  1284
marci@653
  1285
    bool forward(const Edge& e) const { return !e.backward; }
marci@653
  1286
    bool backward(const Edge& e) const { return e.backward; }
marci@653
  1287
marci@653
  1288
//     void augment(const Edge& e, Number a) const {
marci@653
  1289
//       if (!e.backward)  
marci@653
  1290
// // 	flow->set(e.out, flow->get(e.out)+a);
marci@653
  1291
// 	flow->set(e, (*flow)[e]+a);
marci@653
  1292
//       else  
marci@653
  1293
// // 	flow->set(e.in, flow->get(e.in)-a);
marci@653
  1294
// 	flow->set(e, (*flow)[e]-a);
marci@650
  1295
//     }
marci@650
  1296
marci@653
  1297
    bool enabled(const Edge& e) const { 
marci@653
  1298
      if (!e.backward) 
marci@653
  1299
//	return (capacity->get(e.out)-flow->get(e.out)); 
marci@653
  1300
	//return ((*capacity)[e]-(*flow)[e]);
marci@653
  1301
	return true;
marci@653
  1302
      else 
marci@653
  1303
//	return (flow->get(e.in)); 
marci@653
  1304
	//return ((*flow)[e]); 
marci@653
  1305
	return true;
marci@653
  1306
    }
marci@650
  1307
marci@653
  1308
//     Number enabled(typename Graph::OutEdgeIt out) const { 
marci@653
  1309
// //      return (capacity->get(out)-flow->get(out)); 
marci@653
  1310
//       return ((*capacity)[out]-(*flow)[out]); 
marci@653
  1311
//     }
marci@653
  1312
    
marci@653
  1313
//     Number enabled(typename Graph::InEdgeIt in) const { 
marci@653
  1314
// //      return (flow->get(in)); 
marci@653
  1315
//       return ((*flow)[in]); 
marci@650
  1316
//     }
marci@650
  1317
marci@653
  1318
    template <typename T>
marci@653
  1319
    class EdgeMap {
marci@653
  1320
      typename Graph::template EdgeMap<T> forward_map, backward_map; 
marci@653
  1321
    public:
marci@653
  1322
      typedef T ValueType;
marci@653
  1323
      typedef Edge KeyType;
marci@653
  1324
      EdgeMap(const OldBidirGraphWrapper<Graph>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
marci@653
  1325
      EdgeMap(const OldBidirGraphWrapper<Graph>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
marci@653
  1326
      void set(Edge e, T a) { 
marci@653
  1327
	if (!e.backward) 
marci@653
  1328
	  forward_map.set(e/*.out*/, a); 
marci@653
  1329
	else 
marci@653
  1330
	  backward_map.set(e/*.in*/, a); 
marci@653
  1331
      }
marci@653
  1332
      T operator[](Edge e) const { 
marci@653
  1333
	if (!e.backward) 
marci@653
  1334
	  return forward_map[e/*.out*/]; 
marci@653
  1335
	else 
marci@653
  1336
	  return backward_map[e/*.in*/]; 
marci@653
  1337
      }
marci@653
  1338
      void update() { 
marci@653
  1339
	forward_map.update(); 
marci@653
  1340
	backward_map.update();
marci@653
  1341
      }
marci@653
  1342
//       T get(Edge e) const { 
marci@653
  1343
// 	if (e.out_or_in) 
marci@653
  1344
// 	  return forward_map.get(e.out); 
marci@653
  1345
// 	else 
marci@653
  1346
// 	  return backward_map.get(e.in); 
marci@653
  1347
//       }
marci@653
  1348
    };
marci@653
  1349
  };
marci@650
  1350
marci@650
  1351
marci@650
  1352
marci@612
  1353
  /// \brief A bidirected graph template.
marci@612
  1354
  ///
marci@612
  1355
  /// A bidirected graph template.
marci@612
  1356
  /// Such a bidirected graph stores each pair of oppositely directed edges 
marci@612
  1357
  /// ones in the memory, i.e. a directed graph of type 
marci@612
  1358
  /// \c Graph is used for that.
marci@612
  1359
  /// As the oppositely directed edges are logically different ones 
marci@612
  1360
  /// the maps are able to attach different values for them.
marci@612
  1361
  /// \ingroup graphs
marci@612
  1362
  template<typename Graph>
marci@612
  1363
  class BidirGraph : public BidirGraphWrapper<Graph> {
marci@650
  1364
  public:
marci@612
  1365
    typedef UndirGraphWrapper<Graph> Parent;
marci@612
  1366
  protected:
marci@612
  1367
    Graph gr;
marci@612
  1368
  public:
marci@612
  1369
    BidirGraph() : BidirGraphWrapper<Graph>() { 
marci@612
  1370
      Parent::setGraph(gr); 
marci@612
  1371
    }
marci@612
  1372
  };
marci@569
  1373
marci@556
  1374
marci@650
  1375
marci@650
  1376
  template<typename Graph, typename Number,
marci@650
  1377
	   typename CapacityMap, typename FlowMap>
marci@658
  1378
  class ResForwardFilter {
marci@658
  1379
    //    const Graph* graph;
marci@650
  1380
    const CapacityMap* capacity;
marci@650
  1381
    const FlowMap* flow;
marci@650
  1382
  public:
marci@658
  1383
    ResForwardFilter(/*const Graph& _graph, */
marci@658
  1384
		     const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1385
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1386
    ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@658
  1387
    //void setGraph(const Graph& _graph) { graph=&_graph; }
marci@656
  1388
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1389
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1390
    bool operator[](const typename Graph::Edge& e) const {
marci@650
  1391
      return ((*flow)[e] < (*capacity)[e]);
marci@650
  1392
    }
marci@650
  1393
  };
marci@650
  1394
marci@650
  1395
  template<typename Graph, typename Number,
marci@650
  1396
	   typename CapacityMap, typename FlowMap>
marci@658
  1397
  class ResBackwardFilter {
marci@658
  1398
    //const Graph* graph;
marci@650
  1399
    const CapacityMap* capacity;
marci@650
  1400
    const FlowMap* flow;
marci@650
  1401
  public:
marci@658
  1402
    ResBackwardFilter(/*const Graph& _graph,*/ 
marci@658
  1403
		      const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1404
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1405
    ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@658
  1406
    //void setGraph(const Graph& _graph) { graph=&_graph; }
marci@656
  1407
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1408
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1409
    bool operator[](const typename Graph::Edge& e) const {
marci@650
  1410
      return (0 < (*flow)[e]);
marci@650
  1411
    }
marci@650
  1412
  };
marci@650
  1413
marci@653
  1414
  
marci@653
  1415
  /// A wrapper for composing the residual graph for directed flow and circulation problems.
marci@650
  1416
marci@653
  1417
  /// A wrapper for composing the residual graph for directed flow and circulation problems.
marci@650
  1418
  template<typename Graph, typename Number, 
marci@650
  1419
	   typename CapacityMap, typename FlowMap>
marci@653
  1420
  class ResGraphWrapper : 
marci@650
  1421
    public SubBidirGraphWrapper< 
marci@650
  1422
    Graph, 
marci@658
  1423
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1424
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
marci@650
  1425
  public:
marci@650
  1426
    typedef SubBidirGraphWrapper< 
marci@650
  1427
      Graph, 
marci@658
  1428
      ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1429
      ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
marci@650
  1430
  protected:
marci@650
  1431
    const CapacityMap* capacity;
marci@650
  1432
    FlowMap* flow;
marci@658
  1433
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
marci@658
  1434
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
marci@658
  1435
    ResGraphWrapper() : Parent(), 
marci@658
  1436
 			capacity(0), flow(0) { }
marci@658
  1437
    void setCapacityMap(const CapacityMap& _capacity) {
marci@658
  1438
      capacity=&_capacity;
marci@658
  1439
      forward_filter.setCapacity(_capacity);
marci@658
  1440
      backward_filter.setCapacity(_capacity);
marci@658
  1441
    }
marci@658
  1442
    void setFlowMap(FlowMap& _flow) {
marci@658
  1443
      flow=&_flow;
marci@658
  1444
      forward_filter.setFlow(_flow);
marci@658
  1445
      backward_filter.setFlow(_flow);
marci@658
  1446
    }
marci@658
  1447
//     /// \bug does graph reference needed in filtermaps??
marci@658
  1448
//     void setGraph(const Graph& _graph) { 
marci@658
  1449
//       Parent::setGraph(_graph);
marci@658
  1450
//       forward_filter.setGraph(_graph);
marci@658
  1451
//       backward_filter.setGraph(_graph);
marci@656
  1452
//     }
marci@650
  1453
  public:
marci@653
  1454
    ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity, 
marci@650
  1455
		       FlowMap& _flow) : 
marci@650
  1456
      Parent(), capacity(&_capacity), flow(&_flow), 
marci@658
  1457
      forward_filter(/*_graph,*/ _capacity, _flow), 
marci@658
  1458
      backward_filter(/*_graph,*/ _capacity, _flow) {
marci@650
  1459
      Parent::setGraph(_graph);
marci@650
  1460
      Parent::setForwardFilterMap(forward_filter);
marci@650
  1461
      Parent::setBackwardFilterMap(backward_filter);
marci@650
  1462
    }
marci@650
  1463
marci@660
  1464
    typedef typename Parent::Edge Edge;
marci@660
  1465
marci@650
  1466
    //    bool forward(const Parent::Edge& e) const { return Parent::forward(e); }
marci@650
  1467
    //bool backward(const Edge& e) const { return e.backward; }
marci@650
  1468
marci@660
  1469
    void augment(const Edge& e, Number a) const {
marci@650
  1470
      if (Parent::forward(e))  
marci@650
  1471
// 	flow->set(e.out, flow->get(e.out)+a);
marci@650
  1472
	flow->set(e, (*flow)[e]+a);
marci@650
  1473
      else  
marci@650
  1474
	//flow->set(e.in, flow->get(e.in)-a);
marci@650
  1475
	flow->set(e, (*flow)[e]-a);
marci@650
  1476
    }
marci@650
  1477
marci@660
  1478
    /// \deprecated
marci@660
  1479
    ///
marci@660
  1480
    Number resCap(const Edge& e) const { 
marci@650
  1481
      if (Parent::forward(e)) 
marci@650
  1482
//	return (capacity->get(e.out)-flow->get(e.out)); 
marci@650
  1483
	return ((*capacity)[e]-(*flow)[e]); 
marci@650
  1484
      else 
marci@650
  1485
//	return (flow->get(e.in)); 
marci@650
  1486
	return ((*flow)[e]); 
marci@650
  1487
    }
marci@650
  1488
marci@660
  1489
    /// \brief Residual capacity map.
marci@660
  1490
    ///
marci@660
  1491
    /// In generic residual graphs the residual capacity can be obtained as a map.
marci@660
  1492
    class ResCap {
marci@660
  1493
    protected:
marci@660
  1494
      const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
marci@660
  1495
    public:
marci@660
  1496
      typedef Number ValueType;
marci@660
  1497
      typedef Edge KeyType;
marci@660
  1498
      ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _res_graph) : 
marci@660
  1499
	res_graph(&_res_graph) { }
marci@660
  1500
      Number operator[](const Edge& e) const { 
marci@660
  1501
	if (res_graph->forward(e)) 
marci@660
  1502
	  //	return (capacity->get(e.out)-flow->get(e.out)); 
marci@660
  1503
	  return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e]; 
marci@660
  1504
	else 
marci@660
  1505
	  //	return (flow->get(e.in)); 
marci@660
  1506
	  return (*(res_graph->flow))[e]; 
marci@660
  1507
      }
marci@660
  1508
      /// \bug not needed with dynamic maps, or does it?
marci@660
  1509
      void update() { }
marci@660
  1510
    };
marci@660
  1511
marci@650
  1512
  };
marci@650
  1513
marci@650
  1514
marci@556
  1515
  template<typename Graph, typename Number, 
marci@556
  1516
	   typename CapacityMap, typename FlowMap>
marci@653
  1517
  class OldResGraphWrapper : public GraphWrapper<Graph> {
marci@650
  1518
  public:
marci@650
  1519
    typedef GraphWrapper<Graph> Parent; 
marci@556
  1520
  protected:
marci@556
  1521
    const CapacityMap* capacity;
marci@556
  1522
    FlowMap* flow;
marci@556
  1523
marci@653
  1524
    OldResGraphWrapper() : GraphWrapper<Graph>(0), 
marci@556
  1525
			capacity(0), flow(0) { }
marci@560
  1526
    void setCapacityMap(const CapacityMap& _capacity) {
marci@560
  1527
      capacity=&_capacity;
marci@556
  1528
    }
marci@556
  1529
    void setFlowMap(FlowMap& _flow) {
marci@556
  1530
      flow=&_flow;
marci@556
  1531
    }
marci@556
  1532
marci@556
  1533
  public:
marci@556
  1534
marci@653
  1535
    OldResGraphWrapper(Graph& _graph, const CapacityMap& _capacity, 
marci@556
  1536
		    FlowMap& _flow) : 
marci@556
  1537
      GraphWrapper<Graph>(_graph), capacity(&_capacity), flow(&_flow) { }
marci@556
  1538
marci@556
  1539
    class Edge; 
marci@556
  1540
    class OutEdgeIt; 
marci@556
  1541
    friend class Edge; 
marci@556
  1542
    friend class OutEdgeIt; 
marci@556
  1543
marci@556
  1544
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
  1545
    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@556
  1546
    class Edge : public Graph::Edge {
marci@653
  1547
      friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
marci@556
  1548
    protected:
marci@565
  1549
      bool backward; //true, iff backward
marci@556
  1550
//      typename Graph::Edge e;
marci@556
  1551
    public:
marci@556
  1552
      Edge() { }
marci@565
  1553
      Edge(const typename Graph::Edge& _e, bool _backward) : 
marci@565
  1554
	Graph::Edge(_e), backward(_backward) { }
marci@565
  1555
      Edge(const Invalid& i) : Graph::Edge(i), backward(true) { }
marci@556
  1556
//the unique invalid iterator
marci@556
  1557
      friend bool operator==(const Edge& u, const Edge& v) { 
marci@565
  1558
	return (v.backward==u.backward && 
marci@556
  1559
		static_cast<typename Graph::Edge>(u)==
marci@556
  1560
		static_cast<typename Graph::Edge>(v));
marci@556
  1561
      } 
marci@556
  1562
      friend bool operator!=(const Edge& u, const Edge& v) { 
marci@565
  1563
	return (v.backward!=u.backward || 
marci@556
  1564
		static_cast<typename Graph::Edge>(u)!=
marci@556
  1565
		static_cast<typename Graph::Edge>(v));
marci@556
  1566
      } 
marci@556
  1567
    };
marci@556
  1568
marci@556
  1569
    class OutEdgeIt {
marci@653
  1570
      friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
marci@556
  1571
    protected:
marci@556
  1572
      typename Graph::OutEdgeIt out;
marci@556
  1573
      typename Graph::InEdgeIt in;
marci@565
  1574
      bool backward;
marci@556
  1575
    public:
marci@556
  1576
      OutEdgeIt() { }
marci@556
  1577
      //FIXME
marci@556
  1578
//      OutEdgeIt(const Edge& e) : Edge(e) { }
marci@565
  1579
      OutEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
marci@556
  1580
//the unique invalid iterator
marci@653
  1581
      OutEdgeIt(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, Node v) { 
marci@565
  1582
	backward=false;
marci@569
  1583
	_G.graph->first(out, v);
marci@569
  1584
	while( _G.graph->valid(out) && !(_G.resCap(*this)>0) ) { _G.graph->next(out); }
marci@569
  1585
	if (!_G.graph->valid(out)) {
marci@565
  1586
	  backward=true;
marci@569
  1587
	  _G.graph->first(in, v);
marci@569
  1588
	  while( _G.graph->valid(in) && !(_G.resCap(*this)>0) ) { _G.graph->next(in); }
marci@556
  1589
	}
marci@556
  1590
      }
marci@556
  1591
      operator Edge() const { 
marci@556
  1592
//	Edge e;
marci@556
  1593
//	e.forward=this->forward;
marci@556
  1594
//	if (this->forward) e=out; else e=in;
marci@556
  1595
//	return e;
marci@565
  1596
	if (this->backward) 
marci@565
  1597
	  return Edge(in, this->backward); 
marci@556
  1598
	else 
marci@565
  1599
	  return Edge(out, this->backward);
marci@556
  1600
      }
marci@556
  1601
    };
marci@556
  1602
marci@556
  1603
    class InEdgeIt {
marci@653
  1604
      friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
marci@556
  1605
    protected:
marci@556
  1606
      typename Graph::OutEdgeIt out;
marci@556
  1607
      typename Graph::InEdgeIt in;
marci@565
  1608
      bool backward;
marci@556
  1609
    public:
marci@556
  1610
      InEdgeIt() { }
marci@556
  1611
      //FIXME
marci@556
  1612
//      OutEdgeIt(const Edge& e) : Edge(e) { }
marci@565
  1613
      InEdgeIt(const Invalid& i) : out(i), in(i), backward(true) { }
marci@556
  1614
//the unique invalid iterator
marci@653
  1615
      InEdgeIt(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, Node v) { 
marci@565
  1616
	backward=false;
marci@569
  1617
	_G.graph->first(in, v);
marci@569
  1618
	while( _G.graph->valid(in) && !(_G.resCap(*this)>0) ) { _G.graph->next(in); }
marci@569
  1619
	if (!_G.graph->valid(in)) {
marci@565
  1620
	  backward=true;
marci@569
  1621
	  _G.graph->first(out, v);
marci@569
  1622
	  while( _G.graph->valid(out) && !(_G.resCap(*this)>0) ) { _G.graph->next(out); }
marci@556
  1623
	}
marci@556
  1624
      }
marci@556
  1625
      operator Edge() const { 
marci@556
  1626
//	Edge e;
marci@556
  1627
//	e.forward=this->forward;
marci@556
  1628
//	if (this->forward) e=out; else e=in;
marci@556
  1629
//	return e;
marci@565
  1630
	if (this->backward) 
marci@565
  1631
	  return Edge(out, this->backward); 
marci@556
  1632
	else 
marci@565
  1633
	  return Edge(in, this->backward);
marci@556
  1634
      }
marci@556
  1635
    };
marci@556
  1636
marci@556
  1637
    class EdgeIt {
marci@653
  1638
      friend class OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>;
marci@556
  1639
    protected:
marci@556
  1640
      typename Graph::EdgeIt e;
marci@565
  1641
      bool backward;
marci@556
  1642
    public:
marci@556
  1643
      EdgeIt() { }
marci@565
  1644
      EdgeIt(const Invalid& i) : e(i), backward(true) { }
marci@653
  1645
      EdgeIt(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G) { 
marci@565
  1646
	backward=false;
marci@569
  1647
	_G.graph->first(e);
marci@569
  1648
	while (_G.graph->valid(e) && !(_G.resCap(*this)>0)) _G.graph->next(e);
marci@569
  1649
	if (!_G.graph->valid(e)) {
marci@565
  1650
	  backward=true;
marci@569
  1651
	  _G.graph->first(e);
marci@569
  1652
	  while (_G.graph->valid(e) && !(_G.resCap(*this)>0)) _G.graph->next(e);
marci@556
  1653
	}
marci@556
  1654
      }
marci@556
  1655
      operator Edge() const { 
marci@565
  1656
	return Edge(e, this->backward);
marci@556
  1657
      }
marci@556
  1658
    };
marci@556
  1659
marci@556
  1660
    using GraphWrapper<Graph>::first;
marci@556
  1661
//     NodeIt& first(NodeIt& i) const { 
marci@556
  1662
//       i=NodeIt(*this); return i;
marci@556
  1663
//     }
marci@556
  1664
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
  1665
      i=OutEdgeIt(*this, p); return i;
marci@556
  1666
    }
marci@556
  1667
//    FIXME not tested
marci@556
  1668
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
  1669
      i=InEdgeIt(*this, p); return i;
marci@556
  1670
    }
marci@556
  1671
    EdgeIt& first(EdgeIt& i) const { 
marci@556
  1672
      i=EdgeIt(*this); return i;
marci@556
  1673
    }
marci@556
  1674
  
marci@556
  1675
    using GraphWrapper<Graph>::next;
marci@556
  1676
//    NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
marci@556
  1677
    OutEdgeIt& next(OutEdgeIt& e) const { 
marci@565
  1678
      if (!e.backward) {
marci@556
  1679
	Node v=this->graph->aNode(e.out);
marci@556
  1680
	this->graph->next(e.out);
marci@556
  1681
	while( this->graph->valid(e.out) && !(resCap(e)>0) ) { 
marci@556
  1682
	  this->graph->next(e.out); }
marci@556
  1683
	if (!this->graph->valid(e.out)) {
marci@565
  1684
	  e.backward=true;
marci@556
  1685
	  this->graph->first(e.in, v); 
marci@556
  1686
	  while( this->graph->valid(e.in) && !(resCap(e)>0) ) { 
marci@556
  1687
	    this->graph->next(e.in); }
marci@556
  1688
	}
marci@556
  1689
      } else {
marci@556
  1690
	this->graph->next(e.in);
marci@556
  1691
	while( this->graph->valid(e.in) && !(resCap(e)>0) ) { 
marci@556
  1692
	  this->graph->next(e.in); } 
marci@556
  1693
      }
marci@556
  1694
      return e;
marci@556
  1695
    }
marci@556
  1696
//     FIXME Not tested
marci@556
  1697
    InEdgeIt& next(InEdgeIt& e) const { 
marci@565
  1698
      if (!e.backward) {
marci@556
  1699
	Node v=this->graph->aNode(e.in);
marci@556
  1700
	this->graph->next(e.in);
marci@556
  1701
	while( this->graph->valid(e.in) && !(resCap(e)>0) ) { 
marci@556
  1702
	  this->graph->next(e.in); }
marci@556
  1703
	if (!this->graph->valid(e.in)) {
marci@565
  1704
	  e.backward=true;
marci@556
  1705
	  this->graph->first(e.out, v); 
marci@556
  1706
	  while( this->graph->valid(e.out) && !(resCap(e)>0) ) { 
marci@556
  1707
	    this->graph->next(e.out); }
marci@556
  1708
	}
marci@556
  1709
      } else {
marci@556
  1710
	this->graph->next(e.out);
marci@556
  1711
	while( this->graph->valid(e.out) && !(resCap(e)>0) ) { 
marci@556
  1712
	  this->graph->next(e.out); } 
marci@556
  1713
      }
marci@556
  1714
      return e;
marci@556
  1715
    }
marci@556
  1716
    EdgeIt& next(EdgeIt& e) const {
marci@565
  1717
      if (!e.backward) {
marci@556
  1718
	this->graph->next(e.e);
marci@556
  1719
	while( this->graph->valid(e.e) && !(resCap(e)>0) ) { 
marci@556
  1720
	  this->graph->next(e.e); }
marci@556
  1721
	if (!this->graph->valid(e.e)) {
marci@565
  1722
	  e.backward=true;
marci@556
  1723
	  this->graph->first(e.e); 
marci@556
  1724
	  while( this->graph->valid(e.e) && !(resCap(e)>0) ) { 
marci@556
  1725
	    this->graph->next(e.e); }
marci@556
  1726
	}
marci@556
  1727
      } else {
marci@556
  1728
	this->graph->next(e.e);
marci@556
  1729
	while( this->graph->valid(e.e) && !(resCap(e)>0) ) { 
marci@556
  1730
	  this->graph->next(e.e); } 
marci@556
  1731
      }
marci@556
  1732
      return e;
marci@556
  1733
    }
marci@556
  1734
marci@556
  1735
    Node tail(Edge e) const { 
marci@565
  1736
      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
marci@556
  1737
    Node head(Edge e) const { 
marci@565
  1738
      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
marci@556
  1739
marci@556
  1740
    Node aNode(OutEdgeIt e) const { 
marci@565
  1741
      return ((!e.backward) ? this->graph->aNode(e.out) : 
marci@556
  1742
	      this->graph->aNode(e.in)); }
marci@556
  1743
    Node bNode(OutEdgeIt e) const { 
marci@565
  1744
      return ((!e.backward) ? this->graph->bNode(e.out) : 
marci@556
  1745
	      this->graph->bNode(e.in)); }
marci@556
  1746
marci@556
  1747
    Node aNode(InEdgeIt e) const { 
marci@565
  1748
      return ((!e.backward) ? this->graph->aNode(e.in) : 
marci@556
  1749
	      this->graph->aNode(e.out)); }
marci@556
  1750
    Node bNode(InEdgeIt e) const { 
marci@565
  1751
      return ((!e.backward) ? this->graph->bNode(e.in) : 
marci@556
  1752
	      this->graph->bNode(e.out)); }
marci@556
  1753
marci@556
  1754
//    int nodeNum() const { return graph->nodeNum(); }
marci@556
  1755
    //FIXME
marci@556
  1756
    void edgeNum() const { }
marci@556
  1757
    //int edgeNum() const { return graph->edgeNum(); }
marci@556
  1758
marci@556
  1759
marci@556
  1760
//    int id(Node v) const { return graph->id(v); }
marci@556
  1761
marci@556
  1762
    bool valid(Node n) const { return GraphWrapper<Graph>::valid(n); }
marci@556
  1763
    bool valid(Edge e) const { 
marci@556
  1764
      return this->graph->valid(e);
marci@556
  1765
	//return e.forward ? graph->valid(e.out) : graph->valid(e.in); 
marci@556
  1766
    }
marci@556
  1767
marci@565
  1768
    bool forward(const Edge& e) const { return !e.backward; }
marci@565
  1769
    bool backward(const Edge& e) const { return e.backward; }
marci@556
  1770
marci@556
  1771
    void augment(const Edge& e, Number a) const {
marci@565
  1772
      if (!e.backward)  
marci@556
  1773
// 	flow->set(e.out, flow->get(e.out)+a);
marci@556
  1774
	flow->set(e, (*flow)[e]+a);
marci@556
  1775
      else  
marci@556
  1776
// 	flow->set(e.in, flow->get(e.in)-a);
marci@556
  1777
	flow->set(e, (*flow)[e]-a);
marci@556
  1778
    }
marci@556
  1779
marci@556
  1780
    Number resCap(const Edge& e) const { 
marci@565
  1781
      if (!e.backward) 
marci@556
  1782
//	return (capacity->get(e.out)-flow->get(e.out)); 
marci@556
  1783
	return ((*capacity)[e]-(*flow)[e]); 
marci@556
  1784
      else 
marci@556
  1785
//	return (flow->get(e.in)); 
marci@556
  1786
	return ((*flow)[e]); 
marci@556
  1787
    }
marci@556
  1788
marci@556
  1789
//     Number resCap(typename Graph::OutEdgeIt out) const { 
marci@556
  1790
// //      return (capacity->get(out)-flow->get(out)); 
marci@556
  1791
//       return ((*capacity)[out]-(*flow)[out]); 
marci@556
  1792
//     }
marci@556
  1793
    
marci@556
  1794
//     Number resCap(typename Graph::InEdgeIt in) const { 
marci@556
  1795
// //      return (flow->get(in)); 
marci@556
  1796
//       return ((*flow)[in]); 
marci@556
  1797
//     }
marci@556
  1798
marci@556
  1799
    template <typename T>
marci@556
  1800
    class EdgeMap {
marci@556
  1801
      typename Graph::template EdgeMap<T> forward_map, backward_map; 
marci@556
  1802
    public:
marci@624
  1803
      typedef T ValueType;
marci@624
  1804
      typedef Edge KeyType;
marci@653
  1805
      EdgeMap(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G) : forward_map(*(_G.graph)), backward_map(*(_G.graph)) { }
marci@653
  1806
      EdgeMap(const OldResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _G, T a) : forward_map(*(_G.graph), a), backward_map(*(_G.graph), a) { }
marci@556
  1807
      void set(Edge e, T a) { 
marci@565
  1808
	if (!e.backward) 
marci@624
  1809
	  forward_map.set(e/*.out*/, a); 
marci@556
  1810
	else 
marci@624
  1811
	  backward_map.set(e/*.in*/, a); 
marci@556
  1812
      }
marci@556
  1813
      T operator[](Edge e) const { 
marci@565
  1814
	if (!e.backward) 
marci@624
  1815
	  return forward_map[e/*.out*/]; 
marci@556
  1816
	else 
marci@624
  1817
	  return backward_map[e/*.in*/]; 
marci@556
  1818
      }
marci@625
  1819
      void update() { 
marci@625
  1820
	forward_map.update(); 
marci@625
  1821
	backward_map.update();
marci@625
  1822
      }
marci@556
  1823
//       T get(Edge e) const { 
marci@556
  1824
// 	if (e.out_or_in) 
marci@556
  1825
// 	  return forward_map.get(e.out); 
marci@556
  1826
// 	else 
marci@556
  1827
// 	  return backward_map.get(e.in); 
marci@556
  1828
//       }
marci@556
  1829
    };
marci@556
  1830
  };
marci@556
  1831
marci@569
  1832
marci@569
  1833
marci@612
  1834
  /// For blocking flows.
marci@556
  1835
marci@612
  1836
  /// This graph wrapper is used for Dinits blocking flow computations.
marci@612
  1837
  /// For each node, an out-edge is stored which is used when the 
marci@612
  1838
  /// \code 
marci@612
  1839
  /// OutEdgeIt& first(OutEdgeIt&, const Node&)
marci@612
  1840
  /// \endcode
marci@612
  1841
  /// is called. 
marci@556
  1842
  ///
marci@556
  1843
  ///\author Marton Makai
marci@556
  1844
  template<typename Graph, typename FirstOutEdgesMap>
marci@556
  1845
  class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
marci@650
  1846
  public:
marci@650
  1847
    typedef GraphWrapper<Graph> Parent; 
marci@556
  1848
  protected:
marci@556
  1849
    FirstOutEdgesMap* first_out_edges;
marci@556
  1850
  public:
marci@556
  1851
    ErasingFirstGraphWrapper(Graph& _graph, 
marci@556
  1852
			     FirstOutEdgesMap& _first_out_edges) : 
marci@556
  1853
      GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }  
marci@556
  1854
marci@556
  1855
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
  1856
//     class NodeIt { 
marci@556
  1857
//       friend class GraphWrapper<Graph>;
marci@556
  1858
//       friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@556
  1859
//       typename Graph::NodeIt n;
marci@556
  1860
//      public:
marci@556
  1861
//       NodeIt() { }
marci@556
  1862
//       NodeIt(const typename Graph::NodeIt& _n) : n(_n) { }
marci@556
  1863
//       NodeIt(const Invalid& i) : n(i) { }
marci@556
  1864
//       NodeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) : 
marci@556
  1865
// 	n(*(_G.graph)) { }
marci@556
  1866
//       operator Node() const { return Node(typename Graph::Node(n)); }
marci@556
  1867
//     };
marci@556
  1868
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@556
  1869
    class OutEdgeIt { 
marci@556
  1870
      friend class GraphWrapper<Graph>;
marci@556
  1871
      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@556
  1872
//      typedef typename Graph::OutEdgeIt GraphOutEdgeIt;
marci@556
  1873
      typename Graph::OutEdgeIt e;
marci@556
  1874
    public:
marci@556
  1875
      OutEdgeIt() { }
marci@556
  1876
      OutEdgeIt(const typename Graph::OutEdgeIt& _e) : e(_e) { }
marci@556
  1877
      OutEdgeIt(const Invalid& i) : e(i) { }
marci@556
  1878
      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G, 
marci@556
  1879
		const Node& _n) : 
marci@556
  1880
	e((*_G.first_out_edges)[_n]) { }
marci@556
  1881
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
  1882
    };
marci@556
  1883
    class InEdgeIt { 
marci@556
  1884
      friend class GraphWrapper<Graph>;
marci@556
  1885
      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@556
  1886
//      typedef typename Graph::InEdgeIt GraphInEdgeIt;
marci@556
  1887
      typename Graph::InEdgeIt e;
marci@556
  1888
    public:
marci@556
  1889
      InEdgeIt() { }
marci@556
  1890
      InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
marci@556
  1891
      InEdgeIt(const Invalid& i) : e(i) { }
marci@556
  1892
      InEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G, 
marci@556
  1893
	       const Node& _n) : 
marci@556
  1894
	e(*(_G.graph), typename Graph::Node(_n)) { }
marci@556
  1895
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
  1896
    };
marci@556
  1897
    //typedef typename Graph::SymEdgeIt SymEdgeIt;
marci@556
  1898
    class EdgeIt { 
marci@556
  1899
      friend class GraphWrapper<Graph>;
marci@556
  1900
      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@556
  1901
//      typedef typename Graph::EdgeIt GraphEdgeIt;
marci@556
  1902
      typename Graph::EdgeIt e;
marci@556
  1903
    public:
marci@556
  1904
      EdgeIt() { }
marci@556
  1905
      EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
marci@556
  1906
      EdgeIt(const Invalid& i) : e(i) { }
marci@556
  1907
      EdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) : 
marci@556
  1908
	e(*(_G.graph)) { }
marci@556
  1909
      operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@556
  1910
    };
marci@556
  1911
marci@556
  1912
    using GraphWrapper<Graph>::first;
marci@556
  1913
//     NodeIt& first(NodeIt& i) const { 
marci@556
  1914
//       i=NodeIt(*this); return i;
marci@556
  1915
//     }
marci@556
  1916
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
  1917
      i=OutEdgeIt(*this, p); return i;
marci@556
  1918
    }
marci@556
  1919
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
  1920
      i=InEdgeIt(*this, p); return i;
marci@556
  1921
    }
marci@556
  1922
    EdgeIt& first(EdgeIt& i) const { 
marci@556
  1923
      i=EdgeIt(*this); return i;
marci@556
  1924
    }
marci@556
  1925
marci@556
  1926
    using GraphWrapper<Graph>::next;
marci@556
  1927
//    NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
marci@556
  1928
    OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
marci@556
  1929
    InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
marci@556
  1930
    EdgeIt& next(EdgeIt& i) const { this->graph->next(i.e); return i; }    
marci@556
  1931
    
marci@556
  1932
    Node aNode(const OutEdgeIt& e) const { 
marci@556
  1933
      return Node(this->graph->aNode(e.e)); }
marci@556
  1934
    Node aNode(const InEdgeIt& e) const { 
marci@556
  1935
      return Node(this->graph->aNode(e.e)); }
marci@556
  1936
    Node bNode(const OutEdgeIt& e) const { 
marci@556
  1937
      return Node(this->graph->bNode(e.e)); }
marci@556
  1938
    Node bNode(const InEdgeIt& e) const { 
marci@556
  1939
      return Node(this->graph->bNode(e.e)); }
marci@556
  1940
marci@556
  1941
    void erase(const OutEdgeIt& e) const {
marci@556
  1942
      OutEdgeIt f=e;
marci@556
  1943
      this->next(f);
marci@556
  1944
      first_out_edges->set(this->tail(e), f.e);
marci@556
  1945
    }
marci@556
  1946
  };
marci@556
  1947
marci@556
  1948
  ///@}
marci@556
  1949
marci@556
  1950
} //namespace hugo
marci@556
  1951
marci@556
  1952
#endif //HUGO_GRAPH_WRAPPER_H
marci@556
  1953