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