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