src/lemon/graph_wrapper.h
author ladanyi
Mon, 11 Oct 2004 18:02:48 +0000
changeset 942 75fdd0c6866d
parent 932 ade3cdb9b45d
child 970 09f9abe22df2
permissions -rw-r--r--
Naming and coding style fixes and various other changes.
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@933
   371
  For other examples see also the documentation of NodeSubGraphWrapper and 
marci@933
   372
  EdgeSubGraphWrapper.
marci@930
   373
marci@930
   374
  \author Marton Makai
marci@930
   375
  */
marci@556
   376
  template<typename Graph, typename NodeFilterMap, 
marci@556
   377
	   typename EdgeFilterMap>
marci@556
   378
  class SubGraphWrapper : public GraphWrapper<Graph> {
marci@650
   379
  public:
marci@650
   380
    typedef GraphWrapper<Graph> Parent;
marci@556
   381
  protected:
marci@556
   382
    NodeFilterMap* node_filter_map;
marci@556
   383
    EdgeFilterMap* edge_filter_map;
marci@556
   384
marci@612
   385
    SubGraphWrapper() : GraphWrapper<Graph>(), 
marci@556
   386
			node_filter_map(0), edge_filter_map(0) { }
marci@556
   387
    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
marci@556
   388
      node_filter_map=&_node_filter_map;
marci@556
   389
    }
marci@556
   390
    void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
marci@556
   391
      edge_filter_map=&_edge_filter_map;
marci@556
   392
    }
marci@556
   393
    
marci@556
   394
  public:
marci@556
   395
    SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map, 
marci@556
   396
		    EdgeFilterMap& _edge_filter_map) : 
marci@556
   397
      GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map), 
marci@556
   398
      edge_filter_map(&_edge_filter_map) { }  
marci@556
   399
marci@556
   400
    typedef typename GraphWrapper<Graph>::Node Node;
marci@775
   401
    class NodeIt : public Node { 
marci@775
   402
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   403
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@775
   404
    public:
marci@556
   405
      NodeIt() { }
marci@775
   406
      NodeIt(Invalid i) : Node(i) { }
marci@775
   407
      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) : 
marci@854
   408
	Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { 
marci@854
   409
	while (*static_cast<Node*>(this)!=INVALID && 
marci@861
   410
	       !(*(gw->node_filter_map))[*this]) 
marci@854
   411
	  *(static_cast<Node*>(this))=
marci@854
   412
	    ++(typename Graph::NodeIt(*(gw->graph), *this));
marci@854
   413
      }
marci@775
   414
      NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   415
	     const Node& n) : 
marci@775
   416
	Node(n), gw(&_gw) { }
marci@775
   417
      NodeIt& operator++() { 
marci@775
   418
	*(static_cast<Node*>(this))=
marci@775
   419
	  ++(typename Graph::NodeIt(*(gw->graph), *this));
marci@775
   420
	while (*static_cast<Node*>(this)!=INVALID && 
marci@775
   421
	       !(*(gw->node_filter_map))[*this]) 
marci@775
   422
	  *(static_cast<Node*>(this))=
marci@775
   423
	    ++(typename Graph::NodeIt(*(gw->graph), *this));
marci@775
   424
	return *this; 
marci@556
   425
      }
marci@556
   426
    };
marci@556
   427
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@775
   428
    class OutEdgeIt : public Edge { 
marci@775
   429
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   430
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   431
    public:
marci@556
   432
      OutEdgeIt() { }
marci@775
   433
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@775
   434
      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) : 
marci@854
   435
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { 
marci@854
   436
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@854
   437
	       !(*(gw->edge_filter_map))[*this]) 
marci@854
   438
	  *(static_cast<Edge*>(this))=
marci@854
   439
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@854
   440
      }
marci@775
   441
      OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   442
	     const Edge& e) : 
marci@775
   443
	Edge(e), gw(&_gw) { }
marci@775
   444
      OutEdgeIt& operator++() { 
marci@775
   445
	*(static_cast<Edge*>(this))=
marci@775
   446
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   447
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   448
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   449
	  *(static_cast<Edge*>(this))=
marci@775
   450
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   451
	return *this; 
marci@556
   452
      }
marci@556
   453
    };
marci@775
   454
    class InEdgeIt : public Edge { 
marci@775
   455
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   456
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   457
    public:
marci@556
   458
      InEdgeIt() { }
marci@775
   459
      //      InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
marci@775
   460
      InEdgeIt(Invalid i) : Edge(i) { }
marci@775
   461
      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) : 
marci@854
   462
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { 
marci@854
   463
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@854
   464
	       !(*(gw->edge_filter_map))[*this]) 
marci@854
   465
	  *(static_cast<Edge*>(this))=
marci@854
   466
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@854
   467
      }
marci@775
   468
      InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   469
	     const Edge& e) : 
marci@775
   470
	Edge(e), gw(&_gw) { }
marci@775
   471
      InEdgeIt& operator++() { 
marci@775
   472
	*(static_cast<Edge*>(this))=
marci@775
   473
	  ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   474
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   475
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   476
	  *(static_cast<Edge*>(this))=
marci@775
   477
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   478
	return *this; 
marci@556
   479
      }
marci@556
   480
    };
marci@775
   481
    class EdgeIt : public Edge { 
marci@775
   482
      const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
marci@556
   483
      friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
marci@556
   484
    public:
marci@556
   485
      EdgeIt() { }
marci@775
   486
      EdgeIt(Invalid i) : Edge(i) { }
marci@775
   487
      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) : 
marci@854
   488
	Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { 
marci@854
   489
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@854
   490
	       !(*(gw->edge_filter_map))[*this]) 
marci@854
   491
	  *(static_cast<Edge*>(this))=
marci@854
   492
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@854
   493
      }
marci@775
   494
      EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, 
marci@775
   495
	     const Edge& e) : 
marci@775
   496
	Edge(e), gw(&_gw) { }
marci@775
   497
      EdgeIt& operator++() { 
marci@775
   498
	*(static_cast<Edge*>(this))=
marci@775
   499
	  ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   500
	while (*static_cast<Edge*>(this)!=INVALID && 
marci@775
   501
	       !(*(gw->edge_filter_map))[*this]) 
marci@775
   502
	  *(static_cast<Edge*>(this))=
marci@775
   503
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
   504
	return *this; 
marci@556
   505
      }
marci@556
   506
    };
marci@556
   507
marci@556
   508
    NodeIt& first(NodeIt& i) const { 
marci@556
   509
      i=NodeIt(*this); return i;
marci@556
   510
    }
marci@556
   511
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   512
      i=OutEdgeIt(*this, p); return i;
marci@556
   513
    }
marci@556
   514
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@556
   515
      i=InEdgeIt(*this, p); return i;
marci@556
   516
    }
marci@556
   517
    EdgeIt& first(EdgeIt& i) const { 
marci@556
   518
      i=EdgeIt(*this); return i;
marci@556
   519
    }
marci@556
   520
    
marci@561
   521
    /// This function hides \c n in the graph, i.e. the iteration 
marci@561
   522
    /// jumps over it. This is done by simply setting the value of \c n  
marci@561
   523
    /// to be false in the corresponding node-map.
marci@556
   524
    void hide(const Node& n) const { node_filter_map->set(n, false); }
marci@561
   525
marci@561
   526
    /// This function hides \c e in the graph, i.e. the iteration 
marci@561
   527
    /// jumps over it. This is done by simply setting the value of \c e  
marci@561
   528
    /// to be false in the corresponding edge-map.
marci@556
   529
    void hide(const Edge& e) const { edge_filter_map->set(e, false); }
marci@556
   530
marci@561
   531
    /// The value of \c n is set to be true in the node-map which stores 
marci@561
   532
    /// hide information. If \c n was hidden previuosly, then it is shown 
marci@561
   533
    /// again
marci@561
   534
     void unHide(const Node& n) const { node_filter_map->set(n, true); }
marci@561
   535
marci@561
   536
    /// The value of \c e is set to be true in the edge-map which stores 
marci@561
   537
    /// hide information. If \c e was hidden previuosly, then it is shown 
marci@561
   538
    /// again
marci@556
   539
    void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
marci@556
   540
marci@561
   541
    /// Returns true if \c n is hidden.
marci@561
   542
    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
marci@561
   543
marci@561
   544
    /// Returns true if \c n is hidden.
marci@561
   545
    bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
marci@593
   546
marci@792
   547
    /// \warning This is a linear time operation and works only if 
marci@792
   548
    /// \c Graph::NodeIt is defined.
marci@593
   549
    int nodeNum() const { 
marci@593
   550
      int i=0;
marci@792
   551
      for (NodeIt n(*this); n!=INVALID; ++n) ++i;
marci@593
   552
      return i; 
marci@593
   553
    }
marci@593
   554
marci@792
   555
    /// \warning This is a linear time operation and works only if 
marci@792
   556
    /// \c Graph::EdgeIt is defined.
marci@593
   557
    int edgeNum() const { 
marci@593
   558
      int i=0;
marci@792
   559
      for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
marci@593
   560
      return i; 
marci@593
   561
    }
marci@593
   562
deba@891
   563
    //    KEEP_MAPS(Parent, SubGraphWrapper);
marci@556
   564
  };
marci@556
   565
marci@569
   566
marci@933
   567
  /*! \brief A wrapper for hiding nodes from a graph.
marci@933
   568
marci@933
   569
  \warning Graph wrappers are in even more experimental state than the other
marci@933
   570
  parts of the lib. Use them at you own risk.
marci@933
   571
  
marci@933
   572
  A wrapper for hiding nodes from a graph.
marci@933
   573
  This wrapper specializes SubGraphWrapper in the way that only the node-set 
marci@933
   574
  can be filtered. Note that this does not mean of considering induced 
marci@933
   575
  subgraph, the edge-iterators consider the original edge-set.
marci@933
   576
  \author Marton Makai
marci@933
   577
  */
marci@933
   578
  template<typename Graph, typename NodeFilterMap>
marci@933
   579
  class NodeSubGraphWrapper : 
marci@933
   580
    public SubGraphWrapper<Graph, NodeFilterMap, 
marci@933
   581
			   ConstMap<typename Graph::Edge,bool> > {
marci@933
   582
  public:
marci@933
   583
    typedef SubGraphWrapper<Graph, NodeFilterMap, 
marci@933
   584
			    ConstMap<typename Graph::Edge,bool> > Parent;
marci@933
   585
  protected:
marci@933
   586
    ConstMap<typename Graph::Edge, bool> const_true_map;
marci@933
   587
  public:
marci@933
   588
    NodeSubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map) : 
marci@933
   589
      Parent(), const_true_map(true) { 
marci@933
   590
      Parent::setGraph(_graph);
marci@933
   591
      Parent::setNodeFilterMap(_node_filter_map);
marci@933
   592
      Parent::setEdgeFilterMap(const_true_map);
marci@933
   593
    }
marci@933
   594
  };
marci@933
   595
marci@933
   596
marci@932
   597
  /*! \brief A wrapper for hiding edges from a graph.
marci@932
   598
marci@932
   599
  \warning Graph wrappers are in even more experimental state than the other
marci@932
   600
  parts of the lib. Use them at you own risk.
marci@932
   601
  
marci@932
   602
  A wrapper for hiding edges from a graph.
marci@932
   603
  This wrapper specializes SubGraphWrapper in the way that only the edge-set 
marci@933
   604
  can be filtered. The usefulness of this wrapper is demonstrated in the 
marci@933
   605
  problem of searching a maximum number of edge-disjoint shortest paths 
marci@933
   606
  between 
marci@933
   607
  two nodes \c s and \c t. Shortest here means being shortest w.r.t. 
marci@933
   608
  non-negative edge-lengths. Note that 
marci@933
   609
  the comprehension of the presented solution 
marci@933
   610
  need's some knowledge from elementary combinatorial optimization. 
marci@933
   611
marci@933
   612
  If a single shortest path is to be 
marci@933
   613
  searched between two nodes \c s and \c t, then this can be done easily by 
marci@933
   614
  applying the Dijkstra algorithm class. What happens, if a maximum number of 
marci@933
   615
  edge-disjoint shortest paths is to be computed. It can be proved that an 
marci@933
   616
  edge can be in a shortest path if and only if it is tight with respect to 
marci@933
   617
  the potential function computed by Dijkstra. Moreover, any path containing 
marci@933
   618
  only such edges is a shortest one. Thus we have to compute a maximum number 
marci@933
   619
  of edge-disjoint paths between \c s and \c t in the graph which has edge-set 
marci@933
   620
  all the tight edges. The computation will be demonstrated on the following 
marci@933
   621
  graph, which is read from a dimacs file.
marci@933
   622
  
marci@933
   623
  \dot
marci@933
   624
  digraph lemon_dot_example {
marci@933
   625
  node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
marci@933
   626
  n0 [ label="0 (s)" ];
marci@933
   627
  n1 [ label="1" ];
marci@933
   628
  n2 [ label="2" ];
marci@933
   629
  n3 [ label="3" ];
marci@933
   630
  n4 [ label="4" ];
marci@933
   631
  n5 [ label="5" ];
marci@933
   632
  n6 [ label="6 (t)" ];
marci@933
   633
  edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
marci@933
   634
  n5 ->  n6 [ label="9, length:4" ];
marci@933
   635
  n4 ->  n6 [ label="8, length:2" ];
marci@933
   636
  n3 ->  n5 [ label="7, length:1" ];
marci@933
   637
  n2 ->  n5 [ label="6, length:3" ];
marci@933
   638
  n2 ->  n6 [ label="5, length:5" ];
marci@933
   639
  n2 ->  n4 [ label="4, length:2" ];
marci@933
   640
  n1 ->  n4 [ label="3, length:3" ];
marci@933
   641
  n0 ->  n3 [ label="2, length:1" ];
marci@933
   642
  n0 ->  n2 [ label="1, length:2" ];
marci@933
   643
  n0 ->  n1 [ label="0, length:3" ];
marci@933
   644
  }
marci@933
   645
  \enddot
marci@933
   646
marci@933
   647
  \code
marci@933
   648
  Graph g;
marci@933
   649
  Node s, t;
marci@933
   650
  LengthMap length(g);
marci@933
   651
marci@933
   652
  readDimacs(std::cin, g, length, s, t);
marci@933
   653
marci@933
   654
  cout << "edges with lengths (of form id, tail--length->head): " << endl;
marci@933
   655
  for(EdgeIt e(g); e!=INVALID; ++e) 
marci@933
   656
    cout << g.id(e) << ", " << g.id(g.tail(e)) << "--" 
marci@933
   657
         << length[e] << "->" << g.id(g.head(e)) << endl;
marci@933
   658
marci@933
   659
  cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
marci@933
   660
  \endcode
marci@933
   661
  Next, the potential function is computed with Dijkstra.
marci@933
   662
  \code
marci@933
   663
  typedef Dijkstra<Graph, LengthMap> Dijkstra;
marci@933
   664
  Dijkstra dijkstra(g, length);
marci@933
   665
  dijkstra.run(s);
marci@933
   666
  \endcode
marci@933
   667
  Next, we consrtruct a map which filters the edge-set to the tight edges.
marci@933
   668
  \code
marci@933
   669
  typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap> 
marci@933
   670
    TightEdgeFilter;
marci@933
   671
  TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
marci@933
   672
  
marci@933
   673
  typedef EdgeSubGraphWrapper<Graph, TightEdgeFilter> SubGW;
marci@933
   674
  SubGW gw(g, tight_edge_filter);
marci@933
   675
  \endcode
marci@933
   676
  Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed 
marci@933
   677
  with a max flow algorithm Preflow.
marci@933
   678
  \code
marci@933
   679
  ConstMap<Edge, int> const_1_map(1);
marci@933
   680
  Graph::EdgeMap<int> flow(g, 0);
marci@933
   681
marci@933
   682
  Preflow<SubGW, int, ConstMap<Edge, int>, Graph::EdgeMap<int> > 
marci@933
   683
    preflow(gw, s, t, const_1_map, flow);
marci@933
   684
  preflow.run();
marci@933
   685
  \endcode
marci@933
   686
  Last, the output is:
marci@933
   687
  \code  
marci@933
   688
  cout << "maximum number of edge-disjoint shortest path: " 
marci@933
   689
       << preflow.flowValue() << endl;
marci@933
   690
  cout << "edges of the maximum number of edge-disjoint shortest s-t paths: " 
marci@933
   691
       << endl;
marci@933
   692
  for(EdgeIt e(g); e!=INVALID; ++e) 
marci@933
   693
    if (flow[e])
marci@933
   694
      cout << " " << g.id(g.tail(e)) << "--" 
marci@933
   695
	   << length[e] << "->" << g.id(g.head(e)) << endl;
marci@933
   696
  \endcode
marci@933
   697
  The program has the following (expected :-)) output:
marci@933
   698
  \code
marci@933
   699
  edges with lengths (of form id, tail--length->head):
marci@933
   700
   9, 5--4->6
marci@933
   701
   8, 4--2->6
marci@933
   702
   7, 3--1->5
marci@933
   703
   6, 2--3->5
marci@933
   704
   5, 2--5->6
marci@933
   705
   4, 2--2->4
marci@933
   706
   3, 1--3->4
marci@933
   707
   2, 0--1->3
marci@933
   708
   1, 0--2->2
marci@933
   709
   0, 0--3->1
marci@933
   710
  s: 0 t: 6
marci@933
   711
  maximum number of edge-disjoint shortest path: 2
marci@933
   712
  edges of the maximum number of edge-disjoint shortest s-t paths:
marci@933
   713
   9, 5--4->6
marci@933
   714
   8, 4--2->6
marci@933
   715
   7, 3--1->5
marci@933
   716
   4, 2--2->4
marci@933
   717
   2, 0--1->3
marci@933
   718
   1, 0--2->2
marci@933
   719
  \endcode
marci@933
   720
marci@932
   721
  \author Marton Makai
marci@932
   722
  */
marci@932
   723
  template<typename Graph, typename EdgeFilterMap>
marci@932
   724
  class EdgeSubGraphWrapper : 
marci@932
   725
    public SubGraphWrapper<Graph, ConstMap<typename Graph::Node,bool>, 
marci@932
   726
			   EdgeFilterMap> {
marci@932
   727
  public:
marci@932
   728
    typedef SubGraphWrapper<Graph, ConstMap<typename Graph::Node,bool>, 
marci@932
   729
			    EdgeFilterMap> Parent;
marci@932
   730
  protected:
marci@932
   731
    ConstMap<typename Graph::Node, bool> const_true_map;
marci@932
   732
  public:
marci@932
   733
    EdgeSubGraphWrapper(Graph& _graph, EdgeFilterMap& _edge_filter_map) : 
marci@932
   734
      Parent(), const_true_map(true) { 
marci@932
   735
      Parent::setGraph(_graph);
marci@932
   736
      Parent::setNodeFilterMap(const_true_map);
marci@932
   737
      Parent::setEdgeFilterMap(_edge_filter_map);
marci@932
   738
    }
marci@932
   739
  };
marci@932
   740
marci@569
   741
marci@556
   742
  template<typename Graph>
marci@556
   743
  class UndirGraphWrapper : public GraphWrapper<Graph> {
marci@650
   744
  public:
marci@650
   745
    typedef GraphWrapper<Graph> Parent; 
marci@556
   746
  protected:
marci@556
   747
    UndirGraphWrapper() : GraphWrapper<Graph>() { }
marci@556
   748
    
marci@556
   749
  public:
marci@556
   750
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
   751
    typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
marci@556
   752
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@556
   753
    typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
marci@556
   754
marci@556
   755
    UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }  
marci@556
   756
marci@556
   757
    class OutEdgeIt {
marci@556
   758
      friend class UndirGraphWrapper<Graph>;
marci@556
   759
      bool out_or_in; //true iff out
marci@556
   760
      typename Graph::OutEdgeIt out;
marci@556
   761
      typename Graph::InEdgeIt in;
marci@556
   762
    public:
marci@556
   763
      OutEdgeIt() { }
marci@556
   764
      OutEdgeIt(const Invalid& i) : Edge(i) { }
marci@556
   765
      OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
marci@556
   766
	out_or_in=true; _G.graph->first(out, _n);
marci@556
   767
	if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n);	}
marci@556
   768
      } 
marci@556
   769
      operator Edge() const { 
marci@556
   770
	if (out_or_in) return Edge(out); else return Edge(in); 
marci@556
   771
      }
marci@556
   772
    };
marci@556
   773
marci@556
   774
    typedef OutEdgeIt InEdgeIt; 
marci@556
   775
marci@556
   776
    using GraphWrapper<Graph>::first;
marci@556
   777
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
   778
      i=OutEdgeIt(*this, p); return i;
marci@556
   779
    }
marci@556
   780
marci@556
   781
    using GraphWrapper<Graph>::next;
alpar@878
   782
marci@556
   783
    OutEdgeIt& next(OutEdgeIt& e) const {
marci@556
   784
      if (e.out_or_in) {
marci@556
   785
	typename Graph::Node n=this->graph->tail(e.out);
marci@556
   786
	this->graph->next(e.out);
marci@556
   787
	if (!this->graph->valid(e.out)) { 
marci@556
   788
	  e.out_or_in=false; this->graph->first(e.in, n); }
marci@556
   789
      } else {
marci@556
   790
	this->graph->next(e.in);
marci@556
   791
      }
marci@556
   792
      return e;
marci@556
   793
    }
marci@556
   794
marci@556
   795
    Node aNode(const OutEdgeIt& e) const { 
marci@556
   796
      if (e.out_or_in) return this->graph->tail(e); else 
marci@556
   797
	return this->graph->head(e); }
marci@556
   798
    Node bNode(const OutEdgeIt& e) const { 
marci@556
   799
      if (e.out_or_in) return this->graph->head(e); else 
marci@556
   800
	return this->graph->tail(e); }
deba@877
   801
deba@891
   802
    //    KEEP_MAPS(Parent, UndirGraphWrapper);
deba@877
   803
marci@556
   804
  };
marci@556
   805
  
marci@910
   806
//   /// \brief An undirected graph template.
marci@910
   807
//   ///
marci@910
   808
//   ///\warning Graph wrappers are in even more experimental state than the other
marci@910
   809
//   ///parts of the lib. Use them at your own risk.
marci@910
   810
//   ///
marci@910
   811
//   /// An undirected graph template.
marci@910
   812
//   /// This class works as an undirected graph and a directed graph of 
marci@910
   813
//   /// class \c Graph is used for the physical storage.
marci@910
   814
//   /// \ingroup graphs
marci@556
   815
  template<typename Graph>
marci@556
   816
  class UndirGraph : public UndirGraphWrapper<Graph> {
marci@556
   817
    typedef UndirGraphWrapper<Graph> Parent;
marci@556
   818
  protected:
marci@556
   819
    Graph gr;
marci@556
   820
  public:
marci@556
   821
    UndirGraph() : UndirGraphWrapper<Graph>() { 
marci@556
   822
      Parent::setGraph(gr); 
marci@556
   823
    }
deba@877
   824
deba@891
   825
    //    KEEP_MAPS(Parent, UndirGraph);
marci@556
   826
  };
marci@556
   827
marci@569
   828
marci@650
   829
marci@650
   830
  ///\brief A wrapper for composing a subgraph of a 
marci@792
   831
  /// bidirected graph made from a directed one. 
marci@612
   832
  ///
alpar@911
   833
  /// A wrapper for composing a subgraph of a 
alpar@911
   834
  /// bidirected graph made from a directed one. 
alpar@911
   835
  ///
alpar@879
   836
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
   837
  ///parts of the lib. Use them at you own risk.
alpar@879
   838
  ///
marci@923
   839
  /// Let \f$G=(V, A)\f$ be a directed graph and for each directed edge 
marci@923
   840
  /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by
marci@923
   841
  /// reversing its orientation. We are given moreover two bool valued 
marci@923
   842
  /// maps on the edge-set, 
marci@923
   843
  /// \f$forward\_filter\f$, and \f$backward\_filter\f$. 
marci@923
   844
  /// SubBidirGraphWrapper implements the graph structure with node-set 
marci@923
   845
  /// \f$V\f$ and edge-set 
marci@923
   846
  /// \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
   847
  /// The purpose of writing + instead of union is because parallel 
marci@923
   848
  /// edges can arise. (Similarly, antiparallel edges also can arise).
marci@792
   849
  /// In other words, a subgraph of the bidirected graph obtained, which 
marci@792
   850
  /// is given by orienting the edges of the original graph in both directions.
marci@923
   851
  /// As the oppositely directed edges are logically different, 
marci@923
   852
  /// the maps are able to attach different values for them. 
marci@923
   853
  ///
marci@923
   854
  /// An example for such a construction is \c RevGraphWrapper where the 
marci@792
   855
  /// forward_filter is everywhere false and the backward_filter is 
marci@792
   856
  /// everywhere true. We note that for sake of efficiency, 
marci@792
   857
  /// \c RevGraphWrapper is implemented in a different way. 
marci@792
   858
  /// But BidirGraphWrapper is obtained from 
marci@792
   859
  /// SubBidirGraphWrapper by considering everywhere true 
marci@910
   860
  /// valued maps both for forward_filter and backward_filter. 
marci@792
   861
  /// Finally, one of the most important applications of SubBidirGraphWrapper 
marci@792
   862
  /// is ResGraphWrapper, which stands for the residual graph in directed 
marci@792
   863
  /// flow and circulation problems. 
marci@792
   864
  /// As wrappers usually, the SubBidirGraphWrapper implements the 
marci@792
   865
  /// above mentioned graph structure without its physical storage, 
marci@923
   866
  /// that is the whole stuff is stored in constant memory. 
marci@650
   867
  template<typename Graph, 
marci@650
   868
	   typename ForwardFilterMap, typename BackwardFilterMap>
marci@650
   869
  class SubBidirGraphWrapper : public GraphWrapper<Graph> {
marci@650
   870
  public:
marci@650
   871
    typedef GraphWrapper<Graph> Parent; 
marci@569
   872
  protected:
marci@650
   873
    ForwardFilterMap* forward_filter;
marci@650
   874
    BackwardFilterMap* backward_filter;
marci@650
   875
marci@792
   876
    SubBidirGraphWrapper() : GraphWrapper<Graph>() { }
marci@650
   877
    void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
marci@650
   878
      forward_filter=&_forward_filter;
marci@650
   879
    }
marci@650
   880
    void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
marci@650
   881
      backward_filter=&_backward_filter;
marci@650
   882
    }
marci@569
   883
marci@569
   884
  public:
marci@569
   885
marci@650
   886
    SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter, 
marci@650
   887
			 BackwardFilterMap& _backward_filter) : 
marci@650
   888
      GraphWrapper<Graph>(_graph), 
marci@650
   889
      forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
alpar@774
   890
    SubBidirGraphWrapper(const SubBidirGraphWrapper<Graph, 
alpar@774
   891
			 ForwardFilterMap, BackwardFilterMap>& gw) : 
alpar@774
   892
      Parent(gw), 
alpar@774
   893
      forward_filter(gw.forward_filter), 
alpar@774
   894
      backward_filter(gw.backward_filter) { }
marci@569
   895
marci@569
   896
    class Edge; 
marci@569
   897
    class OutEdgeIt; 
marci@569
   898
    friend class Edge; 
marci@569
   899
    friend class OutEdgeIt; 
marci@569
   900
marci@621
   901
    template<typename T> class EdgeMap;
marci@621
   902
marci@569
   903
    typedef typename GraphWrapper<Graph>::Node Node;
marci@621
   904
alpar@774
   905
    typedef typename Graph::Edge GraphEdge;
marci@792
   906
    /// SubBidirGraphWrapper<..., ..., ...>::Edge is inherited from 
marci@910
   907
    /// Graph::Edge. It contains an extra bool flag which is true 
marci@910
   908
    /// if and only if the 
marci@792
   909
    /// edge is the backward version of the original edge.
marci@569
   910
    class Edge : public Graph::Edge {
marci@650
   911
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   912
					ForwardFilterMap, BackwardFilterMap>;
marci@621
   913
      template<typename T> friend class EdgeMap;
marci@569
   914
    protected:
marci@569
   915
      bool backward; //true, iff backward
marci@569
   916
    public:
marci@569
   917
      Edge() { }
marci@792
   918
      /// \todo =false is needed, or causes problems?
marci@792
   919
      /// If \c _backward is false, then we get an edge corresponding to the 
marci@792
   920
      /// original one, otherwise its oppositely directed pair is obtained.
alpar@774
   921
      Edge(const typename Graph::Edge& e, bool _backward/*=false*/) : 
alpar@774
   922
	Graph::Edge(e), backward(_backward) { }
alpar@774
   923
      Edge(Invalid i) : Graph::Edge(i), backward(true) { }
alpar@774
   924
      bool operator==(const Edge& v) const { 
alpar@774
   925
	return (this->backward==v.backward && 
alpar@774
   926
		static_cast<typename Graph::Edge>(*this)==
marci@569
   927
		static_cast<typename Graph::Edge>(v));
marci@569
   928
      } 
alpar@774
   929
      bool operator!=(const Edge& v) const { 
alpar@774
   930
	return (this->backward!=v.backward || 
alpar@774
   931
		static_cast<typename Graph::Edge>(*this)!=
marci@569
   932
		static_cast<typename Graph::Edge>(v));
alpar@774
   933
      }
marci@569
   934
    };
marci@569
   935
alpar@774
   936
    class OutEdgeIt : public Edge {
marci@650
   937
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   938
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   939
    protected:
alpar@774
   940
      const SubBidirGraphWrapper<Graph, 
alpar@774
   941
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
   942
    public:
marci@569
   943
      OutEdgeIt() { }
alpar@774
   944
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@650
   945
      OutEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   946
		ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) : 
alpar@774
   947
	Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), false), gw(&_gw) { 
alpar@774
   948
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   949
	       !(*(gw->forward_filter))[*this]) 
alpar@774
   950
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   951
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   952
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   953
	  *static_cast<Edge*>(this)=
alpar@774
   954
	    Edge(typename Graph::InEdgeIt(*(_gw.graph), n), true);
marci@775
   955
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   956
		 !(*(gw->backward_filter))[*this]) 
marci@775
   957
	    *(static_cast<GraphEdge*>(this))=
marci@775
   958
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   959
	}
alpar@774
   960
      }
alpar@774
   961
      OutEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
   962
		ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
   963
	Edge(e), gw(&_gw) { }
alpar@774
   964
      OutEdgeIt& operator++() { 
alpar@774
   965
	if (!this->backward) {
alpar@774
   966
	  Node n=gw->tail(*this);
alpar@774
   967
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   968
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
   969
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   970
		 !(*(gw->forward_filter))[*this]) 
alpar@774
   971
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   972
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
   973
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
   974
	    *static_cast<Edge*>(this)=
alpar@774
   975
	      Edge(typename Graph::InEdgeIt(*(gw->graph), n), true);
marci@775
   976
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
   977
		   !(*(gw->backward_filter))[*this]) 
marci@775
   978
	      *(static_cast<GraphEdge*>(this))=
marci@775
   979
		++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
   980
	  }
alpar@774
   981
	} else {
alpar@774
   982
	  *(static_cast<GraphEdge*>(this))=
alpar@774
   983
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
   984
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
   985
		 !(*(gw->backward_filter))[*this]) 
alpar@774
   986
	    *(static_cast<GraphEdge*>(this))=
alpar@774
   987
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@569
   988
	}
alpar@774
   989
	return *this;
marci@569
   990
      }
marci@569
   991
    };
marci@569
   992
alpar@774
   993
    class InEdgeIt : public Edge {
marci@650
   994
      friend class SubBidirGraphWrapper<Graph, 
marci@650
   995
					ForwardFilterMap, BackwardFilterMap>;
marci@569
   996
    protected:
alpar@774
   997
      const SubBidirGraphWrapper<Graph, 
alpar@774
   998
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
   999
    public:
marci@569
  1000
      InEdgeIt() { }
alpar@774
  1001
      InEdgeIt(Invalid i) : Edge(i) { }
marci@650
  1002
      InEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
  1003
	       ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) : 
alpar@774
  1004
	Edge(typename Graph::InEdgeIt(*(_gw.graph), n), false), gw(&_gw) { 
alpar@774
  1005
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
  1006
	       !(*(gw->forward_filter))[*this]) 
alpar@774
  1007
	  *(static_cast<GraphEdge*>(this))=
alpar@774
  1008
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
  1009
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
  1010
	  *static_cast<Edge*>(this)=
alpar@774
  1011
	    Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), true);
marci@775
  1012
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
  1013
		 !(*(gw->backward_filter))[*this]) 
marci@775
  1014
	    *(static_cast<GraphEdge*>(this))=
marci@775
  1015
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
  1016
	}
alpar@774
  1017
      }
alpar@774
  1018
      InEdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
  1019
	       ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
  1020
	Edge(e), gw(&_gw) { }
alpar@774
  1021
      InEdgeIt& operator++() { 
alpar@774
  1022
	if (!this->backward) {
marci@775
  1023
	  Node n=gw->tail(*this);
alpar@774
  1024
	  *(static_cast<GraphEdge*>(this))=
alpar@774
  1025
	    ++(typename Graph::InEdgeIt(*(gw->graph), *this));
alpar@774
  1026
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
  1027
		 !(*(gw->forward_filter))[*this]) 
alpar@774
  1028
	    *(static_cast<GraphEdge*>(this))=
alpar@774
  1029
	      ++(typename Graph::InEdgeIt(*(gw->graph), *this));
marci@775
  1030
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
  1031
	    *static_cast<Edge*>(this)=
alpar@774
  1032
	      Edge(typename Graph::OutEdgeIt(*(gw->graph), n), true);
marci@775
  1033
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
  1034
		   !(*(gw->backward_filter))[*this]) 
marci@775
  1035
	      *(static_cast<GraphEdge*>(this))=
marci@775
  1036
		++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@775
  1037
	  }
alpar@774
  1038
	} else {
alpar@774
  1039
	  *(static_cast<GraphEdge*>(this))=
alpar@774
  1040
	    ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
alpar@774
  1041
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
  1042
		 !(*(gw->backward_filter))[*this]) 
alpar@774
  1043
	    *(static_cast<GraphEdge*>(this))=
alpar@774
  1044
	      ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@569
  1045
	}
alpar@774
  1046
	return *this;
marci@569
  1047
      }
marci@569
  1048
    };
marci@569
  1049
alpar@774
  1050
    class EdgeIt : public Edge {
marci@650
  1051
      friend class SubBidirGraphWrapper<Graph, 
marci@650
  1052
					ForwardFilterMap, BackwardFilterMap>;
marci@569
  1053
    protected:
alpar@774
  1054
      const SubBidirGraphWrapper<Graph, 
alpar@774
  1055
				 ForwardFilterMap, BackwardFilterMap>* gw;
marci@569
  1056
    public:
marci@569
  1057
      EdgeIt() { }
alpar@774
  1058
      EdgeIt(Invalid i) : Edge(i) { }
marci@650
  1059
      EdgeIt(const SubBidirGraphWrapper<Graph, 
marci@775
  1060
	     ForwardFilterMap, BackwardFilterMap>& _gw) : 
marci@892
  1061
	Edge(typename Graph::EdgeIt(*(_gw.graph)), false), gw(&_gw) { 
alpar@774
  1062
	while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
  1063
	       !(*(gw->forward_filter))[*this]) 
alpar@774
  1064
	  *(static_cast<GraphEdge*>(this))=
alpar@774
  1065
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
  1066
	if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
  1067
	  *static_cast<Edge*>(this)=
alpar@774
  1068
	    Edge(typename Graph::EdgeIt(*(_gw.graph)), true);
marci@775
  1069
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
  1070
		 !(*(gw->backward_filter))[*this]) 
marci@775
  1071
	    *(static_cast<GraphEdge*>(this))=
marci@775
  1072
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
  1073
	}
alpar@774
  1074
      }
alpar@774
  1075
      EdgeIt(const SubBidirGraphWrapper<Graph, 
alpar@774
  1076
	     ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) : 
alpar@774
  1077
	Edge(e), gw(&_gw) { }
alpar@774
  1078
      EdgeIt& operator++() { 
alpar@774
  1079
	if (!this->backward) {
alpar@774
  1080
	  *(static_cast<GraphEdge*>(this))=
alpar@774
  1081
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
  1082
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
  1083
		 !(*(gw->forward_filter))[*this]) 
alpar@774
  1084
	    *(static_cast<GraphEdge*>(this))=
alpar@774
  1085
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
  1086
	  if (*static_cast<GraphEdge*>(this)==INVALID) {
alpar@774
  1087
	    *static_cast<Edge*>(this)=
alpar@774
  1088
	      Edge(typename Graph::EdgeIt(*(gw->graph)), true);
marci@775
  1089
	    while (*static_cast<GraphEdge*>(this)!=INVALID && 
marci@775
  1090
		   !(*(gw->backward_filter))[*this]) 
marci@775
  1091
	      *(static_cast<GraphEdge*>(this))=
marci@775
  1092
		++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@775
  1093
	  }
alpar@774
  1094
	} else {
alpar@774
  1095
	  *(static_cast<GraphEdge*>(this))=
alpar@774
  1096
	    ++(typename Graph::EdgeIt(*(gw->graph), *this));
alpar@774
  1097
	  while (*static_cast<GraphEdge*>(this)!=INVALID && 
alpar@774
  1098
		 !(*(gw->backward_filter))[*this]) 
alpar@774
  1099
	    *(static_cast<GraphEdge*>(this))=
alpar@774
  1100
	      ++(typename Graph::EdgeIt(*(gw->graph), *this));
marci@569
  1101
	}
alpar@774
  1102
	return *this;
marci@569
  1103
      }
marci@569
  1104
    };
marci@569
  1105
marci@569
  1106
    using GraphWrapper<Graph>::first;
marci@569
  1107
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@569
  1108
      i=OutEdgeIt(*this, p); return i;
marci@569
  1109
    }
marci@569
  1110
    InEdgeIt& first(InEdgeIt& i, const Node& p) const { 
marci@569
  1111
      i=InEdgeIt(*this, p); return i;
marci@569
  1112
    }
marci@569
  1113
    EdgeIt& first(EdgeIt& i) const { 
marci@569
  1114
      i=EdgeIt(*this); return i;
marci@569
  1115
    }
marci@556
  1116
  
marci@569
  1117
marci@569
  1118
    Node tail(Edge e) const { 
marci@569
  1119
      return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
marci@569
  1120
    Node head(Edge e) const { 
marci@569
  1121
      return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
marci@569
  1122
marci@572
  1123
    /// Gives back the opposite edge.
marci@572
  1124
    Edge opposite(const Edge& e) const { 
marci@572
  1125
      Edge f=e;
marci@572
  1126
      f.backward=!f.backward;
marci@572
  1127
      return f;
marci@572
  1128
    }
marci@572
  1129
marci@792
  1130
    /// \warning This is a linear time operation and works only if 
marci@792
  1131
    /// \c Graph::EdgeIt is defined.
marci@792
  1132
    int edgeNum() const { 
marci@792
  1133
      int i=0;
marci@792
  1134
      for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
marci@792
  1135
      return i; 
marci@792
  1136
    }
marci@569
  1137
marci@569
  1138
    bool forward(const Edge& e) const { return !e.backward; }
marci@569
  1139
    bool backward(const Edge& e) const { return e.backward; }
marci@569
  1140
marci@569
  1141
marci@569
  1142
    template <typename T>
marci@792
  1143
    /// \c SubBidirGraphWrapper<..., ..., ...>::EdgeMap contains two 
marci@792
  1144
    /// Graph::EdgeMap one for the forward edges and 
marci@792
  1145
    /// one for the backward edges.
marci@569
  1146
    class EdgeMap {
deba@891
  1147
      template <typename TT> friend class EdgeMap;
marci@569
  1148
      typename Graph::template EdgeMap<T> forward_map, backward_map; 
marci@569
  1149
    public:
marci@623
  1150
      typedef T ValueType;
marci@623
  1151
      typedef Edge KeyType;
deba@891
  1152
marci@650
  1153
      EdgeMap(const SubBidirGraphWrapper<Graph, 
alpar@774
  1154
	      ForwardFilterMap, BackwardFilterMap>& g) : 
alpar@774
  1155
	forward_map(*(g.graph)), backward_map(*(g.graph)) { }
deba@891
  1156
marci@650
  1157
      EdgeMap(const SubBidirGraphWrapper<Graph, 
alpar@774
  1158
	      ForwardFilterMap, BackwardFilterMap>& g, T a) : 
alpar@774
  1159
	forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
deba@891
  1160
deba@891
  1161
      template <typename TT>
deba@891
  1162
      EdgeMap(const EdgeMap<TT>& copy) 
deba@891
  1163
	: forward_map(copy.forward_map), backward_map(copy.backward_map) {}
deba@891
  1164
deba@891
  1165
      template <typename TT>
deba@891
  1166
      EdgeMap& operator=(const EdgeMap<TT>& copy) {
deba@891
  1167
	forward_map = copy.forward_map;
deba@891
  1168
	backward_map = copy.backward_map;
deba@891
  1169
	return *this;
deba@891
  1170
      }
deba@891
  1171
      
marci@569
  1172
      void set(Edge e, T a) { 
marci@569
  1173
	if (!e.backward) 
marci@792
  1174
	  forward_map.set(e, a); 
marci@569
  1175
	else 
marci@792
  1176
	  backward_map.set(e, a); 
marci@569
  1177
      }
deba@891
  1178
deba@891
  1179
      typename Graph::template EdgeMap<T>::ConstReferenceType 
deba@891
  1180
      operator[](Edge e) const { 
marci@569
  1181
	if (!e.backward) 
marci@792
  1182
	  return forward_map[e]; 
marci@569
  1183
	else 
marci@792
  1184
	  return backward_map[e]; 
marci@569
  1185
      }
deba@891
  1186
deba@891
  1187
      typename Graph::template EdgeMap<T>::ReferenceType 
deba@891
  1188
      operator[](Edge e) { 
deba@891
  1189
	if (!e.backward) 
deba@891
  1190
	  return forward_map[e]; 
deba@891
  1191
	else 
deba@891
  1192
	  return backward_map[e]; 
deba@891
  1193
      }
deba@891
  1194
marci@625
  1195
      void update() { 
marci@625
  1196
	forward_map.update(); 
marci@625
  1197
	backward_map.update();
marci@625
  1198
      }
marci@569
  1199
    };
deba@877
  1200
deba@877
  1201
deba@891
  1202
    //    KEEP_NODE_MAP(Parent, SubBidirGraphWrapper);
deba@877
  1203
marci@569
  1204
  };
marci@569
  1205
marci@650
  1206
marci@650
  1207
  ///\brief A wrapper for composing bidirected graph from a directed one. 
marci@650
  1208
  ///
alpar@879
  1209
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
  1210
  ///parts of the lib. Use them at you own risk.
alpar@879
  1211
  ///
marci@650
  1212
  /// A wrapper for composing bidirected graph from a directed one. 
marci@650
  1213
  /// A bidirected graph is composed over the directed one without physical 
marci@650
  1214
  /// storage. As the oppositely directed edges are logically different ones 
marci@650
  1215
  /// the maps are able to attach different values for them.
marci@650
  1216
  template<typename Graph>
marci@650
  1217
  class BidirGraphWrapper : 
marci@650
  1218
    public SubBidirGraphWrapper<
marci@650
  1219
    Graph, 
marci@650
  1220
    ConstMap<typename Graph::Edge, bool>, 
marci@650
  1221
    ConstMap<typename Graph::Edge, bool> > {
marci@650
  1222
  public:
marci@650
  1223
    typedef  SubBidirGraphWrapper<
marci@650
  1224
      Graph, 
marci@650
  1225
      ConstMap<typename Graph::Edge, bool>, 
marci@650
  1226
      ConstMap<typename Graph::Edge, bool> > Parent; 
marci@650
  1227
  protected:
marci@650
  1228
    ConstMap<typename Graph::Edge, bool> cm;
marci@650
  1229
marci@655
  1230
    BidirGraphWrapper() : Parent(), cm(true) { 
marci@655
  1231
      Parent::setForwardFilterMap(cm);
marci@655
  1232
      Parent::setBackwardFilterMap(cm);
marci@655
  1233
    }
marci@650
  1234
  public:
marci@650
  1235
    BidirGraphWrapper(Graph& _graph) : Parent() { 
marci@650
  1236
      Parent::setGraph(_graph);
marci@650
  1237
      Parent::setForwardFilterMap(cm);
marci@650
  1238
      Parent::setBackwardFilterMap(cm);
marci@650
  1239
    }
marci@738
  1240
marci@738
  1241
    int edgeNum() const { 
marci@738
  1242
      return 2*this->graph->edgeNum();
marci@738
  1243
    }
deba@891
  1244
    //    KEEP_MAPS(Parent, BidirGraphWrapper);
marci@650
  1245
  };
marci@650
  1246
marci@650
  1247
marci@612
  1248
  /// \brief A bidirected graph template.
marci@612
  1249
  ///
alpar@879
  1250
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
  1251
  ///parts of the lib. Use them at you own risk.
alpar@879
  1252
  ///
marci@612
  1253
  /// A bidirected graph template.
marci@612
  1254
  /// Such a bidirected graph stores each pair of oppositely directed edges 
marci@612
  1255
  /// ones in the memory, i.e. a directed graph of type 
marci@612
  1256
  /// \c Graph is used for that.
marci@612
  1257
  /// As the oppositely directed edges are logically different ones 
marci@612
  1258
  /// the maps are able to attach different values for them.
marci@612
  1259
  /// \ingroup graphs
marci@612
  1260
  template<typename Graph>
marci@612
  1261
  class BidirGraph : public BidirGraphWrapper<Graph> {
marci@650
  1262
  public:
marci@612
  1263
    typedef UndirGraphWrapper<Graph> Parent;
marci@612
  1264
  protected:
marci@612
  1265
    Graph gr;
marci@612
  1266
  public:
marci@612
  1267
    BidirGraph() : BidirGraphWrapper<Graph>() { 
marci@612
  1268
      Parent::setGraph(gr); 
marci@612
  1269
    }
deba@891
  1270
    //    KEEP_MAPS(Parent, BidirGraph);
marci@612
  1271
  };
marci@569
  1272
marci@556
  1273
marci@650
  1274
marci@650
  1275
  template<typename Graph, typename Number,
marci@650
  1276
	   typename CapacityMap, typename FlowMap>
marci@658
  1277
  class ResForwardFilter {
marci@658
  1278
    //    const Graph* graph;
marci@650
  1279
    const CapacityMap* capacity;
marci@650
  1280
    const FlowMap* flow;
marci@650
  1281
  public:
marci@658
  1282
    ResForwardFilter(/*const Graph& _graph, */
marci@658
  1283
		     const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1284
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1285
    ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@656
  1286
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1287
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1288
    bool operator[](const typename Graph::Edge& e) const {
marci@738
  1289
      return (Number((*flow)[e]) < Number((*capacity)[e]));
marci@650
  1290
    }
marci@650
  1291
  };
marci@650
  1292
marci@650
  1293
  template<typename Graph, typename Number,
marci@650
  1294
	   typename CapacityMap, typename FlowMap>
marci@658
  1295
  class ResBackwardFilter {
marci@650
  1296
    const CapacityMap* capacity;
marci@650
  1297
    const FlowMap* flow;
marci@650
  1298
  public:
marci@658
  1299
    ResBackwardFilter(/*const Graph& _graph,*/ 
marci@658
  1300
		      const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1301
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1302
    ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@656
  1303
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1304
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1305
    bool operator[](const typename Graph::Edge& e) const {
marci@738
  1306
      return (Number(0) < Number((*flow)[e]));
marci@650
  1307
    }
marci@650
  1308
  };
marci@650
  1309
marci@653
  1310
  
marci@653
  1311
  /// A wrapper for composing the residual graph for directed flow and circulation problems.
marci@650
  1312
alpar@879
  1313
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
  1314
  ///parts of the lib. Use them at you own risk.
alpar@879
  1315
  ///
marci@653
  1316
  /// A wrapper for composing the residual graph for directed flow and circulation problems.
marci@650
  1317
  template<typename Graph, typename Number, 
marci@650
  1318
	   typename CapacityMap, typename FlowMap>
marci@653
  1319
  class ResGraphWrapper : 
marci@650
  1320
    public SubBidirGraphWrapper< 
marci@650
  1321
    Graph, 
marci@658
  1322
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1323
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
marci@650
  1324
  public:
marci@650
  1325
    typedef SubBidirGraphWrapper< 
marci@650
  1326
      Graph, 
marci@658
  1327
      ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1328
      ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
marci@650
  1329
  protected:
marci@650
  1330
    const CapacityMap* capacity;
marci@650
  1331
    FlowMap* flow;
marci@658
  1332
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
marci@658
  1333
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
marci@658
  1334
    ResGraphWrapper() : Parent(), 
marci@658
  1335
 			capacity(0), flow(0) { }
marci@658
  1336
    void setCapacityMap(const CapacityMap& _capacity) {
marci@658
  1337
      capacity=&_capacity;
marci@658
  1338
      forward_filter.setCapacity(_capacity);
marci@658
  1339
      backward_filter.setCapacity(_capacity);
marci@658
  1340
    }
marci@658
  1341
    void setFlowMap(FlowMap& _flow) {
marci@658
  1342
      flow=&_flow;
marci@658
  1343
      forward_filter.setFlow(_flow);
marci@658
  1344
      backward_filter.setFlow(_flow);
marci@658
  1345
    }
marci@650
  1346
  public:
marci@653
  1347
    ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity, 
marci@650
  1348
		       FlowMap& _flow) : 
marci@650
  1349
      Parent(), capacity(&_capacity), flow(&_flow), 
marci@658
  1350
      forward_filter(/*_graph,*/ _capacity, _flow), 
marci@658
  1351
      backward_filter(/*_graph,*/ _capacity, _flow) {
marci@650
  1352
      Parent::setGraph(_graph);
marci@650
  1353
      Parent::setForwardFilterMap(forward_filter);
marci@650
  1354
      Parent::setBackwardFilterMap(backward_filter);
marci@650
  1355
    }
marci@650
  1356
marci@660
  1357
    typedef typename Parent::Edge Edge;
marci@660
  1358
marci@660
  1359
    void augment(const Edge& e, Number a) const {
marci@650
  1360
      if (Parent::forward(e))  
marci@650
  1361
	flow->set(e, (*flow)[e]+a);
marci@650
  1362
      else  
marci@650
  1363
	flow->set(e, (*flow)[e]-a);
marci@650
  1364
    }
marci@650
  1365
marci@660
  1366
    /// \brief Residual capacity map.
marci@660
  1367
    ///
marci@910
  1368
    /// In generic residual graphs the residual capacity can be obtained 
marci@910
  1369
    /// as a map. 
marci@660
  1370
    class ResCap {
marci@660
  1371
    protected:
marci@660
  1372
      const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
marci@660
  1373
    public:
marci@660
  1374
      typedef Number ValueType;
marci@660
  1375
      typedef Edge KeyType;
marci@888
  1376
      ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& 
marci@888
  1377
	     _res_graph) : res_graph(&_res_graph) { }
marci@660
  1378
      Number operator[](const Edge& e) const { 
marci@660
  1379
	if (res_graph->forward(e)) 
marci@660
  1380
	  return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e]; 
marci@660
  1381
	else 
marci@660
  1382
	  return (*(res_graph->flow))[e]; 
marci@660
  1383
      }
marci@660
  1384
    };
marci@660
  1385
deba@891
  1386
    //    KEEP_MAPS(Parent, ResGraphWrapper);
marci@650
  1387
  };
marci@650
  1388
marci@650
  1389
marci@612
  1390
  /// For blocking flows.
marci@556
  1391
alpar@879
  1392
  ///\warning Graph wrappers are in even more experimental state than the other
alpar@879
  1393
  ///parts of the lib. Use them at you own risk.
alpar@879
  1394
  ///
marci@792
  1395
  /// This graph wrapper is used for on-the-fly 
marci@792
  1396
  /// Dinits blocking flow computations.
marci@612
  1397
  /// For each node, an out-edge is stored which is used when the 
marci@612
  1398
  /// \code 
marci@612
  1399
  /// OutEdgeIt& first(OutEdgeIt&, const Node&)
marci@612
  1400
  /// \endcode
marci@612
  1401
  /// is called. 
marci@556
  1402
  ///
marci@792
  1403
  /// \author Marton Makai
marci@556
  1404
  template<typename Graph, typename FirstOutEdgesMap>
marci@556
  1405
  class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
marci@650
  1406
  public:
marci@650
  1407
    typedef GraphWrapper<Graph> Parent; 
marci@556
  1408
  protected:
marci@556
  1409
    FirstOutEdgesMap* first_out_edges;
marci@556
  1410
  public:
marci@556
  1411
    ErasingFirstGraphWrapper(Graph& _graph, 
marci@556
  1412
			     FirstOutEdgesMap& _first_out_edges) : 
marci@556
  1413
      GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }  
marci@556
  1414
marci@556
  1415
    typedef typename GraphWrapper<Graph>::Node Node;
marci@556
  1416
    typedef typename GraphWrapper<Graph>::Edge Edge;
marci@777
  1417
    class OutEdgeIt : public Edge { 
marci@556
  1418
      friend class GraphWrapper<Graph>;
marci@556
  1419
      friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
marci@777
  1420
      const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>* gw;
marci@556
  1421
    public:
marci@556
  1422
      OutEdgeIt() { }
marci@777
  1423
      OutEdgeIt(Invalid i) : Edge(i) { }
marci@777
  1424
      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw, 
marci@777
  1425
		const Node& n) : 
marci@777
  1426
	Edge((*(_gw.first_out_edges))[n]), gw(&_gw) { }
marci@777
  1427
      OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw, 
marci@777
  1428
		const Edge& e) : 
marci@777
  1429
	Edge(e), gw(&_gw) { }
marci@777
  1430
      OutEdgeIt& operator++() { 
marci@777
  1431
	*(static_cast<Edge*>(this))=
marci@777
  1432
	  ++(typename Graph::OutEdgeIt(*(gw->graph), *this));
marci@777
  1433
	return *this; 
marci@777
  1434
      }
marci@556
  1435
    };
marci@556
  1436
marci@556
  1437
    using GraphWrapper<Graph>::first;
marci@556
  1438
    OutEdgeIt& first(OutEdgeIt& i, const Node& p) const { 
marci@556
  1439
      i=OutEdgeIt(*this, p); return i;
marci@556
  1440
    }
marci@777
  1441
    void erase(const Edge& e) const {
marci@777
  1442
      Node n=tail(e);
deba@844
  1443
      typename Graph::OutEdgeIt f(*Parent::graph, n);
marci@777
  1444
      ++f;
marci@777
  1445
      first_out_edges->set(n, f);
marci@556
  1446
    }
deba@877
  1447
deba@891
  1448
    //    KEEP_MAPS(Parent, ErasingFirstGraphWrapper);
marci@556
  1449
  };
marci@556
  1450
marci@556
  1451
  ///@}
marci@556
  1452
alpar@921
  1453
} //namespace lemon
marci@556
  1454
alpar@921
  1455
#endif //LEMON_GRAPH_WRAPPER_H
marci@556
  1456