src/lemon/graph_wrapper.h
author marci
Fri, 01 Oct 2004 10:08:43 +0000
changeset 932 ade3cdb9b45d
parent 930 e89f3bd26fd4
child 933 1b7c88fbb950
permissions -rw-r--r--
New EdgeSubGraphWrapper class specializing SubGraphWrapper in the way that only the edge-set can be filtered.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/graph_wrapper.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_GRAPH_WRAPPER_H
alpar@921
    18
#define LEMON_GRAPH_WRAPPER_H
marci@556
    19
marci@556
    20
///\ingroup gwrappers
marci@556
    21
///\file
marci@556
    22
///\brief Several graph wrappers.
marci@556
    23
///
marci@556
    24
///This file contains several useful graph wrapper functions.
marci@556
    25
///
marci@556
    26
///\author Marton Makai
marci@556
    27
alpar@921
    28
#include <lemon/invalid.h>
alpar@921
    29
#include <lemon/maps.h>
alpar@921
    30
#include <lemon/map_defines.h>
alpar@774
    31
#include <iostream>
marci@556
    32
alpar@921
    33
namespace lemon {
marci@556
    34
marci@556
    35
  // Graph wrappers
marci@556
    36
marci@556
    37
  /// \addtogroup gwrappers
marci@923
    38
  /// The main parts of LEMON are the different graph structures, 
marci@556
    39
  /// generic graph algorithms, graph concepts which couple these, and 
marci@556
    40
  /// graph wrappers. While the previous ones are more or less clear, the 
marci@556
    41
  /// latter notion needs further explanation.
marci@556
    42
  /// Graph wrappers are graph classes which serve for considering graph 
marci@556
    43
  /// structures in different ways. A short example makes the notion much 
marci@556
    44
  /// clearer. 
marci@556
    45
  /// Suppose that we have an instance \c g of a directed graph
marci@556
    46
  /// type say \c ListGraph and an algorithm 
marci@556
    47
  /// \code template<typename Graph> int algorithm(const Graph&); \endcode 
marci@556
    48
  /// is needed to run on the reversely oriented graph. 
marci@556
    49
  /// It may be expensive (in time or in memory usage) to copy 
marci@556
    50
  /// \c g with the reverse orientation. 
marci@556
    51
  /// Thus, a wrapper class
marci@556
    52
  /// \code template<typename Graph> class RevGraphWrapper; \endcode is used. 
marci@556
    53
  /// The code looks as follows
marci@556
    54
  /// \code
marci@556
    55
  /// ListGraph g;
marci@556
    56
  /// RevGraphWrapper<ListGraph> rgw(g);
marci@556
    57
  /// int result=algorithm(rgw);
marci@556
    58
  /// \endcode
marci@556
    59
  /// After running the algorithm, the original graph \c g 
marci@556
    60
  /// remains untouched. Thus the graph wrapper used above is to consider the 
marci@556
    61
  /// original graph with reverse orientation. 
marci@556
    62
  /// This techniques gives rise to an elegant code, and 
marci@556
    63
  /// based on stable graph wrappers, complex algorithms can be 
marci@556
    64
  /// implemented easily. 
marci@556
    65
  /// In flow, circulation and bipartite matching problems, the residual 
marci@556
    66
  /// graph is of particular importance. Combining a wrapper implementing 
marci@556
    67
  /// this, shortest path algorithms and minimum mean cycle algorithms, 
marci@556
    68
  /// a range of weighted and cardinality optimization algorithms can be 
marci@556
    69
  /// obtained. For lack of space, for other examples, 
marci@556
    70
  /// the interested user is referred to the detailed documentation of graph 
marci@556
    71
  /// wrappers. 
marci@556
    72
  /// The behavior of graph wrappers can be very different. Some of them keep 
marci@556
    73
  /// capabilities of the original graph while in other cases this would be 
marci@556
    74
  /// meaningless. This means that the concepts that they are a model of depend 
marci@556
    75
  /// on the graph wrapper, and the wrapped graph(s). 
marci@556
    76
  /// If an edge of \c rgw is deleted, this is carried out by 
marci@556
    77
  /// deleting the corresponding edge of \c g. But for a residual 
marci@556
    78
  /// graph, this operation has no sense. 
marci@556
    79
  /// Let we stand one more example here to simplify your work. 
marci@556
    80
  /// wrapper class
marci@556
    81
  /// \code template<typename Graph> class RevGraphWrapper; \endcode 
marci@556
    82
  /// has constructor 
marci@556
    83
  /// <tt> RevGraphWrapper(Graph& _g)</tt>. 
marci@556
    84
  /// This means that in a situation, 
marci@556
    85
  /// when a <tt> const ListGraph& </tt> reference to a graph is given, 
marci@556
    86
  /// then it have to be instantiated with <tt>Graph=const ListGraph</tt>.
marci@556
    87
  /// \code
marci@556
    88
  /// int algorithm1(const ListGraph& g) {
marci@556
    89
  ///   RevGraphWrapper<const ListGraph> rgw(g);
marci@556
    90
  ///   return algorithm2(rgw);
marci@556
    91
  /// }
marci@556
    92
  /// \endcode
marci@556
    93
marci@556
    94
  /// \addtogroup gwrappers
marci@556
    95
  /// @{
marci@556
    96
marci@556
    97
  ///Base type for the Graph Wrappers
marci@556
    98
alpar@879
    99
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
   100
  ///parts of the lib. Use them at you own risk.
alpar@879
   101
  ///
marci@923
   102
  /// This is the base type for most of LEMON graph wrappers. 
marci@923
   103
  /// This class implements a trivial graph wrapper i.e. it only wraps the 
marci@923
   104
  /// functions and types of the graph. The purpose of this class is to 
marci@923
   105
  /// make easier implementing graph wrappers. E.g. if a wrapper is 
marci@923
   106
  /// considered which differs from the wrapped graph only in some of its 
marci@923
   107
  /// functions or types, then it can be derived from GraphWrapper, and only the 
marci@923
   108
  /// differences should be implemented.
marci@556
   109
  ///
marci@612
   110
  ///\author Marton Makai 
marci@556
   111
  template<typename Graph>
marci@556
   112
  class GraphWrapper {
marci@556
   113
  protected:
marci@556
   114
    Graph* graph;
marci@556
   115
    GraphWrapper() : graph(0) { }
marci@556
   116
    void setGraph(Graph& _graph) { graph=&_graph; }
marci@556
   117
marci@556
   118
  public:
marci@556
   119
    typedef Graph BaseGraph;
marci@556
   120
    typedef Graph ParentGraph;
marci@556
   121
marci@556
   122
    GraphWrapper(Graph& _graph) : graph(&_graph) { }
alpar@774
   123
    GraphWrapper(const GraphWrapper<Graph>& gw) : graph(gw.graph) { }
marci@556
   124
 
alpar@774
   125
    typedef typename Graph::Node Node;
alpar@774
   126
    class NodeIt : public Node { 
alpar@774
   127
      const GraphWrapper<Graph>* gw;
marci@556
   128
      friend class GraphWrapper<Graph>;
marci@556
   129
     public:
marci@556
   130
      NodeIt() { }
alpar@774
   131
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   132
      NodeIt(const GraphWrapper<Graph>& _gw) : 
alpar@774
   133
	Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { }
alpar@774
   134
      NodeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   135
	Node(n), gw(&_gw) { }
alpar@774
   136
      NodeIt& operator++() { 
alpar@774
   137
	*(static_cast<Node*>(this))=
alpar@774
   138
	  ++(typename Graph::NodeIt(*(gw->graph), *this));
alpar@774
   139
	return *this; 
alpar@774
   140
      }
marci@556
   141
    };
alpar@774
   142
    typedef typename Graph::Edge Edge;
alpar@774
   143
    class OutEdgeIt : public Edge { 
alpar@774
   144
      const GraphWrapper<Graph>* gw;
marci@556
   145
      friend class GraphWrapper<Graph>;
alpar@774
   146
     public:
alpar@774
   147
      OutEdgeIt() { }
alpar@774
   148
      OutEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   149
      OutEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   150
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   151
      OutEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   152
	Edge(e), gw(&_gw) { }
alpar@774
   153
      OutEdgeIt& operator++() { 
alpar@774
   154
	*(static_cast<Edge*>(this))=
alpar@774
   155
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   156
	return *this; 
alpar@774
   157
      }
marci@556
   158
    };
alpar@774
   159
    class InEdgeIt : public Edge { 
alpar@774
   160
      const GraphWrapper<Graph>* gw;
marci@556
   161
      friend class GraphWrapper<Graph>;
alpar@774
   162
     public:
marci@556
   163
      InEdgeIt() { }
alpar@774
   164
      InEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   165
      InEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   166
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   167
      InEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   168
	Edge(e), gw(&_gw) { }
alpar@774
   169
      InEdgeIt& operator++() { 
alpar@774
   170
	*(static_cast<Edge*>(this))=
alpar@774
   171
	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   172
	return *this; 
alpar@774
   173
      }
marci@556
   174
    };
alpar@774
   175
    class EdgeIt : public Edge { 
alpar@774
   176
      const GraphWrapper<Graph>* gw;
marci@556
   177
      friend class GraphWrapper<Graph>;
alpar@774
   178
     public:
marci@556
   179
      EdgeIt() { }
alpar@774
   180
      EdgeIt(Invalid i) : Edge(i) { }
alpar@774
   181
      EdgeIt(const GraphWrapper<Graph>& _gw) : 
alpar@774
   182
	Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { }
alpar@774
   183
      EdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) : 
marci@777
   184
	Edge(e), gw(&_gw) { }
alpar@774
   185
      EdgeIt& operator++() { 
alpar@774
   186
	*(static_cast<Edge*>(this))=
alpar@774
   187
	  ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
   188
	return *this; 
alpar@774
   189
      }
marci@556
   190
    };
marci@556
   191
   
marci@556
   192
    NodeIt& first(NodeIt& i) const { 
marci@556
   193
      i=NodeIt(*this); return i;
marci@556
   194
    }
marci@556
   195
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   196
      i=OutEdgeIt(*this, p); return i;
marci@556
   197
    }
marci@556
   198
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   199
      i=InEdgeIt(*this, p); return i;
marci@556
   200
    }
marci@556
   201
    EdgeIt& first(EdgeIt& i) const { 
marci@556
   202
      i=EdgeIt(*this); return i;
marci@556
   203
    }
marci@556
   204
marci@556
   205
    Node tail(const Edge& e) const { 
marci@556
   206
      return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
marci@556
   207
    Node head(const Edge& e) const { 
marci@556
   208
      return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
marci@556
   209
marci@556
   210
    int nodeNum() const { return graph->nodeNum(); }
marci@556
   211
    int edgeNum() const { return graph->edgeNum(); }
marci@556
   212
  
marci@556
   213
    Node addNode() const { return Node(graph->addNode()); }
marci@556
   214
    Edge addEdge(const Node& tail, const Node& head) const { 
marci@556
   215
      return Edge(graph->addEdge(tail, head)); }
marci@556
   216
marci@556
   217
    void erase(const Node& i) const { graph->erase(i); }
marci@556
   218
    void erase(const Edge& i) const { graph->erase(i); }
marci@556
   219
  
marci@556
   220
    void clear() const { graph->clear(); }
marci@556
   221
    
alpar@736
   222
    bool forward(const Edge& e) const { return graph->forward(e); }
alpar@736
   223
    bool backward(const Edge& e) const { return graph->backward(e); }
marci@739
   224
marci@739
   225
    int id(const Node& v) const { return graph->id(v); }
marci@739
   226
    int id(const Edge& e) const { return graph->id(e); }
marci@650
   227
    
marci@738
   228
    Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); }
marci@650
   229
marci@556
   230
deba@877
   231
    IMPORT_NODE_MAP(Graph, *(gw.graph), GraphWrapper, gw);    
deba@877
   232
    IMPORT_EDGE_MAP(Graph, *(gw.graph), GraphWrapper, gw);
deba@877
   233
    
deba@877
   234
marci@556
   235
  };
marci@556
   236
marci@569
   237
marci@569
   238
marci@556
   239
  /// A graph wrapper which reverses the orientation of the edges.
marci@556
   240
alpar@879
   241
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
   242
  ///parts of the lib. Use them at you own risk.
alpar@879
   243
  ///
marci@923
   244
  /// Let \f$G=(V, A)\f$ be a directed graph and 
marci@923
   245
  /// suppose that a graph instange \c g of type 
marci@923
   246
  /// \c ListGraph implements \f$G\f$.
marci@923
   247
  /// \code
marci@923
   248
  /// ListGraph g;
marci@923
   249
  /// \endcode
marci@923
   250
  /// For each directed edge 
marci@923
   251
  /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by 
marci@923
   252
  /// reversing its orientation. 
marci@923
   253
  /// Then RevGraphWrapper implements the graph structure with node-set 
marci@923
   254
  /// \f$V\f$ and edge-set 
marci@923
   255
  /// \f$\{\bar e : e\in A \}\f$, i.e. the graph obtained from \f$G\f$ be 
marci@923
   256
  /// reversing the orientation of its edges. The following code shows how 
marci@923
   257
  /// such an instance can be constructed.
marci@923
   258
  /// \code
marci@923
   259
  /// RevGraphWrapper<ListGraph> gw(g);
marci@923
   260
  /// \endcode
marci@556
   261
  ///\author Marton Makai
marci@556
   262
  template<typename Graph>
marci@556
   263
  class RevGraphWrapper : public GraphWrapper<Graph> {
marci@650
   264
  public:
marci@650
   265
    typedef GraphWrapper<Graph> Parent; 
marci@556
   266
  protected:
marci@612
   267
    RevGraphWrapper() : GraphWrapper<Graph>() { }
marci@556
   268
  public:
marci@556
   269
    RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }  
alpar@774
   270
    RevGraphWrapper(const RevGraphWrapper<Graph>& gw) : Parent(gw) { }
marci@556
   271
marci@556
   272
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
   273
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@792
   274
    //remark: OutEdgeIt and InEdgeIt cannot be typedef-ed to each other
marci@792
   275
    //because this does not work is some of them are not defined in the 
marci@792
   276
    //original graph. The problem with this is that typedef-ed stuff 
marci@792
   277
    //are instantiated in c++.
alpar@774
   278
    class OutEdgeIt : public Edge { 
alpar@774
   279
      const RevGraphWrapper<Graph>* gw;
marci@556
   280
      friend class GraphWrapper<Graph>;
alpar@774
   281
     public:
marci@556
   282
      OutEdgeIt() { }
alpar@774
   283
      OutEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   284
      OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   285
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   286
      OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   287
	Edge(e), gw(&_gw) { }
alpar@774
   288
      OutEdgeIt& operator++() { 
alpar@774
   289
	*(static_cast<Edge*>(this))=
alpar@774
   290
	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   291
	return *this; 
alpar@774
   292
      }
marci@556
   293
    };
alpar@774
   294
    class InEdgeIt : public Edge { 
alpar@774
   295
      const RevGraphWrapper<Graph>* gw;
marci@556
   296
      friend class GraphWrapper<Graph>;
alpar@774
   297
     public:
marci@556
   298
      InEdgeIt() { }
alpar@774
   299
      InEdgeIt(Invalid i) : Edge(i) { }
alpar@774
   300
      InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) : 
alpar@774
   301
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
alpar@774
   302
      InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) : 
alpar@774
   303
	Edge(e), gw(&_gw) { }
alpar@774
   304
      InEdgeIt& operator++() { 
alpar@774
   305
	*(static_cast<Edge*>(this))=
alpar@774
   306
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   307
	return *this; 
alpar@774
   308
      }
marci@556
   309
    };
marci@556
   310
marci@556
   311
    using GraphWrapper<Graph>::first;
marci@556
   312
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   313
      i=OutEdgeIt(*this, p); return i;
marci@556
   314
    }
marci@556
   315
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   316
      i=InEdgeIt(*this, p); return i;
marci@556
   317
    }
marci@556
   318
marci@556
   319
    Node tail(const Edge& e) const { 
marci@556
   320
      return GraphWrapper<Graph>::head(e); }
marci@556
   321
    Node head(const Edge& e) const { 
marci@556
   322
      return GraphWrapper<Graph>::tail(e); }
marci@556
   323
deba@891
   324
    //    KEEP_MAPS(Parent, RevGraphWrapper);
deba@877
   325
marci@556
   326
  };
marci@556
   327
marci@775
   328
marci@775
   329
marci@930
   330
  /*! \brief A graph wrapper for hiding nodes and edges from a graph.
marci@556
   331
  
marci@930
   332
  \warning Graph wrappers are in even more experimental state than the other
marci@930
   333
  parts of the lib. Use them at you own risk.
marci@930
   334
  
marci@930
   335
  This wrapper shows a graph with filtered node-set and 
marci@930
   336
  edge-set. 
marci@930
   337
  Given a bool-valued map on the node-set and one on 
marci@930
   338
  the edge-set of the graph, the iterators show only the objects 
marci@930
   339
  having true value. We have to note that this does not mean that an 
marci@930
   340
  induced subgraph is obtained, the node-iterator cares only the filter 
marci@930
   341
  on the node-set, and the edge-iterators care only the filter on the 
marci@930
   342
  edge-set.
marci@930
   343
  \code
marci@930
   344
  typedef SmartGraph Graph;
marci@930
   345
  Graph g;
marci@930
   346
  typedef Graph::Node Node;
marci@930
   347
  typedef Graph::Edge Edge;
marci@930
   348
  Node u=g.addNode(); //node of id 0
marci@930
   349
  Node v=g.addNode(); //node of id 1
marci@930
   350
  Node e=g.addEdge(u, v); //edge of id 0
marci@930
   351
  Node f=g.addEdge(v, u); //edge of id 1
marci@930
   352
  Graph::NodeMap<bool> nm(g, true);
marci@930
   353
  nm.set(u, false);
marci@930
   354
  Graph::EdgeMap<bool> em(g, true);
marci@930
   355
  em.set(e, false);
marci@930
   356
  typedef SubGraphWrapper<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGW;
marci@930
   357
  SubGW gw(g, nm, em);
marci@930
   358
  for (SubGW::NodeIt n(gw); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
marci@930
   359
  std::cout << ":-)" << std::endl;
marci@930
   360
  for (SubGW::EdgeIt e(gw); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
marci@930
   361
  \endcode
marci@930
   362
  The output of the above code is the following.
marci@930
   363
  \code
marci@930
   364
  1
marci@930
   365
  :-)
marci@930
   366
  1
marci@930
   367
  \endcode
marci@930
   368
  Note that \c n is of type \c SubGW::NodeIt, but it can be converted to
marci@930
   369
  \c Graph::Node that is why \c g.id(n) can be applied.
marci@930
   370
marci@930
   371
  Consider now a mathematically more invloved problem, where the application 
marci@930
   372
  of SubGraphWrapper is reasonable sure enough. If a shortest path is to be 
marci@930
   373
  searched between two nodes \c s and \c t, then this can be done easily by 
marci@930
   374
  applying the Dijkstra algorithm class. What happens, if a maximum number of 
marci@930
   375
  edge-disjoint shortest paths is to be computed. It can be proved that an 
marci@930
   376
  edge can be in a shortest path if and only if it is tight with respect to 
marci@930
   377
  the potential function computed by Dijkstra. Moreover, any path containing 
marci@930
   378
  only such edges is a shortest one. Thus we have to compute a maximum number 
marci@930
   379
  of edge-disjoint path between \c s and \c t in the graph which has edge-set 
marci@930
   380
  all the tight edges. The computation will be demonstrated on the following 
marci@930
   381
  graph, which is read from a dimacs file.
marci@930
   382
  
marci@930
   383
  \dot
marci@930
   384
  digraph lemon_dot_example {
marci@930
   385
  node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
marci@930
   386
  n0 [ label="0 (s)" ];
marci@930
   387
  n1 [ label="1" ];
marci@930
   388
  n2 [ label="2" ];
marci@930
   389
  n3 [ label="3" ];
marci@930
   390
  n4 [ label="4" ];
marci@930
   391
  n5 [ label="5" ];
marci@930
   392
  n6 [ label="6 (t)" ];
marci@930
   393
  edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
marci@930
   394
  n5 ->  n6 [ label="9, length:4" ];
marci@930
   395
  n4 ->  n6 [ label="8, length:2" ];
marci@930
   396
  n3 ->  n5 [ label="7, length:1" ];
marci@930
   397
  n2 ->  n5 [ label="6, length:3" ];
marci@930
   398
  n2 ->  n6 [ label="5, length:5" ];
marci@930
   399
  n2 ->  n4 [ label="4, length:2" ];
marci@930
   400
  n1 ->  n4 [ label="3, length:3" ];
marci@930
   401
  n0 ->  n3 [ label="2, length:1" ];
marci@930
   402
  n0 ->  n2 [ label="1, length:2" ];
marci@930
   403
  n0 ->  n1 [ label="0, length:3" ];
marci@930
   404
  }
marci@930
   405
  \enddot
marci@930
   406
marci@930
   407
  \code
marci@930
   408
  Graph g;
marci@930
   409
  Node s, t;
marci@930
   410
  LengthMap length(g);
marci@930
   411
marci@930
   412
  readDimacs(std::cin, g, length, s, t);
marci@930
   413
marci@930
   414
  cout << "edges with lengths (of form id, tail--length->head): " << endl;
marci@930
   415
  for(EdgeIt e(g); e!=INVALID; ++e) 
marci@930
   416
    cout << g.id(e) << ", " << g.id(g.tail(e)) << "--" 
marci@930
   417
         << length[e] << "->" << g.id(g.head(e)) << endl;
marci@930
   418
marci@930
   419
  cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
marci@930
   420
  \endcode
marci@930
   421
  Next, the potential function is computed with Dijkstra.
marci@930
   422
  \code
marci@930
   423
  typedef Dijkstra<Graph, LengthMap> Dijkstra;
marci@930
   424
  Dijkstra dijkstra(g, length);
marci@930
   425
  dijkstra.run(s);
marci@930
   426
  \endcode
marci@930
   427
  Next, we consrtruct a map which filters the edge-set to the tight edges.
marci@930
   428
  \code
marci@930
   429
  typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap> 
marci@930
   430
    TightEdgeFilter;
marci@930
   431
  TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
marci@930
   432
  
marci@932
   433
  typedef EdgeSubGraphWrapper<Graph, TightEdgeFilter> SubGW;
marci@932
   434
  SubGW gw(g, tight_edge_filter);
marci@930
   435
  \endcode
marci@930
   436
  Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed 
marci@930
   437
  with a max flow algorithm Preflow.
marci@930
   438
  \code
marci@930
   439
  ConstMap<Edge, int> const_1_map(1);
marci@930
   440
  Graph::EdgeMap<int> flow(g, 0);
marci@930
   441
marci@930
   442
  Preflow<SubGW, int, ConstMap<Edge, int>, Graph::EdgeMap<int> > 
marci@930
   443
    preflow(gw, s, t, const_1_map, flow);
marci@930
   444
  preflow.run();
marci@930
   445
  \endcode
marci@930
   446
  Last, the output is:
marci@930
   447
  \code  
marci@930
   448
  cout << "maximum number of edge-disjoint shortest path: " 
marci@930
   449
       << preflow.flowValue() << endl;
marci@930
   450
  cout << "edges of the maximum number of edge-disjoint shortest s-t paths: " 
marci@930
   451
       << endl;
marci@930
   452
  for(EdgeIt e(g); e!=INVALID; ++e) 
marci@930
   453
    if (flow[e])
marci@930
   454
      cout << " " << g.id(g.tail(e)) << "--" 
marci@930
   455
	   << length[e] << "->" << g.id(g.head(e)) << endl;
marci@930
   456
  \endcode
marci@930
   457
  The program has the following (expected :-)) output:
marci@930
   458
  \code
marci@930
   459
  edges with lengths (of form id, tail--length->head):
marci@930
   460
   9, 5--4->6
marci@930
   461
   8, 4--2->6
marci@930
   462
   7, 3--1->5
marci@930
   463
   6, 2--3->5
marci@930
   464
   5, 2--5->6
marci@930
   465
   4, 2--2->4
marci@930
   466
   3, 1--3->4
marci@930
   467
   2, 0--1->3
marci@930
   468
   1, 0--2->2
marci@930
   469
   0, 0--3->1
marci@930
   470
  s: 0 t: 6
marci@930
   471
  maximum number of edge-disjoint shortest path: 2
marci@930
   472
  edges of the maximum number of edge-disjoint shortest s-t paths:
marci@930
   473
   9, 5--4->6
marci@930
   474
   8, 4--2->6
marci@930
   475
   7, 3--1->5
marci@930
   476
   4, 2--2->4
marci@930
   477
   2, 0--1->3
marci@930
   478
   1, 0--2->2
marci@930
   479
  \endcode
marci@930
   480
  \author Marton Makai
marci@930
   481
  */
marci@556
   482
  template<typename Graph, typename NodeFilterMap, 
marci@556
   483
	   typename EdgeFilterMap>
marci@556
   484
  class SubGraphWrapper : public GraphWrapper<Graph> {
marci@650
   485
  public:
marci@650
   486
    typedef GraphWrapper<Graph> Parent;
marci@556
   487
  protected:
marci@556
   488
    NodeFilterMap* node_filter_map;
marci@556
   489
    EdgeFilterMap* edge_filter_map;
marci@556
   490
marci@612
   491
    SubGraphWrapper() : GraphWrapper<Graph>(), 
marci@556
   492
			node_filter_map(0), edge_filter_map(0) { }
marci@556
   493
    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
marci@556
   494
      node_filter_map=&_node_filter_map;
marci@556
   495
    }
marci@556
   496
    void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
marci@556
   497
      edge_filter_map=&_edge_filter_map;
marci@556
   498
    }
marci@556
   499
    
marci@556
   500
  public:
marci@556
   501
    SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map, 
marci@556
   502
		    EdgeFilterMap& _edge_filter_map) : 
marci@556
   503
      GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map), 
marci@556
   504
      edge_filter_map(&_edge_filter_map) { }  
marci@556
   505
marci@556
   506
    typedef typename GraphWrapper<Graph>::Node Node;
marci@775
   507
    class NodeIt : public Node { 
marci@775
   508
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   509
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@775
   510
    public:
marci@556
   511
      NodeIt() { }
marci@775
   512
      NodeIt(Invalid i) : Node(i) { }
marci@775
   513
      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) : 
marci@854
   514
	Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { 
marci@854
   515
	while (*static_cast<Node*>(this)!=INVALID && 
marci@861
   516
	       !(*(gw->node_filter_map))[*this]) 
marci@854
   517
	  *(static_cast<Node*>(this))=
marci@854
   518
	    ++(typename Graph::NodeIt(*(gw->graph), *this));
marci@854
   519
      }
marci@775
   520
      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   521
	     const Node& n) : 
marci@775
   522
	Node(n), gw(&_gw) { }
marci@775
   523
      NodeIt& operator++() { 
marci@775
   524
	*(static_cast<Node*>(this))=
marci@775
   525
	  ++(typename Graph::NodeIt(*(gw->graph), *this));
marci@775
   526
	while (*static_cast<Node*>(this)!=INVALID && 
marci@775
   527
	       !(*(gw->node_filter_map))[*this]) 
marci@775
   528
	  *(static_cast<Node*>(this))=
marci@775
   529
	    ++(typename Graph::NodeIt(*(gw->graph), *this));
marci@775
   530
	return *this; 
marci@556
   531
      }
marci@556
   532
    };
marci@556
   533
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@775
   534
    class OutEdgeIt : public Edge { 
marci@775
   535
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   536
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   537
    public:
marci@556
   538
      OutEdgeIt() { }
marci@775
   539
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@775
   540
      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) : 
marci@854
   541
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { 
marci@854
   542
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@854
   543
	       !(*(gw->edge_filter_map))[*this]) 
marci@854
   544
	  *(static_cast<Edge*>(this))=
marci@854
   545
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@854
   546
      }
marci@775
   547
      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   548
	     const Edge& e) : 
marci@775
   549
	Edge(e), gw(&_gw) { }
marci@775
   550
      OutEdgeIt& operator++() { 
marci@775
   551
	*(static_cast<Edge*>(this))=
marci@775
   552
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   553
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   554
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   555
	  *(static_cast<Edge*>(this))=
marci@775
   556
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   557
	return *this; 
marci@556
   558
      }
marci@556
   559
    };
marci@775
   560
    class InEdgeIt : public Edge { 
marci@775
   561
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   562
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   563
    public:
marci@556
   564
      InEdgeIt() { }
marci@775
   565
      //      InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
marci@775
   566
      InEdgeIt(Invalid i) : Edge(i) { }
marci@775
   567
      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) : 
marci@854
   568
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { 
marci@854
   569
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@854
   570
	       !(*(gw->edge_filter_map))[*this]) 
marci@854
   571
	  *(static_cast<Edge*>(this))=
marci@854
   572
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@854
   573
      }
marci@775
   574
      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   575
	     const Edge& e) : 
marci@775
   576
	Edge(e), gw(&_gw) { }
marci@775
   577
      InEdgeIt& operator++() { 
marci@775
   578
	*(static_cast<Edge*>(this))=
marci@775
   579
	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   580
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   581
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   582
	  *(static_cast<Edge*>(this))=
marci@775
   583
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   584
	return *this; 
marci@556
   585
      }
marci@556
   586
    };
marci@775
   587
    class EdgeIt : public Edge { 
marci@775
   588
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   589
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   590
    public:
marci@556
   591
      EdgeIt() { }
marci@775
   592
      EdgeIt(Invalid i) : Edge(i) { }
marci@775
   593
      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) : 
marci@854
   594
	Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { 
marci@854
   595
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@854
   596
	       !(*(gw->edge_filter_map))[*this]) 
marci@854
   597
	  *(static_cast<Edge*>(this))=
marci@854
   598
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@854
   599
      }
marci@775
   600
      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   601
	     const Edge& e) : 
marci@775
   602
	Edge(e), gw(&_gw) { }
marci@775
   603
      EdgeIt& operator++() { 
marci@775
   604
	*(static_cast<Edge*>(this))=
marci@775
   605
	  ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   606
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   607
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   608
	  *(static_cast<Edge*>(this))=
marci@775
   609
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   610
	return *this; 
marci@556
   611
      }
marci@556
   612
    };
marci@556
   613
marci@556
   614
    NodeIt& first(NodeIt& i) const { 
marci@556
   615
      i=NodeIt(*this); return i;
marci@556
   616
    }
marci@556
   617
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   618
      i=OutEdgeIt(*this, p); return i;
marci@556
   619
    }
marci@556
   620
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   621
      i=InEdgeIt(*this, p); return i;
marci@556
   622
    }
marci@556
   623
    EdgeIt& first(EdgeIt& i) const { 
marci@556
   624
      i=EdgeIt(*this); return i;
marci@556
   625
    }
marci@556
   626
    
marci@561
   627
    /// This function hides \c n in the graph, i.e. the iteration 
marci@561
   628
    /// jumps over it. This is done by simply setting the value of \c n  
marci@561
   629
    /// to be false in the corresponding node-map.
marci@556
   630
    void hide(const Node& n) const { node_filter_map->set(n, false); }
marci@561
   631
marci@561
   632
    /// This function hides \c e in the graph, i.e. the iteration 
marci@561
   633
    /// jumps over it. This is done by simply setting the value of \c e  
marci@561
   634
    /// to be false in the corresponding edge-map.
marci@556
   635
    void hide(const Edge& e) const { edge_filter_map->set(e, false); }
marci@556
   636
marci@561
   637
    /// The value of \c n is set to be true in the node-map which stores 
marci@561
   638
    /// hide information. If \c n was hidden previuosly, then it is shown 
marci@561
   639
    /// again
marci@561
   640
     void unHide(const Node& n) const { node_filter_map->set(n, true); }
marci@561
   641
marci@561
   642
    /// The value of \c e is set to be true in the edge-map which stores 
marci@561
   643
    /// hide information. If \c e was hidden previuosly, then it is shown 
marci@561
   644
    /// again
marci@556
   645
    void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
marci@556
   646
marci@561
   647
    /// Returns true if \c n is hidden.
marci@561
   648
    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
marci@561
   649
marci@561
   650
    /// Returns true if \c n is hidden.
marci@561
   651
    bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
marci@593
   652
marci@792
   653
    /// \warning This is a linear time operation and works only if 
marci@792
   654
    /// \c Graph::NodeIt is defined.
marci@593
   655
    int nodeNum() const { 
marci@593
   656
      int i=0;
marci@792
   657
      for (NodeIt n(*this); n!=INVALID; ++n) ++i;
marci@593
   658
      return i; 
marci@593
   659
    }
marci@593
   660
marci@792
   661
    /// \warning This is a linear time operation and works only if 
marci@792
   662
    /// \c Graph::EdgeIt is defined.
marci@593
   663
    int edgeNum() const { 
marci@593
   664
      int i=0;
marci@792
   665
      for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
marci@593
   666
      return i; 
marci@593
   667
    }
marci@593
   668
deba@891
   669
    //    KEEP_MAPS(Parent, SubGraphWrapper);
marci@556
   670
  };
marci@556
   671
marci@569
   672
marci@932
   673
  /*! \brief A wrapper for hiding edges from a graph.
marci@932
   674
marci@932
   675
  \warning Graph wrappers are in even more experimental state than the other
marci@932
   676
  parts of the lib. Use them at you own risk.
marci@932
   677
  
marci@932
   678
  A wrapper for hiding edges from a graph.
marci@932
   679
  This wrapper specializes SubGraphWrapper in the way that only the edge-set 
marci@932
   680
  can be filtered.
marci@932
   681
  \author Marton Makai
marci@932
   682
  */
marci@932
   683
  template<typename Graph, typename EdgeFilterMap>
marci@932
   684
  class EdgeSubGraphWrapper : 
marci@932
   685
    public SubGraphWrapper<Graph, ConstMap<typename Graph::Node,bool>, 
marci@932
   686
			   EdgeFilterMap> {
marci@932
   687
  public:
marci@932
   688
    typedef SubGraphWrapper<Graph, ConstMap<typename Graph::Node,bool>, 
marci@932
   689
			    EdgeFilterMap> Parent;
marci@932
   690
  protected:
marci@932
   691
    ConstMap<typename Graph::Node, bool> const_true_map;
marci@932
   692
  public:
marci@932
   693
    EdgeSubGraphWrapper(Graph& _graph, EdgeFilterMap& _edge_filter_map) : 
marci@932
   694
      Parent(), const_true_map(true) { 
marci@932
   695
      Parent::setGraph(_graph);
marci@932
   696
      Parent::setNodeFilterMap(const_true_map);
marci@932
   697
      Parent::setEdgeFilterMap(_edge_filter_map);
marci@932
   698
    }
marci@932
   699
  };
marci@932
   700
marci@569
   701
marci@556
   702
  template<typename Graph>
marci@556
   703
  class UndirGraphWrapper : public GraphWrapper<Graph> {
marci@650
   704
  public:
marci@650
   705
    typedef GraphWrapper<Graph> Parent; 
marci@556
   706
  protected:
marci@556
   707
    UndirGraphWrapper() : GraphWrapper<Graph>() { }
marci@556
   708
    
marci@556
   709
  public:
marci@556
   710
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
   711
    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@556
   712
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@556
   713
    typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
marci@556
   714
marci@556
   715
    UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }  
marci@556
   716
marci@556
   717
    class OutEdgeIt {
marci@556
   718
      friend class UndirGraphWrapper<Graph>;
marci@556
   719
      bool out_or_in; //true iff out
marci@556
   720
      typename Graph::OutEdgeIt out;
marci@556
   721
      typename Graph::InEdgeIt in;
marci@556
   722
    public:
marci@556
   723
      OutEdgeIt() { }
marci@556
   724
      OutEdgeIt(const Invalid& i) : Edge(i) { }
marci@556
   725
      OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
marci@556
   726
	out_or_in=true; _G.graph->first(out, _n);
marci@556
   727
	if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n);	}
marci@556
   728
      } 
marci@556
   729
      operator Edge() const { 
marci@556
   730
	if (out_or_in) return Edge(out); else return Edge(in); 
marci@556
   731
      }
marci@556
   732
    };
marci@556
   733
marci@556
   734
    typedef OutEdgeIt InEdgeIt; 
marci@556
   735
marci@556
   736
    using GraphWrapper<Graph>::first;
marci@556
   737
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   738
      i=OutEdgeIt(*this, p); return i;
marci@556
   739
    }
marci@556
   740
marci@556
   741
    using GraphWrapper<Graph>::next;
alpar@878
   742
marci@556
   743
    OutEdgeIt& next(OutEdgeIt& e) const {
marci@556
   744
      if (e.out_or_in) {
marci@556
   745
	typename Graph::Node n=this->graph->tail(e.out);
marci@556
   746
	this->graph->next(e.out);
marci@556
   747
	if (!this->graph->valid(e.out)) { 
marci@556
   748
	  e.out_or_in=false; this->graph->first(e.in, n); }
marci@556
   749
      } else {
marci@556
   750
	this->graph->next(e.in);
marci@556
   751
      }
marci@556
   752
      return e;
marci@556
   753
    }
marci@556
   754
marci@556
   755
    Node aNode(const OutEdgeIt& e) const { 
marci@556
   756
      if (e.out_or_in) return this->graph->tail(e); else 
marci@556
   757
	return this->graph->head(e); }
marci@556
   758
    Node bNode(const OutEdgeIt& e) const { 
marci@556
   759
      if (e.out_or_in) return this->graph->head(e); else 
marci@556
   760
	return this->graph->tail(e); }
deba@877
   761
deba@891
   762
    //    KEEP_MAPS(Parent, UndirGraphWrapper);
deba@877
   763
marci@556
   764
  };
marci@556
   765
  
marci@910
   766
//   /// \brief An undirected graph template.
marci@910
   767
//   ///
marci@910
   768
//   ///\warning Graph wrappers are in even more experimental state than the other
marci@910
   769
//   ///parts of the lib. Use them at your own risk.
marci@910
   770
//   ///
marci@910
   771
//   /// An undirected graph template.
marci@910
   772
//   /// This class works as an undirected graph and a directed graph of 
marci@910
   773
//   /// class \c Graph is used for the physical storage.
marci@910
   774
//   /// \ingroup graphs
marci@556
   775
  template<typename Graph>
marci@556
   776
  class UndirGraph : public UndirGraphWrapper<Graph> {
marci@556
   777
    typedef UndirGraphWrapper<Graph> Parent;
marci@556
   778
  protected:
marci@556
   779
    Graph gr;
marci@556
   780
  public:
marci@556
   781
    UndirGraph() : UndirGraphWrapper<Graph>() { 
marci@556
   782
      Parent::setGraph(gr); 
marci@556
   783
    }
deba@877
   784
deba@891
   785
    //    KEEP_MAPS(Parent, UndirGraph);
marci@556
   786
  };
marci@556
   787
marci@569
   788
marci@650
   789
marci@650
   790
  ///\brief A wrapper for composing a subgraph of a 
marci@792
   791
  /// bidirected graph made from a directed one. 
marci@612
   792
  ///
alpar@911
   793
  /// A wrapper for composing a subgraph of a 
alpar@911
   794
  /// bidirected graph made from a directed one. 
alpar@911
   795
  ///
alpar@879
   796
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
   797
  ///parts of the lib. Use them at you own risk.
alpar@879
   798
  ///
marci@923
   799
  /// Let \f$G=(V, A)\f$ be a directed graph and for each directed edge 
marci@923
   800
  /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by
marci@923
   801
  /// reversing its orientation. We are given moreover two bool valued 
marci@923
   802
  /// maps on the edge-set, 
marci@923
   803
  /// \f$forward\_filter\f$, and \f$backward\_filter\f$. 
marci@923
   804
  /// SubBidirGraphWrapper implements the graph structure with node-set 
marci@923
   805
  /// \f$V\f$ and edge-set 
marci@923
   806
  /// \f$\{e : e\in A \mbox{ and } forward\_filter(e) \mbox{ is true}\}+\{\bar e : e\in A \mbox{ and } backward\_filter(e) \mbox{ is true}\}\f$. 
marci@792
   807
  /// The purpose of writing + instead of union is because parallel 
marci@923
   808
  /// edges can arise. (Similarly, antiparallel edges also can arise).
marci@792
   809
  /// In other words, a subgraph of the bidirected graph obtained, which 
marci@792
   810
  /// is given by orienting the edges of the original graph in both directions.
marci@923
   811
  /// As the oppositely directed edges are logically different, 
marci@923
   812
  /// the maps are able to attach different values for them. 
marci@923
   813
  ///
marci@923
   814
  /// An example for such a construction is \c RevGraphWrapper where the 
marci@792
   815
  /// forward_filter is everywhere false and the backward_filter is 
marci@792
   816
  /// everywhere true. We note that for sake of efficiency, 
marci@792
   817
  /// \c RevGraphWrapper is implemented in a different way. 
marci@792
   818
  /// But BidirGraphWrapper is obtained from 
marci@792
   819
  /// SubBidirGraphWrapper by considering everywhere true 
marci@910
   820
  /// valued maps both for forward_filter and backward_filter. 
marci@792
   821
  /// Finally, one of the most important applications of SubBidirGraphWrapper 
marci@792
   822
  /// is ResGraphWrapper, which stands for the residual graph in directed 
marci@792
   823
  /// flow and circulation problems. 
marci@792
   824
  /// As wrappers usually, the SubBidirGraphWrapper implements the 
marci@792
   825
  /// above mentioned graph structure without its physical storage, 
marci@923
   826
  /// that is the whole stuff is stored in constant memory. 
marci@650
   827
  template<typename Graph, 
marci@650
   828
	   typename ForwardFilterMap, typename BackwardFilterMap>
marci@650
   829
  class SubBidirGraphWrapper : public GraphWrapper<Graph> {
marci@650
   830
  public:
marci@650
   831
    typedef GraphWrapper<Graph> Parent; 
marci@569
   832
  protected:
marci@650
   833
    ForwardFilterMap* forward_filter;
marci@650
   834
    BackwardFilterMap* backward_filter;
marci@650
   835
marci@792
   836
    SubBidirGraphWrapper() : GraphWrapper<Graph>() { }
marci@650
   837
    void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
marci@650
   838
      forward_filter=&_forward_filter;
marci@650
   839
    }
marci@650
   840
    void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
marci@650
   841
      backward_filter=&_backward_filter;
marci@650
   842
    }
marci@569
   843
marci@569
   844
  public:
marci@569
   845
marci@650
   846
    SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter, 
marci@650
   847
			 BackwardFilterMap& _backward_filter) : 
marci@650
   848
      GraphWrapper<Graph>(_graph), 
marci@650
   849
      forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
alpar@774
   850
    SubBidirGraphWrapper(const SubBidirGraphWrapper<Graph, 
alpar@774
   851
			 ForwardFilterMap, BackwardFilterMap>& gw) : 
alpar@774
   852
      Parent(gw), 
alpar@774
   853
      forward_filter(gw.forward_filter), 
alpar@774
   854
      backward_filter(gw.backward_filter) { }
marci@569
   855
marci@569
   856
    class Edge; 
marci@569
   857
    class OutEdgeIt; 
marci@569
   858
    friend class Edge; 
marci@569
   859
    friend class OutEdgeIt; 
marci@569
   860
marci@621
   861
    template<typename T> class EdgeMap;
marci@621
   862
marci@569
   863
    typedef typename GraphWrapper<Graph>::Node Node;
marci@621
   864
alpar@774
   865
    typedef typename Graph::Edge GraphEdge;
marci@792
   866
    /// SubBidirGraphWrapper<..., ..., ...>::Edge is inherited from 
marci@910
   867
    /// Graph::Edge. It contains an extra bool flag which is true 
marci@910
   868
    /// if and only if the 
marci@792
   869
    /// edge is the backward version of the original edge.
marci@569
   870
    class Edge : public Graph::Edge {
marci@650
   871
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   872
					ForwardFilterMap, BackwardFilterMap>;
marci@621
   873
      template<typename T> friend class EdgeMap;
marci@569
   874
    protected:
marci@569
   875
      bool backward; //true, iff backward
marci@569
   876
    public:
marci@569
   877
      Edge() { }
marci@792
   878
      /// \todo =false is needed, or causes problems?
marci@792
   879
      /// If \c _backward is false, then we get an edge corresponding to the 
marci@792
   880
      /// original one, otherwise its oppositely directed pair is obtained.
alpar@774
   881
      Edge(const typename Graph::Edge& e, bool _backward/*=false*/) : 
alpar@774
   882
	Graph::Edge(e), backward(_backward) { }
alpar@774
   883
      Edge(Invalid i) : Graph::Edge(i), backward(true) { }
alpar@774
   884
      bool operator==(const Edge& v) const { 
alpar@774
   885
	return (this->backward==v.backward && 
alpar@774
   886
		static_cast<typename Graph::Edge>(*this)==
marci@569
   887
		static_cast<typename Graph::Edge>(v));
marci@569
   888
      } 
alpar@774
   889
      bool operator!=(const Edge& v) const { 
alpar@774
   890
	return (this->backward!=v.backward || 
alpar@774
   891
		static_cast<typename Graph::Edge>(*this)!=
marci@569
   892
		static_cast<typename Graph::Edge>(v));
alpar@774
   893
      }
marci@569
   894
    };
marci@569
   895
alpar@774
   896
    class OutEdgeIt : public Edge {
marci@650
   897
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   898
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   899
    protected:
alpar@774
   900
      const SubBidirGraphWrapper<Graph, 
alpar@774
   901
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
   902
    public:
marci@569
   903
      OutEdgeIt() { }
alpar@774
   904
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@650
   905
      OutEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   906
		ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) : 
alpar@774
   907
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), false), gw(&_gw) { 
alpar@774
   908
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   909
	       !(*(gw->forward_filter))[*this]) 
alpar@774
   910
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   911
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   912
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   913
	  *static_cast<Edge*>(this)=
alpar@774
   914
	    Edge(typename Graph::InEdgeIt(*(_gw.graph), n), true);
marci@775
   915
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   916
		 !(*(gw->backward_filter))[*this]) 
marci@775
   917
	    *(static_cast<GraphEdge*>(this))=
marci@775
   918
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   919
	}
alpar@774
   920
      }
alpar@774
   921
      OutEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   922
		ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
   923
	Edge(e), gw(&_gw) { }
alpar@774
   924
      OutEdgeIt& operator++() { 
alpar@774
   925
	if (!this->backward) {
alpar@774
   926
	  Node n=gw->tail(*this);
alpar@774
   927
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   928
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   929
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   930
		 !(*(gw->forward_filter))[*this]) 
alpar@774
   931
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   932
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   933
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   934
	    *static_cast<Edge*>(this)=
alpar@774
   935
	      Edge(typename Graph::InEdgeIt(*(gw->graph), n), true);
marci@775
   936
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   937
		   !(*(gw->backward_filter))[*this]) 
marci@775
   938
	      *(static_cast<GraphEdge*>(this))=
marci@775
   939
		++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   940
	  }
alpar@774
   941
	} else {
alpar@774
   942
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   943
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   944
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   945
		 !(*(gw->backward_filter))[*this]) 
alpar@774
   946
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   947
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@569
   948
	}
alpar@774
   949
	return *this;
marci@569
   950
      }
marci@569
   951
    };
marci@569
   952
alpar@774
   953
    class InEdgeIt : public Edge {
marci@650
   954
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   955
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   956
    protected:
alpar@774
   957
      const SubBidirGraphWrapper<Graph, 
alpar@774
   958
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
   959
    public:
marci@569
   960
      InEdgeIt() { }
alpar@774
   961
      InEdgeIt(Invalid i) : Edge(i) { }
marci@650
   962
      InEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   963
	       ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) : 
alpar@774
   964
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n), false), gw(&_gw) { 
alpar@774
   965
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   966
	       !(*(gw->forward_filter))[*this]) 
alpar@774
   967
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   968
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   969
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   970
	  *static_cast<Edge*>(this)=
alpar@774
   971
	    Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), true);
marci@775
   972
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   973
		 !(*(gw->backward_filter))[*this]) 
marci@775
   974
	    *(static_cast<GraphEdge*>(this))=
marci@775
   975
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   976
	}
alpar@774
   977
      }
alpar@774
   978
      InEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   979
	       ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
   980
	Edge(e), gw(&_gw) { }
alpar@774
   981
      InEdgeIt& operator++() { 
alpar@774
   982
	if (!this->backward) {
marci@775
   983
	  Node n=gw->tail(*this);
alpar@774
   984
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   985
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   986
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   987
		 !(*(gw->forward_filter))[*this]) 
alpar@774
   988
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   989
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   990
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   991
	    *static_cast<Edge*>(this)=
alpar@774
   992
	      Edge(typename Graph::OutEdgeIt(*(gw->graph), n), true);
marci@775
   993
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   994
		   !(*(gw->backward_filter))[*this]) 
marci@775
   995
	      *(static_cast<GraphEdge*>(this))=
marci@775
   996
		++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   997
	  }
alpar@774
   998
	} else {
alpar@774
   999
	  *(static_cast<GraphEdge*>(this))=
alpar@774
  1000
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
  1001
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
  1002
		 !(*(gw->backward_filter))[*this]) 
alpar@774
  1003
	    *(static_cast<GraphEdge*>(this))=
alpar@774
  1004
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@569
  1005
	}
alpar@774
  1006
	return *this;
marci@569
  1007
      }
marci@569
  1008
    };
marci@569
  1009
alpar@774
  1010
    class EdgeIt : public Edge {
marci@650
  1011
      friend class SubBidirGraphWrapper<Graph, 
marci@650
  1012
					ForwardFilterMap, BackwardFilterMap>;
marci@569
  1013
    protected:
alpar@774
  1014
      const SubBidirGraphWrapper<Graph, 
alpar@774
  1015
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
  1016
    public:
marci@569
  1017
      EdgeIt() { }
alpar@774
  1018
      EdgeIt(Invalid i) : Edge(i) { }
marci@650
  1019
      EdgeIt(const SubBidirGraphWrapper<Graph, 
marci@775
  1020
	     ForwardFilterMap, BackwardFilterMap>& _gw) : 
marci@892
  1021
	Edge(typename Graph::EdgeIt(*(_gw.graph)), false), gw(&_gw) { 
alpar@774
  1022
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
  1023
	       !(*(gw->forward_filter))[*this]) 
alpar@774
  1024
	  *(static_cast<GraphEdge*>(this))=
alpar@774
  1025
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
  1026
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
  1027
	  *static_cast<Edge*>(this)=
alpar@774
  1028
	    Edge(typename Graph::EdgeIt(*(_gw.graph)), true);
marci@775
  1029
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
  1030
		 !(*(gw->backward_filter))[*this]) 
marci@775
  1031
	    *(static_cast<GraphEdge*>(this))=
marci@775
  1032
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
  1033
	}
alpar@774
  1034
      }
alpar@774
  1035
      EdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
  1036
	     ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
  1037
	Edge(e), gw(&_gw) { }
alpar@774
  1038
      EdgeIt& operator++() { 
alpar@774
  1039
	if (!this->backward) {
alpar@774
  1040
	  *(static_cast<GraphEdge*>(this))=
alpar@774
  1041
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
  1042
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
  1043
		 !(*(gw->forward_filter))[*this]) 
alpar@774
  1044
	    *(static_cast<GraphEdge*>(this))=
alpar@774
  1045
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
  1046
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
  1047
	    *static_cast<Edge*>(this)=
alpar@774
  1048
	      Edge(typename Graph::EdgeIt(*(gw->graph)), true);
marci@775
  1049
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
  1050
		   !(*(gw->backward_filter))[*this]) 
marci@775
  1051
	      *(static_cast<GraphEdge*>(this))=
marci@775
  1052
		++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
  1053
	  }
alpar@774
  1054
	} else {
alpar@774
  1055
	  *(static_cast<GraphEdge*>(this))=
alpar@774
  1056
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
  1057
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
  1058
		 !(*(gw->backward_filter))[*this]) 
alpar@774
  1059
	    *(static_cast<GraphEdge*>(this))=
alpar@774
  1060
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@569
  1061
	}
alpar@774
  1062
	return *this;
marci@569
  1063
      }
marci@569
  1064
    };
marci@569
  1065
marci@569
  1066
    using GraphWrapper<Graph>::first;
marci@569
  1067
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@569
  1068
      i=OutEdgeIt(*this, p); return i;
marci@569
  1069
    }
marci@569
  1070
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@569
  1071
      i=InEdgeIt(*this, p); return i;
marci@569
  1072
    }
marci@569
  1073
    EdgeIt& first(EdgeIt& i) const { 
marci@569
  1074
      i=EdgeIt(*this); return i;
marci@569
  1075
    }
marci@556
  1076
  
marci@569
  1077
marci@569
  1078
    Node tail(Edge e) const { 
marci@569
  1079
      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
marci@569
  1080
    Node head(Edge e) const { 
marci@569
  1081
      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
marci@569
  1082
marci@572
  1083
    /// Gives back the opposite edge.
marci@572
  1084
    Edge opposite(const Edge& e) const { 
marci@572
  1085
      Edge f=e;
marci@572
  1086
      f.backward=!f.backward;
marci@572
  1087
      return f;
marci@572
  1088
    }
marci@572
  1089
marci@792
  1090
    /// \warning This is a linear time operation and works only if 
marci@792
  1091
    /// \c Graph::EdgeIt is defined.
marci@792
  1092
    int edgeNum() const { 
marci@792
  1093
      int i=0;
marci@792
  1094
      for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
marci@792
  1095
      return i; 
marci@792
  1096
    }
marci@569
  1097
marci@569
  1098
    bool forward(const Edge& e) const { return !e.backward; }
marci@569
  1099
    bool backward(const Edge& e) const { return e.backward; }
marci@569
  1100
marci@569
  1101
marci@569
  1102
    template <typename T>
marci@792
  1103
    /// \c SubBidirGraphWrapper<..., ..., ...>::EdgeMap contains two 
marci@792
  1104
    /// Graph::EdgeMap one for the forward edges and 
marci@792
  1105
    /// one for the backward edges.
marci@569
  1106
    class EdgeMap {
deba@891
  1107
      template <typename TT> friend class EdgeMap;
marci@569
  1108
      typename Graph::template EdgeMap<T> forward_map, backward_map; 
marci@569
  1109
    public:
marci@623
  1110
      typedef T ValueType;
marci@623
  1111
      typedef Edge KeyType;
deba@891
  1112
marci@650
  1113
      EdgeMap(const SubBidirGraphWrapper<Graph, 
alpar@774
  1114
	      ForwardFilterMap, BackwardFilterMap>& g) : 
alpar@774
  1115
	forward_map(*(g.graph)), backward_map(*(g.graph)) { }
deba@891
  1116
marci@650
  1117
      EdgeMap(const SubBidirGraphWrapper<Graph, 
alpar@774
  1118
	      ForwardFilterMap, BackwardFilterMap>& g, T a) : 
alpar@774
  1119
	forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
deba@891
  1120
deba@891
  1121
      template <typename TT>
deba@891
  1122
      EdgeMap(const EdgeMap<TT>& copy) 
deba@891
  1123
	: forward_map(copy.forward_map), backward_map(copy.backward_map) {}
deba@891
  1124
deba@891
  1125
      template <typename TT>
deba@891
  1126
      EdgeMap& operator=(const EdgeMap<TT>& copy) {
deba@891
  1127
	forward_map = copy.forward_map;
deba@891
  1128
	backward_map = copy.backward_map;
deba@891
  1129
	return *this;
deba@891
  1130
      }
deba@891
  1131
      
marci@569
  1132
      void set(Edge e, T a) { 
marci@569
  1133
	if (!e.backward) 
marci@792
  1134
	  forward_map.set(e, a); 
marci@569
  1135
	else 
marci@792
  1136
	  backward_map.set(e, a); 
marci@569
  1137
      }
deba@891
  1138
deba@891
  1139
      typename Graph::template EdgeMap<T>::ConstReferenceType 
deba@891
  1140
      operator[](Edge e) const { 
marci@569
  1141
	if (!e.backward) 
marci@792
  1142
	  return forward_map[e]; 
marci@569
  1143
	else 
marci@792
  1144
	  return backward_map[e]; 
marci@569
  1145
      }
deba@891
  1146
deba@891
  1147
      typename Graph::template EdgeMap<T>::ReferenceType 
deba@891
  1148
      operator[](Edge e) { 
deba@891
  1149
	if (!e.backward) 
deba@891
  1150
	  return forward_map[e]; 
deba@891
  1151
	else 
deba@891
  1152
	  return backward_map[e]; 
deba@891
  1153
      }
deba@891
  1154
marci@625
  1155
      void update() { 
marci@625
  1156
	forward_map.update(); 
marci@625
  1157
	backward_map.update();
marci@625
  1158
      }
marci@569
  1159
    };
deba@877
  1160
deba@877
  1161
deba@891
  1162
    //    KEEP_NODE_MAP(Parent, SubBidirGraphWrapper);
deba@877
  1163
marci@569
  1164
  };
marci@569
  1165
marci@650
  1166
marci@650
  1167
  ///\brief A wrapper for composing bidirected graph from a directed one. 
marci@650
  1168
  ///
alpar@879
  1169
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
  1170
  ///parts of the lib. Use them at you own risk.
alpar@879
  1171
  ///
marci@650
  1172
  /// A wrapper for composing bidirected graph from a directed one. 
marci@650
  1173
  /// A bidirected graph is composed over the directed one without physical 
marci@650
  1174
  /// storage. As the oppositely directed edges are logically different ones 
marci@650
  1175
  /// the maps are able to attach different values for them.
marci@650
  1176
  template<typename Graph>
marci@650
  1177
  class BidirGraphWrapper : 
marci@650
  1178
    public SubBidirGraphWrapper<
marci@650
  1179
    Graph, 
marci@650
  1180
    ConstMap<typename Graph::Edge, bool>, 
marci@650
  1181
    ConstMap<typename Graph::Edge, bool> > {
marci@650
  1182
  public:
marci@650
  1183
    typedef  SubBidirGraphWrapper<
marci@650
  1184
      Graph, 
marci@650
  1185
      ConstMap<typename Graph::Edge, bool>, 
marci@650
  1186
      ConstMap<typename Graph::Edge, bool> > Parent; 
marci@650
  1187
  protected:
marci@650
  1188
    ConstMap<typename Graph::Edge, bool> cm;
marci@650
  1189
marci@655
  1190
    BidirGraphWrapper() : Parent(), cm(true) { 
marci@655
  1191
      Parent::setForwardFilterMap(cm);
marci@655
  1192
      Parent::setBackwardFilterMap(cm);
marci@655
  1193
    }
marci@650
  1194
  public:
marci@650
  1195
    BidirGraphWrapper(Graph& _graph) : Parent() { 
marci@650
  1196
      Parent::setGraph(_graph);
marci@650
  1197
      Parent::setForwardFilterMap(cm);
marci@650
  1198
      Parent::setBackwardFilterMap(cm);
marci@650
  1199
    }
marci@738
  1200
marci@738
  1201
    int edgeNum() const { 
marci@738
  1202
      return 2*this->graph->edgeNum();
marci@738
  1203
    }
deba@891
  1204
    //    KEEP_MAPS(Parent, BidirGraphWrapper);
marci@650
  1205
  };
marci@650
  1206
marci@650
  1207
marci@612
  1208
  /// \brief A bidirected graph template.
marci@612
  1209
  ///
alpar@879
  1210
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
  1211
  ///parts of the lib. Use them at you own risk.
alpar@879
  1212
  ///
marci@612
  1213
  /// A bidirected graph template.
marci@612
  1214
  /// Such a bidirected graph stores each pair of oppositely directed edges 
marci@612
  1215
  /// ones in the memory, i.e. a directed graph of type 
marci@612
  1216
  /// \c Graph is used for that.
marci@612
  1217
  /// As the oppositely directed edges are logically different ones 
marci@612
  1218
  /// the maps are able to attach different values for them.
marci@612
  1219
  /// \ingroup graphs
marci@612
  1220
  template<typename Graph>
marci@612
  1221
  class BidirGraph : public BidirGraphWrapper<Graph> {
marci@650
  1222
  public:
marci@612
  1223
    typedef UndirGraphWrapper<Graph> Parent;
marci@612
  1224
  protected:
marci@612
  1225
    Graph gr;
marci@612
  1226
  public:
marci@612
  1227
    BidirGraph() : BidirGraphWrapper<Graph>() { 
marci@612
  1228
      Parent::setGraph(gr); 
marci@612
  1229
    }
deba@891
  1230
    //    KEEP_MAPS(Parent, BidirGraph);
marci@612
  1231
  };
marci@569
  1232
marci@556
  1233
marci@650
  1234
marci@650
  1235
  template<typename Graph, typename Number,
marci@650
  1236
	   typename CapacityMap, typename FlowMap>
marci@658
  1237
  class ResForwardFilter {
marci@658
  1238
    //    const Graph* graph;
marci@650
  1239
    const CapacityMap* capacity;
marci@650
  1240
    const FlowMap* flow;
marci@650
  1241
  public:
marci@658
  1242
    ResForwardFilter(/*const Graph& _graph, */
marci@658
  1243
		     const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1244
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1245
    ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@656
  1246
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1247
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1248
    bool operator[](const typename Graph::Edge& e) const {
marci@738
  1249
      return (Number((*flow)[e]) < Number((*capacity)[e]));
marci@650
  1250
    }
marci@650
  1251
  };
marci@650
  1252
marci@650
  1253
  template<typename Graph, typename Number,
marci@650
  1254
	   typename CapacityMap, typename FlowMap>
marci@658
  1255
  class ResBackwardFilter {
marci@650
  1256
    const CapacityMap* capacity;
marci@650
  1257
    const FlowMap* flow;
marci@650
  1258
  public:
marci@658
  1259
    ResBackwardFilter(/*const Graph& _graph,*/ 
marci@658
  1260
		      const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1261
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1262
    ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@656
  1263
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1264
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1265
    bool operator[](const typename Graph::Edge& e) const {
marci@738
  1266
      return (Number(0) < Number((*flow)[e]));
marci@650
  1267
    }
marci@650
  1268
  };
marci@650
  1269
marci@653
  1270
  
marci@653
  1271
  /// A wrapper for composing the residual graph for directed flow and circulation problems.
marci@650
  1272
alpar@879
  1273
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
  1274
  ///parts of the lib. Use them at you own risk.
alpar@879
  1275
  ///
marci@653
  1276
  /// A wrapper for composing the residual graph for directed flow and circulation problems.
marci@650
  1277
  template<typename Graph, typename Number, 
marci@650
  1278
	   typename CapacityMap, typename FlowMap>
marci@653
  1279
  class ResGraphWrapper : 
marci@650
  1280
    public SubBidirGraphWrapper< 
marci@650
  1281
    Graph, 
marci@658
  1282
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1283
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
marci@650
  1284
  public:
marci@650
  1285
    typedef SubBidirGraphWrapper< 
marci@650
  1286
      Graph, 
marci@658
  1287
      ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1288
      ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
marci@650
  1289
  protected:
marci@650
  1290
    const CapacityMap* capacity;
marci@650
  1291
    FlowMap* flow;
marci@658
  1292
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
marci@658
  1293
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
marci@658
  1294
    ResGraphWrapper() : Parent(), 
marci@658
  1295
 			capacity(0), flow(0) { }
marci@658
  1296
    void setCapacityMap(const CapacityMap& _capacity) {
marci@658
  1297
      capacity=&_capacity;
marci@658
  1298
      forward_filter.setCapacity(_capacity);
marci@658
  1299
      backward_filter.setCapacity(_capacity);
marci@658
  1300
    }
marci@658
  1301
    void setFlowMap(FlowMap& _flow) {
marci@658
  1302
      flow=&_flow;
marci@658
  1303
      forward_filter.setFlow(_flow);
marci@658
  1304
      backward_filter.setFlow(_flow);
marci@658
  1305
    }
marci@650
  1306
  public:
marci@653
  1307
    ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity, 
marci@650
  1308
		       FlowMap& _flow) : 
marci@650
  1309
      Parent(), capacity(&_capacity), flow(&_flow), 
marci@658
  1310
      forward_filter(/*_graph,*/ _capacity, _flow), 
marci@658
  1311
      backward_filter(/*_graph,*/ _capacity, _flow) {
marci@650
  1312
      Parent::setGraph(_graph);
marci@650
  1313
      Parent::setForwardFilterMap(forward_filter);
marci@650
  1314
      Parent::setBackwardFilterMap(backward_filter);
marci@650
  1315
    }
marci@650
  1316
marci@660
  1317
    typedef typename Parent::Edge Edge;
marci@660
  1318
marci@660
  1319
    void augment(const Edge& e, Number a) const {
marci@650
  1320
      if (Parent::forward(e))  
marci@650
  1321
	flow->set(e, (*flow)[e]+a);
marci@650
  1322
      else  
marci@650
  1323
	flow->set(e, (*flow)[e]-a);
marci@650
  1324
    }
marci@650
  1325
marci@660
  1326
    /// \brief Residual capacity map.
marci@660
  1327
    ///
marci@910
  1328
    /// In generic residual graphs the residual capacity can be obtained 
marci@910
  1329
    /// as a map. 
marci@660
  1330
    class ResCap {
marci@660
  1331
    protected:
marci@660
  1332
      const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
marci@660
  1333
    public:
marci@660
  1334
      typedef Number ValueType;
marci@660
  1335
      typedef Edge KeyType;
marci@888
  1336
      ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& 
marci@888
  1337
	     _res_graph) : res_graph(&_res_graph) { }
marci@660
  1338
      Number operator[](const Edge& e) const { 
marci@660
  1339
	if (res_graph->forward(e)) 
marci@660
  1340
	  return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e]; 
marci@660
  1341
	else 
marci@660
  1342
	  return (*(res_graph->flow))[e]; 
marci@660
  1343
      }
marci@660
  1344
    };
marci@660
  1345
deba@891
  1346
    //    KEEP_MAPS(Parent, ResGraphWrapper);
marci@650
  1347
  };
marci@650
  1348
marci@650
  1349
marci@612
  1350
  /// For blocking flows.
marci@556
  1351
alpar@879
  1352
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
  1353
  ///parts of the lib. Use them at you own risk.
alpar@879
  1354
  ///
marci@792
  1355
  /// This graph wrapper is used for on-the-fly 
marci@792
  1356
  /// Dinits blocking flow computations.
marci@612
  1357
  /// For each node, an out-edge is stored which is used when the 
marci@612
  1358
  /// \code 
marci@612
  1359
  /// OutEdgeIt& first(OutEdgeIt&, const Node&)
marci@612
  1360
  /// \endcode
marci@612
  1361
  /// is called. 
marci@556
  1362
  ///
marci@792
  1363
  /// \author Marton Makai
marci@556
  1364
  template<typename Graph, typename FirstOutEdgesMap>
marci@556
  1365
  class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
marci@650
  1366
  public:
marci@650
  1367
    typedef GraphWrapper<Graph> Parent; 
marci@556
  1368
  protected:
marci@556
  1369
    FirstOutEdgesMap* first_out_edges;
marci@556
  1370
  public:
marci@556
  1371
    ErasingFirstGraphWrapper(Graph& _graph, 
marci@556
  1372
			     FirstOutEdgesMap& _first_out_edges) : 
marci@556
  1373
      GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }  
marci@556
  1374
marci@556
  1375
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
  1376
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@777
  1377
    class OutEdgeIt : public Edge { 
marci@556
  1378
      friend class GraphWrapper<Graph>;
marci@556
  1379
      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@777
  1380
      const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>* gw;
marci@556
  1381
    public:
marci@556
  1382
      OutEdgeIt() { }
marci@777
  1383
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@777
  1384
      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw, 
marci@777
  1385
		const Node& n) : 
marci@777
  1386
	Edge((*(_gw.first_out_edges))[n]), gw(&_gw) { }
marci@777
  1387
      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw, 
marci@777
  1388
		const Edge& e) : 
marci@777
  1389
	Edge(e), gw(&_gw) { }
marci@777
  1390
      OutEdgeIt& operator++() { 
marci@777
  1391
	*(static_cast<Edge*>(this))=
marci@777
  1392
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@777
  1393
	return *this; 
marci@777
  1394
      }
marci@556
  1395
    };
marci@556
  1396
marci@556
  1397
    using GraphWrapper<Graph>::first;
marci@556
  1398
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
  1399
      i=OutEdgeIt(*this, p); return i;
marci@556
  1400
    }
marci@777
  1401
    void erase(const Edge& e) const {
marci@777
  1402
      Node n=tail(e);
deba@844
  1403
      typename Graph::OutEdgeIt f(*Parent::graph, n);
marci@777
  1404
      ++f;
marci@777
  1405
      first_out_edges->set(n, f);
marci@556
  1406
    }
deba@877
  1407
deba@891
  1408
    //    KEEP_MAPS(Parent, ErasingFirstGraphWrapper);
marci@556
  1409
  };
marci@556
  1410
marci@556
  1411
  ///@}
marci@556
  1412
alpar@921
  1413
} //namespace lemon
marci@556
  1414
alpar@921
  1415
#endif //LEMON_GRAPH_WRAPPER_H
marci@556
  1416