src/hugo/graph_wrapper.h
author marci
Tue, 14 Sep 2004 10:09:24 +0000
changeset 849 cc3867a7d380
parent 844 9bf990cb066d
child 854 baf0b6e40211
permissions -rw-r--r--
(none)
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>
alpar@774
    15
#include <iostream>
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) { }
alpar@774
    99
    GraphWrapper(const GraphWrapper<Graph>& gw) : graph(gw.graph) { }
marci@556
   100
//     Graph& getGraph() const { return *graph; }
marci@556
   101
 
alpar@774
   102
    typedef typename Graph::Node Node;
alpar@774
   103
    class NodeIt : public Node { 
alpar@774
   104
      const GraphWrapper<Graph>* gw;
marci@556
   105
      friend class GraphWrapper<Graph>;
marci@556
   106
     public:
marci@556
   107
      NodeIt() { }
alpar@774
   108
      //      NodeIt(const NodeIt& n) : Node(n), gw(n.gw) { }
alpar@774
   109
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   110
      NodeIt(const GraphWrapper<Graph>& _gw) : 
alpar@774
   111
	Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { }
alpar@774
   112
      NodeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   113
	Node(n), gw(&_gw) { }
alpar@774
   114
      NodeIt& operator++() { 
alpar@774
   115
	*(static_cast<Node*>(this))=
alpar@774
   116
	  ++(typename Graph::NodeIt(*(gw->graph), *this));
alpar@774
   117
	return *this; 
alpar@774
   118
      }
marci@556
   119
    };
alpar@774
   120
    typedef typename Graph::Edge Edge;
alpar@774
   121
    class OutEdgeIt : public Edge { 
alpar@774
   122
      const GraphWrapper<Graph>* gw;
marci@556
   123
      friend class GraphWrapper<Graph>;
alpar@774
   124
     public:
alpar@774
   125
      OutEdgeIt() { }
alpar@774
   126
      //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
alpar@774
   127
      OutEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   128
      OutEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   129
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   130
      OutEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   131
	Edge(e), gw(&_gw) { }
alpar@774
   132
      OutEdgeIt& operator++() { 
alpar@774
   133
	*(static_cast<Edge*>(this))=
alpar@774
   134
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   135
	return *this; 
alpar@774
   136
      }
marci@556
   137
    };
alpar@774
   138
    class InEdgeIt : public Edge { 
alpar@774
   139
      const GraphWrapper<Graph>* gw;
marci@556
   140
      friend class GraphWrapper<Graph>;
alpar@774
   141
     public:
marci@556
   142
      InEdgeIt() { }
alpar@774
   143
      //InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
alpar@774
   144
      InEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   145
      InEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   146
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   147
      InEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   148
	Edge(e), gw(&_gw) { }
alpar@774
   149
      InEdgeIt& operator++() { 
alpar@774
   150
	*(static_cast<Edge*>(this))=
alpar@774
   151
	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   152
	return *this; 
alpar@774
   153
      }
marci@556
   154
    };
marci@556
   155
    //typedef typename Graph::SymEdgeIt SymEdgeIt;
alpar@774
   156
    class EdgeIt : public Edge { 
alpar@774
   157
      const GraphWrapper<Graph>* gw;
marci@556
   158
      friend class GraphWrapper<Graph>;
alpar@774
   159
     public:
marci@556
   160
      EdgeIt() { }
alpar@774
   161
      //EdgeIt(const EdgeIt& e) : Edge(e), gw(e.gw) { }
alpar@774
   162
      EdgeIt(Invalid i) : Edge(i) { }
alpar@774
   163
      EdgeIt(const GraphWrapper<Graph>& _gw) : 
alpar@774
   164
	Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { }
alpar@774
   165
      EdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
marci@777
   166
	Edge(e), gw(&_gw) { }
alpar@774
   167
      EdgeIt& operator++() { 
alpar@774
   168
	*(static_cast<Edge*>(this))=
alpar@774
   169
	  ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
   170
	return *this; 
alpar@774
   171
      }
marci@556
   172
    };
marci@556
   173
   
marci@556
   174
    NodeIt& first(NodeIt& i) const { 
marci@556
   175
      i=NodeIt(*this); return i;
marci@556
   176
    }
marci@556
   177
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   178
      i=OutEdgeIt(*this, p); return i;
marci@556
   179
    }
marci@556
   180
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   181
      i=InEdgeIt(*this, p); return i;
marci@556
   182
    }
marci@556
   183
    EdgeIt& first(EdgeIt& i) const { 
marci@556
   184
      i=EdgeIt(*this); return i;
marci@556
   185
    }
marci@556
   186
alpar@774
   187
//     NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
alpar@774
   188
//     OutEdgeIt& next(OutEdgeIt& i) const { graph->next(i.e); return i; }
alpar@774
   189
//     InEdgeIt& next(InEdgeIt& i) const { graph->next(i.e); return i; }
alpar@774
   190
//     EdgeIt& next(EdgeIt& i) const { graph->next(i.e); return i; }    
marci@556
   191
marci@556
   192
    Node tail(const Edge& e) const { 
marci@556
   193
      return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
marci@556
   194
    Node head(const Edge& e) const { 
marci@556
   195
      return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
marci@556
   196
alpar@774
   197
//     bool valid(const Node& n) const { 
alpar@774
   198
//       return graph->valid(static_cast<typename Graph::Node>(n)); }
alpar@774
   199
//     bool valid(const Edge& e) const { 
alpar@774
   200
//       return graph->valid(static_cast<typename Graph::Edge>(e)); }
marci@556
   201
marci@556
   202
    int nodeNum() const { return graph->nodeNum(); }
marci@556
   203
    int edgeNum() const { return graph->edgeNum(); }
marci@556
   204
  
alpar@774
   205
//     Node aNode(const OutEdgeIt& e) const { return Node(graph->aNode(e.e)); }
alpar@774
   206
//     Node aNode(const InEdgeIt& e) const { return Node(graph->aNode(e.e)); }
alpar@774
   207
//     Node bNode(const OutEdgeIt& e) const { return Node(graph->bNode(e.e)); }
alpar@774
   208
//     Node bNode(const InEdgeIt& e) const { return Node(graph->bNode(e.e)); }
marci@556
   209
  
marci@556
   210
    Node addNode() const { return Node(graph->addNode()); }
marci@556
   211
    Edge addEdge(const Node& tail, const Node& head) const { 
marci@556
   212
      return Edge(graph->addEdge(tail, head)); }
marci@556
   213
marci@556
   214
    void erase(const Node& i) const { graph->erase(i); }
marci@556
   215
    void erase(const Edge& i) const { graph->erase(i); }
marci@556
   216
  
marci@556
   217
    void clear() const { graph->clear(); }
marci@556
   218
    
alpar@736
   219
    bool forward(const Edge& e) const { return graph->forward(e); }
alpar@736
   220
    bool backward(const Edge& e) const { return graph->backward(e); }
marci@739
   221
marci@739
   222
    int id(const Node& v) const { return graph->id(v); }
marci@739
   223
    int id(const Edge& e) const { return graph->id(e); }
marci@650
   224
    
marci@738
   225
    Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); }
marci@650
   226
marci@556
   227
    template<typename T> class NodeMap : public Graph::template NodeMap<T> { 
marci@556
   228
      typedef typename Graph::template NodeMap<T> Parent;
marci@556
   229
    public:
alpar@774
   230
      NodeMap(const GraphWrapper<Graph>& gw) :  Parent(*(gw.graph)) { }
alpar@774
   231
      NodeMap(const GraphWrapper<Graph>& gw, T a) : Parent(*(gw.graph), a) { }
marci@849
   232
//       NodeMap(const NodeMap<T>& map) : Parent(map) { }
marci@849
   233
//       template<typename Map>
marci@849
   234
//       NodeMap(const Map& map) :  Parent(map) { }
marci@556
   235
    };
marci@556
   236
marci@556
   237
    template<typename T> class EdgeMap : public Graph::template EdgeMap<T> { 
marci@556
   238
      typedef typename Graph::template EdgeMap<T> Parent;
marci@556
   239
    public:
alpar@774
   240
      EdgeMap(const GraphWrapper<Graph>& gw) : Parent(*(gw.graph)) { }
alpar@774
   241
      EdgeMap(const GraphWrapper<Graph>& gw, T a) : Parent(*(gw.graph), a) { }
marci@849
   242
//       EdgeMap(const EdgeMap<T>& map) : Parent(map) { }
marci@849
   243
//       template<typename Map>
marci@849
   244
//       EdgeMap(const Map& map) :  Parent(map) { }
marci@556
   245
    };
marci@556
   246
  };
marci@556
   247
marci@569
   248
marci@569
   249
marci@556
   250
  /// A graph wrapper which reverses the orientation of the edges.
marci@556
   251
marci@556
   252
  /// A graph wrapper which reverses the orientation of the edges.
marci@612
   253
  /// Thus \c Graph have to be a directed graph type.
marci@556
   254
  ///
marci@556
   255
  ///\author Marton Makai
marci@556
   256
  template<typename Graph>
marci@556
   257
  class RevGraphWrapper : public GraphWrapper<Graph> {
marci@650
   258
  public:
marci@650
   259
    typedef GraphWrapper<Graph> Parent; 
marci@556
   260
  protected:
marci@612
   261
    RevGraphWrapper() : GraphWrapper<Graph>() { }
marci@556
   262
  public:
marci@556
   263
    RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }  
alpar@774
   264
    RevGraphWrapper(const RevGraphWrapper<Graph>& gw) : Parent(gw) { }
marci@556
   265
marci@556
   266
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
   267
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@792
   268
    //remark: OutEdgeIt and InEdgeIt cannot be typedef-ed to each other
marci@792
   269
    //because this does not work is some of them are not defined in the 
marci@792
   270
    //original graph. The problem with this is that typedef-ed stuff 
marci@792
   271
    //are instantiated in c++.
alpar@774
   272
    class OutEdgeIt : public Edge { 
alpar@774
   273
      const RevGraphWrapper<Graph>* gw;
marci@556
   274
      friend class GraphWrapper<Graph>;
alpar@774
   275
     public:
marci@556
   276
      OutEdgeIt() { }
alpar@774
   277
      //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
alpar@774
   278
      OutEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   279
      OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   280
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   281
      OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   282
	Edge(e), gw(&_gw) { }
alpar@774
   283
      OutEdgeIt& operator++() { 
alpar@774
   284
	*(static_cast<Edge*>(this))=
alpar@774
   285
	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   286
	return *this; 
alpar@774
   287
      }
marci@556
   288
    };
alpar@774
   289
    class InEdgeIt : public Edge { 
alpar@774
   290
      const RevGraphWrapper<Graph>* gw;
marci@556
   291
      friend class GraphWrapper<Graph>;
alpar@774
   292
     public:
marci@556
   293
      InEdgeIt() { }
alpar@774
   294
      //InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
alpar@774
   295
      InEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   296
      InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   297
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   298
      InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   299
	Edge(e), gw(&_gw) { }
alpar@774
   300
      InEdgeIt& operator++() { 
alpar@774
   301
	*(static_cast<Edge*>(this))=
alpar@774
   302
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   303
	return *this; 
alpar@774
   304
      }
marci@556
   305
    };
marci@556
   306
marci@556
   307
    using GraphWrapper<Graph>::first;
marci@556
   308
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   309
      i=OutEdgeIt(*this, p); return i;
marci@556
   310
    }
marci@556
   311
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   312
      i=InEdgeIt(*this, p); return i;
marci@556
   313
    }
marci@556
   314
alpar@774
   315
//     using GraphWrapper<Graph>::next;
alpar@774
   316
//     OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
alpar@774
   317
//     InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
marci@556
   318
alpar@774
   319
//     Node aNode(const OutEdgeIt& e) const { 
alpar@774
   320
//       return Node(this->graph->aNode(e.e)); }
alpar@774
   321
//     Node aNode(const InEdgeIt& e) const { 
alpar@774
   322
//       return Node(this->graph->aNode(e.e)); }
alpar@774
   323
//     Node bNode(const OutEdgeIt& e) const { 
alpar@774
   324
//       return Node(this->graph->bNode(e.e)); }
alpar@774
   325
//     Node bNode(const InEdgeIt& e) const { 
alpar@774
   326
//       return Node(this->graph->bNode(e.e)); }
marci@556
   327
marci@556
   328
    Node tail(const Edge& e) const { 
marci@556
   329
      return GraphWrapper<Graph>::head(e); }
marci@556
   330
    Node head(const Edge& e) const { 
marci@556
   331
      return GraphWrapper<Graph>::tail(e); }
marci@556
   332
marci@556
   333
  };
marci@556
   334
marci@775
   335
marci@775
   336
marci@612
   337
  /// A graph wrapper for hiding nodes and edges from a graph.
marci@556
   338
  
marci@556
   339
  /// This wrapper shows a graph with filtered node-set and 
marci@838
   340
  /// edge-set. Given a bool-valued map on the node-set and one on 
marci@838
   341
  /// the edge-set of the graphs, the iterators shows only the objects 
marci@838
   342
  /// having true value. 
marci@838
   343
  /// The quick brown fox iterators jump over 
marci@838
   344
  /// the lazy dog nodes or edges if their values for are false in the 
marci@838
   345
  /// corresponding bool maps. 
marci@556
   346
  ///
marci@556
   347
  ///\author Marton Makai
marci@556
   348
  template<typename Graph, typename NodeFilterMap, 
marci@556
   349
	   typename EdgeFilterMap>
marci@556
   350
  class SubGraphWrapper : public GraphWrapper<Graph> {
marci@650
   351
  public:
marci@650
   352
    typedef GraphWrapper<Graph> Parent;
marci@556
   353
  protected:
marci@556
   354
    NodeFilterMap* node_filter_map;
marci@556
   355
    EdgeFilterMap* edge_filter_map;
marci@556
   356
marci@612
   357
    SubGraphWrapper() : GraphWrapper<Graph>(), 
marci@556
   358
			node_filter_map(0), edge_filter_map(0) { }
marci@556
   359
    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
marci@556
   360
      node_filter_map=&_node_filter_map;
marci@556
   361
    }
marci@556
   362
    void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
marci@556
   363
      edge_filter_map=&_edge_filter_map;
marci@556
   364
    }
marci@556
   365
    
marci@556
   366
  public:
marci@556
   367
    SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map, 
marci@556
   368
		    EdgeFilterMap& _edge_filter_map) : 
marci@556
   369
      GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map), 
marci@556
   370
      edge_filter_map(&_edge_filter_map) { }  
marci@556
   371
marci@556
   372
    typedef typename GraphWrapper<Graph>::Node Node;
marci@775
   373
    class NodeIt : public Node { 
marci@775
   374
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   375
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@775
   376
    public:
marci@556
   377
      NodeIt() { }
marci@775
   378
      //      NodeIt(const NodeIt& n) : Node(n), gw(n.gw) { }
marci@775
   379
      NodeIt(Invalid i) : Node(i) { }
marci@775
   380
      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) : 
marci@775
   381
	Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { }
marci@775
   382
      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   383
	     const Node& n) : 
marci@775
   384
	Node(n), gw(&_gw) { }
marci@775
   385
      NodeIt& operator++() { 
marci@775
   386
	*(static_cast<Node*>(this))=
marci@775
   387
	  ++(typename Graph::NodeIt(*(gw->graph), *this));
marci@775
   388
	while (*static_cast<Node*>(this)!=INVALID && 
marci@775
   389
	       !(*(gw->node_filter_map))[*this]) 
marci@775
   390
	  *(static_cast<Node*>(this))=
marci@775
   391
	    ++(typename Graph::NodeIt(*(gw->graph), *this));
marci@775
   392
	return *this; 
marci@556
   393
      }
marci@556
   394
    };
marci@556
   395
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@775
   396
    class OutEdgeIt : public Edge { 
marci@775
   397
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   398
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   399
    public:
marci@556
   400
      OutEdgeIt() { }
marci@775
   401
      //      OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
marci@775
   402
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@775
   403
      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) : 
marci@777
   404
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
marci@775
   405
      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   406
	     const Edge& e) : 
marci@775
   407
	Edge(e), gw(&_gw) { }
marci@775
   408
      OutEdgeIt& operator++() { 
marci@775
   409
	*(static_cast<Edge*>(this))=
marci@775
   410
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   411
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   412
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   413
	  *(static_cast<Edge*>(this))=
marci@775
   414
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   415
	return *this; 
marci@556
   416
      }
marci@556
   417
    };
marci@775
   418
    class InEdgeIt : public Edge { 
marci@775
   419
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   420
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   421
    public:
marci@556
   422
      InEdgeIt() { }
marci@775
   423
      //      InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
marci@775
   424
      InEdgeIt(Invalid i) : Edge(i) { }
marci@775
   425
      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) : 
marci@777
   426
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
marci@775
   427
      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   428
	     const Edge& e) : 
marci@775
   429
	Edge(e), gw(&_gw) { }
marci@775
   430
      InEdgeIt& operator++() { 
marci@775
   431
	*(static_cast<Edge*>(this))=
marci@775
   432
	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   433
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   434
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   435
	  *(static_cast<Edge*>(this))=
marci@775
   436
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   437
	return *this; 
marci@556
   438
      }
marci@556
   439
    };
marci@775
   440
    class EdgeIt : public Edge { 
marci@775
   441
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   442
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   443
    public:
marci@556
   444
      EdgeIt() { }
marci@775
   445
      //      EdgeIt(const EdgeIt& e) : Edge(e), gw(e.gw) { }
marci@775
   446
      EdgeIt(Invalid i) : Edge(i) { }
marci@775
   447
      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) : 
marci@775
   448
	Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { }
marci@775
   449
      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   450
	     const Edge& e) : 
marci@775
   451
	Edge(e), gw(&_gw) { }
marci@775
   452
      EdgeIt& operator++() { 
marci@775
   453
	*(static_cast<Edge*>(this))=
marci@775
   454
	  ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   455
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   456
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   457
	  *(static_cast<Edge*>(this))=
marci@775
   458
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   459
	return *this; 
marci@556
   460
      }
marci@556
   461
    };
marci@556
   462
marci@556
   463
    NodeIt& first(NodeIt& i) const { 
marci@556
   464
      i=NodeIt(*this); return i;
marci@556
   465
    }
marci@556
   466
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   467
      i=OutEdgeIt(*this, p); return i;
marci@556
   468
    }
marci@556
   469
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   470
      i=InEdgeIt(*this, p); return i;
marci@556
   471
    }
marci@556
   472
    EdgeIt& first(EdgeIt& i) const { 
marci@556
   473
      i=EdgeIt(*this); return i;
marci@556
   474
    }
marci@556
   475
    
marci@775
   476
//     NodeIt& next(NodeIt& i) const {
marci@775
   477
//       this->graph->next(i.n); 
marci@775
   478
//       while (this->graph->valid(i) && !(*node_filter_map)[i.n]) { 
marci@775
   479
// 	this->graph->next(i.n); }
marci@775
   480
//       return i;
marci@775
   481
//     }
marci@775
   482
//     OutEdgeIt& next(OutEdgeIt& i) const {
marci@775
   483
//       this->graph->next(i.e); 
marci@775
   484
//       while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) { 
marci@775
   485
// 	this->graph->next(i.e); }
marci@775
   486
//       return i;
marci@775
   487
//     }
marci@775
   488
//     InEdgeIt& next(InEdgeIt& i) const {
marci@775
   489
//       this->graph->next(i.e); 
marci@775
   490
//       while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) { 
marci@775
   491
// 	this->graph->next(i.e); }
marci@775
   492
//       return i;
marci@775
   493
//     }
marci@775
   494
//     EdgeIt& next(EdgeIt& i) const {
marci@775
   495
//       this->graph->next(i.e); 
marci@775
   496
//       while (this->graph->valid(i) && !(*edge_filter_map)[i.e]) { 
marci@775
   497
// 	this->graph->next(i.e); }
marci@775
   498
//       return i;
marci@775
   499
//     }
marci@556
   500
marci@775
   501
//     Node aNode(const OutEdgeIt& e) const { 
marci@775
   502
//       return Node(this->graph->aNode(e.e)); }
marci@775
   503
//     Node aNode(const InEdgeIt& e) const { 
marci@775
   504
//       return Node(this->graph->aNode(e.e)); }
marci@775
   505
//     Node bNode(const OutEdgeIt& e) const { 
marci@775
   506
//       return Node(this->graph->bNode(e.e)); }
marci@775
   507
//     Node bNode(const InEdgeIt& e) const { 
marci@775
   508
//       return Node(this->graph->bNode(e.e)); }
marci@556
   509
marci@561
   510
    /// This function hides \c n in the graph, i.e. the iteration 
marci@561
   511
    /// jumps over it. This is done by simply setting the value of \c n  
marci@561
   512
    /// to be false in the corresponding node-map.
marci@556
   513
    void hide(const Node& n) const { node_filter_map->set(n, false); }
marci@561
   514
marci@561
   515
    /// This function hides \c e in the graph, i.e. the iteration 
marci@561
   516
    /// jumps over it. This is done by simply setting the value of \c e  
marci@561
   517
    /// to be false in the corresponding edge-map.
marci@556
   518
    void hide(const Edge& e) const { edge_filter_map->set(e, false); }
marci@556
   519
marci@561
   520
    /// The value of \c n is set to be true in the node-map which stores 
marci@561
   521
    /// hide information. If \c n was hidden previuosly, then it is shown 
marci@561
   522
    /// again
marci@561
   523
     void unHide(const Node& n) const { node_filter_map->set(n, true); }
marci@561
   524
marci@561
   525
    /// The value of \c e is set to be true in the edge-map which stores 
marci@561
   526
    /// hide information. If \c e was hidden previuosly, then it is shown 
marci@561
   527
    /// again
marci@556
   528
    void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
marci@556
   529
marci@561
   530
    /// Returns true if \c n is hidden.
marci@561
   531
    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
marci@561
   532
marci@561
   533
    /// Returns true if \c n is hidden.
marci@561
   534
    bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
marci@593
   535
marci@792
   536
    /// \warning This is a linear time operation and works only if 
marci@792
   537
    /// \c Graph::NodeIt is defined.
marci@593
   538
    int nodeNum() const { 
marci@593
   539
      int i=0;
marci@792
   540
      for (NodeIt n(*this); n!=INVALID; ++n) ++i;
marci@593
   541
      return i; 
marci@593
   542
    }
marci@593
   543
marci@792
   544
    /// \warning This is a linear time operation and works only if 
marci@792
   545
    /// \c Graph::EdgeIt is defined.
marci@593
   546
    int edgeNum() const { 
marci@593
   547
      int i=0;
marci@792
   548
      for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
marci@593
   549
      return i; 
marci@593
   550
    }
marci@593
   551
marci@556
   552
  };
marci@556
   553
marci@569
   554
marci@569
   555
marci@838
   556
//   /// \brief A wrapper for forgetting the orientation of a graph.
marci@838
   557
//   ///
marci@838
   558
//   /// A wrapper for getting an undirected graph by forgetting
marci@838
   559
//   /// the orientation of a directed one.
marci@838
   560
//   ///
marci@838
   561
//   /// \author Marton Makai
marci@838
   562
//   /// does not work in the new concept.
marci@556
   563
  template<typename Graph>
marci@556
   564
  class UndirGraphWrapper : public GraphWrapper<Graph> {
marci@650
   565
  public:
marci@650
   566
    typedef GraphWrapper<Graph> Parent; 
marci@556
   567
  protected:
marci@556
   568
    UndirGraphWrapper() : GraphWrapper<Graph>() { }
marci@556
   569
    
marci@556
   570
  public:
marci@556
   571
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
   572
    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@556
   573
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@556
   574
    typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
marci@556
   575
marci@556
   576
    UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }  
marci@556
   577
marci@556
   578
    class OutEdgeIt {
marci@556
   579
      friend class UndirGraphWrapper<Graph>;
marci@556
   580
      bool out_or_in; //true iff out
marci@556
   581
      typename Graph::OutEdgeIt out;
marci@556
   582
      typename Graph::InEdgeIt in;
marci@556
   583
    public:
marci@556
   584
      OutEdgeIt() { }
marci@556
   585
      OutEdgeIt(const Invalid& i) : Edge(i) { }
marci@556
   586
      OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
marci@556
   587
	out_or_in=true; _G.graph->first(out, _n);
marci@556
   588
	if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n);	}
marci@556
   589
      } 
marci@556
   590
      operator Edge() const { 
marci@556
   591
	if (out_or_in) return Edge(out); else return Edge(in); 
marci@556
   592
      }
marci@556
   593
    };
marci@556
   594
marci@556
   595
//FIXME InEdgeIt
marci@556
   596
    typedef OutEdgeIt InEdgeIt; 
marci@556
   597
marci@556
   598
    using GraphWrapper<Graph>::first;
marci@556
   599
//     NodeIt& first(NodeIt& i) const { 
marci@556
   600
//       i=NodeIt(*this); return i;
marci@556
   601
//     }
marci@556
   602
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   603
      i=OutEdgeIt(*this, p); return i;
marci@556
   604
    }
marci@556
   605
//FIXME
marci@556
   606
//     InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   607
//       i=InEdgeIt(*this, p); return i;
marci@556
   608
//     }
marci@556
   609
//     EdgeIt& first(EdgeIt& i) const { 
marci@556
   610
//       i=EdgeIt(*this); return i;
marci@556
   611
//     }
marci@556
   612
marci@556
   613
    using GraphWrapper<Graph>::next;
marci@556
   614
//     NodeIt& next(NodeIt& n) const {
marci@556
   615
//       GraphWrapper<Graph>::next(n);
marci@556
   616
//       return n;
marci@556
   617
//     }
marci@556
   618
    OutEdgeIt& next(OutEdgeIt& e) const {
marci@556
   619
      if (e.out_or_in) {
marci@556
   620
	typename Graph::Node n=this->graph->tail(e.out);
marci@556
   621
	this->graph->next(e.out);
marci@556
   622
	if (!this->graph->valid(e.out)) { 
marci@556
   623
	  e.out_or_in=false; this->graph->first(e.in, n); }
marci@556
   624
      } else {
marci@556
   625
	this->graph->next(e.in);
marci@556
   626
      }
marci@556
   627
      return e;
marci@556
   628
    }
marci@556
   629
    //FIXME InEdgeIt
marci@556
   630
//     EdgeIt& next(EdgeIt& e) const {
marci@556
   631
//       GraphWrapper<Graph>::next(n);
marci@556
   632
// //      graph->next(e.e);
marci@556
   633
//       return e;
marci@556
   634
//     }
marci@556
   635
marci@556
   636
    Node aNode(const OutEdgeIt& e) const { 
marci@556
   637
      if (e.out_or_in) return this->graph->tail(e); else 
marci@556
   638
	return this->graph->head(e); }
marci@556
   639
    Node bNode(const OutEdgeIt& e) const { 
marci@556
   640
      if (e.out_or_in) return this->graph->head(e); else 
marci@556
   641
	return this->graph->tail(e); }
marci@556
   642
  };
marci@556
   643
  
marci@612
   644
  /// \brief An undirected graph template.
marci@612
   645
  ///
marci@612
   646
  /// An undirected graph template.
marci@612
   647
  /// This class works as an undirected graph and a directed graph of 
marci@612
   648
  /// class \c Graph is used for the physical storage.
marci@612
   649
  /// \ingroup graphs
marci@556
   650
  template<typename Graph>
marci@556
   651
  class UndirGraph : public UndirGraphWrapper<Graph> {
marci@556
   652
    typedef UndirGraphWrapper<Graph> Parent;
marci@556
   653
  protected:
marci@556
   654
    Graph gr;
marci@556
   655
  public:
marci@556
   656
    UndirGraph() : UndirGraphWrapper<Graph>() { 
marci@556
   657
      Parent::setGraph(gr); 
marci@556
   658
    }
marci@556
   659
  };
marci@556
   660
marci@569
   661
marci@650
   662
marci@650
   663
  ///\brief A wrapper for composing a subgraph of a 
marci@792
   664
  /// bidirected graph made from a directed one. 
marci@612
   665
  ///
marci@792
   666
  /// Suppose that for a directed graph $G=(V, A)$, 
marci@792
   667
  /// two predicates on the edge-set, $forward_filter$, and $backward_filter$ 
marci@792
   668
  /// is given, and we are dealing with the directed graph
marci@792
   669
  /// a $G'=(V, \{uv : uv\in A \mbox{ and } forward_filter(uv) \mbox{ is true}\}+\{vu : uv\in A \mbox{ and } backward_filter(uv) \mbox{ is true}\})$. 
marci@792
   670
  /// The purpose of writing + instead of union is because parallel 
marci@792
   671
  /// edges can arose. 
marci@792
   672
  /// In other words, a subgraph of the bidirected graph obtained, which 
marci@792
   673
  /// is given by orienting the edges of the original graph in both directions.
marci@792
   674
  /// An example for such a construction is the \c RevGraphWrapper where the 
marci@792
   675
  /// forward_filter is everywhere false and the backward_filter is 
marci@792
   676
  /// everywhere true. We note that for sake of efficiency, 
marci@792
   677
  /// \c RevGraphWrapper is implemented in a different way. 
marci@792
   678
  /// But BidirGraphWrapper is obtained from 
marci@792
   679
  /// SubBidirGraphWrapper by considering everywhere true 
marci@792
   680
  /// predicates both forward_filter and backward_filter. 
marci@792
   681
  /// Finally, one of the most important applications of SubBidirGraphWrapper 
marci@792
   682
  /// is ResGraphWrapper, which stands for the residual graph in directed 
marci@792
   683
  /// flow and circulation problems. 
marci@792
   684
  /// As wrappers usually, the SubBidirGraphWrapper implements the 
marci@792
   685
  /// above mentioned graph structure without its physical storage, 
marci@792
   686
  /// that is the whole stuff eats constant memory. 
marci@792
   687
  /// As the oppositely directed edges are logical different, 
marci@792
   688
  /// the maps are able to attach different values for them. 
marci@650
   689
  template<typename Graph, 
marci@650
   690
	   typename ForwardFilterMap, typename BackwardFilterMap>
marci@650
   691
  class SubBidirGraphWrapper : public GraphWrapper<Graph> {
marci@650
   692
  public:
marci@650
   693
    typedef GraphWrapper<Graph> Parent; 
marci@569
   694
  protected:
marci@650
   695
    ForwardFilterMap* forward_filter;
marci@650
   696
    BackwardFilterMap* backward_filter;
marci@650
   697
marci@792
   698
    SubBidirGraphWrapper() : GraphWrapper<Graph>() { }
marci@650
   699
    void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
marci@650
   700
      forward_filter=&_forward_filter;
marci@650
   701
    }
marci@650
   702
    void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
marci@650
   703
      backward_filter=&_backward_filter;
marci@650
   704
    }
marci@569
   705
marci@569
   706
  public:
marci@569
   707
marci@650
   708
    SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter, 
marci@650
   709
			 BackwardFilterMap& _backward_filter) : 
marci@650
   710
      GraphWrapper<Graph>(_graph), 
marci@650
   711
      forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
alpar@774
   712
    SubBidirGraphWrapper(const SubBidirGraphWrapper<Graph, 
alpar@774
   713
			 ForwardFilterMap, BackwardFilterMap>& gw) : 
alpar@774
   714
      Parent(gw), 
alpar@774
   715
      forward_filter(gw.forward_filter), 
alpar@774
   716
      backward_filter(gw.backward_filter) { }
marci@569
   717
marci@569
   718
    class Edge; 
marci@569
   719
    class OutEdgeIt; 
marci@569
   720
    friend class Edge; 
marci@569
   721
    friend class OutEdgeIt; 
marci@569
   722
marci@621
   723
    template<typename T> class EdgeMap;
marci@621
   724
marci@569
   725
    typedef typename GraphWrapper<Graph>::Node Node;
marci@621
   726
alpar@774
   727
    typedef typename Graph::Edge GraphEdge;
marci@792
   728
    /// SubBidirGraphWrapper<..., ..., ...>::Edge is inherited from 
marci@792
   729
    /// Graph::Edge. It contains an extra bool flag which shows if the 
marci@792
   730
    /// edge is the backward version of the original edge.
marci@569
   731
    class Edge : public Graph::Edge {
marci@650
   732
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   733
					ForwardFilterMap, BackwardFilterMap>;
marci@621
   734
      template<typename T> friend class EdgeMap;
marci@569
   735
    protected:
marci@569
   736
      bool backward; //true, iff backward
marci@569
   737
    public:
marci@569
   738
      Edge() { }
marci@792
   739
      /// \todo =false is needed, or causes problems?
marci@792
   740
      /// If \c _backward is false, then we get an edge corresponding to the 
marci@792
   741
      /// original one, otherwise its oppositely directed pair is obtained.
alpar@774
   742
      Edge(const typename Graph::Edge& e, bool _backward/*=false*/) : 
alpar@774
   743
	Graph::Edge(e), backward(_backward) { }
alpar@774
   744
      Edge(Invalid i) : Graph::Edge(i), backward(true) { }
marci@569
   745
//the unique invalid iterator
alpar@774
   746
//       friend bool operator==(const Edge& u, const Edge& v) { 
alpar@774
   747
// 	return (u.backward==v.backward && 
alpar@774
   748
// 		static_cast<typename Graph::Edge>(u)==
alpar@774
   749
// 		static_cast<typename Graph::Edge>(v));
alpar@774
   750
//       } 
alpar@774
   751
//       friend bool operator!=(const Edge& u, const Edge& v) { 
alpar@774
   752
// 	return (u.backward!=v.backward || 
alpar@774
   753
// 		static_cast<typename Graph::Edge>(u)!=
alpar@774
   754
// 		static_cast<typename Graph::Edge>(v));
alpar@774
   755
//       }
alpar@774
   756
      bool operator==(const Edge& v) const { 
alpar@774
   757
	return (this->backward==v.backward && 
alpar@774
   758
		static_cast<typename Graph::Edge>(*this)==
marci@569
   759
		static_cast<typename Graph::Edge>(v));
marci@569
   760
      } 
alpar@774
   761
      bool operator!=(const Edge& v) const { 
alpar@774
   762
	return (this->backward!=v.backward || 
alpar@774
   763
		static_cast<typename Graph::Edge>(*this)!=
marci@569
   764
		static_cast<typename Graph::Edge>(v));
alpar@774
   765
      }
marci@569
   766
    };
marci@569
   767
alpar@774
   768
    class OutEdgeIt : public Edge {
marci@650
   769
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   770
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   771
    protected:
alpar@774
   772
      const SubBidirGraphWrapper<Graph, 
alpar@774
   773
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
   774
    public:
marci@569
   775
      OutEdgeIt() { }
alpar@774
   776
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@569
   777
//the unique invalid iterator
marci@650
   778
      OutEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   779
		ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) : 
alpar@774
   780
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), false), gw(&_gw) { 
alpar@774
   781
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   782
	       !(*(gw->forward_filter))[*this]) 
alpar@774
   783
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   784
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   785
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   786
	  *static_cast<Edge*>(this)=
alpar@774
   787
	    Edge(typename Graph::InEdgeIt(*(_gw.graph), n), true);
marci@775
   788
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   789
		 !(*(gw->backward_filter))[*this]) 
marci@775
   790
	    *(static_cast<GraphEdge*>(this))=
marci@775
   791
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   792
	}
alpar@774
   793
      }
alpar@774
   794
      OutEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   795
		ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
   796
	Edge(e), gw(&_gw) { }
alpar@774
   797
      OutEdgeIt& operator++() { 
alpar@774
   798
	if (!this->backward) {
alpar@774
   799
	  Node n=gw->tail(*this);
alpar@774
   800
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   801
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   802
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   803
		 !(*(gw->forward_filter))[*this]) 
alpar@774
   804
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   805
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   806
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   807
	    *static_cast<Edge*>(this)=
alpar@774
   808
	      Edge(typename Graph::InEdgeIt(*(gw->graph), n), true);
marci@775
   809
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   810
		   !(*(gw->backward_filter))[*this]) 
marci@775
   811
	      *(static_cast<GraphEdge*>(this))=
marci@775
   812
		++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   813
	  }
alpar@774
   814
	} else {
alpar@774
   815
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   816
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   817
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   818
		 !(*(gw->backward_filter))[*this]) 
alpar@774
   819
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   820
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@569
   821
	}
alpar@774
   822
	return *this;
marci@569
   823
      }
marci@569
   824
    };
marci@569
   825
alpar@774
   826
    class InEdgeIt : public Edge {
marci@650
   827
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   828
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   829
    protected:
alpar@774
   830
      const SubBidirGraphWrapper<Graph, 
alpar@774
   831
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
   832
    public:
marci@569
   833
      InEdgeIt() { }
alpar@774
   834
      InEdgeIt(Invalid i) : Edge(i) { }
marci@569
   835
//the unique invalid iterator
marci@650
   836
      InEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   837
	       ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) : 
alpar@774
   838
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n), false), gw(&_gw) { 
alpar@774
   839
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   840
	       !(*(gw->forward_filter))[*this]) 
alpar@774
   841
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   842
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   843
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   844
	  *static_cast<Edge*>(this)=
alpar@774
   845
	    Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), true);
marci@775
   846
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   847
		 !(*(gw->backward_filter))[*this]) 
marci@775
   848
	    *(static_cast<GraphEdge*>(this))=
marci@775
   849
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   850
	}
alpar@774
   851
      }
alpar@774
   852
      InEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   853
	       ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
   854
	Edge(e), gw(&_gw) { }
alpar@774
   855
      InEdgeIt& operator++() { 
alpar@774
   856
	if (!this->backward) {
marci@775
   857
	  Node n=gw->tail(*this);
alpar@774
   858
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   859
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   860
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   861
		 !(*(gw->forward_filter))[*this]) 
alpar@774
   862
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   863
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   864
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   865
	    *static_cast<Edge*>(this)=
alpar@774
   866
	      Edge(typename Graph::OutEdgeIt(*(gw->graph), n), true);
marci@775
   867
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   868
		   !(*(gw->backward_filter))[*this]) 
marci@775
   869
	      *(static_cast<GraphEdge*>(this))=
marci@775
   870
		++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   871
	  }
alpar@774
   872
	} else {
alpar@774
   873
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   874
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   875
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   876
		 !(*(gw->backward_filter))[*this]) 
alpar@774
   877
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   878
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@569
   879
	}
alpar@774
   880
	return *this;
marci@569
   881
      }
marci@569
   882
    };
marci@569
   883
alpar@774
   884
    class EdgeIt : public Edge {
marci@650
   885
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   886
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   887
    protected:
alpar@774
   888
      const SubBidirGraphWrapper<Graph, 
alpar@774
   889
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
   890
    public:
marci@569
   891
      EdgeIt() { }
alpar@774
   892
      EdgeIt(Invalid i) : Edge(i) { }
alpar@774
   893
//the unique invalid iterator
marci@650
   894
      EdgeIt(const SubBidirGraphWrapper<Graph, 
marci@775
   895
	     ForwardFilterMap, BackwardFilterMap>& _gw) : 
marci@775
   896
	Edge(typename Graph::OutEdgeIt(*(_gw.graph)), false), gw(&_gw) { 
alpar@774
   897
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   898
	       !(*(gw->forward_filter))[*this]) 
alpar@774
   899
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   900
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   901
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   902
	  *static_cast<Edge*>(this)=
alpar@774
   903
	    Edge(typename Graph::EdgeIt(*(_gw.graph)), true);
marci@775
   904
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   905
		 !(*(gw->backward_filter))[*this]) 
marci@775
   906
	    *(static_cast<GraphEdge*>(this))=
marci@775
   907
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   908
	}
alpar@774
   909
      }
alpar@774
   910
      EdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   911
	     ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
   912
	Edge(e), gw(&_gw) { }
alpar@774
   913
      EdgeIt& operator++() { 
alpar@774
   914
	if (!this->backward) {
alpar@774
   915
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   916
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
   917
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   918
		 !(*(gw->forward_filter))[*this]) 
alpar@774
   919
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   920
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   921
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   922
	    *static_cast<Edge*>(this)=
alpar@774
   923
	      Edge(typename Graph::EdgeIt(*(gw->graph)), true);
marci@775
   924
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   925
		   !(*(gw->backward_filter))[*this]) 
marci@775
   926
	      *(static_cast<GraphEdge*>(this))=
marci@775
   927
		++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   928
	  }
alpar@774
   929
	} else {
alpar@774
   930
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   931
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
   932
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   933
		 !(*(gw->backward_filter))[*this]) 
alpar@774
   934
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   935
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@569
   936
	}
alpar@774
   937
	return *this;
marci@569
   938
      }
marci@569
   939
    };
marci@569
   940
marci@569
   941
    using GraphWrapper<Graph>::first;
marci@569
   942
//     NodeIt& first(NodeIt& i) const { 
marci@569
   943
//       i=NodeIt(*this); return i;
marci@569
   944
//     }
marci@569
   945
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@569
   946
      i=OutEdgeIt(*this, p); return i;
marci@569
   947
    }
marci@569
   948
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@569
   949
      i=InEdgeIt(*this, p); return i;
marci@569
   950
    }
marci@569
   951
    EdgeIt& first(EdgeIt& i) const { 
marci@569
   952
      i=EdgeIt(*this); return i;
marci@569
   953
    }
marci@556
   954
  
alpar@774
   955
//     using GraphWrapper<Graph>::next;
alpar@774
   956
// //    NodeIt& next(NodeIt& n) const { GraphWrapper<Graph>::next(n); return n; }
alpar@774
   957
//     OutEdgeIt& next(OutEdgeIt& e) const { 
alpar@774
   958
//       if (!e.backward) {
alpar@774
   959
// 	Node v=this->graph->aNode(e.out);
alpar@774
   960
// 	this->graph->next(e.out);
alpar@774
   961
// 	while(this->graph->valid(e.out) && !(*forward_filter)[e]) { 
alpar@774
   962
// 	  this->graph->next(e.out); }
alpar@774
   963
// 	if (!this->graph->valid(e.out)) {
alpar@774
   964
// 	  e.backward=true;
alpar@774
   965
// 	  this->graph->first(e.in, v); 
alpar@774
   966
// 	  while(this->graph->valid(e.in) && !(*backward_filter)[e]) { 
alpar@774
   967
// 	    this->graph->next(e.in); }
alpar@774
   968
// 	}
alpar@774
   969
//       } else {
alpar@774
   970
// 	this->graph->next(e.in);
alpar@774
   971
// 	while(this->graph->valid(e.in) && !(*backward_filter)[e]) { 
alpar@774
   972
// 	  this->graph->next(e.in); } 
alpar@774
   973
//       }
alpar@774
   974
//       return e;
alpar@774
   975
//     }
alpar@774
   976
// //     FIXME Not tested
alpar@774
   977
//     InEdgeIt& next(InEdgeIt& e) const { 
alpar@774
   978
//       if (!e.backward) {
alpar@774
   979
// 	Node v=this->graph->aNode(e.in);
alpar@774
   980
// 	this->graph->next(e.in);
alpar@774
   981
// 	while(this->graph->valid(e.in) && !(*forward_filter)[e]) { 
alpar@774
   982
// 	  this->graph->next(e.in); }
alpar@774
   983
// 	if (!this->graph->valid(e.in)) {
alpar@774
   984
// 	  e.backward=true;
alpar@774
   985
// 	  this->graph->first(e.out, v); 
alpar@774
   986
// 	  while(this->graph->valid(e.out) && !(*backward_filter)[e]) { 
alpar@774
   987
// 	    this->graph->next(e.out); }
alpar@774
   988
// 	}
alpar@774
   989
//       } else {
alpar@774
   990
// 	this->graph->next(e.out);
alpar@774
   991
// 	while(this->graph->valid(e.out) && !(*backward_filter)[e]) { 
alpar@774
   992
// 	  this->graph->next(e.out); } 
alpar@774
   993
//       }
alpar@774
   994
//       return e;
alpar@774
   995
//     }
alpar@774
   996
//     EdgeIt& next(EdgeIt& e) const {
alpar@774
   997
//       if (!e.backward) {
alpar@774
   998
// 	this->graph->next(e.e);
alpar@774
   999
// 	while(this->graph->valid(e.e) && !(*forward_filter)[e]) { 
alpar@774
  1000
// 	  this->graph->next(e.e); }
alpar@774
  1001
// 	if (!this->graph->valid(e.e)) {
alpar@774
  1002
// 	  e.backward=true;
alpar@774
  1003
// 	  this->graph->first(e.e); 
alpar@774
  1004
// 	  while(this->graph->valid(e.e) && !(*backward_filter)[e]) { 
alpar@774
  1005
// 	    this->graph->next(e.e); }
alpar@774
  1006
// 	}
alpar@774
  1007
//       } else {
alpar@774
  1008
// 	this->graph->next(e.e);
alpar@774
  1009
// 	while(this->graph->valid(e.e) && !(*backward_filter)[e]) { 
alpar@774
  1010
// 	  this->graph->next(e.e); } 
alpar@774
  1011
//       }
alpar@774
  1012
//       return e;
alpar@774
  1013
//     }
marci@569
  1014
marci@569
  1015
    Node tail(Edge e) const { 
marci@569
  1016
      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
marci@569
  1017
    Node head(Edge e) const { 
marci@569
  1018
      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
marci@569
  1019
alpar@774
  1020
//     Node aNode(OutEdgeIt e) const { 
alpar@774
  1021
//       return ((!e.backward) ? this->graph->aNode(e.out) : 
alpar@774
  1022
// 	      this->graph->aNode(e.in)); }
alpar@774
  1023
//     Node bNode(OutEdgeIt e) const { 
alpar@774
  1024
//       return ((!e.backward) ? this->graph->bNode(e.out) : 
alpar@774
  1025
// 	      this->graph->bNode(e.in)); }
marci@569
  1026
alpar@774
  1027
//     Node aNode(InEdgeIt e) const { 
alpar@774
  1028
//       return ((!e.backward) ? this->graph->aNode(e.in) : 
alpar@774
  1029
// 	      this->graph->aNode(e.out)); }
alpar@774
  1030
//     Node bNode(InEdgeIt e) const { 
alpar@774
  1031
//       return ((!e.backward) ? this->graph->bNode(e.in) : 
alpar@774
  1032
// 	      this->graph->bNode(e.out)); }
marci@569
  1033
marci@572
  1034
    /// Gives back the opposite edge.
marci@572
  1035
    Edge opposite(const Edge& e) const { 
marci@572
  1036
      Edge f=e;
marci@572
  1037
      f.backward=!f.backward;
marci@572
  1038
      return f;
marci@572
  1039
    }
marci@572
  1040
marci@792
  1041
    /// \warning This is a linear time operation and works only if 
marci@792
  1042
    /// \c Graph::EdgeIt is defined.
marci@792
  1043
    int edgeNum() const { 
marci@792
  1044
      int i=0;
marci@792
  1045
      for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
marci@792
  1046
      return i; 
marci@792
  1047
    }
marci@569
  1048
marci@569
  1049
    bool forward(const Edge& e) const { return !e.backward; }
marci@569
  1050
    bool backward(const Edge& e) const { return e.backward; }
marci@569
  1051
marci@569
  1052
marci@569
  1053
    template <typename T>
marci@792
  1054
    /// \c SubBidirGraphWrapper<..., ..., ...>::EdgeMap contains two 
marci@792
  1055
    /// Graph::EdgeMap one for the forward edges and 
marci@792
  1056
    /// one for the backward edges.
marci@569
  1057
    class EdgeMap {
marci@569
  1058
      typename Graph::template EdgeMap<T> forward_map, backward_map; 
marci@569
  1059
    public:
marci@623
  1060
      typedef T ValueType;
marci@623
  1061
      typedef Edge KeyType;
marci@650
  1062
      EdgeMap(const SubBidirGraphWrapper<Graph, 
alpar@774
  1063
	      ForwardFilterMap, BackwardFilterMap>& g) : 
alpar@774
  1064
	forward_map(*(g.graph)), backward_map(*(g.graph)) { }
marci@650
  1065
      EdgeMap(const SubBidirGraphWrapper<Graph, 
alpar@774
  1066
	      ForwardFilterMap, BackwardFilterMap>& g, T a) : 
alpar@774
  1067
	forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
marci@569
  1068
      void set(Edge e, T a) { 
marci@569
  1069
	if (!e.backward) 
marci@792
  1070
	  forward_map.set(e, a); 
marci@569
  1071
	else 
marci@792
  1072
	  backward_map.set(e, a); 
marci@569
  1073
      }
marci@569
  1074
      T operator[](Edge e) const { 
marci@569
  1075
	if (!e.backward) 
marci@792
  1076
	  return forward_map[e]; 
marci@569
  1077
	else 
marci@792
  1078
	  return backward_map[e]; 
marci@569
  1079
      }
marci@625
  1080
      void update() { 
marci@625
  1081
	forward_map.update(); 
marci@625
  1082
	backward_map.update();
marci@625
  1083
      }
marci@569
  1084
//       T get(Edge e) const { 
marci@569
  1085
// 	if (e.out_or_in) 
marci@569
  1086
// 	  return forward_map.get(e.out); 
marci@569
  1087
// 	else 
marci@569
  1088
// 	  return backward_map.get(e.in); 
marci@569
  1089
//       }
marci@569
  1090
    };
marci@569
  1091
  };
marci@569
  1092
marci@650
  1093
marci@650
  1094
  ///\brief A wrapper for composing bidirected graph from a directed one. 
marci@650
  1095
  ///
marci@650
  1096
  /// A wrapper for composing bidirected graph from a directed one. 
marci@650
  1097
  /// A bidirected graph is composed over the directed one without physical 
marci@650
  1098
  /// storage. As the oppositely directed edges are logically different ones 
marci@650
  1099
  /// the maps are able to attach different values for them.
marci@650
  1100
  template<typename Graph>
marci@650
  1101
  class BidirGraphWrapper : 
marci@650
  1102
    public SubBidirGraphWrapper<
marci@650
  1103
    Graph, 
marci@650
  1104
    ConstMap<typename Graph::Edge, bool>, 
marci@650
  1105
    ConstMap<typename Graph::Edge, bool> > {
marci@650
  1106
  public:
marci@650
  1107
    typedef  SubBidirGraphWrapper<
marci@650
  1108
      Graph, 
marci@650
  1109
      ConstMap<typename Graph::Edge, bool>, 
marci@650
  1110
      ConstMap<typename Graph::Edge, bool> > Parent; 
marci@650
  1111
  protected:
marci@650
  1112
    ConstMap<typename Graph::Edge, bool> cm;
marci@650
  1113
marci@655
  1114
    BidirGraphWrapper() : Parent(), cm(true) { 
marci@655
  1115
      Parent::setForwardFilterMap(cm);
marci@655
  1116
      Parent::setBackwardFilterMap(cm);
marci@655
  1117
    }
marci@650
  1118
  public:
marci@650
  1119
    BidirGraphWrapper(Graph& _graph) : Parent() { 
marci@650
  1120
      Parent::setGraph(_graph);
marci@650
  1121
      Parent::setForwardFilterMap(cm);
marci@650
  1122
      Parent::setBackwardFilterMap(cm);
marci@650
  1123
    }
marci@738
  1124
marci@738
  1125
    int edgeNum() const { 
marci@738
  1126
      return 2*this->graph->edgeNum();
marci@738
  1127
    }
marci@650
  1128
  };
marci@650
  1129
marci@650
  1130
marci@612
  1131
  /// \brief A bidirected graph template.
marci@612
  1132
  ///
marci@612
  1133
  /// A bidirected graph template.
marci@612
  1134
  /// Such a bidirected graph stores each pair of oppositely directed edges 
marci@612
  1135
  /// ones in the memory, i.e. a directed graph of type 
marci@612
  1136
  /// \c Graph is used for that.
marci@612
  1137
  /// As the oppositely directed edges are logically different ones 
marci@612
  1138
  /// the maps are able to attach different values for them.
marci@612
  1139
  /// \ingroup graphs
marci@612
  1140
  template<typename Graph>
marci@612
  1141
  class BidirGraph : public BidirGraphWrapper<Graph> {
marci@650
  1142
  public:
marci@612
  1143
    typedef UndirGraphWrapper<Graph> Parent;
marci@612
  1144
  protected:
marci@612
  1145
    Graph gr;
marci@612
  1146
  public:
marci@612
  1147
    BidirGraph() : BidirGraphWrapper<Graph>() { 
marci@612
  1148
      Parent::setGraph(gr); 
marci@612
  1149
    }
marci@612
  1150
  };
marci@569
  1151
marci@556
  1152
marci@650
  1153
marci@650
  1154
  template<typename Graph, typename Number,
marci@650
  1155
	   typename CapacityMap, typename FlowMap>
marci@658
  1156
  class ResForwardFilter {
marci@658
  1157
    //    const Graph* graph;
marci@650
  1158
    const CapacityMap* capacity;
marci@650
  1159
    const FlowMap* flow;
marci@650
  1160
  public:
marci@658
  1161
    ResForwardFilter(/*const Graph& _graph, */
marci@658
  1162
		     const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1163
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1164
    ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@658
  1165
    //void setGraph(const Graph& _graph) { graph=&_graph; }
marci@656
  1166
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1167
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1168
    bool operator[](const typename Graph::Edge& e) const {
marci@738
  1169
      return (Number((*flow)[e]) < Number((*capacity)[e]));
marci@650
  1170
    }
marci@650
  1171
  };
marci@650
  1172
marci@650
  1173
  template<typename Graph, typename Number,
marci@650
  1174
	   typename CapacityMap, typename FlowMap>
marci@658
  1175
  class ResBackwardFilter {
marci@658
  1176
    //const Graph* graph;
marci@650
  1177
    const CapacityMap* capacity;
marci@650
  1178
    const FlowMap* flow;
marci@650
  1179
  public:
marci@658
  1180
    ResBackwardFilter(/*const Graph& _graph,*/ 
marci@658
  1181
		      const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1182
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1183
    ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@658
  1184
    //void setGraph(const Graph& _graph) { graph=&_graph; }
marci@656
  1185
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1186
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1187
    bool operator[](const typename Graph::Edge& e) const {
marci@738
  1188
      return (Number(0) < Number((*flow)[e]));
marci@650
  1189
    }
marci@650
  1190
  };
marci@650
  1191
marci@653
  1192
  
marci@653
  1193
  /// A wrapper for composing the residual graph for directed flow and circulation problems.
marci@650
  1194
marci@653
  1195
  /// A wrapper for composing the residual graph for directed flow and circulation problems.
marci@650
  1196
  template<typename Graph, typename Number, 
marci@650
  1197
	   typename CapacityMap, typename FlowMap>
marci@653
  1198
  class ResGraphWrapper : 
marci@650
  1199
    public SubBidirGraphWrapper< 
marci@650
  1200
    Graph, 
marci@658
  1201
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1202
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
marci@650
  1203
  public:
marci@650
  1204
    typedef SubBidirGraphWrapper< 
marci@650
  1205
      Graph, 
marci@658
  1206
      ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1207
      ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
marci@650
  1208
  protected:
marci@650
  1209
    const CapacityMap* capacity;
marci@650
  1210
    FlowMap* flow;
marci@658
  1211
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
marci@658
  1212
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
marci@658
  1213
    ResGraphWrapper() : Parent(), 
marci@658
  1214
 			capacity(0), flow(0) { }
marci@658
  1215
    void setCapacityMap(const CapacityMap& _capacity) {
marci@658
  1216
      capacity=&_capacity;
marci@658
  1217
      forward_filter.setCapacity(_capacity);
marci@658
  1218
      backward_filter.setCapacity(_capacity);
marci@658
  1219
    }
marci@658
  1220
    void setFlowMap(FlowMap& _flow) {
marci@658
  1221
      flow=&_flow;
marci@658
  1222
      forward_filter.setFlow(_flow);
marci@658
  1223
      backward_filter.setFlow(_flow);
marci@658
  1224
    }
marci@658
  1225
//     /// \bug does graph reference needed in filtermaps??
marci@658
  1226
//     void setGraph(const Graph& _graph) { 
marci@658
  1227
//       Parent::setGraph(_graph);
marci@658
  1228
//       forward_filter.setGraph(_graph);
marci@658
  1229
//       backward_filter.setGraph(_graph);
marci@656
  1230
//     }
marci@650
  1231
  public:
marci@653
  1232
    ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity, 
marci@650
  1233
		       FlowMap& _flow) : 
marci@650
  1234
      Parent(), capacity(&_capacity), flow(&_flow), 
marci@658
  1235
      forward_filter(/*_graph,*/ _capacity, _flow), 
marci@658
  1236
      backward_filter(/*_graph,*/ _capacity, _flow) {
marci@650
  1237
      Parent::setGraph(_graph);
marci@650
  1238
      Parent::setForwardFilterMap(forward_filter);
marci@650
  1239
      Parent::setBackwardFilterMap(backward_filter);
marci@650
  1240
    }
marci@650
  1241
marci@660
  1242
    typedef typename Parent::Edge Edge;
marci@660
  1243
marci@650
  1244
    //    bool forward(const Parent::Edge& e) const { return Parent::forward(e); }
marci@650
  1245
    //bool backward(const Edge& e) const { return e.backward; }
marci@650
  1246
marci@660
  1247
    void augment(const Edge& e, Number a) const {
marci@650
  1248
      if (Parent::forward(e))  
marci@650
  1249
// 	flow->set(e.out, flow->get(e.out)+a);
marci@650
  1250
	flow->set(e, (*flow)[e]+a);
marci@650
  1251
      else  
marci@650
  1252
	//flow->set(e.in, flow->get(e.in)-a);
marci@650
  1253
	flow->set(e, (*flow)[e]-a);
marci@650
  1254
    }
marci@650
  1255
marci@660
  1256
    /// \deprecated
marci@660
  1257
    ///
marci@660
  1258
    Number resCap(const Edge& e) const { 
marci@650
  1259
      if (Parent::forward(e)) 
marci@650
  1260
//	return (capacity->get(e.out)-flow->get(e.out)); 
marci@650
  1261
	return ((*capacity)[e]-(*flow)[e]); 
marci@650
  1262
      else 
marci@650
  1263
//	return (flow->get(e.in)); 
marci@650
  1264
	return ((*flow)[e]); 
marci@650
  1265
    }
marci@650
  1266
marci@660
  1267
    /// \brief Residual capacity map.
marci@660
  1268
    ///
marci@792
  1269
    /// In generic residual graphs the residual capacity can be obtained as a map. Not tested.
marci@660
  1270
    class ResCap {
marci@660
  1271
    protected:
marci@660
  1272
      const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
marci@660
  1273
    public:
marci@660
  1274
      typedef Number ValueType;
marci@660
  1275
      typedef Edge KeyType;
marci@660
  1276
      ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _res_graph) : 
marci@660
  1277
	res_graph(&_res_graph) { }
marci@660
  1278
      Number operator[](const Edge& e) const { 
marci@660
  1279
	if (res_graph->forward(e)) 
marci@660
  1280
	  //	return (capacity->get(e.out)-flow->get(e.out)); 
marci@660
  1281
	  return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e]; 
marci@660
  1282
	else 
marci@660
  1283
	  //	return (flow->get(e.in)); 
marci@660
  1284
	  return (*(res_graph->flow))[e]; 
marci@660
  1285
      }
marci@660
  1286
      /// \bug not needed with dynamic maps, or does it?
marci@660
  1287
      void update() { }
marci@660
  1288
    };
marci@660
  1289
marci@650
  1290
  };
marci@650
  1291
marci@650
  1292
marci@612
  1293
  /// For blocking flows.
marci@556
  1294
marci@792
  1295
  /// This graph wrapper is used for on-the-fly 
marci@792
  1296
  /// Dinits blocking flow computations.
marci@612
  1297
  /// For each node, an out-edge is stored which is used when the 
marci@612
  1298
  /// \code 
marci@612
  1299
  /// OutEdgeIt& first(OutEdgeIt&, const Node&)
marci@612
  1300
  /// \endcode
marci@612
  1301
  /// is called. 
marci@556
  1302
  ///
marci@792
  1303
  /// \author Marton Makai
marci@556
  1304
  template<typename Graph, typename FirstOutEdgesMap>
marci@556
  1305
  class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
marci@650
  1306
  public:
marci@650
  1307
    typedef GraphWrapper<Graph> Parent; 
marci@556
  1308
  protected:
marci@556
  1309
    FirstOutEdgesMap* first_out_edges;
marci@556
  1310
  public:
marci@556
  1311
    ErasingFirstGraphWrapper(Graph& _graph, 
marci@556
  1312
			     FirstOutEdgesMap& _first_out_edges) : 
marci@556
  1313
      GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }  
marci@556
  1314
marci@556
  1315
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
  1316
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@777
  1317
    class OutEdgeIt : public Edge { 
marci@556
  1318
      friend class GraphWrapper<Graph>;
marci@556
  1319
      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@777
  1320
      const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>* gw;
marci@556
  1321
    public:
marci@556
  1322
      OutEdgeIt() { }
marci@777
  1323
      //OutEdgeIt(const OutEdgeIt& e) : Edge(e), gw(e.gw) { }
marci@777
  1324
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@777
  1325
      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw, 
marci@777
  1326
		const Node& n) : 
marci@777
  1327
	Edge((*(_gw.first_out_edges))[n]), gw(&_gw) { }
marci@777
  1328
      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw, 
marci@777
  1329
		const Edge& e) : 
marci@777
  1330
	Edge(e), gw(&_gw) { }
marci@777
  1331
      OutEdgeIt& operator++() { 
marci@777
  1332
	*(static_cast<Edge*>(this))=
marci@777
  1333
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@777
  1334
	return *this; 
marci@777
  1335
      }
marci@556
  1336
    };
marci@777
  1337
//     class InEdgeIt { 
marci@777
  1338
//       friend class GraphWrapper<Graph>;
marci@777
  1339
//       friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@777
  1340
// //      typedef typename Graph::InEdgeIt GraphInEdgeIt;
marci@777
  1341
//       typename Graph::InEdgeIt e;
marci@777
  1342
//     public:
marci@777
  1343
//       InEdgeIt() { }
marci@777
  1344
//       InEdgeIt(const typename Graph::InEdgeIt& _e) : e(_e) { }
marci@777
  1345
//       InEdgeIt(const Invalid& i) : e(i) { }
marci@777
  1346
//       InEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G, 
marci@777
  1347
// 	       const Node& _n) : 
marci@777
  1348
// 	e(*(_G.graph), typename Graph::Node(_n)) { }
marci@777
  1349
//       operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@777
  1350
//     };	
marci@556
  1351
    //typedef typename Graph::SymEdgeIt SymEdgeIt;
marci@777
  1352
//     class EdgeIt { 
marci@777
  1353
//       friend class GraphWrapper<Graph>;
marci@777
  1354
//       friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@777
  1355
// //      typedef typename Graph::EdgeIt GraphEdgeIt;
marci@777
  1356
//       typename Graph::EdgeIt e;
marci@777
  1357
//     public:
marci@777
  1358
//       EdgeIt() { }
marci@777
  1359
//       EdgeIt(const typename Graph::EdgeIt& _e) : e(_e) { }
marci@777
  1360
//       EdgeIt(const Invalid& i) : e(i) { }
marci@777
  1361
//       EdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _G) : 
marci@777
  1362
// 	e(*(_G.graph)) { }
marci@777
  1363
//       operator Edge() const { return Edge(typename Graph::Edge(e)); }
marci@777
  1364
//     };
marci@556
  1365
marci@556
  1366
    using GraphWrapper<Graph>::first;
marci@556
  1367
//     NodeIt& first(NodeIt& i) const { 
marci@556
  1368
//       i=NodeIt(*this); return i;
marci@556
  1369
//     }
marci@556
  1370
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
  1371
      i=OutEdgeIt(*this, p); return i;
marci@556
  1372
    }
marci@777
  1373
//     InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@777
  1374
//       i=InEdgeIt(*this, p); return i;
marci@777
  1375
//     }
marci@777
  1376
//     EdgeIt& first(EdgeIt& i) const { 
marci@777
  1377
//       i=EdgeIt(*this); return i;
marci@777
  1378
//     }
marci@556
  1379
marci@777
  1380
//     using GraphWrapper<Graph>::next;
marci@777
  1381
// //    NodeIt& next(NodeIt& i) const { graph->next(i.n); return i; }
marci@777
  1382
//     OutEdgeIt& next(OutEdgeIt& i) const { this->graph->next(i.e); return i; }
marci@777
  1383
//     InEdgeIt& next(InEdgeIt& i) const { this->graph->next(i.e); return i; }
marci@777
  1384
//     EdgeIt& next(EdgeIt& i) const { this->graph->next(i.e); return i; }    
marci@556
  1385
    
marci@777
  1386
//     Node aNode(const OutEdgeIt& e) const { 
marci@777
  1387
//       return Node(this->graph->aNode(e.e)); }
marci@777
  1388
//     Node aNode(const InEdgeIt& e) const { 
marci@777
  1389
//       return Node(this->graph->aNode(e.e)); }
marci@777
  1390
//     Node bNode(const OutEdgeIt& e) const { 
marci@777
  1391
//       return Node(this->graph->bNode(e.e)); }
marci@777
  1392
//     Node bNode(const InEdgeIt& e) const { 
marci@777
  1393
//       return Node(this->graph->bNode(e.e)); }
marci@556
  1394
marci@777
  1395
    void erase(const Edge& e) const {
marci@777
  1396
      Node n=tail(e);
deba@844
  1397
      typename Graph::OutEdgeIt f(*Parent::graph, n);
marci@777
  1398
      ++f;
marci@777
  1399
      first_out_edges->set(n, f);
marci@556
  1400
    }
marci@556
  1401
  };
marci@556
  1402
marci@556
  1403
  ///@}
marci@556
  1404
marci@556
  1405
} //namespace hugo
marci@556
  1406
marci@556
  1407
#endif //HUGO_GRAPH_WRAPPER_H
marci@556
  1408