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