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