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