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