lemon/sub_graph.h
author deba
Tue, 17 Oct 2006 10:50:57 +0000
changeset 2247 269a0dcee70b
parent 2006 00d59f733817
child 2391 14a343be7a5a
permissions -rw-r--r--
Update the Path concept
Concept check for paths

DirPath renamed to Path
The interface updated to the new lemon interface
Make difference between the empty path and the path from one node
Builder interface have not been changed
// I wanted but there was not accordance about it

UPath is removed
It was a buggy implementation, it could not iterate on the
nodes in the right order
Right way to use undirected paths => path of edges in undirected graphs

The tests have been modified to the current implementation
deba@1866
     1
/* -*- C++ -*-
deba@1866
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1866
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1866
     8
 *
deba@1866
     9
 * Permission to use, modify and distribute this software is granted
deba@1866
    10
 * provided that this copyright notice appears in all copies. For
deba@1866
    11
 * precise terms see the accompanying LICENSE file.
deba@1866
    12
 *
deba@1866
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1866
    14
 * express or implied, and with no claim as to its suitability for any
deba@1866
    15
 * purpose.
deba@1866
    16
 *
deba@1866
    17
 */
deba@1866
    18
deba@1866
    19
#ifndef LEMON_SUB_GRAPH_H
deba@1866
    20
#define LEMON_SUB_GRAPH_H
deba@1866
    21
deba@1866
    22
#include <lemon/graph_adaptor.h>
deba@1990
    23
#include <lemon/bits/graph_adaptor_extender.h>
deba@1990
    24
#include <lemon/bits/default_map.h>
deba@1866
    25
deba@2224
    26
/// \ingroup semi_adaptors
deba@2224
    27
/// \file
deba@2224
    28
/// \brief Subgraphs.
deba@2224
    29
///
deba@2224
    30
/// Graphs with filtered edge and node set.
deba@2224
    31
deba@1866
    32
namespace lemon {
deba@1866
    33
deba@1866
    34
  /// \brief Base for the SubGraph.
deba@1866
    35
  ///
deba@1866
    36
  /// Base for the SubGraph.
deba@1866
    37
  template <typename _Graph>
deba@1866
    38
  class SubGraphBase : public GraphAdaptorBase<const _Graph> {
deba@1866
    39
  public:
deba@1866
    40
    typedef _Graph Graph;
deba@1866
    41
    typedef SubGraphBase<_Graph> SubGraph;
deba@1866
    42
    typedef GraphAdaptorBase<const _Graph> Parent;
deba@1866
    43
    typedef Parent Base;
deba@1866
    44
deba@1866
    45
    typedef typename Parent::Node Node;
deba@1866
    46
    typedef typename Parent::Edge Edge;
deba@1866
    47
deba@1866
    48
deba@1866
    49
  protected:
deba@1866
    50
deba@1866
    51
    class NodesImpl;
deba@1866
    52
    class EdgesImpl;
deba@1866
    53
deba@1866
    54
    SubGraphBase() {}
deba@1866
    55
deba@1866
    56
    void construct(const Graph& _graph, NodesImpl& _nodes, EdgesImpl& _edges) {
deba@1866
    57
      Parent::setGraph(_graph);
deba@1866
    58
      nodes = &_nodes;
deba@1866
    59
      edges = &_edges;
deba@1866
    60
      firstNode = INVALID;
deba@1866
    61
deba@1866
    62
      Node node;
deba@1866
    63
      Parent::first(node);
deba@1866
    64
      while (node != INVALID) {
deba@1866
    65
	(*nodes)[node].prev = node;
deba@1866
    66
	(*nodes)[node].firstIn = INVALID;
deba@1866
    67
	(*nodes)[node].firstOut = INVALID;
deba@1866
    68
	Parent::next(node);
deba@1866
    69
      }
deba@1866
    70
deba@1866
    71
      Edge edge;
deba@1866
    72
      Parent::first(edge);
deba@1866
    73
      while (edge != INVALID) {
deba@1866
    74
	(*edges)[edge].prevOut = edge;
deba@1866
    75
	Parent::next(edge);
deba@1866
    76
      }
deba@1866
    77
    }
deba@1866
    78
deba@1866
    79
  public:
deba@1866
    80
deba@1866
    81
    void first(Node& node) const {
deba@1866
    82
      node = firstNode;
deba@1866
    83
    }
deba@1866
    84
    void next(Node& node) const {
deba@1866
    85
      node = (*nodes)[node].next;
deba@1866
    86
    }
deba@1866
    87
deba@1866
    88
    void first(Edge& edge) const {
deba@1866
    89
      Node node = firstNode;
deba@1866
    90
      while (node != INVALID && (*nodes)[node].firstOut == INVALID) {
deba@1866
    91
	node = (*nodes)[node].next;
deba@1866
    92
      }
deba@1866
    93
      if (node == INVALID) {
deba@1866
    94
	edge = INVALID;
deba@1866
    95
      } else {
deba@1866
    96
	edge = (*nodes)[node].firstOut;
deba@1866
    97
      }
deba@1866
    98
    }
deba@1866
    99
    void next(Edge& edge) const {
deba@1866
   100
      if ((*edges)[edge].nextOut != INVALID) {
deba@1866
   101
	edge = (*edges)[edge].nextOut;
deba@1866
   102
      } else {
deba@1866
   103
	Node node = (*nodes)[source(edge)].next;
deba@1866
   104
	while (node != INVALID && (*nodes)[node].firstOut == INVALID) {
deba@1866
   105
	  node = (*nodes)[node].next;
deba@1866
   106
	}
deba@1866
   107
	if (node == INVALID) {
deba@1866
   108
	  edge = INVALID;
deba@1866
   109
	} else {
deba@1866
   110
	  edge = (*nodes)[node].firstOut;
deba@1866
   111
	}
deba@1866
   112
      }
deba@1866
   113
    }
deba@1866
   114
deba@1866
   115
    void firstOut(Edge& edge, const Node& node) const {
deba@1866
   116
      edge = (*nodes)[node].firstOut;
deba@1866
   117
    }
deba@1866
   118
    void nextOut(Edge& edge) const {
deba@1866
   119
      edge = (*edges)[edge].nextOut;
deba@1866
   120
    }
deba@1866
   121
deba@1866
   122
    void firstIn(Edge& edge, const Node& node) const {
deba@1866
   123
      edge = (*nodes)[node].firstIn;
deba@1866
   124
    }
deba@1866
   125
    void nextIn(Edge& edge) const {
deba@1866
   126
      edge = (*edges)[edge].nextIn;
deba@1866
   127
    }
deba@1866
   128
deba@1866
   129
    /// \brief Returns true when the given node is hidden.
deba@1866
   130
    ///
deba@1866
   131
    /// Returns true when the given node is hidden.
deba@1866
   132
    bool hidden(const Node& node) const {
deba@1866
   133
      return (*nodes)[node].prev == node;
deba@1866
   134
    }
deba@1866
   135
deba@1866
   136
    /// \brief Hide the given node in the sub-graph.
deba@1866
   137
    ///
deba@1866
   138
    /// Hide the given node in the sub graph. It just lace out from
deba@1866
   139
    /// the linked lists the given node. If there are incoming or outgoing
deba@1866
   140
    /// edges into or from this node then all of these will be hidden.
deba@1866
   141
    void hide(const Node& node) {
deba@1866
   142
      if (hidden(node)) return;
deba@1866
   143
      Edge edge;
deba@1866
   144
      firstOut(edge, node);
deba@1866
   145
      while (edge != INVALID) {
deba@1866
   146
	hide(edge);
deba@1866
   147
	firstOut(edge, node);
deba@1866
   148
      }
deba@1866
   149
deba@1866
   150
      firstOut(edge, node);
deba@1866
   151
      while (edge != INVALID) {
deba@1866
   152
	hide(edge);
deba@1866
   153
	firstOut(edge, node);
deba@1866
   154
      }
deba@1866
   155
      if ((*nodes)[node].prev != INVALID) {
deba@1866
   156
	(*nodes)[(*nodes)[node].prev].next = (*nodes)[node].next;
deba@1866
   157
      } else {
deba@1866
   158
	firstNode = (*nodes)[node].next;
deba@1866
   159
      }
deba@1866
   160
      if ((*nodes)[node].next != INVALID) {
deba@1866
   161
	(*nodes)[(*nodes)[node].next].prev = (*nodes)[node].prev;
deba@1866
   162
      }
deba@1866
   163
      (*nodes)[node].prev = node;
deba@1866
   164
      (*nodes)[node].firstIn = INVALID;
deba@1866
   165
      (*nodes)[node].firstOut = INVALID;
deba@1866
   166
    }
deba@1866
   167
deba@1866
   168
    /// \brief Unhide the given node in the sub-graph.
deba@1866
   169
    ///
deba@1866
   170
    /// Unhide the given node in the sub graph. It just lace in the given
deba@1866
   171
    /// node into the linked lists.
deba@1866
   172
    void unHide(const Node& node) {
deba@1866
   173
      if (!hidden(node)) return;
deba@1866
   174
      (*nodes)[node].next = firstNode;
deba@1866
   175
      (*nodes)[node].prev = INVALID;
deba@1866
   176
      if ((*nodes)[node].next != INVALID) {
deba@1866
   177
	(*nodes)[(*nodes)[node].next].prev = node;
deba@1866
   178
      }
deba@1866
   179
      firstNode = node;
deba@1866
   180
    }
deba@1866
   181
deba@1866
   182
    /// \brief Returns true when the given edge is hidden.
deba@1866
   183
    ///
deba@1866
   184
    /// Returns true when the given edge is hidden.
deba@1866
   185
    bool hidden(const Edge& edge) const {
deba@1866
   186
      return (*edges)[edge].prevOut == edge;
deba@1866
   187
    }
deba@1866
   188
deba@1866
   189
    /// \brief Hide the given edge in the sub-graph.
deba@1866
   190
    ///
deba@1866
   191
    /// Hide the given edge in the sub graph. It just lace out from
deba@1866
   192
    /// the linked lists the given edge.
deba@1866
   193
    void hide(const Edge& edge) {
deba@1866
   194
      if (hidden(edge)) return;
deba@1866
   195
      if ((*edges)[edge].prevOut == edge) return;
deba@1866
   196
      if ((*edges)[edge].prevOut != INVALID) {
deba@1866
   197
	(*edges)[(*edges)[edge].prevOut].nextOut = (*edges)[edge].nextOut;
deba@1866
   198
      } else {
deba@1866
   199
	(*nodes)[source(edge)].firstOut = (*edges)[edge].nextOut;
deba@1866
   200
      }
deba@1866
   201
      if ((*edges)[edge].nextOut != INVALID) {
deba@1866
   202
	(*edges)[(*edges)[edge].nextOut].prevOut = (*edges)[edge].prevOut;
deba@1866
   203
      }
deba@1866
   204
deba@1866
   205
      if ((*edges)[edge].prevIn != INVALID) {
deba@1866
   206
	(*edges)[(*edges)[edge].prevIn].nextIn = (*edges)[edge].nextIn;
deba@1866
   207
      } else {
deba@1866
   208
	(*nodes)[target(edge)].firstIn = (*edges)[edge].nextIn;
deba@1866
   209
      }
deba@1866
   210
      if ((*edges)[edge].nextIn != INVALID) {
deba@1866
   211
	(*edges)[(*edges)[edge].nextIn].prevIn = (*edges)[edge].prevIn;
deba@1866
   212
      }
deba@1866
   213
      (*edges)[edge].next = edge;
deba@1866
   214
    }
deba@1866
   215
deba@1866
   216
    /// \brief Unhide the given edge in the sub-graph.
deba@1866
   217
    ///
deba@1866
   218
    /// Unhide the given edge in the sub graph. It just lace in the given
deba@1866
   219
    /// edge into the linked lists. If the source or the target of the
deba@1866
   220
    /// node is hidden then it will unhide it.
deba@1866
   221
    void unHide(const Edge& edge) {
deba@1866
   222
      if (!hidden(edge)) return;
deba@1866
   223
deba@1866
   224
      Node node;
deba@1866
   225
deba@1866
   226
      node = Parent::source(edge);
deba@1866
   227
      unHide(node);
deba@1866
   228
      (*edges)[edge].nextOut = (*nodes)[node].firstOut;
deba@1866
   229
      (*edges)[edge].prevOut = INVALID;
deba@1866
   230
      if ((*edges)[edge].nextOut != INVALID) {
deba@1866
   231
	(*edges)[(*edges)[edge].nextOut].prevOut = edge;
deba@1866
   232
      }
deba@1866
   233
      (*nodes)[node].firstOut = edge;
deba@1866
   234
deba@1866
   235
      node = Parent::target(edge);
deba@1866
   236
      unHide(node);
deba@1866
   237
      (*edges)[edge].nextIn = (*nodes)[node].firstIn;
deba@1866
   238
      (*edges)[edge].prevIn = INVALID;
deba@1866
   239
      if ((*edges)[edge].nextIn != INVALID) {
deba@1866
   240
	(*edges)[(*edges)[edge].nextIn].prevIn = edge;
deba@1866
   241
      }
deba@1866
   242
      (*nodes)[node].firstIn = edge;      
deba@1866
   243
    }
deba@1866
   244
    
deba@1866
   245
    typedef False NodeNumTag;
deba@1866
   246
    typedef False EdgeNumTag;
deba@1866
   247
deba@1866
   248
  protected:
deba@1866
   249
    struct NodeT {
deba@1866
   250
      Node prev, next;
deba@1866
   251
      Edge firstIn, firstOut;
deba@1866
   252
    };
deba@1990
   253
    class NodesImpl : public DefaultMap<Graph, Node, NodeT> {
deba@1866
   254
      friend class SubGraphBase;
deba@1866
   255
    public:
deba@1990
   256
      typedef DefaultMap<Graph, Node, NodeT> Parent;
deba@1866
   257
deba@1866
   258
      NodesImpl(SubGraph& _adaptor, const Graph& _graph) 
deba@1866
   259
	: Parent(_graph), adaptor(_adaptor) {}
deba@1866
   260
deba@1866
   261
      virtual ~NodesImpl() {}
deba@1866
   262
deba@1866
   263
      virtual void build() {
deba@1866
   264
	Parent::build();
deba@1866
   265
	Node node;
deba@1866
   266
	adaptor.Base::first(node);
deba@1866
   267
	while (node != INVALID) {
deba@1866
   268
	  Parent::operator[](node).prev = node;
deba@1866
   269
	  Parent::operator[](node).firstIn = INVALID;
deba@1866
   270
	  Parent::operator[](node).firstOut = INVALID;
deba@1866
   271
	  adaptor.Base::next(node);
deba@1866
   272
	}
deba@1866
   273
      }
deba@1866
   274
deba@1866
   275
      virtual void clear() {
deba@1866
   276
	adaptor.firstNode = INVALID;
deba@1866
   277
	Parent::clear();
deba@1866
   278
      }
deba@1866
   279
deba@1866
   280
      virtual void add(const Node& node) {
deba@1866
   281
	Parent::add(node);
deba@1866
   282
	Parent::operator[](node).prev = node;
deba@1866
   283
	Parent::operator[](node).firstIn = INVALID;
deba@1866
   284
	Parent::operator[](node).firstOut = INVALID;
deba@1866
   285
      }
deba@1964
   286
deba@1866
   287
      virtual void add(const std::vector<Node>& nodes) {
deba@1866
   288
	Parent::add(nodes);
deba@1866
   289
	for (int i = 0; i < (int)nodes.size(); ++i) {
deba@1866
   290
	  Parent::operator[](nodes[i]).prev = nodes[i];
deba@1866
   291
	  Parent::operator[](nodes[i]).firstIn = INVALID;
deba@1866
   292
	  Parent::operator[](nodes[i]).firstOut = INVALID;
deba@1866
   293
	}
deba@1866
   294
      } 
deba@1866
   295
deba@1866
   296
      virtual void erase(const Node& node) {
deba@1866
   297
	adaptor.hide(node);
deba@1866
   298
	Parent::erase(node);
deba@1866
   299
      }
deba@1866
   300
deba@1866
   301
      virtual void erase(const std::vector<Node>& nodes) {
deba@1866
   302
	for (int i = 0; i < (int)nodes.size(); ++i) {
deba@1866
   303
	  adaptor.hide(nodes[i]);
deba@1866
   304
	}
deba@1866
   305
	Parent::erase(nodes);
deba@1866
   306
      }
deba@1866
   307
deba@1866
   308
    private:
deba@1866
   309
      SubGraph& adaptor;
deba@1866
   310
    };
deba@1866
   311
deba@1866
   312
    struct EdgeT {
deba@1866
   313
      Edge prevOut, nextOut;
deba@1866
   314
      Edge prevIn, nextIn;
deba@1866
   315
    };
deba@1990
   316
    class EdgesImpl : public DefaultMap<Graph, Edge, EdgeT> {
deba@1866
   317
      friend class SubGraphBase;
deba@1866
   318
    public:
deba@1990
   319
      typedef DefaultMap<Graph, Edge, EdgeT> Parent;
deba@1866
   320
deba@1866
   321
      EdgesImpl(SubGraph& _adaptor, const Graph& _graph) 
deba@1866
   322
	: Parent(_graph), adaptor(_adaptor) {}
deba@1866
   323
deba@1866
   324
      virtual ~EdgesImpl() {}
deba@1866
   325
deba@1866
   326
      virtual void build() {
deba@1866
   327
	Parent::build();
deba@1866
   328
	Edge edge;
deba@1866
   329
	adaptor.Base::first(edge);
deba@1866
   330
	while (edge != INVALID) {
deba@1866
   331
	  Parent::operator[](edge).prevOut = edge;
deba@1866
   332
	  adaptor.Base::next(edge);
deba@1866
   333
	}
deba@1866
   334
      }
deba@1866
   335
deba@1866
   336
      virtual void clear() {
deba@1866
   337
	Node node;
deba@1866
   338
	adaptor.first(node);
deba@1866
   339
	while (node != INVALID) {
deba@1866
   340
	  (*adaptor.nodes).firstIn = INVALID;
deba@1866
   341
	  (*adaptor.nodes).firstOut = INVALID;
deba@1866
   342
	  adaptor.next(node);
deba@1866
   343
	}
deba@1866
   344
	Parent::clear();
deba@1866
   345
      }
deba@1866
   346
deba@1866
   347
      virtual void add(const Edge& edge) {
deba@1866
   348
	Parent::add(edge);
deba@1866
   349
	Parent::operator[](edge).prevOut = edge;
deba@1866
   350
      }
deba@1866
   351
deba@1866
   352
      virtual void add(const std::vector<Edge>& edges) {
deba@1866
   353
	Parent::add(edges);
deba@1866
   354
	for (int i = 0; i < (int)edges.size(); ++i) {
deba@1866
   355
	  Parent::operator[](edges[i]).prevOut = edges[i];
deba@1866
   356
	}
deba@1866
   357
      }
deba@1866
   358
deba@1866
   359
      virtual void erase(const Edge& edge) {
deba@1866
   360
	adaptor.hide(edge);
deba@1866
   361
	Parent::erase(edge);
deba@1866
   362
      }
deba@1866
   363
deba@1866
   364
      virtual void erase(const std::vector<Edge>& edges) {
deba@1866
   365
	for (int i = 0; i < (int)edges.size(); ++i) {
deba@1866
   366
	  adaptor.hide(edges[i]);
deba@1866
   367
	}
deba@1964
   368
	Parent::erase(edges);
deba@1866
   369
      }
deba@1866
   370
deba@1866
   371
    private:
deba@1866
   372
      SubGraph& adaptor;
deba@1866
   373
    };
deba@1866
   374
deba@1866
   375
    NodesImpl* nodes;
deba@1866
   376
    EdgesImpl* edges;
deba@1866
   377
    Node firstNode;
deba@1866
   378
  };
deba@1866
   379
deba@1866
   380
  /// \ingroup semi_adaptors
deba@1866
   381
  ///
alpar@2006
   382
  /// \brief Graph which uses a subset of another graph's nodes and edges.
deba@1866
   383
  ///
alpar@2006
   384
  /// Graph which uses a subset of another graph's nodes and edges. This class
deba@1866
   385
  /// is an alternative to the SubGraphAdaptor which is created for the
deba@1866
   386
  /// same reason. The main difference between the two class that it
deba@1866
   387
  /// makes linked lists on the unhidden nodes and edges what cause that
deba@1866
   388
  /// on sparse subgraphs the algorithms can be more efficient and some times
deba@1866
   389
  /// provide better time complexity. On other way this implemetation is
deba@1866
   390
  /// less efficient in most case when the subgraph filters out only
deba@1866
   391
  /// a few nodes or edges.
deba@1866
   392
  /// 
deba@1866
   393
  /// \see SubGraphAdaptor
deba@1866
   394
  /// \see EdgeSubGraphBase
deba@1866
   395
  template <typename Graph>
deba@1866
   396
  class SubGraph 
deba@1979
   397
    : public GraphAdaptorExtender< SubGraphBase<Graph> > {
deba@1866
   398
  public:
deba@1979
   399
    typedef GraphAdaptorExtender< SubGraphBase<Graph> > Parent;
deba@1866
   400
  public:
deba@1866
   401
    /// \brief Constructor for sub-graph.
deba@1866
   402
    ///
deba@1866
   403
    /// Constructor for sub-graph. Initially all the edges and nodes
deba@1866
   404
    /// are hidden in the graph.
deba@1866
   405
    SubGraph(const Graph& _graph) 
deba@1866
   406
      : Parent(), nodes(*this, _graph), edges(*this, _graph) { 
deba@1866
   407
      Parent::construct(_graph, nodes, edges);
deba@1866
   408
    }
deba@1866
   409
  private:
deba@1866
   410
    typename Parent::NodesImpl nodes;
deba@1866
   411
    typename Parent::EdgesImpl edges;
deba@1866
   412
  };
deba@1866
   413
deba@1866
   414
  /// \brief Base for the EdgeSubGraph.
deba@1866
   415
  ///
deba@1866
   416
  /// Base for the EdgeSubGraph.
deba@1866
   417
  template <typename _Graph>
deba@1866
   418
  class EdgeSubGraphBase : public GraphAdaptorBase<const _Graph> {
deba@1866
   419
  public:
deba@1866
   420
    typedef _Graph Graph;
deba@1866
   421
    typedef EdgeSubGraphBase<_Graph> SubGraph;
deba@1866
   422
    typedef GraphAdaptorBase<const _Graph> Parent;
deba@1866
   423
    typedef Parent Base;
deba@1866
   424
deba@1866
   425
    typedef typename Parent::Node Node;
deba@1866
   426
    typedef typename Parent::Edge Edge;
deba@1866
   427
deba@1866
   428
deba@1866
   429
  protected:
deba@1866
   430
deba@1866
   431
    class NodesImpl;
deba@1866
   432
    class EdgesImpl;
deba@1866
   433
deba@1866
   434
    EdgeSubGraphBase() {}
deba@1866
   435
deba@1866
   436
    void construct(const Graph& _graph, NodesImpl& _nodes, EdgesImpl& _edges) {
deba@1866
   437
      Parent::setGraph(_graph);
deba@1866
   438
      nodes = &_nodes;
deba@1866
   439
      edges = &_edges;
deba@1866
   440
deba@1866
   441
      Node node;
deba@1866
   442
      Parent::first(node);
deba@1866
   443
      while (node != INVALID) {
deba@1866
   444
	(*nodes)[node].firstIn = INVALID;
deba@1866
   445
	(*nodes)[node].firstOut = INVALID;
deba@1866
   446
	Parent::next(node);
deba@1866
   447
      }
deba@1866
   448
deba@1866
   449
      Edge edge;
deba@1866
   450
      Parent::first(edge);
deba@1866
   451
      while (edge != INVALID) {
deba@1866
   452
	(*edges)[edge].prevOut = edge;
deba@1866
   453
	Parent::next(edge);
deba@1866
   454
      }
deba@1866
   455
    }
deba@1866
   456
deba@1866
   457
  public:
deba@1866
   458
deba@1866
   459
    void first(Node& node) const {
deba@1866
   460
      Parent::first(node);
deba@1866
   461
    }
deba@1866
   462
    void next(Node& node) const {
deba@1866
   463
      Parent::next(node);
deba@1866
   464
    }
deba@1866
   465
deba@1866
   466
    void first(Edge& edge) const {
deba@1866
   467
      Node node;
deba@1866
   468
      Parent::first(node);
deba@1866
   469
      while (node != INVALID && (*nodes)[node].firstOut == INVALID) {
deba@1866
   470
	Parent::next(node);
deba@1866
   471
      }
deba@1866
   472
      if (node == INVALID) {
deba@1866
   473
	edge = INVALID;
deba@1866
   474
      } else {
deba@1866
   475
	edge = (*nodes)[node].firstOut;
deba@1866
   476
      }
deba@1866
   477
    }
deba@1866
   478
    void next(Edge& edge) const {
deba@1866
   479
      if ((*edges)[edge].nextOut != INVALID) {
deba@1866
   480
	edge = (*edges)[edge].nextOut;
deba@1866
   481
      } else {
deba@1866
   482
	Node node = source(edge);
deba@1866
   483
	Parent::next(node);
deba@1866
   484
	while (node != INVALID && (*nodes)[node].firstOut == INVALID) {
deba@1866
   485
	  Parent::next(node);
deba@1866
   486
	}
deba@1866
   487
	if (node == INVALID) {
deba@1866
   488
	  edge = INVALID;
deba@1866
   489
	} else {
deba@1866
   490
	  edge = (*nodes)[node].firstOut;
deba@1866
   491
	}
deba@1866
   492
      }
deba@1866
   493
    }
deba@1866
   494
deba@1866
   495
    void firstOut(Edge& edge, const Node& node) const {
deba@1866
   496
      edge = (*nodes)[node].firstOut;
deba@1866
   497
    }
deba@1866
   498
    void nextOut(Edge& edge) const {
deba@1866
   499
      edge = (*edges)[edge].nextOut;
deba@1866
   500
    }
deba@1866
   501
deba@1866
   502
    void firstIn(Edge& edge, const Node& node) const {
deba@1866
   503
      edge = (*nodes)[node].firstIn;
deba@1866
   504
    }
deba@1866
   505
    void nextIn(Edge& edge) const {
deba@1866
   506
      edge = (*edges)[edge].nextIn;
deba@1866
   507
    }
deba@1866
   508
deba@1866
   509
    /// \brief Returns true when the given edge is hidden.
deba@1866
   510
    ///
deba@1866
   511
    /// Returns true when the given edge is hidden.
deba@1866
   512
    bool hidden(const Edge& edge) const {
deba@1866
   513
      return (*edges)[edge].prevOut == edge;
deba@1866
   514
    }
deba@1866
   515
deba@1866
   516
    /// \brief Hide the given edge in the sub-graph.
deba@1866
   517
    ///
deba@1866
   518
    /// Hide the given edge in the sub graph. It just lace out from
deba@1866
   519
    /// the linked lists the given edge.
deba@1866
   520
    void hide(const Edge& edge) {
deba@1866
   521
      if (hidden(edge)) return;
deba@1866
   522
      if ((*edges)[edge].prevOut != INVALID) {
deba@1866
   523
	(*edges)[(*edges)[edge].prevOut].nextOut = (*edges)[edge].nextOut;
deba@1866
   524
      } else {
deba@1866
   525
	(*nodes)[source(edge)].firstOut = (*edges)[edge].nextOut;
deba@1866
   526
      }
deba@1866
   527
      if ((*edges)[edge].nextOut != INVALID) {
deba@1866
   528
	(*edges)[(*edges)[edge].nextOut].prevOut = (*edges)[edge].prevOut;
deba@1866
   529
      }
deba@1866
   530
deba@1866
   531
      if ((*edges)[edge].prevIn != INVALID) {
deba@1866
   532
	(*edges)[(*edges)[edge].prevIn].nextIn = (*edges)[edge].nextIn;
deba@1866
   533
      } else {
deba@1866
   534
	(*nodes)[target(edge)].firstIn = (*edges)[edge].nextIn;
deba@1866
   535
      }
deba@1866
   536
      if ((*edges)[edge].nextIn != INVALID) {
deba@1866
   537
	(*edges)[(*edges)[edge].nextIn].prevIn = (*edges)[edge].prevIn;
deba@1866
   538
      }
deba@1866
   539
      (*edges)[edge].prevOut = edge;
deba@1866
   540
    }
deba@1866
   541
deba@1866
   542
    /// \brief Unhide the given edge in the sub-graph.
deba@1866
   543
    ///
deba@1866
   544
    /// Unhide the given edge in the sub graph. It just lace in the given
deba@1866
   545
    /// edge into the linked lists.
deba@1866
   546
    void unHide(const Edge& edge) {
deba@1866
   547
      if (!hidden(edge)) return;
deba@1866
   548
      Node node;
deba@1866
   549
deba@1866
   550
      node = Parent::source(edge);
deba@1866
   551
      (*edges)[edge].nextOut = (*nodes)[node].firstOut;
deba@1866
   552
      (*edges)[edge].prevOut = INVALID;
deba@1866
   553
      if ((*edges)[edge].nextOut != INVALID) {
deba@1866
   554
	(*edges)[(*edges)[edge].nextOut].prevOut = edge;
deba@1866
   555
      }
deba@1866
   556
      (*nodes)[node].firstOut = edge;
deba@1866
   557
deba@1866
   558
      node = Parent::target(edge);
deba@1866
   559
      (*edges)[edge].nextIn = (*nodes)[node].firstIn;
deba@1866
   560
      (*edges)[edge].prevIn = INVALID;
deba@1866
   561
      if ((*edges)[edge].nextIn != INVALID) {
deba@1866
   562
	(*edges)[(*edges)[edge].nextIn].prevIn = edge;
deba@1866
   563
      }
deba@1866
   564
      (*nodes)[node].firstIn = edge;      
deba@1866
   565
    }
deba@1866
   566
    
deba@1866
   567
  protected:
deba@1866
   568
    struct NodeT {
deba@1866
   569
      Edge firstIn, firstOut;
deba@1866
   570
    };
deba@1990
   571
    class NodesImpl : public DefaultMap<Graph, Node, NodeT> {
deba@1866
   572
      friend class EdgeSubGraphBase;
deba@1866
   573
    public:
deba@1990
   574
      typedef DefaultMap<Graph, Node, NodeT> Parent;
deba@1866
   575
deba@1866
   576
      NodesImpl(SubGraph& _adaptor, const Graph& _graph) 
deba@1866
   577
	: Parent(_graph), adaptor(_adaptor) {}
deba@1866
   578
deba@1866
   579
      virtual ~NodesImpl() {}
deba@1866
   580
deba@1866
   581
      virtual void build() {
deba@1866
   582
	Parent::build();
deba@1866
   583
	Node node;
deba@1866
   584
	adaptor.Base::first(node);
deba@1866
   585
	while (node != INVALID) {
deba@1866
   586
	  Parent::operator[](node).firstIn = INVALID;
deba@1866
   587
	  Parent::operator[](node).firstOut = INVALID;
deba@1866
   588
	  adaptor.Base::next(node);
deba@1866
   589
	}
deba@1866
   590
      }
deba@1866
   591
deba@1866
   592
      virtual void add(const Node& node) {
deba@1866
   593
	Parent::add(node);
deba@1866
   594
	Parent::operator[](node).firstIn = INVALID;
deba@1866
   595
	Parent::operator[](node).firstOut = INVALID;
deba@1866
   596
      }
deba@1866
   597
deba@1964
   598
      virtual void add(const std::vector<Node>& nodes) {
deba@1964
   599
        Parent::add(nodes);
deba@1964
   600
        for (int i = 0; i < (int)nodes.size(); ++i) {
deba@1964
   601
          Parent::operator[](nodes[i]).firstIn = INVALID;
deba@1964
   602
          Parent::operator[](nodes[i]).firstOut = INVALID;
deba@1964
   603
        }
deba@1964
   604
      }
deba@1964
   605
deba@1866
   606
    private:
deba@1866
   607
      SubGraph& adaptor;
deba@1866
   608
    };
deba@1866
   609
deba@1866
   610
    struct EdgeT {
deba@1866
   611
      Edge prevOut, nextOut;
deba@1866
   612
      Edge prevIn, nextIn;
deba@1866
   613
    };
deba@1990
   614
    class EdgesImpl : public DefaultMap<Graph, Edge, EdgeT> {
deba@1866
   615
      friend class EdgeSubGraphBase;
deba@1866
   616
    public:
deba@1990
   617
      typedef DefaultMap<Graph, Edge, EdgeT> Parent;
deba@1866
   618
deba@1866
   619
      EdgesImpl(SubGraph& _adaptor, const Graph& _graph) 
deba@1866
   620
	: Parent(_graph), adaptor(_adaptor) {}
deba@1866
   621
deba@1866
   622
      virtual ~EdgesImpl() {}
deba@1866
   623
deba@1866
   624
      virtual void build() {
deba@1866
   625
	Parent::build();
deba@1866
   626
	Edge edge;
deba@1866
   627
	adaptor.Base::first(edge);
deba@1866
   628
	while (edge != INVALID) {
deba@1866
   629
	  Parent::operator[](edge).prevOut = edge;
deba@1866
   630
	  adaptor.Base::next(edge);
deba@1866
   631
	}
deba@1866
   632
      }
deba@1866
   633
deba@1866
   634
      virtual void clear() {
deba@1866
   635
	Node node;
deba@1866
   636
	adaptor.Base::first(node);
deba@1866
   637
	while (node != INVALID) {
deba@1866
   638
	  (*adaptor.nodes)[node].firstIn = INVALID;
deba@1866
   639
	  (*adaptor.nodes)[node].firstOut = INVALID;
deba@1866
   640
	  adaptor.Base::next(node);
deba@1866
   641
	}
deba@1866
   642
	Parent::clear();
deba@1866
   643
      }
deba@1866
   644
deba@1866
   645
      virtual void add(const Edge& edge) {
deba@1866
   646
	Parent::add(edge);
deba@1866
   647
	Parent::operator[](edge).prevOut = edge;
deba@1866
   648
      }
deba@1866
   649
deba@1866
   650
      virtual void add(const std::vector<Edge>& edges) {
deba@1866
   651
	Parent::add(edges);
deba@1866
   652
	for (int i = 0; i < (int)edges.size(); ++i) {
deba@1866
   653
	  Parent::operator[](edges[i]).prevOut = edges[i];
deba@1866
   654
	}
deba@1866
   655
      }
deba@1866
   656
deba@1866
   657
      virtual void erase(const Edge& edge) {
deba@1866
   658
	adaptor.hide(edge);
deba@1866
   659
	Parent::erase(edge);
deba@1866
   660
      }
deba@1866
   661
deba@1866
   662
      virtual void erase(const std::vector<Edge>& edges) {
deba@1866
   663
	for (int i = 0; i < (int)edges.size(); ++i) {
deba@1866
   664
	  adaptor.hide(edges[i]);
deba@1866
   665
	}
deba@1964
   666
	Parent::erase(edges);
deba@1866
   667
      }
deba@1866
   668
deba@1866
   669
    private:
deba@1866
   670
      SubGraph& adaptor;
deba@1866
   671
    };
deba@1866
   672
deba@1866
   673
    NodesImpl* nodes;
deba@1866
   674
    EdgesImpl* edges;
deba@1866
   675
  };
deba@1866
   676
deba@1866
   677
  /// \ingroup semi_adaptors
deba@1866
   678
  ///
alpar@2006
   679
  /// \brief Graph which uses a subset of another graph's edges.
deba@1866
   680
  ///
alpar@2006
   681
  /// Graph which uses a subset of another graph's edges. This class
deba@1866
   682
  /// is an alternative to the EdgeSubGraphAdaptor which is created for the
deba@1866
   683
  /// same reason. The main difference between the two class that it
deba@1866
   684
  /// makes linked lists on the unhidden edges what cause that
deba@1866
   685
  /// on sparse subgraphs the algorithms can be more efficient and some times
deba@1866
   686
  /// provide better time complexity. On other way this implemetation is
deba@1866
   687
  /// less efficient in most case when the subgraph filters out only
deba@1866
   688
  /// a few edges.
deba@1866
   689
  /// 
deba@1866
   690
  /// \see EdgeSubGraphAdaptor
deba@1866
   691
  /// \see EdgeSubGraphBase
deba@1866
   692
  template <typename Graph>
deba@1866
   693
  class EdgeSubGraph 
deba@1979
   694
    : public GraphAdaptorExtender< EdgeSubGraphBase<Graph> > {
deba@1866
   695
  public:
deba@1979
   696
    typedef GraphAdaptorExtender< EdgeSubGraphBase<Graph> > Parent;
deba@1866
   697
  public:
deba@1866
   698
    /// \brief Constructor for sub-graph.
deba@1866
   699
    ///
deba@1866
   700
    /// Constructor for sub-graph. Initially all the edges are hidden in the 
deba@1866
   701
    /// graph.
deba@1866
   702
    EdgeSubGraph(const Graph& _graph) 
deba@1866
   703
      : Parent(), nodes(*this, _graph), edges(*this, _graph) { 
deba@1866
   704
      Parent::construct(_graph, nodes, edges);
deba@1866
   705
    }
deba@1866
   706
  private:
deba@1866
   707
    typename Parent::NodesImpl nodes;
deba@1866
   708
    typename Parent::EdgesImpl edges;
deba@1866
   709
  };
deba@1866
   710
deba@1866
   711
deba@1866
   712
//   template<typename Graph, typename Number, 
deba@1866
   713
// 	   typename CapacityMap, typename FlowMap>
deba@1866
   714
//   class ResGraph
deba@1866
   715
//     : public IterableGraphExtender<EdgeSubGraphBase<
klao@1909
   716
//     UGraphAdaptor<Graph> > > {
deba@1866
   717
//   public:
deba@1866
   718
//     typedef IterableGraphExtender<EdgeSubGraphBase<
klao@1909
   719
//       UGraphAdaptor<Graph> > > Parent;
deba@1866
   720
deba@1866
   721
//   protected:
klao@1909
   722
//     UGraphAdaptor<Graph> u;
deba@1866
   723
deba@1866
   724
//     const CapacityMap* capacity;
deba@1866
   725
//     FlowMap* flow;
deba@1866
   726
deba@1866
   727
//     typename Parent::NodesImpl nodes;
deba@1866
   728
//     typename Parent::EdgesImpl edges;
deba@1866
   729
deba@1866
   730
//     void setCapacityMap(const CapacityMap& _capacity) {
deba@1866
   731
//       capacity=&_capacity;
deba@1866
   732
//     }
deba@1866
   733
deba@1866
   734
//     void setFlowMap(FlowMap& _flow) {
deba@1866
   735
//       flow=&_flow;
deba@1866
   736
//     }
deba@1866
   737
deba@1866
   738
//   public:
deba@1866
   739
klao@1909
   740
//     typedef typename UGraphAdaptor<Graph>::Node Node;
klao@1909
   741
//     typedef typename UGraphAdaptor<Graph>::Edge Edge;
klao@1909
   742
//     typedef typename UGraphAdaptor<Graph>::UEdge UEdge;
deba@1866
   743
deba@1866
   744
//     ResGraphAdaptor(Graph& _graph, 
deba@1866
   745
// 		    const CapacityMap& _capacity, FlowMap& _flow) 
klao@1909
   746
//       : Parent(), u(_graph), capacity(&_capacity), flow(&_flow),
deba@1866
   747
// 	nodes(*this, _graph), edges(*this, _graph) {
klao@1909
   748
//       Parent::construct(u, nodes, edges);
deba@1866
   749
//       setFlowMap(_flow);
deba@1866
   750
//       setCapacityMap(_capacity);
deba@1866
   751
//       typename Graph::Edge edge; 
deba@1866
   752
//       for (_graph.first(edge); edge != INVALID; _graph.next(edge)) {
deba@1866
   753
// 	if ((*flow)[edge] != (*capacity)[edge]) {
deba@1866
   754
// 	  Parent::unHide(direct(edge, true));
deba@1866
   755
// 	}
deba@1866
   756
// 	if ((*flow)[edge] != 0) {
deba@1866
   757
// 	  Parent::unHide(direct(edge, false));
deba@1866
   758
// 	}
deba@1866
   759
//       }
deba@1866
   760
//     }
deba@1866
   761
deba@1866
   762
//     void augment(const Edge& e, Number a) {
deba@1866
   763
//       if (direction(e)) {
deba@1866
   764
// 	flow->set(e, (*flow)[e]+a);
deba@1866
   765
//       } else { 
deba@1866
   766
// 	flow->set(e, (*flow)[e]-a);
deba@1866
   767
//       }
deba@1866
   768
//       if ((*flow)[e] == (*capacity)[e]) {
deba@1866
   769
// 	Parent::hide(e);
deba@1866
   770
//       } else {
deba@1866
   771
// 	Parent::unHide(e);
deba@1866
   772
//       }
deba@1866
   773
//       if ((*flow)[e] == 0) {
deba@1866
   774
// 	Parent::hide(oppositeEdge(e));
deba@1866
   775
//       } else {
deba@1866
   776
// 	Parent::unHide(oppositeEdge(e));
deba@1866
   777
//       }
deba@1866
   778
//     }
deba@1866
   779
deba@1866
   780
//     Number resCap(const Edge& e) {
deba@1866
   781
//       if (direction(e)) { 
deba@1866
   782
// 	return (*capacity)[e]-(*flow)[e]; 
deba@1866
   783
//       } else { 
deba@1866
   784
// 	return (*flow)[e];
deba@1866
   785
//       }
deba@1866
   786
//     }
deba@1866
   787
    
deba@1866
   788
//     bool direction(const Edge& edge) const {
deba@1866
   789
//       return Parent::getGraph().direction(edge);
deba@1866
   790
//     }
deba@1866
   791
klao@1909
   792
//     Edge direct(const UEdge& edge, bool direction) const {
deba@1866
   793
//       return Parent::getGraph().direct(edge, direction);
deba@1866
   794
//     }
deba@1866
   795
klao@1909
   796
//     Edge direct(const UEdge& edge, const Node& node) const {
deba@1866
   797
//       return Parent::getGraph().direct(edge, node);
deba@1866
   798
//     }
deba@1866
   799
deba@1866
   800
//     Edge oppositeEdge(const Edge& edge) const {
deba@1866
   801
//       return Parent::getGraph().oppositeEdge(edge);
deba@1866
   802
//     }
deba@1866
   803
deba@1866
   804
//     /// \brief Residual capacity map.
deba@1866
   805
//     ///
deba@1866
   806
//     /// In generic residual graphs the residual capacity can be obtained 
deba@1866
   807
//     /// as a map. 
deba@1866
   808
//     class ResCap {
deba@1866
   809
//     protected:
deba@1866
   810
//       const ResGraphAdaptor* res_graph;
deba@1866
   811
//     public:
deba@1866
   812
//       typedef Number Value;
deba@1866
   813
//       typedef Edge Key;
deba@1866
   814
//       ResCap(const ResGraphAdaptor& _res_graph) 
deba@1866
   815
// 	: res_graph(&_res_graph) {}
deba@1866
   816
//       Number operator[](const Edge& e) const {
deba@1866
   817
// 	return res_graph->resCap(e);
deba@1866
   818
//       }
deba@1866
   819
//     };
deba@1866
   820
//   };
deba@1866
   821
deba@1866
   822
}
deba@1866
   823
deba@1866
   824
#endif