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