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