lemon/graph_adaptor.h
author ladanyi
Wed, 17 Aug 2005 20:39:11 +0000
changeset 1639 aec340db8fc2
parent 1627 3fd1ba6e9872
child 1669 66ae78d29f1e
permissions -rw-r--r--
fixed a typo
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/graph_adaptor.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@1401
    17
#ifndef LEMON_GRAPH_ADAPTOR_H
alpar@1401
    18
#define LEMON_GRAPH_ADAPTOR_H
marci@556
    19
alpar@1401
    20
///\ingroup graph_adaptors
marci@556
    21
///\file
alpar@1401
    22
///\brief Several graph adaptors.
marci@556
    23
///
alpar@1401
    24
///This file contains several useful graph adaptor 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@1472
    30
#include <lemon/bits/erasable_graph_extender.h>
deba@1472
    31
#include <lemon/bits/clearable_graph_extender.h>
deba@1472
    32
#include <lemon/bits/extendable_graph_extender.h>
deba@1307
    33
#include <lemon/bits/iterable_graph_extender.h>
deba@1472
    34
#include <lemon/bits/alteration_notifier.h>
deba@1472
    35
#include <lemon/bits/default_map.h>
marci@1383
    36
#include <lemon/bits/undir_graph_extender.h>
alpar@774
    37
#include <iostream>
marci@556
    38
alpar@921
    39
namespace lemon {
marci@556
    40
alpar@1401
    41
  // Graph adaptors
marci@556
    42
marci@1172
    43
  /*!
alpar@1401
    44
    \addtogroup graph_adaptors
marci@1004
    45
    @{
marci@1172
    46
   */
marci@556
    47
marci@1172
    48
  /*! 
alpar@1401
    49
    Base type for the Graph Adaptors
marci@1242
    50
    
alpar@1401
    51
    \warning Graph adaptors are in even more experimental state than the other
marci@1004
    52
    parts of the lib. Use them at you own risk.
marci@1242
    53
    
alpar@1401
    54
    This is the base type for most of LEMON graph adaptors. 
alpar@1401
    55
    This class implements a trivial graph adaptor i.e. it only wraps the 
marci@1004
    56
    functions and types of the graph. The purpose of this class is to 
alpar@1401
    57
    make easier implementing graph adaptors. E.g. if an adaptor is 
marci@1004
    58
    considered which differs from the wrapped graph only in some of its 
alpar@1401
    59
    functions or types, then it can be derived from GraphAdaptor, and only the 
marci@1004
    60
    differences should be implemented.
marci@1004
    61
  
marci@1004
    62
    \author Marton Makai 
marci@1004
    63
  */
marci@970
    64
  template<typename _Graph>
alpar@1401
    65
  class GraphAdaptorBase {
marci@970
    66
  public:
marci@970
    67
    typedef _Graph Graph;
marci@970
    68
    /// \todo Is it needed?
marci@970
    69
    typedef Graph BaseGraph;
marci@970
    70
    typedef Graph ParentGraph;
marci@970
    71
marci@556
    72
  protected:
marci@556
    73
    Graph* graph;
alpar@1401
    74
    GraphAdaptorBase() : graph(0) { }
marci@556
    75
    void setGraph(Graph& _graph) { graph=&_graph; }
marci@556
    76
marci@556
    77
  public:
alpar@1401
    78
    GraphAdaptorBase(Graph& _graph) : graph(&_graph) { }
marci@556
    79
 
alpar@774
    80
    typedef typename Graph::Node Node;
alpar@774
    81
    typedef typename Graph::Edge Edge;
marci@556
    82
   
marci@970
    83
    void first(Node& i) const { graph->first(i); }
marci@970
    84
    void first(Edge& i) const { graph->first(i); }
marci@970
    85
    void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
marci@970
    86
    void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
marci@556
    87
marci@970
    88
    void next(Node& i) const { graph->next(i); }
marci@970
    89
    void next(Edge& i) const { graph->next(i); }
marci@970
    90
    void nextIn(Edge& i) const { graph->nextIn(i); }
marci@970
    91
    void nextOut(Edge& i) const { graph->nextOut(i); }
marci@970
    92
alpar@986
    93
    Node source(const Edge& e) const { return graph->source(e); }
alpar@986
    94
    Node target(const Edge& e) const { return graph->target(e); }
marci@556
    95
marci@556
    96
    int nodeNum() const { return graph->nodeNum(); }
marci@556
    97
    int edgeNum() const { return graph->edgeNum(); }
marci@556
    98
  
marci@556
    99
    Node addNode() const { return Node(graph->addNode()); }
alpar@986
   100
    Edge addEdge(const Node& source, const Node& target) const { 
alpar@986
   101
      return Edge(graph->addEdge(source, target)); }
marci@556
   102
marci@556
   103
    void erase(const Node& i) const { graph->erase(i); }
marci@556
   104
    void erase(const Edge& i) const { graph->erase(i); }
marci@556
   105
  
marci@556
   106
    void clear() const { graph->clear(); }
marci@556
   107
    
marci@739
   108
    int id(const Node& v) const { return graph->id(v); }
marci@739
   109
    int id(const Edge& e) const { return graph->id(e); }
marci@650
   110
    
deba@1627
   111
    Edge oppositeNode(const Edge& e) const { 
deba@1627
   112
      return Edge(graph->opposite(e)); 
deba@1627
   113
    }
marci@650
   114
marci@970
   115
    template <typename _Value>
marci@970
   116
    class NodeMap : public _Graph::template NodeMap<_Value> {
marci@970
   117
    public:
marci@970
   118
      typedef typename _Graph::template NodeMap<_Value> Parent;
alpar@1401
   119
      NodeMap(const GraphAdaptorBase<_Graph>& gw) : Parent(*gw.graph) { }
alpar@1401
   120
      NodeMap(const GraphAdaptorBase<_Graph>& gw, const _Value& value)
marci@970
   121
      : Parent(*gw.graph, value) { }
marci@970
   122
    };
marci@556
   123
marci@970
   124
    template <typename _Value>
marci@970
   125
    class EdgeMap : public _Graph::template EdgeMap<_Value> {
marci@970
   126
    public:
marci@970
   127
      typedef typename _Graph::template EdgeMap<_Value> Parent;
alpar@1401
   128
      EdgeMap(const GraphAdaptorBase<_Graph>& gw) : Parent(*gw.graph) { }
alpar@1401
   129
      EdgeMap(const GraphAdaptorBase<_Graph>& gw, const _Value& value)
marci@970
   130
      : Parent(*gw.graph, value) { }
marci@970
   131
    };
deba@877
   132
marci@556
   133
  };
marci@556
   134
marci@970
   135
  template <typename _Graph>
alpar@1401
   136
  class GraphAdaptor :
alpar@1401
   137
    public IterableGraphExtender<GraphAdaptorBase<_Graph> > { 
marci@970
   138
  public:
marci@970
   139
    typedef _Graph Graph;
alpar@1401
   140
    typedef IterableGraphExtender<GraphAdaptorBase<_Graph> > Parent;
marci@970
   141
  protected:
alpar@1401
   142
    GraphAdaptor() : Parent() { }
marci@569
   143
marci@970
   144
  public:
alpar@1401
   145
    GraphAdaptor(Graph& _graph) { setGraph(_graph); }
marci@970
   146
  };
marci@569
   147
marci@997
   148
  template <typename _Graph>
alpar@1401
   149
  class RevGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
marci@997
   150
  public:
marci@997
   151
    typedef _Graph Graph;
alpar@1401
   152
    typedef GraphAdaptorBase<_Graph> Parent;
marci@997
   153
  protected:
alpar@1401
   154
    RevGraphAdaptorBase() : Parent() { }
marci@997
   155
  public:
marci@997
   156
    typedef typename Parent::Node Node;
marci@997
   157
    typedef typename Parent::Edge Edge;
marci@997
   158
marci@1383
   159
    //    using Parent::first;
marci@997
   160
    void firstIn(Edge& i, const Node& n) const { Parent::firstOut(i, n); }
marci@997
   161
    void firstOut(Edge& i, const Node& n ) const { Parent::firstIn(i, n); }
marci@997
   162
marci@1383
   163
    //    using Parent::next;
marci@997
   164
    void nextIn(Edge& i) const { Parent::nextOut(i); }
marci@997
   165
    void nextOut(Edge& i) const { Parent::nextIn(i); }
marci@997
   166
marci@997
   167
    Node source(const Edge& e) const { return Parent::target(e); }
marci@997
   168
    Node target(const Edge& e) const { return Parent::source(e); }
marci@997
   169
  };
marci@997
   170
    
marci@997
   171
alpar@1401
   172
  /// A graph adaptor which reverses the orientation of the edges.
marci@556
   173
alpar@1401
   174
  ///\warning Graph adaptors are in even more experimental state than the other
alpar@879
   175
  ///parts of the lib. Use them at you own risk.
alpar@879
   176
  ///
marci@923
   177
  /// Let \f$G=(V, A)\f$ be a directed graph and 
marci@923
   178
  /// suppose that a graph instange \c g of type 
marci@923
   179
  /// \c ListGraph implements \f$G\f$.
marci@923
   180
  /// \code
marci@923
   181
  /// ListGraph g;
marci@923
   182
  /// \endcode
marci@923
   183
  /// For each directed edge 
marci@923
   184
  /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by 
marci@923
   185
  /// reversing its orientation. 
alpar@1401
   186
  /// Then RevGraphAdaptor implements the graph structure with node-set 
marci@923
   187
  /// \f$V\f$ and edge-set 
marci@923
   188
  /// \f$\{\bar e : e\in A \}\f$, i.e. the graph obtained from \f$G\f$ be 
marci@923
   189
  /// reversing the orientation of its edges. The following code shows how 
marci@923
   190
  /// such an instance can be constructed.
marci@923
   191
  /// \code
alpar@1401
   192
  /// RevGraphAdaptor<ListGraph> gw(g);
marci@923
   193
  /// \endcode
marci@556
   194
  ///\author Marton Makai
marci@997
   195
  template<typename _Graph>
alpar@1401
   196
  class RevGraphAdaptor : 
alpar@1401
   197
    public IterableGraphExtender<RevGraphAdaptorBase<_Graph> > {
marci@650
   198
  public:
marci@997
   199
    typedef _Graph Graph;
marci@997
   200
    typedef IterableGraphExtender<
alpar@1401
   201
      RevGraphAdaptorBase<_Graph> > Parent;
marci@556
   202
  protected:
alpar@1401
   203
    RevGraphAdaptor() { }
marci@556
   204
  public:
alpar@1401
   205
    RevGraphAdaptor(_Graph& _graph) { setGraph(_graph); }
marci@997
   206
  };
marci@556
   207
marci@992
   208
  
marci@992
   209
  template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
alpar@1401
   210
  class SubGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
marci@992
   211
  public:
marci@992
   212
    typedef _Graph Graph;
alpar@1401
   213
    typedef GraphAdaptorBase<_Graph> Parent;
marci@992
   214
  protected:
marci@992
   215
    NodeFilterMap* node_filter_map;
marci@992
   216
    EdgeFilterMap* edge_filter_map;
alpar@1401
   217
    SubGraphAdaptorBase() : Parent(), 
marci@992
   218
			    node_filter_map(0), edge_filter_map(0) { }
marci@775
   219
marci@992
   220
    void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
marci@992
   221
      node_filter_map=&_node_filter_map;
marci@992
   222
    }
marci@992
   223
    void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
marci@992
   224
      edge_filter_map=&_edge_filter_map;
marci@992
   225
    }
marci@992
   226
marci@992
   227
  public:
alpar@1401
   228
//     SubGraphAdaptorBase(Graph& _graph, 
marci@992
   229
// 			NodeFilterMap& _node_filter_map, 
marci@992
   230
// 			EdgeFilterMap& _edge_filter_map) : 
marci@992
   231
//       Parent(&_graph), 
marci@992
   232
//       node_filter_map(&node_filter_map), 
marci@992
   233
//       edge_filter_map(&edge_filter_map) { }
marci@992
   234
marci@992
   235
    typedef typename Parent::Node Node;
marci@992
   236
    typedef typename Parent::Edge Edge;
marci@992
   237
marci@992
   238
    void first(Node& i) const { 
marci@992
   239
      Parent::first(i); 
marci@992
   240
      while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i); 
marci@992
   241
    }
marci@992
   242
    void first(Edge& i) const { 
marci@992
   243
      Parent::first(i); 
marci@992
   244
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i); 
marci@992
   245
    }
marci@992
   246
    void firstIn(Edge& i, const Node& n) const { 
marci@992
   247
      Parent::firstIn(i, n); 
marci@992
   248
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i); 
marci@992
   249
    }
marci@992
   250
    void firstOut(Edge& i, const Node& n) const { 
marci@992
   251
      Parent::firstOut(i, n); 
marci@992
   252
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i); 
marci@992
   253
    }
marci@992
   254
marci@992
   255
    void next(Node& i) const { 
marci@992
   256
      Parent::next(i); 
marci@992
   257
      while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i); 
marci@992
   258
    }
marci@992
   259
    void next(Edge& i) const { 
marci@992
   260
      Parent::next(i); 
marci@992
   261
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i); 
marci@992
   262
    }
marci@992
   263
    void nextIn(Edge& i) const { 
marci@992
   264
      Parent::nextIn(i); 
marci@992
   265
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i); 
marci@992
   266
    }
marci@992
   267
    void nextOut(Edge& i) const { 
marci@992
   268
      Parent::nextOut(i); 
marci@992
   269
      while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i); 
marci@992
   270
    }
marci@992
   271
marci@992
   272
    /// This function hides \c n in the graph, i.e. the iteration 
marci@992
   273
    /// jumps over it. This is done by simply setting the value of \c n  
marci@992
   274
    /// to be false in the corresponding node-map.
marci@992
   275
    void hide(const Node& n) const { node_filter_map->set(n, false); }
marci@992
   276
marci@992
   277
    /// This function hides \c e in the graph, i.e. the iteration 
marci@992
   278
    /// jumps over it. This is done by simply setting the value of \c e  
marci@992
   279
    /// to be false in the corresponding edge-map.
marci@992
   280
    void hide(const Edge& e) const { edge_filter_map->set(e, false); }
marci@992
   281
marci@992
   282
    /// The value of \c n is set to be true in the node-map which stores 
marci@992
   283
    /// hide information. If \c n was hidden previuosly, then it is shown 
marci@992
   284
    /// again
marci@992
   285
     void unHide(const Node& n) const { node_filter_map->set(n, true); }
marci@992
   286
marci@992
   287
    /// The value of \c e is set to be true in the edge-map which stores 
marci@992
   288
    /// hide information. If \c e was hidden previuosly, then it is shown 
marci@992
   289
    /// again
marci@992
   290
    void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
marci@992
   291
marci@992
   292
    /// Returns true if \c n is hidden.
marci@992
   293
    bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
marci@992
   294
marci@992
   295
    /// Returns true if \c n is hidden.
marci@992
   296
    bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
marci@992
   297
marci@992
   298
    /// \warning This is a linear time operation and works only if s
marci@992
   299
    /// \c Graph::NodeIt is defined.
marci@992
   300
    /// \todo assign tags.
marci@992
   301
    int nodeNum() const { 
marci@992
   302
      int i=0;
marci@992
   303
      Node n;
marci@992
   304
      for (first(n); n!=INVALID; next(n)) ++i;
marci@992
   305
      return i; 
marci@992
   306
    }
marci@992
   307
marci@992
   308
    /// \warning This is a linear time operation and works only if 
marci@992
   309
    /// \c Graph::EdgeIt is defined.
marci@992
   310
    /// \todo assign tags.
marci@992
   311
    int edgeNum() const { 
marci@992
   312
      int i=0;
marci@992
   313
      Edge e;
marci@992
   314
      for (first(e); e!=INVALID; next(e)) ++i;
marci@992
   315
      return i; 
marci@992
   316
    }
marci@992
   317
marci@992
   318
marci@992
   319
  };
marci@775
   320
alpar@1401
   321
  /*! \brief A graph adaptor for hiding nodes and edges from a graph.
marci@1242
   322
    
alpar@1401
   323
  \warning Graph adaptors are in even more experimental state than the other
marci@930
   324
  parts of the lib. Use them at you own risk.
marci@930
   325
  
alpar@1401
   326
  SubGraphAdaptor shows the graph with filtered node-set and 
marci@930
   327
  edge-set. 
marci@1242
   328
  Let \f$G=(V, A)\f$ be a directed graph 
marci@1242
   329
  and suppose that the graph instance \c g of type ListGraph implements 
marci@1242
   330
  \f$G\f$. 
marci@1242
   331
  Let moreover \f$b_V\f$ and 
marci@1242
   332
  \f$b_A\f$ be bool-valued functions resp. on the node-set and edge-set. 
alpar@1401
   333
  SubGraphAdaptor<...>::NodeIt iterates 
marci@1242
   334
  on the node-set \f$\{v\in V : b_V(v)=true\}\f$ and 
alpar@1401
   335
  SubGraphAdaptor<...>::EdgeIt iterates 
marci@1242
   336
  on the edge-set \f$\{e\in A : b_A(e)=true\}\f$. Similarly, 
alpar@1401
   337
  SubGraphAdaptor<...>::OutEdgeIt and SubGraphAdaptor<...>::InEdgeIt iterates 
marci@1242
   338
  only on edges leaving and entering a specific node which have true value.
marci@1242
   339
marci@1242
   340
  We have to note that this does not mean that an 
marci@930
   341
  induced subgraph is obtained, the node-iterator cares only the filter 
marci@930
   342
  on the node-set, and the edge-iterators care only the filter on the 
marci@1242
   343
  edge-set. 
marci@930
   344
  \code
marci@1242
   345
  typedef ListGraph Graph;
marci@930
   346
  Graph g;
marci@930
   347
  typedef Graph::Node Node;
marci@930
   348
  typedef Graph::Edge Edge;
marci@930
   349
  Node u=g.addNode(); //node of id 0
marci@930
   350
  Node v=g.addNode(); //node of id 1
marci@930
   351
  Node e=g.addEdge(u, v); //edge of id 0
marci@930
   352
  Node f=g.addEdge(v, u); //edge of id 1
marci@930
   353
  Graph::NodeMap<bool> nm(g, true);
marci@930
   354
  nm.set(u, false);
marci@930
   355
  Graph::EdgeMap<bool> em(g, true);
marci@930
   356
  em.set(e, false);
alpar@1401
   357
  typedef SubGraphAdaptor<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGW;
marci@930
   358
  SubGW gw(g, nm, em);
marci@930
   359
  for (SubGW::NodeIt n(gw); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
marci@930
   360
  std::cout << ":-)" << std::endl;
marci@930
   361
  for (SubGW::EdgeIt e(gw); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
marci@930
   362
  \endcode
marci@930
   363
  The output of the above code is the following.
marci@930
   364
  \code
marci@930
   365
  1
marci@930
   366
  :-)
marci@930
   367
  1
marci@930
   368
  \endcode
marci@930
   369
  Note that \c n is of type \c SubGW::NodeIt, but it can be converted to
marci@930
   370
  \c Graph::Node that is why \c g.id(n) can be applied.
marci@930
   371
alpar@1401
   372
  For other examples see also the documentation of NodeSubGraphAdaptor and 
alpar@1401
   373
  EdgeSubGraphAdaptor.
marci@930
   374
marci@930
   375
  \author Marton Makai
marci@930
   376
  */
marci@992
   377
  template<typename _Graph, typename NodeFilterMap, 
marci@556
   378
	   typename EdgeFilterMap>
alpar@1401
   379
  class SubGraphAdaptor : 
marci@992
   380
    public IterableGraphExtender<
alpar@1401
   381
    SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap> > {
marci@650
   382
  public:
marci@992
   383
    typedef _Graph Graph;
marci@992
   384
    typedef IterableGraphExtender<
alpar@1401
   385
      SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent;
marci@556
   386
  protected:
alpar@1401
   387
    SubGraphAdaptor() { }
marci@992
   388
  public:
alpar@1401
   389
    SubGraphAdaptor(_Graph& _graph, NodeFilterMap& _node_filter_map, 
marci@992
   390
		    EdgeFilterMap& _edge_filter_map) { 
marci@992
   391
      setGraph(_graph);
marci@992
   392
      setNodeFilterMap(_node_filter_map);
marci@992
   393
      setEdgeFilterMap(_edge_filter_map);
marci@992
   394
    }
marci@992
   395
  };
marci@556
   396
marci@556
   397
marci@569
   398
alpar@1401
   399
  /*! \brief An adaptor for hiding nodes from a graph.
marci@933
   400
alpar@1401
   401
  \warning Graph adaptors are in even more experimental state than the other
marci@933
   402
  parts of the lib. Use them at you own risk.
marci@933
   403
  
alpar@1401
   404
  An adaptor for hiding nodes from a graph.
alpar@1401
   405
  This adaptor specializes SubGraphAdaptor in the way that only the node-set 
marci@933
   406
  can be filtered. Note that this does not mean of considering induced 
marci@933
   407
  subgraph, the edge-iterators consider the original edge-set.
marci@933
   408
  \author Marton Makai
marci@933
   409
  */
marci@933
   410
  template<typename Graph, typename NodeFilterMap>
alpar@1401
   411
  class NodeSubGraphAdaptor : 
alpar@1401
   412
    public SubGraphAdaptor<Graph, NodeFilterMap, 
marci@933
   413
			   ConstMap<typename Graph::Edge,bool> > {
marci@933
   414
  public:
alpar@1401
   415
    typedef SubGraphAdaptor<Graph, NodeFilterMap, 
marci@933
   416
			    ConstMap<typename Graph::Edge,bool> > Parent;
marci@933
   417
  protected:
marci@933
   418
    ConstMap<typename Graph::Edge, bool> const_true_map;
marci@933
   419
  public:
alpar@1401
   420
    NodeSubGraphAdaptor(Graph& _graph, NodeFilterMap& _node_filter_map) : 
marci@933
   421
      Parent(), const_true_map(true) { 
marci@933
   422
      Parent::setGraph(_graph);
marci@933
   423
      Parent::setNodeFilterMap(_node_filter_map);
marci@933
   424
      Parent::setEdgeFilterMap(const_true_map);
marci@933
   425
    }
marci@933
   426
  };
marci@933
   427
marci@933
   428
alpar@1401
   429
  /*! \brief An adaptor for hiding edges from a graph.
marci@932
   430
alpar@1401
   431
  \warning Graph adaptors are in even more experimental state than the other
marci@932
   432
  parts of the lib. Use them at you own risk.
marci@932
   433
  
alpar@1401
   434
  An adaptor for hiding edges from a graph.
alpar@1401
   435
  This adaptor specializes SubGraphAdaptor in the way that only the edge-set 
alpar@1401
   436
  can be filtered. The usefulness of this adaptor is demonstrated in the 
marci@933
   437
  problem of searching a maximum number of edge-disjoint shortest paths 
marci@933
   438
  between 
marci@933
   439
  two nodes \c s and \c t. Shortest here means being shortest w.r.t. 
marci@933
   440
  non-negative edge-lengths. Note that 
marci@933
   441
  the comprehension of the presented solution 
marci@1252
   442
  need's some elementary knowledge from combinatorial optimization. 
marci@933
   443
marci@933
   444
  If a single shortest path is to be 
marci@1252
   445
  searched between \c s and \c t, then this can be done easily by 
marci@1252
   446
  applying the Dijkstra algorithm. What happens, if a maximum number of 
marci@933
   447
  edge-disjoint shortest paths is to be computed. It can be proved that an 
marci@933
   448
  edge can be in a shortest path if and only if it is tight with respect to 
marci@933
   449
  the potential function computed by Dijkstra. Moreover, any path containing 
marci@933
   450
  only such edges is a shortest one. Thus we have to compute a maximum number 
marci@933
   451
  of edge-disjoint paths between \c s and \c t in the graph which has edge-set 
marci@933
   452
  all the tight edges. The computation will be demonstrated on the following 
alpar@1536
   453
  graph, which is read from the dimacs file \c sub_graph_adaptor_demo.dim. 
marci@1425
   454
  The full source code is available in \ref sub_graph_adaptor_demo.cc. 
marci@1425
   455
  If you are interested in more demo programs, you can use 
marci@1425
   456
  \ref dim_to_dot.cc to generate .dot files from dimacs files. 
athos@1576
   457
  The .dot file of the following figure was generated by  
marci@1425
   458
  the demo program \ref dim_to_dot.cc.
marci@1425
   459
marci@933
   460
  \dot
marci@933
   461
  digraph lemon_dot_example {
marci@933
   462
  node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
marci@933
   463
  n0 [ label="0 (s)" ];
marci@933
   464
  n1 [ label="1" ];
marci@933
   465
  n2 [ label="2" ];
marci@933
   466
  n3 [ label="3" ];
marci@933
   467
  n4 [ label="4" ];
marci@933
   468
  n5 [ label="5" ];
marci@933
   469
  n6 [ label="6 (t)" ];
marci@933
   470
  edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
marci@933
   471
  n5 ->  n6 [ label="9, length:4" ];
marci@933
   472
  n4 ->  n6 [ label="8, length:2" ];
marci@933
   473
  n3 ->  n5 [ label="7, length:1" ];
marci@933
   474
  n2 ->  n5 [ label="6, length:3" ];
marci@933
   475
  n2 ->  n6 [ label="5, length:5" ];
marci@933
   476
  n2 ->  n4 [ label="4, length:2" ];
marci@933
   477
  n1 ->  n4 [ label="3, length:3" ];
marci@933
   478
  n0 ->  n3 [ label="2, length:1" ];
marci@933
   479
  n0 ->  n2 [ label="1, length:2" ];
marci@933
   480
  n0 ->  n1 [ label="0, length:3" ];
marci@933
   481
  }
marci@933
   482
  \enddot
marci@933
   483
marci@933
   484
  \code
marci@933
   485
  Graph g;
marci@933
   486
  Node s, t;
marci@933
   487
  LengthMap length(g);
marci@933
   488
marci@933
   489
  readDimacs(std::cin, g, length, s, t);
marci@933
   490
alpar@986
   491
  cout << "edges with lengths (of form id, source--length->target): " << endl;
marci@933
   492
  for(EdgeIt e(g); e!=INVALID; ++e) 
alpar@986
   493
    cout << g.id(e) << ", " << g.id(g.source(e)) << "--" 
alpar@986
   494
         << length[e] << "->" << g.id(g.target(e)) << endl;
marci@933
   495
marci@933
   496
  cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
marci@933
   497
  \endcode
marci@933
   498
  Next, the potential function is computed with Dijkstra.
marci@933
   499
  \code
marci@933
   500
  typedef Dijkstra<Graph, LengthMap> Dijkstra;
marci@933
   501
  Dijkstra dijkstra(g, length);
marci@933
   502
  dijkstra.run(s);
marci@933
   503
  \endcode
marci@933
   504
  Next, we consrtruct a map which filters the edge-set to the tight edges.
marci@933
   505
  \code
marci@933
   506
  typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap> 
marci@933
   507
    TightEdgeFilter;
marci@933
   508
  TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
marci@933
   509
  
alpar@1401
   510
  typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGW;
marci@933
   511
  SubGW gw(g, tight_edge_filter);
marci@933
   512
  \endcode
marci@933
   513
  Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed 
marci@933
   514
  with a max flow algorithm Preflow.
marci@933
   515
  \code
marci@933
   516
  ConstMap<Edge, int> const_1_map(1);
marci@933
   517
  Graph::EdgeMap<int> flow(g, 0);
marci@933
   518
marci@933
   519
  Preflow<SubGW, int, ConstMap<Edge, int>, Graph::EdgeMap<int> > 
marci@933
   520
    preflow(gw, s, t, const_1_map, flow);
marci@933
   521
  preflow.run();
marci@933
   522
  \endcode
marci@933
   523
  Last, the output is:
marci@933
   524
  \code  
marci@933
   525
  cout << "maximum number of edge-disjoint shortest path: " 
marci@933
   526
       << preflow.flowValue() << endl;
marci@933
   527
  cout << "edges of the maximum number of edge-disjoint shortest s-t paths: " 
marci@933
   528
       << endl;
marci@933
   529
  for(EdgeIt e(g); e!=INVALID; ++e) 
marci@933
   530
    if (flow[e])
alpar@986
   531
      cout << " " << g.id(g.source(e)) << "--" 
alpar@986
   532
	   << length[e] << "->" << g.id(g.target(e)) << endl;
marci@933
   533
  \endcode
marci@933
   534
  The program has the following (expected :-)) output:
marci@933
   535
  \code
alpar@986
   536
  edges with lengths (of form id, source--length->target):
marci@933
   537
   9, 5--4->6
marci@933
   538
   8, 4--2->6
marci@933
   539
   7, 3--1->5
marci@933
   540
   6, 2--3->5
marci@933
   541
   5, 2--5->6
marci@933
   542
   4, 2--2->4
marci@933
   543
   3, 1--3->4
marci@933
   544
   2, 0--1->3
marci@933
   545
   1, 0--2->2
marci@933
   546
   0, 0--3->1
marci@933
   547
  s: 0 t: 6
marci@933
   548
  maximum number of edge-disjoint shortest path: 2
marci@933
   549
  edges of the maximum number of edge-disjoint shortest s-t paths:
marci@933
   550
   9, 5--4->6
marci@933
   551
   8, 4--2->6
marci@933
   552
   7, 3--1->5
marci@933
   553
   4, 2--2->4
marci@933
   554
   2, 0--1->3
marci@933
   555
   1, 0--2->2
marci@933
   556
  \endcode
marci@933
   557
marci@932
   558
  \author Marton Makai
marci@932
   559
  */
marci@932
   560
  template<typename Graph, typename EdgeFilterMap>
alpar@1401
   561
  class EdgeSubGraphAdaptor : 
alpar@1401
   562
    public SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>, 
marci@932
   563
			   EdgeFilterMap> {
marci@932
   564
  public:
alpar@1401
   565
    typedef SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>, 
marci@932
   566
			    EdgeFilterMap> Parent;
marci@932
   567
  protected:
marci@932
   568
    ConstMap<typename Graph::Node, bool> const_true_map;
marci@932
   569
  public:
alpar@1401
   570
    EdgeSubGraphAdaptor(Graph& _graph, EdgeFilterMap& _edge_filter_map) : 
marci@932
   571
      Parent(), const_true_map(true) { 
marci@932
   572
      Parent::setGraph(_graph);
marci@932
   573
      Parent::setNodeFilterMap(const_true_map);
marci@932
   574
      Parent::setEdgeFilterMap(_edge_filter_map);
marci@932
   575
    }
marci@932
   576
  };
marci@932
   577
marci@1383
   578
  template <typename _Graph>
alpar@1401
   579
  class UndirGraphAdaptorBase : 
alpar@1401
   580
    public UndirGraphExtender<GraphAdaptorBase<_Graph> > {
marci@1383
   581
  public:
marci@1383
   582
    typedef _Graph Graph;
alpar@1401
   583
    typedef UndirGraphExtender<GraphAdaptorBase<_Graph> > Parent;
marci@1383
   584
  protected:
alpar@1401
   585
    UndirGraphAdaptorBase() : Parent() { }
marci@1383
   586
  public:
marci@1383
   587
    typedef typename Parent::UndirEdge UndirEdge;
marci@1383
   588
    typedef typename Parent::Edge Edge;
marci@1383
   589
    
marci@1383
   590
    /// \bug Why cant an edge say that it is forward or not??? 
marci@1383
   591
    /// By this, a pointer to the graph have to be stored
marci@1383
   592
    /// The implementation
marci@1383
   593
    template <typename T>
marci@1383
   594
    class EdgeMap {
marci@1383
   595
    protected:
alpar@1401
   596
      const UndirGraphAdaptorBase<_Graph>* g;
marci@1383
   597
      template <typename TT> friend class EdgeMap;
marci@1383
   598
      typename _Graph::template EdgeMap<T> forward_map, backward_map; 
marci@1383
   599
    public:
marci@1383
   600
      typedef T Value;
marci@1383
   601
      typedef Edge Key;
marci@1383
   602
      
alpar@1401
   603
      EdgeMap(const UndirGraphAdaptorBase<_Graph>& _g) : g(&_g), 
marci@1383
   604
	forward_map(*(g->graph)), backward_map(*(g->graph)) { }
marci@569
   605
alpar@1401
   606
      EdgeMap(const UndirGraphAdaptorBase<_Graph>& _g, T a) : g(&_g), 
marci@1383
   607
	forward_map(*(g->graph), a), backward_map(*(g->graph), a) { }
marci@1383
   608
      
marci@1383
   609
      void set(Edge e, T a) { 
deba@1627
   610
	if (g->direction(e)) 
marci@1383
   611
	  forward_map.set(e, a); 
marci@1383
   612
	else 
marci@1383
   613
	  backward_map.set(e, a); 
marci@1383
   614
      }
marci@556
   615
marci@1383
   616
      T operator[](Edge e) const { 
deba@1627
   617
	if (g->direction(e)) 
marci@1383
   618
	  return forward_map[e]; 
marci@1383
   619
	else 
marci@1383
   620
	  return backward_map[e]; 
marci@556
   621
      }
marci@556
   622
    };
marci@1383
   623
        
marci@1383
   624
    template <typename T>
marci@1383
   625
    class UndirEdgeMap {
marci@1383
   626
      template <typename TT> friend class UndirEdgeMap;
marci@1383
   627
      typename _Graph::template EdgeMap<T> map; 
marci@1383
   628
    public:
marci@1383
   629
      typedef T Value;
marci@1383
   630
      typedef UndirEdge Key;
marci@1383
   631
      
alpar@1401
   632
      UndirEdgeMap(const UndirGraphAdaptorBase<_Graph>& g) : 
marci@1383
   633
	map(*(g.graph)) { }
marci@556
   634
alpar@1401
   635
      UndirEdgeMap(const UndirGraphAdaptorBase<_Graph>& g, T a) : 
marci@1383
   636
	map(*(g.graph), a) { }
marci@1383
   637
      
marci@1383
   638
      void set(UndirEdge e, T a) { 
marci@1383
   639
	map.set(e, a); 
marci@1383
   640
      }
marci@556
   641
marci@1383
   642
      T operator[](UndirEdge e) const { 
marci@1383
   643
	return map[e]; 
marci@1383
   644
      }
marci@1383
   645
    };
marci@1383
   646
      
marci@1383
   647
  };
marci@1383
   648
alpar@1401
   649
  /// \brief An undirected graph is made from a directed graph by an adaptor
marci@1383
   650
  ///
marci@1383
   651
  /// Undocumented, untested!!!
marci@1383
   652
  /// If somebody knows nice demo application, let's polulate it.
marci@1383
   653
  /// 
marci@1383
   654
  /// \author Marton Makai
marci@1383
   655
  template<typename _Graph>
alpar@1401
   656
  class UndirGraphAdaptor : 
marci@1383
   657
    public IterableUndirGraphExtender<
alpar@1401
   658
    UndirGraphAdaptorBase<_Graph> > {
marci@1383
   659
  public:
marci@1383
   660
    typedef _Graph Graph;
marci@1383
   661
    typedef IterableUndirGraphExtender<
alpar@1401
   662
      UndirGraphAdaptorBase<_Graph> > Parent;
marci@1383
   663
  protected:
alpar@1401
   664
    UndirGraphAdaptor() { }
marci@1383
   665
  public:
alpar@1401
   666
    UndirGraphAdaptor(_Graph& _graph) { 
marci@1383
   667
      setGraph(_graph);
marci@556
   668
    }
marci@556
   669
  };
marci@556
   670
marci@992
   671
  
marci@992
   672
  template <typename _Graph, 
marci@992
   673
	    typename ForwardFilterMap, typename BackwardFilterMap>
alpar@1401
   674
  class SubBidirGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
marci@992
   675
  public:
marci@992
   676
    typedef _Graph Graph;
alpar@1401
   677
    typedef GraphAdaptorBase<_Graph> Parent;
marci@992
   678
  protected:
marci@992
   679
    ForwardFilterMap* forward_filter;
marci@992
   680
    BackwardFilterMap* backward_filter;
alpar@1401
   681
    SubBidirGraphAdaptorBase() : Parent(), 
marci@992
   682
				 forward_filter(0), backward_filter(0) { }
marci@992
   683
marci@992
   684
    void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
marci@992
   685
      forward_filter=&_forward_filter;
marci@992
   686
    }
marci@992
   687
    void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
marci@992
   688
      backward_filter=&_backward_filter;
marci@992
   689
    }
marci@992
   690
marci@992
   691
  public:
alpar@1401
   692
//     SubGraphAdaptorBase(Graph& _graph, 
marci@992
   693
// 			NodeFilterMap& _node_filter_map, 
marci@992
   694
// 			EdgeFilterMap& _edge_filter_map) : 
marci@992
   695
//       Parent(&_graph), 
marci@992
   696
//       node_filter_map(&node_filter_map), 
marci@992
   697
//       edge_filter_map(&edge_filter_map) { }
marci@992
   698
marci@992
   699
    typedef typename Parent::Node Node;
marci@992
   700
    typedef typename _Graph::Edge GraphEdge;
marci@992
   701
    template <typename T> class EdgeMap;
alpar@1401
   702
    /// SubBidirGraphAdaptorBase<..., ..., ...>::Edge is inherited from 
marci@992
   703
    /// _Graph::Edge. It contains an extra bool flag which is true 
marci@992
   704
    /// if and only if the 
marci@992
   705
    /// edge is the backward version of the original edge.
marci@992
   706
    class Edge : public _Graph::Edge {
alpar@1401
   707
      friend class SubBidirGraphAdaptorBase<
marci@992
   708
	Graph, ForwardFilterMap, BackwardFilterMap>;
marci@992
   709
      template<typename T> friend class EdgeMap;
marci@992
   710
    protected:
marci@992
   711
      bool backward; //true, iff backward
marci@992
   712
    public:
marci@992
   713
      Edge() { }
marci@992
   714
      /// \todo =false is needed, or causes problems?
marci@992
   715
      /// If \c _backward is false, then we get an edge corresponding to the 
marci@992
   716
      /// original one, otherwise its oppositely directed pair is obtained.
marci@992
   717
      Edge(const typename _Graph::Edge& e, bool _backward/*=false*/) : 
marci@992
   718
	_Graph::Edge(e), backward(_backward) { }
marci@992
   719
      Edge(Invalid i) : _Graph::Edge(i), backward(true) { }
marci@992
   720
      bool operator==(const Edge& v) const { 
marci@992
   721
	return (this->backward==v.backward && 
marci@992
   722
		static_cast<typename _Graph::Edge>(*this)==
marci@992
   723
		static_cast<typename _Graph::Edge>(v));
marci@992
   724
      } 
marci@992
   725
      bool operator!=(const Edge& v) const { 
marci@992
   726
	return (this->backward!=v.backward || 
marci@992
   727
		static_cast<typename _Graph::Edge>(*this)!=
marci@992
   728
		static_cast<typename _Graph::Edge>(v));
marci@992
   729
      }
marci@992
   730
    };
marci@992
   731
marci@992
   732
    void first(Node& i) const { 
marci@992
   733
      Parent::first(i); 
marci@992
   734
    }
marci@992
   735
marci@992
   736
    void first(Edge& i) const { 
marci@992
   737
      Parent::first(i); 
marci@992
   738
      i.backward=false;
marci@992
   739
      while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   740
	     !(*forward_filter)[i]) Parent::next(i);
marci@992
   741
      if (*static_cast<GraphEdge*>(&i)==INVALID) {
marci@992
   742
	Parent::first(i); 
marci@992
   743
	i.backward=true;
marci@992
   744
	while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   745
	       !(*backward_filter)[i]) Parent::next(i);
marci@992
   746
      }
marci@992
   747
    }
marci@992
   748
marci@992
   749
    void firstIn(Edge& i, const Node& n) const { 
marci@992
   750
      Parent::firstIn(i, n); 
marci@992
   751
      i.backward=false;
marci@992
   752
      while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@1269
   753
	     !(*forward_filter)[i]) Parent::nextIn(i);
marci@992
   754
      if (*static_cast<GraphEdge*>(&i)==INVALID) {
marci@992
   755
	Parent::firstOut(i, n); 
marci@992
   756
	i.backward=true;
marci@992
   757
	while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   758
	       !(*backward_filter)[i]) Parent::nextOut(i);
marci@992
   759
      }
marci@992
   760
    }
marci@992
   761
marci@992
   762
    void firstOut(Edge& i, const Node& n) const { 
marci@992
   763
      Parent::firstOut(i, n); 
marci@992
   764
      i.backward=false;
marci@992
   765
      while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   766
	     !(*forward_filter)[i]) Parent::nextOut(i);
marci@992
   767
      if (*static_cast<GraphEdge*>(&i)==INVALID) {
marci@992
   768
	Parent::firstIn(i, n); 
marci@992
   769
	i.backward=true;
marci@992
   770
	while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   771
	       !(*backward_filter)[i]) Parent::nextIn(i);
marci@992
   772
      }
marci@992
   773
    }
marci@992
   774
marci@992
   775
    void next(Node& i) const { 
marci@992
   776
      Parent::next(i); 
marci@992
   777
    }
marci@992
   778
marci@992
   779
    void next(Edge& i) const { 
marci@992
   780
      if (!(i.backward)) {
marci@992
   781
	Parent::next(i);
marci@992
   782
	while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   783
	       !(*forward_filter)[i]) Parent::next(i);
marci@992
   784
	if (*static_cast<GraphEdge*>(&i)==INVALID) {
marci@992
   785
	  Parent::first(i); 
marci@992
   786
	  i.backward=true;
marci@992
   787
	  while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   788
		 !(*backward_filter)[i]) Parent::next(i);
marci@992
   789
	}
marci@992
   790
      } else {
marci@992
   791
	Parent::next(i);
marci@992
   792
	while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   793
	       !(*backward_filter)[i]) Parent::next(i);
marci@992
   794
      }
marci@992
   795
    }
marci@992
   796
marci@992
   797
    void nextIn(Edge& i) const { 
marci@992
   798
      if (!(i.backward)) {
marci@992
   799
	Node n=Parent::target(i);
marci@992
   800
	Parent::nextIn(i);
marci@992
   801
	while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   802
	       !(*forward_filter)[i]) Parent::nextIn(i);
marci@992
   803
	if (*static_cast<GraphEdge*>(&i)==INVALID) {
marci@992
   804
	  Parent::firstOut(i, n); 
marci@992
   805
	  i.backward=true;
marci@992
   806
	  while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   807
		 !(*backward_filter)[i]) Parent::nextOut(i);
marci@992
   808
	}
marci@992
   809
      } else {
marci@992
   810
	Parent::nextOut(i);
marci@992
   811
	while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   812
	       !(*backward_filter)[i]) Parent::nextOut(i);
marci@992
   813
      }
marci@992
   814
    }
marci@992
   815
marci@992
   816
    void nextOut(Edge& i) const { 
marci@992
   817
      if (!(i.backward)) {
marci@992
   818
	Node n=Parent::source(i);
marci@992
   819
	Parent::nextOut(i);
marci@992
   820
	while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   821
	       !(*forward_filter)[i]) Parent::nextOut(i);
marci@992
   822
	if (*static_cast<GraphEdge*>(&i)==INVALID) {
marci@992
   823
	  Parent::firstIn(i, n); 
marci@992
   824
	  i.backward=true;
marci@992
   825
	  while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   826
		 !(*backward_filter)[i]) Parent::nextIn(i);
marci@992
   827
	}
marci@992
   828
      } else {
marci@992
   829
	Parent::nextIn(i);
marci@992
   830
	while (*static_cast<GraphEdge*>(&i)!=INVALID && 
marci@992
   831
	       !(*backward_filter)[i]) Parent::nextIn(i);
marci@992
   832
      }
marci@992
   833
    }
marci@992
   834
marci@992
   835
    Node source(Edge e) const { 
marci@992
   836
      return ((!e.backward) ? this->graph->source(e) : this->graph->target(e)); }
marci@992
   837
    Node target(Edge e) const { 
marci@992
   838
      return ((!e.backward) ? this->graph->target(e) : this->graph->source(e)); }
marci@992
   839
marci@992
   840
    /// Gives back the opposite edge.
marci@992
   841
    Edge opposite(const Edge& e) const { 
marci@992
   842
      Edge f=e;
marci@992
   843
      f.backward=!f.backward;
marci@992
   844
      return f;
marci@992
   845
    }
marci@992
   846
marci@992
   847
    /// \warning This is a linear time operation and works only if 
marci@992
   848
    /// \c Graph::EdgeIt is defined.
marci@992
   849
    /// \todo hmm
marci@992
   850
    int edgeNum() const { 
marci@992
   851
      int i=0;
marci@992
   852
      Edge e;
marci@992
   853
      for (first(e); e!=INVALID; next(e)) ++i;
marci@992
   854
      return i; 
marci@992
   855
    }
marci@992
   856
marci@992
   857
    bool forward(const Edge& e) const { return !e.backward; }
marci@992
   858
    bool backward(const Edge& e) const { return e.backward; }
marci@992
   859
marci@992
   860
    template <typename T>
alpar@1401
   861
    /// \c SubBidirGraphAdaptorBase<..., ..., ...>::EdgeMap contains two 
marci@992
   862
    /// _Graph::EdgeMap one for the forward edges and 
marci@992
   863
    /// one for the backward edges.
marci@992
   864
    class EdgeMap {
marci@992
   865
      template <typename TT> friend class EdgeMap;
marci@992
   866
      typename _Graph::template EdgeMap<T> forward_map, backward_map; 
marci@992
   867
    public:
marci@992
   868
      typedef T Value;
marci@992
   869
      typedef Edge Key;
marci@992
   870
alpar@1401
   871
      EdgeMap(const SubBidirGraphAdaptorBase<_Graph, 
marci@992
   872
	      ForwardFilterMap, BackwardFilterMap>& g) : 
marci@992
   873
	forward_map(*(g.graph)), backward_map(*(g.graph)) { }
marci@992
   874
alpar@1401
   875
      EdgeMap(const SubBidirGraphAdaptorBase<_Graph, 
marci@992
   876
	      ForwardFilterMap, BackwardFilterMap>& g, T a) : 
marci@992
   877
	forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
marci@992
   878
      
marci@992
   879
      void set(Edge e, T a) { 
marci@992
   880
	if (!e.backward) 
marci@992
   881
	  forward_map.set(e, a); 
marci@992
   882
	else 
marci@992
   883
	  backward_map.set(e, a); 
marci@992
   884
      }
marci@992
   885
marci@992
   886
//       typename _Graph::template EdgeMap<T>::ConstReference 
marci@992
   887
//       operator[](Edge e) const { 
marci@992
   888
// 	if (!e.backward) 
marci@992
   889
// 	  return forward_map[e]; 
marci@992
   890
// 	else 
marci@992
   891
// 	  return backward_map[e]; 
marci@992
   892
//       }
marci@992
   893
marci@992
   894
//      typename _Graph::template EdgeMap<T>::Reference 
marci@1016
   895
      T operator[](Edge e) const { 
marci@992
   896
	if (!e.backward) 
marci@992
   897
	  return forward_map[e]; 
marci@992
   898
	else 
marci@992
   899
	  return backward_map[e]; 
marci@992
   900
      }
marci@992
   901
marci@992
   902
      void update() { 
marci@992
   903
	forward_map.update(); 
marci@992
   904
	backward_map.update();
marci@992
   905
      }
marci@992
   906
    };
marci@992
   907
marci@992
   908
  };
marci@569
   909
marci@650
   910
alpar@1401
   911
  ///\brief An adaptor for composing a subgraph of a 
marci@792
   912
  /// bidirected graph made from a directed one. 
marci@612
   913
  ///
alpar@1401
   914
  /// An adaptor for composing a subgraph of a 
alpar@911
   915
  /// bidirected graph made from a directed one. 
alpar@911
   916
  ///
alpar@1401
   917
  ///\warning Graph adaptors are in even more experimental state than the other
alpar@879
   918
  ///parts of the lib. Use them at you own risk.
alpar@879
   919
  ///
marci@923
   920
  /// Let \f$G=(V, A)\f$ be a directed graph and for each directed edge 
marci@923
   921
  /// \f$e\in A\f$, let \f$\bar e\f$ denote the edge obtained by
marci@923
   922
  /// reversing its orientation. We are given moreover two bool valued 
marci@923
   923
  /// maps on the edge-set, 
marci@923
   924
  /// \f$forward\_filter\f$, and \f$backward\_filter\f$. 
alpar@1401
   925
  /// SubBidirGraphAdaptor implements the graph structure with node-set 
marci@923
   926
  /// \f$V\f$ and edge-set 
marci@923
   927
  /// \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
   928
  /// The purpose of writing + instead of union is because parallel 
marci@923
   929
  /// edges can arise. (Similarly, antiparallel edges also can arise).
marci@792
   930
  /// In other words, a subgraph of the bidirected graph obtained, which 
marci@792
   931
  /// is given by orienting the edges of the original graph in both directions.
marci@923
   932
  /// As the oppositely directed edges are logically different, 
marci@923
   933
  /// the maps are able to attach different values for them. 
marci@923
   934
  ///
alpar@1401
   935
  /// An example for such a construction is \c RevGraphAdaptor where the 
marci@792
   936
  /// forward_filter is everywhere false and the backward_filter is 
marci@792
   937
  /// everywhere true. We note that for sake of efficiency, 
alpar@1401
   938
  /// \c RevGraphAdaptor is implemented in a different way. 
alpar@1401
   939
  /// But BidirGraphAdaptor is obtained from 
alpar@1401
   940
  /// SubBidirGraphAdaptor by considering everywhere true 
marci@910
   941
  /// valued maps both for forward_filter and backward_filter. 
marci@1252
   942
  ///
alpar@1401
   943
  /// The most important application of SubBidirGraphAdaptor 
alpar@1401
   944
  /// is ResGraphAdaptor, which stands for the residual graph in directed 
marci@792
   945
  /// flow and circulation problems. 
alpar@1401
   946
  /// As adaptors usually, the SubBidirGraphAdaptor implements the 
marci@792
   947
  /// above mentioned graph structure without its physical storage, 
marci@923
   948
  /// that is the whole stuff is stored in constant memory. 
marci@992
   949
  template<typename _Graph, 
marci@650
   950
	   typename ForwardFilterMap, typename BackwardFilterMap>
alpar@1401
   951
  class SubBidirGraphAdaptor : 
marci@992
   952
    public IterableGraphExtender<
alpar@1401
   953
    SubBidirGraphAdaptorBase<_Graph, ForwardFilterMap, BackwardFilterMap> > {
marci@650
   954
  public:
marci@992
   955
    typedef _Graph Graph;
marci@992
   956
    typedef IterableGraphExtender<
alpar@1401
   957
      SubBidirGraphAdaptorBase<
marci@992
   958
      _Graph, ForwardFilterMap, BackwardFilterMap> > Parent;
marci@569
   959
  protected:
alpar@1401
   960
    SubBidirGraphAdaptor() { }
marci@992
   961
  public:
alpar@1401
   962
    SubBidirGraphAdaptor(_Graph& _graph, ForwardFilterMap& _forward_filter, 
marci@992
   963
			 BackwardFilterMap& _backward_filter) { 
marci@992
   964
      setGraph(_graph);
marci@992
   965
      setForwardFilterMap(_forward_filter);
marci@992
   966
      setBackwardFilterMap(_backward_filter);
marci@992
   967
    }
marci@992
   968
  };
marci@650
   969
marci@569
   970
marci@650
   971
alpar@1401
   972
  ///\brief An adaptor for composing bidirected graph from a directed one. 
marci@650
   973
  ///
alpar@1401
   974
  ///\warning Graph adaptors are in even more experimental state than the other
alpar@879
   975
  ///parts of the lib. Use them at you own risk.
alpar@879
   976
  ///
alpar@1401
   977
  /// An adaptor for composing bidirected graph from a directed one. 
marci@650
   978
  /// A bidirected graph is composed over the directed one without physical 
marci@650
   979
  /// storage. As the oppositely directed edges are logically different ones 
marci@650
   980
  /// the maps are able to attach different values for them.
marci@650
   981
  template<typename Graph>
alpar@1401
   982
  class BidirGraphAdaptor : 
alpar@1401
   983
    public SubBidirGraphAdaptor<
marci@650
   984
    Graph, 
marci@650
   985
    ConstMap<typename Graph::Edge, bool>, 
marci@650
   986
    ConstMap<typename Graph::Edge, bool> > {
marci@650
   987
  public:
alpar@1401
   988
    typedef  SubBidirGraphAdaptor<
marci@650
   989
      Graph, 
marci@650
   990
      ConstMap<typename Graph::Edge, bool>, 
marci@650
   991
      ConstMap<typename Graph::Edge, bool> > Parent; 
marci@650
   992
  protected:
marci@650
   993
    ConstMap<typename Graph::Edge, bool> cm;
marci@650
   994
alpar@1401
   995
    BidirGraphAdaptor() : Parent(), cm(true) { 
marci@655
   996
      Parent::setForwardFilterMap(cm);
marci@655
   997
      Parent::setBackwardFilterMap(cm);
marci@655
   998
    }
marci@650
   999
  public:
alpar@1401
  1000
    BidirGraphAdaptor(Graph& _graph) : Parent(), cm(true) { 
marci@650
  1001
      Parent::setGraph(_graph);
marci@650
  1002
      Parent::setForwardFilterMap(cm);
marci@650
  1003
      Parent::setBackwardFilterMap(cm);
marci@650
  1004
    }
marci@738
  1005
marci@738
  1006
    int edgeNum() const { 
marci@738
  1007
      return 2*this->graph->edgeNum();
marci@738
  1008
    }
alpar@1401
  1009
    //    KEEP_MAPS(Parent, BidirGraphAdaptor);
marci@650
  1010
  };
marci@650
  1011
marci@650
  1012
marci@650
  1013
  template<typename Graph, typename Number,
marci@650
  1014
	   typename CapacityMap, typename FlowMap>
marci@658
  1015
  class ResForwardFilter {
marci@658
  1016
    //    const Graph* graph;
marci@650
  1017
    const CapacityMap* capacity;
marci@650
  1018
    const FlowMap* flow;
marci@650
  1019
  public:
marci@658
  1020
    ResForwardFilter(/*const Graph& _graph, */
marci@658
  1021
		     const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1022
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1023
    ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@656
  1024
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1025
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1026
    bool operator[](const typename Graph::Edge& e) const {
marci@738
  1027
      return (Number((*flow)[e]) < Number((*capacity)[e]));
marci@650
  1028
    }
marci@650
  1029
  };
marci@650
  1030
marci@650
  1031
  template<typename Graph, typename Number,
marci@650
  1032
	   typename CapacityMap, typename FlowMap>
marci@658
  1033
  class ResBackwardFilter {
marci@650
  1034
    const CapacityMap* capacity;
marci@650
  1035
    const FlowMap* flow;
marci@650
  1036
  public:
marci@658
  1037
    ResBackwardFilter(/*const Graph& _graph,*/ 
marci@658
  1038
		      const CapacityMap& _capacity, const FlowMap& _flow) :
marci@658
  1039
      /*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
marci@658
  1040
    ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
marci@656
  1041
    void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
marci@656
  1042
    void setFlow(const FlowMap& _flow) { flow=&_flow; }
marci@650
  1043
    bool operator[](const typename Graph::Edge& e) const {
marci@738
  1044
      return (Number(0) < Number((*flow)[e]));
marci@650
  1045
    }
marci@650
  1046
  };
marci@650
  1047
marci@653
  1048
  
alpar@1401
  1049
  /*! \brief An adaptor for composing the residual graph for directed flow and circulation problems.
marci@650
  1050
alpar@1401
  1051
  An adaptor for composing the residual graph for directed flow and circulation problems. 
marci@1242
  1052
  Let \f$G=(V, A)\f$ be a directed graph and let \f$F\f$ be a 
marci@1242
  1053
  number type. Let moreover 
marci@1242
  1054
  \f$f,c:A\to F\f$, be functions on the edge-set. 
alpar@1401
  1055
  In the appications of ResGraphAdaptor, \f$f\f$ usually stands for a flow 
marci@1242
  1056
  and \f$c\f$ for a capacity function.   
marci@1242
  1057
  Suppose that a graph instange \c g of type 
marci@1242
  1058
  \c ListGraph implements \f$G\f$.
marci@1242
  1059
  \code
marci@1242
  1060
  ListGraph g;
marci@1242
  1061
  \endcode
alpar@1401
  1062
  Then RevGraphAdaptor implements the graph structure with node-set 
marci@1242
  1063
  \f$V\f$ and edge-set \f$A_{forward}\cup A_{backward}\f$, where 
marci@1242
  1064
  \f$A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\}\f$ and 
marci@1242
  1065
  \f$A_{backward}=\{vu : uv\in A, f(uv)>0\}\f$, 
marci@1242
  1066
  i.e. the so called residual graph. 
marci@1242
  1067
  When we take the union \f$A_{forward}\cup A_{backward}\f$, 
marci@1242
  1068
  multilicities are counted, i.e. if an edge is in both 
alpar@1401
  1069
  \f$A_{forward}\f$ and \f$A_{backward}\f$, then in the adaptor it 
marci@1242
  1070
  appears twice. 
marci@1242
  1071
  The following code shows how 
marci@1242
  1072
  such an instance can be constructed.
marci@1242
  1073
  \code
marci@1242
  1074
  typedef ListGraph Graph;
marci@1242
  1075
  Graph::EdgeMap<int> f(g);
marci@1242
  1076
  Graph::EdgeMap<int> c(g);
alpar@1401
  1077
  ResGraphAdaptor<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > gw(g);
marci@1242
  1078
  \endcode
marci@1242
  1079
  \author Marton Makai
marci@1242
  1080
  */
marci@650
  1081
  template<typename Graph, typename Number, 
marci@650
  1082
	   typename CapacityMap, typename FlowMap>
alpar@1401
  1083
  class ResGraphAdaptor : 
alpar@1401
  1084
    public SubBidirGraphAdaptor< 
marci@650
  1085
    Graph, 
marci@658
  1086
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1087
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
marci@650
  1088
  public:
alpar@1401
  1089
    typedef SubBidirGraphAdaptor< 
marci@650
  1090
      Graph, 
marci@658
  1091
      ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,  
marci@658
  1092
      ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
marci@650
  1093
  protected:
marci@650
  1094
    const CapacityMap* capacity;
marci@650
  1095
    FlowMap* flow;
marci@658
  1096
    ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
marci@658
  1097
    ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
alpar@1401
  1098
    ResGraphAdaptor() : Parent(), 
marci@658
  1099
 			capacity(0), flow(0) { }
marci@658
  1100
    void setCapacityMap(const CapacityMap& _capacity) {
marci@658
  1101
      capacity=&_capacity;
marci@658
  1102
      forward_filter.setCapacity(_capacity);
marci@658
  1103
      backward_filter.setCapacity(_capacity);
marci@658
  1104
    }
marci@658
  1105
    void setFlowMap(FlowMap& _flow) {
marci@658
  1106
      flow=&_flow;
marci@658
  1107
      forward_filter.setFlow(_flow);
marci@658
  1108
      backward_filter.setFlow(_flow);
marci@658
  1109
    }
marci@650
  1110
  public:
alpar@1401
  1111
    ResGraphAdaptor(Graph& _graph, const CapacityMap& _capacity, 
marci@650
  1112
		       FlowMap& _flow) : 
marci@650
  1113
      Parent(), capacity(&_capacity), flow(&_flow), 
marci@658
  1114
      forward_filter(/*_graph,*/ _capacity, _flow), 
marci@658
  1115
      backward_filter(/*_graph,*/ _capacity, _flow) {
marci@650
  1116
      Parent::setGraph(_graph);
marci@650
  1117
      Parent::setForwardFilterMap(forward_filter);
marci@650
  1118
      Parent::setBackwardFilterMap(backward_filter);
marci@650
  1119
    }
marci@650
  1120
marci@660
  1121
    typedef typename Parent::Edge Edge;
marci@660
  1122
marci@660
  1123
    void augment(const Edge& e, Number a) const {
marci@650
  1124
      if (Parent::forward(e))  
marci@650
  1125
	flow->set(e, (*flow)[e]+a);
marci@650
  1126
      else  
marci@650
  1127
	flow->set(e, (*flow)[e]-a);
marci@650
  1128
    }
marci@650
  1129
marci@660
  1130
    /// \brief Residual capacity map.
marci@660
  1131
    ///
marci@910
  1132
    /// In generic residual graphs the residual capacity can be obtained 
marci@910
  1133
    /// as a map. 
marci@660
  1134
    class ResCap {
marci@660
  1135
    protected:
alpar@1401
  1136
      const ResGraphAdaptor<Graph, Number, CapacityMap, FlowMap>* res_graph;
marci@660
  1137
    public:
alpar@987
  1138
      typedef Number Value;
alpar@987
  1139
      typedef Edge Key;
alpar@1401
  1140
      ResCap(const ResGraphAdaptor<Graph, Number, CapacityMap, FlowMap>& 
marci@888
  1141
	     _res_graph) : res_graph(&_res_graph) { }
marci@660
  1142
      Number operator[](const Edge& e) const { 
marci@660
  1143
	if (res_graph->forward(e)) 
marci@660
  1144
	  return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e]; 
marci@660
  1145
	else 
marci@660
  1146
	  return (*(res_graph->flow))[e]; 
marci@660
  1147
      }
marci@660
  1148
    };
marci@660
  1149
alpar@1401
  1150
    //    KEEP_MAPS(Parent, ResGraphAdaptor);
marci@650
  1151
  };
marci@650
  1152
marci@650
  1153
marci@998
  1154
marci@998
  1155
  template <typename _Graph, typename FirstOutEdgesMap>
alpar@1401
  1156
  class ErasingFirstGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
marci@998
  1157
  public:
marci@998
  1158
    typedef _Graph Graph;
alpar@1401
  1159
    typedef GraphAdaptorBase<_Graph> Parent;
marci@998
  1160
  protected:
marci@998
  1161
    FirstOutEdgesMap* first_out_edges;
alpar@1401
  1162
    ErasingFirstGraphAdaptorBase() : Parent(), 
marci@998
  1163
				     first_out_edges(0) { }
marci@998
  1164
marci@998
  1165
    void setFirstOutEdgesMap(FirstOutEdgesMap& _first_out_edges) {
marci@998
  1166
      first_out_edges=&_first_out_edges;
marci@998
  1167
    }
marci@998
  1168
marci@998
  1169
  public:
marci@998
  1170
marci@998
  1171
    typedef typename Parent::Node Node;
marci@998
  1172
    typedef typename Parent::Edge Edge;
marci@998
  1173
marci@998
  1174
    void firstOut(Edge& i, const Node& n) const { 
marci@998
  1175
      i=(*first_out_edges)[n];
marci@998
  1176
    }
marci@998
  1177
marci@998
  1178
    void erase(const Edge& e) const {
marci@998
  1179
      Node n=source(e);
marci@998
  1180
      Edge f=e;
marci@998
  1181
      Parent::nextOut(f);
marci@998
  1182
      first_out_edges->set(n, f);
marci@998
  1183
    }    
marci@998
  1184
  };
marci@998
  1185
marci@998
  1186
marci@612
  1187
  /// For blocking flows.
marci@556
  1188
alpar@1401
  1189
  ///\warning Graph adaptors are in even more experimental state than the other
alpar@879
  1190
  ///parts of the lib. Use them at you own risk.
alpar@879
  1191
  ///
alpar@1401
  1192
  /// This graph adaptor is used for on-the-fly 
marci@792
  1193
  /// Dinits blocking flow computations.
marci@612
  1194
  /// For each node, an out-edge is stored which is used when the 
marci@612
  1195
  /// \code 
marci@612
  1196
  /// OutEdgeIt& first(OutEdgeIt&, const Node&)
marci@612
  1197
  /// \endcode
marci@612
  1198
  /// is called. 
marci@556
  1199
  ///
marci@792
  1200
  /// \author Marton Makai
marci@998
  1201
  template <typename _Graph, typename FirstOutEdgesMap>
alpar@1401
  1202
  class ErasingFirstGraphAdaptor : 
marci@998
  1203
    public IterableGraphExtender<
alpar@1401
  1204
    ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > {
marci@650
  1205
  public:
marci@998
  1206
    typedef _Graph Graph;
marci@998
  1207
    typedef IterableGraphExtender<
alpar@1401
  1208
      ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > Parent;
alpar@1401
  1209
    ErasingFirstGraphAdaptor(Graph& _graph, 
marci@998
  1210
			     FirstOutEdgesMap& _first_out_edges) { 
marci@998
  1211
      setGraph(_graph);
marci@998
  1212
      setFirstOutEdgesMap(_first_out_edges);
marci@998
  1213
    } 
marci@1019
  1214
marci@998
  1215
  };
marci@556
  1216
deba@1472
  1217
  template <typename _Graph>
deba@1472
  1218
  class NewEdgeSetAdaptorBase {
deba@1472
  1219
  public:
deba@1472
  1220
deba@1472
  1221
    typedef _Graph Graph;
deba@1472
  1222
    typedef typename Graph::Node Node;
deba@1472
  1223
    typedef typename Graph::NodeIt NodeIt;
deba@1472
  1224
deba@1472
  1225
  protected:
deba@1472
  1226
deba@1472
  1227
    struct NodeT {
deba@1472
  1228
      int first_out, first_in;
deba@1472
  1229
      NodeT() : first_out(-1), first_in(-1) {}
deba@1472
  1230
    };
deba@1472
  1231
    
deba@1472
  1232
    class NodesImpl : protected Graph::template NodeMap<NodeT> {
deba@1472
  1233
deba@1472
  1234
      typedef typename Graph::template NodeMap<NodeT> Parent;
deba@1472
  1235
      typedef NewEdgeSetAdaptorBase<Graph> Adaptor;
deba@1472
  1236
deba@1472
  1237
      Adaptor& adaptor;
deba@1472
  1238
deba@1472
  1239
    public:
deba@1472
  1240
deba@1472
  1241
      NodesImpl(Adaptor& _adaptor, const Graph& _graph) 
deba@1472
  1242
	: Parent(_graph), adaptor(_adaptor) {}
deba@1472
  1243
deba@1472
  1244
      virtual ~NodesImpl() {}
deba@1472
  1245
deba@1472
  1246
      virtual void build() {
deba@1472
  1247
	Parent::build();
deba@1472
  1248
      }
deba@1472
  1249
deba@1472
  1250
      virtual void clear() {
deba@1472
  1251
	adaptor._clear();
deba@1472
  1252
	Parent::clear();
deba@1472
  1253
      }
deba@1472
  1254
      
deba@1472
  1255
      virtual void add(const Node& node) {
deba@1472
  1256
	Parent::add(node);
deba@1472
  1257
	adaptor._add(node);
deba@1472
  1258
      }
deba@1472
  1259
      
deba@1472
  1260
      virtual void erase(const Node& node) {
deba@1472
  1261
	adaptor._erase(node);
deba@1472
  1262
	Parent::erase(node);
deba@1472
  1263
      }
deba@1472
  1264
deba@1472
  1265
      NodeT& operator[](const Node& node) {
deba@1472
  1266
	return Parent::operator[](node);
deba@1472
  1267
      }
deba@1472
  1268
deba@1472
  1269
      const NodeT& operator[](const Node& node) const {
deba@1472
  1270
	return Parent::operator[](node);
deba@1472
  1271
      }
deba@1472
  1272
      
deba@1472
  1273
    };
deba@1472
  1274
deba@1472
  1275
    NodesImpl* nodes;
deba@1472
  1276
deba@1472
  1277
    struct EdgeT {
deba@1472
  1278
      Node source, target;
deba@1472
  1279
      int next_out, next_in;
deba@1472
  1280
      int prev_out, prev_in;
deba@1472
  1281
      EdgeT() : prev_out(-1), prev_in(-1) {}
deba@1472
  1282
    };
deba@1472
  1283
deba@1472
  1284
    std::vector<EdgeT> edges;
deba@1472
  1285
deba@1472
  1286
    int first_edge;
deba@1472
  1287
    int first_free_edge;
deba@1472
  1288
deba@1472
  1289
    virtual void _clear() = 0;
deba@1472
  1290
    virtual void _add(const Node& node) = 0;
deba@1472
  1291
    virtual void _erase(const Node& node) = 0;
deba@1472
  1292
    
deba@1472
  1293
    const Graph* graph;
deba@1472
  1294
deba@1472
  1295
    void initalize(const Graph& _graph, NodesImpl& _nodes) {
deba@1472
  1296
      graph = &_graph;
deba@1472
  1297
      nodes = &_nodes;
deba@1472
  1298
    }
deba@1472
  1299
    
deba@1472
  1300
  public:
deba@1472
  1301
deba@1472
  1302
    class Edge {
deba@1472
  1303
      friend class NewEdgeSetAdaptorBase<Graph>;
deba@1472
  1304
    protected:
deba@1472
  1305
      Edge(int _id) : id(_id) {}
deba@1472
  1306
      int id;
deba@1472
  1307
    public:
deba@1472
  1308
      Edge() {}
deba@1472
  1309
      Edge(Invalid) : id(-1) {}
deba@1472
  1310
      bool operator==(const Edge& edge) const { return id == edge.id; }
deba@1472
  1311
      bool operator!=(const Edge& edge) const { return id != edge.id; }
deba@1472
  1312
      bool operator<(const Edge& edge) const { return id < edge.id; }
deba@1472
  1313
    };
deba@1472
  1314
deba@1472
  1315
    NewEdgeSetAdaptorBase() : first_edge(-1), first_free_edge(-1) {} 
deba@1472
  1316
    virtual ~NewEdgeSetAdaptorBase() {}
deba@1472
  1317
deba@1472
  1318
    Edge addEdge(const Node& source, const Node& target) {
deba@1472
  1319
      int n;
deba@1472
  1320
      if (first_free_edge == -1) {
deba@1472
  1321
	n = edges.size();
deba@1472
  1322
	edges.push_back(EdgeT());
deba@1472
  1323
      } else {
deba@1472
  1324
	n = first_free_edge;
deba@1472
  1325
	first_free_edge = edges[first_free_edge].next_in;
deba@1472
  1326
      }
deba@1472
  1327
      edges[n].next_in = (*nodes)[target].first_in;
deba@1472
  1328
      (*nodes)[target].first_in = n;
deba@1472
  1329
      edges[n].next_out = (*nodes)[source].first_out;
deba@1472
  1330
      (*nodes)[source].first_out = n;
deba@1472
  1331
      edges[n].source = source;
deba@1472
  1332
      edges[n].target = target;
deba@1472
  1333
      return Edge(n);
deba@1472
  1334
    }
deba@1472
  1335
deba@1472
  1336
    void erase(const Edge& edge) {
deba@1472
  1337
      int n = edge.id;
deba@1472
  1338
      if (edges[n].prev_in != -1) {
deba@1472
  1339
	edges[edges[n].prev_in].next_in = edges[n].next_in;
deba@1472
  1340
      } else {
deba@1472
  1341
	(*nodes)[edges[n].target].first_in = edges[n].next_in;
deba@1472
  1342
      }
deba@1472
  1343
      if (edges[n].next_in != -1) {
deba@1472
  1344
	edges[edges[n].next_in].prev_in = edges[n].prev_in;
deba@1472
  1345
      }
deba@1472
  1346
deba@1472
  1347
      if (edges[n].prev_out != -1) {
deba@1472
  1348
	edges[edges[n].prev_out].next_out = edges[n].next_out;
deba@1472
  1349
      } else {
deba@1472
  1350
	(*nodes)[edges[n].source].first_out = edges[n].next_out;
deba@1472
  1351
      }
deba@1472
  1352
      if (edges[n].next_out != -1) {
deba@1472
  1353
	edges[edges[n].next_out].prev_out = edges[n].prev_out;
deba@1472
  1354
      }
deba@1472
  1355
           
deba@1472
  1356
    }
deba@1472
  1357
deba@1472
  1358
    void first(Node& node) const {
deba@1472
  1359
      graph->first(node);
deba@1472
  1360
    }
deba@1472
  1361
deba@1472
  1362
    void next(Node& node) const {
deba@1472
  1363
      graph->next(node);
deba@1472
  1364
    }
deba@1472
  1365
deba@1472
  1366
    void first(Edge& edge) const {
deba@1472
  1367
      Node node;
deba@1472
  1368
      for (first(node); node != INVALID && (*nodes)[node].first_in == -1; 
deba@1472
  1369
	   next(node));
deba@1472
  1370
      edge.id = (node == INVALID) ? -1 : (*nodes)[node].first_in;
deba@1472
  1371
    }
deba@1472
  1372
deba@1472
  1373
    void next(Edge& edge) const {
deba@1472
  1374
      if (edges[edge.id].next_in != -1) {
deba@1472
  1375
	edge.id = edges[edge.id].next_in;
deba@1472
  1376
      } else {
deba@1472
  1377
	Node node = edges[edge.id].target;
deba@1472
  1378
	for (next(node); node != INVALID && (*nodes)[node].first_in == -1; 
deba@1472
  1379
	     next(node));
deba@1472
  1380
	edge.id = (node == INVALID) ? -1 : (*nodes)[node].first_in;
deba@1472
  1381
      }      
deba@1472
  1382
    }
deba@1472
  1383
deba@1472
  1384
    void firstOut(Edge& edge, const Node& node) const {
deba@1472
  1385
      edge.id = (*nodes)[node].first_out;    
deba@1472
  1386
    }
deba@1472
  1387
    
deba@1472
  1388
    void nextOut(Edge& edge) const {
deba@1472
  1389
      edge.id = edges[edge.id].next_out;        
deba@1472
  1390
    }
deba@1472
  1391
deba@1472
  1392
    void firstIn(Edge& edge, const Node& node) const {
deba@1472
  1393
      edge.id = (*nodes)[node].first_in;          
deba@1472
  1394
    }
deba@1472
  1395
deba@1472
  1396
    void nextIn(Edge& edge) const {
deba@1472
  1397
      edge.id = edges[edge.id].next_in;    
deba@1472
  1398
    }
deba@1472
  1399
deba@1472
  1400
    int id(const Node& node) const { return graph->id(node); }
deba@1472
  1401
    int id(const Edge& edge) const { return edge.id; }
deba@1472
  1402
deba@1472
  1403
    Node fromId(int id, Node) const { return graph->fromId(id, Node()); }
deba@1472
  1404
    Edge fromId(int id, Edge) const { return Edge(id); }
deba@1472
  1405
deba@1472
  1406
    int maxId(Node) const { return graph->maxId(Node()); };
deba@1472
  1407
    int maxId(Edge) const { return edges.size() - 1; }
deba@1472
  1408
deba@1472
  1409
    Node source(const Edge& edge) const { return edges[edge.id].source;}
deba@1472
  1410
    Node target(const Edge& edge) const { return edges[edge.id].target;}
deba@1472
  1411
deba@1472
  1412
  };
deba@1472
  1413
deba@1538
  1414
deba@1538
  1415
  /// \brief Graph adaptor using a node set of another graph and an
deba@1538
  1416
  /// own edge set.
deba@1538
  1417
  ///
deba@1538
  1418
  /// This structure can be used to establish another graph over a node set
deba@1538
  1419
  /// of an existing one. The node iterator will go through the nodes of the
deba@1538
  1420
  /// original graph.
deba@1538
  1421
  ///
deba@1538
  1422
  /// \param _Graph The type of the graph which shares its node set with 
alpar@1631
  1423
  /// this class. Its interface must conform to the \ref concept::StaticGraph
deba@1538
  1424
  /// "StaticGraph" concept.
deba@1538
  1425
  ///
deba@1538
  1426
  /// In the edge extension and removing it conforms to the 
alpar@1631
  1427
  /// \ref concept::ExtendableGraph "ExtendableGraph" concept.
deba@1472
  1428
  template <typename _Graph>
deba@1472
  1429
  class NewEdgeSetAdaptor :
deba@1472
  1430
    public ErasableGraphExtender<
deba@1472
  1431
    ClearableGraphExtender<
deba@1472
  1432
    ExtendableGraphExtender<
deba@1472
  1433
    DefaultMappableGraphExtender<
deba@1472
  1434
    IterableGraphExtender<
deba@1472
  1435
    AlterableGraphExtender<
deba@1472
  1436
    NewEdgeSetAdaptorBase<_Graph> > > > > > > {
deba@1472
  1437
deba@1472
  1438
  public:
deba@1472
  1439
deba@1472
  1440
    typedef ErasableGraphExtender<
deba@1472
  1441
      ClearableGraphExtender<
deba@1472
  1442
      ExtendableGraphExtender<
deba@1472
  1443
      DefaultMappableGraphExtender<
deba@1472
  1444
      IterableGraphExtender<
deba@1472
  1445
      AlterableGraphExtender<
deba@1472
  1446
      NewEdgeSetAdaptorBase<_Graph> > > > > > > Parent;
deba@1472
  1447
    
deba@1472
  1448
deba@1472
  1449
    typedef typename Parent::Node Node;
deba@1472
  1450
    typedef typename Parent::Edge Edge;
deba@1472
  1451
deba@1472
  1452
  private:
deba@1472
  1453
deba@1472
  1454
    virtual void _clear() {
deba@1472
  1455
      Parent::edges.clear();
deba@1472
  1456
      Parent::first_edge = -1;
deba@1472
  1457
      Parent::first_free_edge = -1;
deba@1472
  1458
      Parent::getNotifier(Edge()).clear();
deba@1472
  1459
      Parent::getNotifier(Node()).clear();
deba@1472
  1460
    }
deba@1472
  1461
deba@1472
  1462
    virtual void _add(const Node& node) {
deba@1472
  1463
      Parent::getNotifier(Node()).add(node);
deba@1472
  1464
    }
deba@1472
  1465
deba@1472
  1466
    virtual void _erase(const Node& node) {
deba@1472
  1467
      Edge edge;
deba@1472
  1468
      Parent::firstOut(edge, node);
deba@1472
  1469
      while (edge != INVALID) {
deba@1472
  1470
	Parent::erase(edge);
deba@1472
  1471
	Parent::firstOut(edge, node);
deba@1472
  1472
      }
deba@1472
  1473
deba@1472
  1474
      Parent::firstIn(edge, node);
deba@1472
  1475
      while (edge != INVALID) {
deba@1472
  1476
	Parent::erase(edge);
deba@1472
  1477
	Parent::firstIn(edge, node);
deba@1472
  1478
      }
deba@1472
  1479
      
deba@1472
  1480
      Parent::getNotifier(Node()).erase(node);
deba@1472
  1481
    }
deba@1472
  1482
deba@1472
  1483
deba@1472
  1484
    typedef typename Parent::NodesImpl NodesImpl;
deba@1472
  1485
deba@1472
  1486
    NodesImpl nodes;
deba@1472
  1487
    
deba@1472
  1488
  public:
deba@1472
  1489
deba@1538
  1490
    /// \brief Constructor of the adaptor.
deba@1538
  1491
    /// 
deba@1538
  1492
    /// Constructor of the adaptor.
deba@1472
  1493
    NewEdgeSetAdaptor(const _Graph& _graph) : nodes(*this, _graph) {
deba@1472
  1494
      Parent::initalize(_graph, nodes);
deba@1472
  1495
    }
deba@1472
  1496
deba@1472
  1497
    void clear() {
deba@1538
  1498
      Parent::getNotifier(Edge()).clear();      
deba@1538
  1499
deba@1472
  1500
      Parent::edges.clear();
deba@1472
  1501
      Parent::first_edge = -1;
deba@1472
  1502
      Parent::first_free_edge = -1;
deba@1538
  1503
    }
deba@1538
  1504
    
deba@1538
  1505
  };
deba@1472
  1506
deba@1538
  1507
  /// \brief Graph adaptor using a node set of another graph and an
deba@1538
  1508
  /// own undir edge set.
deba@1538
  1509
  ///
deba@1538
  1510
  /// This structure can be used to establish another undirected graph over 
deba@1538
  1511
  /// a node set of an existing one. The node iterator will go through the 
deba@1538
  1512
  /// nodes of the original graph.
deba@1538
  1513
  ///
deba@1538
  1514
  /// \param _Graph The type of the graph which shares its node set with 
alpar@1631
  1515
  /// this class. Its interface must conform to the \ref concept::StaticGraph
deba@1538
  1516
  /// "StaticGraph" concept.
deba@1538
  1517
  ///
deba@1538
  1518
  /// In the edge extension and removing it conforms to the 
alpar@1631
  1519
  /// \ref concept::ExtendableGraph "ExtendableGraph" concept.
deba@1538
  1520
  template <typename _Graph>
deba@1538
  1521
  class NewUndirEdgeSetAdaptor :
deba@1538
  1522
    public ErasableUndirGraphExtender<
deba@1538
  1523
    ClearableUndirGraphExtender<
deba@1538
  1524
    ExtendableUndirGraphExtender<
deba@1538
  1525
    MappableUndirGraphExtender<
deba@1538
  1526
    IterableUndirGraphExtender<
deba@1538
  1527
    AlterableUndirGraphExtender<
deba@1538
  1528
    UndirGraphExtender<
deba@1538
  1529
    NewEdgeSetAdaptorBase<_Graph> > > > > > > > {
deba@1538
  1530
deba@1538
  1531
  public:
deba@1538
  1532
deba@1538
  1533
    typedef ErasableUndirGraphExtender<
deba@1538
  1534
      ClearableUndirGraphExtender<
deba@1538
  1535
      ExtendableUndirGraphExtender<
deba@1538
  1536
      MappableUndirGraphExtender<
deba@1538
  1537
      IterableUndirGraphExtender<
deba@1538
  1538
      AlterableUndirGraphExtender<
deba@1538
  1539
      UndirGraphExtender<
deba@1538
  1540
      NewEdgeSetAdaptorBase<_Graph> > > > > > > > Parent;
deba@1538
  1541
    
deba@1538
  1542
deba@1538
  1543
    typedef typename Parent::Node Node;
deba@1538
  1544
    typedef typename Parent::Edge Edge;
deba@1538
  1545
    typedef typename Parent::UndirEdge UndirEdge;
deba@1538
  1546
deba@1538
  1547
  private:
deba@1538
  1548
deba@1538
  1549
    virtual void _clear() {
deba@1538
  1550
      Parent::edges.clear();
deba@1538
  1551
      Parent::first_edge = -1;
deba@1538
  1552
      Parent::first_free_edge = -1;
deba@1538
  1553
      Parent::getNotifier(Edge()).clear();
deba@1538
  1554
      Parent::getNotifier(Node()).clear();
deba@1538
  1555
    }
deba@1538
  1556
deba@1538
  1557
    virtual void _add(const Node& node) {
deba@1538
  1558
      Parent::getNotifier(Node()).add(node);
deba@1538
  1559
    }
deba@1538
  1560
deba@1538
  1561
    virtual void _erase(const Node& node) {
deba@1538
  1562
      Edge edge;
deba@1538
  1563
      Parent::firstOut(edge, node);
deba@1538
  1564
      while (edge != INVALID) {
deba@1538
  1565
	Parent::erase(edge);
deba@1538
  1566
	Parent::firstOut(edge, node);
deba@1538
  1567
      }
deba@1538
  1568
deba@1538
  1569
      Parent::firstIn(edge, node);
deba@1538
  1570
      while (edge != INVALID) {
deba@1538
  1571
	Parent::erase(edge);
deba@1538
  1572
	Parent::firstIn(edge, node);
deba@1538
  1573
      }
deba@1538
  1574
      
deba@1538
  1575
      Parent::getNotifier(Node()).erase(node);
deba@1538
  1576
    }
deba@1538
  1577
deba@1538
  1578
    typedef typename Parent::NodesImpl NodesImpl;
deba@1538
  1579
deba@1538
  1580
    NodesImpl nodes;
deba@1538
  1581
    
deba@1538
  1582
  public:
deba@1538
  1583
deba@1538
  1584
deba@1538
  1585
    /// \brief Constructor of the adaptor.
deba@1538
  1586
    /// 
deba@1538
  1587
    /// Constructor of the adaptor.
deba@1538
  1588
    NewUndirEdgeSetAdaptor(const _Graph& _graph) : nodes(*this, _graph) {
deba@1538
  1589
      Parent::initalize(_graph, nodes);
deba@1538
  1590
    }
deba@1538
  1591
deba@1538
  1592
    void clear() {
deba@1472
  1593
      Parent::getNotifier(Edge()).clear();      
deba@1538
  1594
      Parent::getNotifier(UndirEdge()).clear();      
deba@1538
  1595
deba@1538
  1596
      Parent::edges.clear();
deba@1538
  1597
      Parent::first_edge = -1;
deba@1538
  1598
      Parent::first_free_edge = -1;
deba@1472
  1599
    }
deba@1472
  1600
    
deba@1472
  1601
  };
deba@1472
  1602
marci@556
  1603
  ///@}
marci@556
  1604
alpar@921
  1605
} //namespace lemon
marci@556
  1606
alpar@1401
  1607
#endif //LEMON_GRAPH_ADAPTOR_H
marci@556
  1608