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