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