lemon/sub_graph.h
author deba
Mon, 27 Feb 2006 10:17:33 +0000
changeset 1985 8782ff6fd98a
parent 1964 df0b07457083
child 1990 15fb7a4ea6be
permissions -rw-r--r--
Bug fix
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@1964
   278
deba@1866
   279
      virtual void add(const std::vector<Node>& nodes) {
deba@1866
   280
	Parent::add(nodes);
deba@1866
   281
	for (int i = 0; i < (int)nodes.size(); ++i) {
deba@1866
   282
	  Parent::operator[](nodes[i]).prev = nodes[i];
deba@1866
   283
	  Parent::operator[](nodes[i]).firstIn = INVALID;
deba@1866
   284
	  Parent::operator[](nodes[i]).firstOut = INVALID;
deba@1866
   285
	}
deba@1866
   286
      } 
deba@1866
   287
deba@1866
   288
      virtual void erase(const Node& node) {
deba@1866
   289
	adaptor.hide(node);
deba@1866
   290
	Parent::erase(node);
deba@1866
   291
      }
deba@1866
   292
deba@1866
   293
      virtual void erase(const std::vector<Node>& nodes) {
deba@1866
   294
	for (int i = 0; i < (int)nodes.size(); ++i) {
deba@1866
   295
	  adaptor.hide(nodes[i]);
deba@1866
   296
	}
deba@1866
   297
	Parent::erase(nodes);
deba@1866
   298
      }
deba@1866
   299
deba@1866
   300
    private:
deba@1866
   301
      SubGraph& adaptor;
deba@1866
   302
    };
deba@1866
   303
deba@1866
   304
    struct EdgeT {
deba@1866
   305
      Edge prevOut, nextOut;
deba@1866
   306
      Edge prevIn, nextIn;
deba@1866
   307
    };
deba@1866
   308
    class EdgesImpl : public Graph::template EdgeMap<EdgeT> {
deba@1866
   309
      friend class SubGraphBase;
deba@1866
   310
    public:
deba@1866
   311
      typedef typename Graph::template EdgeMap<EdgeT> Parent;
deba@1866
   312
deba@1866
   313
      EdgesImpl(SubGraph& _adaptor, const Graph& _graph) 
deba@1866
   314
	: Parent(_graph), adaptor(_adaptor) {}
deba@1866
   315
deba@1866
   316
      virtual ~EdgesImpl() {}
deba@1866
   317
deba@1866
   318
      virtual void build() {
deba@1866
   319
	Parent::build();
deba@1866
   320
	Edge edge;
deba@1866
   321
	adaptor.Base::first(edge);
deba@1866
   322
	while (edge != INVALID) {
deba@1866
   323
	  Parent::operator[](edge).prevOut = edge;
deba@1866
   324
	  adaptor.Base::next(edge);
deba@1866
   325
	}
deba@1866
   326
      }
deba@1866
   327
deba@1866
   328
      virtual void clear() {
deba@1866
   329
	Node node;
deba@1866
   330
	adaptor.first(node);
deba@1866
   331
	while (node != INVALID) {
deba@1866
   332
	  (*adaptor.nodes).firstIn = INVALID;
deba@1866
   333
	  (*adaptor.nodes).firstOut = INVALID;
deba@1866
   334
	  adaptor.next(node);
deba@1866
   335
	}
deba@1866
   336
	Parent::clear();
deba@1866
   337
      }
deba@1866
   338
deba@1866
   339
      virtual void add(const Edge& edge) {
deba@1866
   340
	Parent::add(edge);
deba@1866
   341
	Parent::operator[](edge).prevOut = edge;
deba@1866
   342
      }
deba@1866
   343
deba@1866
   344
      virtual void add(const std::vector<Edge>& edges) {
deba@1866
   345
	Parent::add(edges);
deba@1866
   346
	for (int i = 0; i < (int)edges.size(); ++i) {
deba@1866
   347
	  Parent::operator[](edges[i]).prevOut = edges[i];
deba@1866
   348
	}
deba@1866
   349
      }
deba@1866
   350
deba@1866
   351
      virtual void erase(const Edge& edge) {
deba@1866
   352
	adaptor.hide(edge);
deba@1866
   353
	Parent::erase(edge);
deba@1866
   354
      }
deba@1866
   355
deba@1866
   356
      virtual void erase(const std::vector<Edge>& edges) {
deba@1866
   357
	for (int i = 0; i < (int)edges.size(); ++i) {
deba@1866
   358
	  adaptor.hide(edges[i]);
deba@1866
   359
	}
deba@1964
   360
	Parent::erase(edges);
deba@1866
   361
      }
deba@1866
   362
deba@1866
   363
    private:
deba@1866
   364
      SubGraph& adaptor;
deba@1866
   365
    };
deba@1866
   366
deba@1866
   367
    NodesImpl* nodes;
deba@1866
   368
    EdgesImpl* edges;
deba@1866
   369
    Node firstNode;
deba@1866
   370
  };
deba@1866
   371
deba@1866
   372
  /// \ingroup semi_adaptors
deba@1866
   373
  ///
deba@1866
   374
  /// \brief Graph which uses a subset of an other graph's nodes and edges.
deba@1866
   375
  ///
deba@1866
   376
  /// Graph which uses a subset of an other graph's nodes and edges. This class
deba@1866
   377
  /// is an alternative to the SubGraphAdaptor which is created for the
deba@1866
   378
  /// same reason. The main difference between the two class that it
deba@1866
   379
  /// makes linked lists on the unhidden nodes and edges what cause that
deba@1866
   380
  /// on sparse subgraphs the algorithms can be more efficient and some times
deba@1866
   381
  /// provide better time complexity. On other way this implemetation is
deba@1866
   382
  /// less efficient in most case when the subgraph filters out only
deba@1866
   383
  /// a few nodes or edges.
deba@1866
   384
  /// 
deba@1866
   385
  /// \see SubGraphAdaptor
deba@1866
   386
  /// \see EdgeSubGraphBase
deba@1866
   387
  template <typename Graph>
deba@1866
   388
  class SubGraph 
deba@1979
   389
    : public GraphAdaptorExtender< SubGraphBase<Graph> > {
deba@1866
   390
  public:
deba@1979
   391
    typedef GraphAdaptorExtender< SubGraphBase<Graph> > Parent;
deba@1866
   392
  public:
deba@1866
   393
    /// \brief Constructor for sub-graph.
deba@1866
   394
    ///
deba@1866
   395
    /// Constructor for sub-graph. Initially all the edges and nodes
deba@1866
   396
    /// are hidden in the graph.
deba@1866
   397
    SubGraph(const Graph& _graph) 
deba@1866
   398
      : Parent(), nodes(*this, _graph), edges(*this, _graph) { 
deba@1866
   399
      Parent::construct(_graph, nodes, edges);
deba@1866
   400
    }
deba@1866
   401
  private:
deba@1866
   402
    typename Parent::NodesImpl nodes;
deba@1866
   403
    typename Parent::EdgesImpl edges;
deba@1866
   404
  };
deba@1866
   405
deba@1866
   406
  /// \brief Base for the EdgeSubGraph.
deba@1866
   407
  ///
deba@1866
   408
  /// Base for the EdgeSubGraph.
deba@1866
   409
  template <typename _Graph>
deba@1866
   410
  class EdgeSubGraphBase : public GraphAdaptorBase<const _Graph> {
deba@1866
   411
  public:
deba@1866
   412
    typedef _Graph Graph;
deba@1866
   413
    typedef EdgeSubGraphBase<_Graph> SubGraph;
deba@1866
   414
    typedef GraphAdaptorBase<const _Graph> Parent;
deba@1866
   415
    typedef Parent Base;
deba@1866
   416
deba@1866
   417
    typedef typename Parent::Node Node;
deba@1866
   418
    typedef typename Parent::Edge Edge;
deba@1866
   419
deba@1866
   420
deba@1866
   421
  protected:
deba@1866
   422
deba@1866
   423
    class NodesImpl;
deba@1866
   424
    class EdgesImpl;
deba@1866
   425
deba@1866
   426
    EdgeSubGraphBase() {}
deba@1866
   427
deba@1866
   428
    void construct(const Graph& _graph, NodesImpl& _nodes, EdgesImpl& _edges) {
deba@1866
   429
      Parent::setGraph(_graph);
deba@1866
   430
      nodes = &_nodes;
deba@1866
   431
      edges = &_edges;
deba@1866
   432
deba@1866
   433
      Node node;
deba@1866
   434
      Parent::first(node);
deba@1866
   435
      while (node != INVALID) {
deba@1866
   436
	(*nodes)[node].firstIn = INVALID;
deba@1866
   437
	(*nodes)[node].firstOut = INVALID;
deba@1866
   438
	Parent::next(node);
deba@1866
   439
      }
deba@1866
   440
deba@1866
   441
      Edge edge;
deba@1866
   442
      Parent::first(edge);
deba@1866
   443
      while (edge != INVALID) {
deba@1866
   444
	(*edges)[edge].prevOut = edge;
deba@1866
   445
	Parent::next(edge);
deba@1866
   446
      }
deba@1866
   447
    }
deba@1866
   448
deba@1866
   449
  public:
deba@1866
   450
deba@1866
   451
    void first(Node& node) const {
deba@1866
   452
      Parent::first(node);
deba@1866
   453
    }
deba@1866
   454
    void next(Node& node) const {
deba@1866
   455
      Parent::next(node);
deba@1866
   456
    }
deba@1866
   457
deba@1866
   458
    void first(Edge& edge) const {
deba@1866
   459
      Node node;
deba@1866
   460
      Parent::first(node);
deba@1866
   461
      while (node != INVALID && (*nodes)[node].firstOut == INVALID) {
deba@1866
   462
	Parent::next(node);
deba@1866
   463
      }
deba@1866
   464
      if (node == INVALID) {
deba@1866
   465
	edge = INVALID;
deba@1866
   466
      } else {
deba@1866
   467
	edge = (*nodes)[node].firstOut;
deba@1866
   468
      }
deba@1866
   469
    }
deba@1866
   470
    void next(Edge& edge) const {
deba@1866
   471
      if ((*edges)[edge].nextOut != INVALID) {
deba@1866
   472
	edge = (*edges)[edge].nextOut;
deba@1866
   473
      } else {
deba@1866
   474
	Node node = source(edge);
deba@1866
   475
	Parent::next(node);
deba@1866
   476
	while (node != INVALID && (*nodes)[node].firstOut == INVALID) {
deba@1866
   477
	  Parent::next(node);
deba@1866
   478
	}
deba@1866
   479
	if (node == INVALID) {
deba@1866
   480
	  edge = INVALID;
deba@1866
   481
	} else {
deba@1866
   482
	  edge = (*nodes)[node].firstOut;
deba@1866
   483
	}
deba@1866
   484
      }
deba@1866
   485
    }
deba@1866
   486
deba@1866
   487
    void firstOut(Edge& edge, const Node& node) const {
deba@1866
   488
      edge = (*nodes)[node].firstOut;
deba@1866
   489
    }
deba@1866
   490
    void nextOut(Edge& edge) const {
deba@1866
   491
      edge = (*edges)[edge].nextOut;
deba@1866
   492
    }
deba@1866
   493
deba@1866
   494
    void firstIn(Edge& edge, const Node& node) const {
deba@1866
   495
      edge = (*nodes)[node].firstIn;
deba@1866
   496
    }
deba@1866
   497
    void nextIn(Edge& edge) const {
deba@1866
   498
      edge = (*edges)[edge].nextIn;
deba@1866
   499
    }
deba@1866
   500
deba@1866
   501
    /// \brief Returns true when the given edge is hidden.
deba@1866
   502
    ///
deba@1866
   503
    /// Returns true when the given edge is hidden.
deba@1866
   504
    bool hidden(const Edge& edge) const {
deba@1866
   505
      return (*edges)[edge].prevOut == edge;
deba@1866
   506
    }
deba@1866
   507
deba@1866
   508
    /// \brief Hide the given edge in the sub-graph.
deba@1866
   509
    ///
deba@1866
   510
    /// Hide the given edge in the sub graph. It just lace out from
deba@1866
   511
    /// the linked lists the given edge.
deba@1866
   512
    void hide(const Edge& edge) {
deba@1866
   513
      if (hidden(edge)) return;
deba@1866
   514
      if ((*edges)[edge].prevOut != INVALID) {
deba@1866
   515
	(*edges)[(*edges)[edge].prevOut].nextOut = (*edges)[edge].nextOut;
deba@1866
   516
      } else {
deba@1866
   517
	(*nodes)[source(edge)].firstOut = (*edges)[edge].nextOut;
deba@1866
   518
      }
deba@1866
   519
      if ((*edges)[edge].nextOut != INVALID) {
deba@1866
   520
	(*edges)[(*edges)[edge].nextOut].prevOut = (*edges)[edge].prevOut;
deba@1866
   521
      }
deba@1866
   522
deba@1866
   523
      if ((*edges)[edge].prevIn != INVALID) {
deba@1866
   524
	(*edges)[(*edges)[edge].prevIn].nextIn = (*edges)[edge].nextIn;
deba@1866
   525
      } else {
deba@1866
   526
	(*nodes)[target(edge)].firstIn = (*edges)[edge].nextIn;
deba@1866
   527
      }
deba@1866
   528
      if ((*edges)[edge].nextIn != INVALID) {
deba@1866
   529
	(*edges)[(*edges)[edge].nextIn].prevIn = (*edges)[edge].prevIn;
deba@1866
   530
      }
deba@1866
   531
      (*edges)[edge].prevOut = edge;
deba@1866
   532
    }
deba@1866
   533
deba@1866
   534
    /// \brief Unhide the given edge in the sub-graph.
deba@1866
   535
    ///
deba@1866
   536
    /// Unhide the given edge in the sub graph. It just lace in the given
deba@1866
   537
    /// edge into the linked lists.
deba@1866
   538
    void unHide(const Edge& edge) {
deba@1866
   539
      if (!hidden(edge)) return;
deba@1866
   540
      Node node;
deba@1866
   541
deba@1866
   542
      node = Parent::source(edge);
deba@1866
   543
      (*edges)[edge].nextOut = (*nodes)[node].firstOut;
deba@1866
   544
      (*edges)[edge].prevOut = INVALID;
deba@1866
   545
      if ((*edges)[edge].nextOut != INVALID) {
deba@1866
   546
	(*edges)[(*edges)[edge].nextOut].prevOut = edge;
deba@1866
   547
      }
deba@1866
   548
      (*nodes)[node].firstOut = edge;
deba@1866
   549
deba@1866
   550
      node = Parent::target(edge);
deba@1866
   551
      (*edges)[edge].nextIn = (*nodes)[node].firstIn;
deba@1866
   552
      (*edges)[edge].prevIn = INVALID;
deba@1866
   553
      if ((*edges)[edge].nextIn != INVALID) {
deba@1866
   554
	(*edges)[(*edges)[edge].nextIn].prevIn = edge;
deba@1866
   555
      }
deba@1866
   556
      (*nodes)[node].firstIn = edge;      
deba@1866
   557
    }
deba@1866
   558
    
deba@1866
   559
  protected:
deba@1866
   560
    struct NodeT {
deba@1866
   561
      Edge firstIn, firstOut;
deba@1866
   562
    };
deba@1866
   563
    class NodesImpl : public Graph::template NodeMap<NodeT> {
deba@1866
   564
      friend class EdgeSubGraphBase;
deba@1866
   565
    public:
deba@1866
   566
      typedef typename Graph::template NodeMap<NodeT> Parent;
deba@1866
   567
deba@1866
   568
      NodesImpl(SubGraph& _adaptor, const Graph& _graph) 
deba@1866
   569
	: Parent(_graph), adaptor(_adaptor) {}
deba@1866
   570
deba@1866
   571
      virtual ~NodesImpl() {}
deba@1866
   572
deba@1866
   573
      virtual void build() {
deba@1866
   574
	Parent::build();
deba@1866
   575
	Node node;
deba@1866
   576
	adaptor.Base::first(node);
deba@1866
   577
	while (node != INVALID) {
deba@1866
   578
	  Parent::operator[](node).firstIn = INVALID;
deba@1866
   579
	  Parent::operator[](node).firstOut = INVALID;
deba@1866
   580
	  adaptor.Base::next(node);
deba@1866
   581
	}
deba@1866
   582
      }
deba@1866
   583
deba@1866
   584
      virtual void add(const Node& node) {
deba@1866
   585
	Parent::add(node);
deba@1866
   586
	Parent::operator[](node).firstIn = INVALID;
deba@1866
   587
	Parent::operator[](node).firstOut = INVALID;
deba@1866
   588
      }
deba@1866
   589
deba@1964
   590
      virtual void add(const std::vector<Node>& nodes) {
deba@1964
   591
        Parent::add(nodes);
deba@1964
   592
        for (int i = 0; i < (int)nodes.size(); ++i) {
deba@1964
   593
          Parent::operator[](nodes[i]).firstIn = INVALID;
deba@1964
   594
          Parent::operator[](nodes[i]).firstOut = INVALID;
deba@1964
   595
        }
deba@1964
   596
      }
deba@1964
   597
deba@1866
   598
    private:
deba@1866
   599
      SubGraph& adaptor;
deba@1866
   600
    };
deba@1866
   601
deba@1866
   602
    struct EdgeT {
deba@1866
   603
      Edge prevOut, nextOut;
deba@1866
   604
      Edge prevIn, nextIn;
deba@1866
   605
    };
deba@1866
   606
    class EdgesImpl : public Graph::template EdgeMap<EdgeT> {
deba@1866
   607
      friend class EdgeSubGraphBase;
deba@1866
   608
    public:
deba@1866
   609
      typedef typename Graph::template EdgeMap<EdgeT> Parent;
deba@1866
   610
deba@1866
   611
      EdgesImpl(SubGraph& _adaptor, const Graph& _graph) 
deba@1866
   612
	: Parent(_graph), adaptor(_adaptor) {}
deba@1866
   613
deba@1866
   614
      virtual ~EdgesImpl() {}
deba@1866
   615
deba@1866
   616
      virtual void build() {
deba@1866
   617
	Parent::build();
deba@1866
   618
	Edge edge;
deba@1866
   619
	adaptor.Base::first(edge);
deba@1866
   620
	while (edge != INVALID) {
deba@1866
   621
	  Parent::operator[](edge).prevOut = edge;
deba@1866
   622
	  adaptor.Base::next(edge);
deba@1866
   623
	}
deba@1866
   624
      }
deba@1866
   625
deba@1866
   626
      virtual void clear() {
deba@1866
   627
	Node node;
deba@1866
   628
	adaptor.Base::first(node);
deba@1866
   629
	while (node != INVALID) {
deba@1866
   630
	  (*adaptor.nodes)[node].firstIn = INVALID;
deba@1866
   631
	  (*adaptor.nodes)[node].firstOut = INVALID;
deba@1866
   632
	  adaptor.Base::next(node);
deba@1866
   633
	}
deba@1866
   634
	Parent::clear();
deba@1866
   635
      }
deba@1866
   636
deba@1866
   637
      virtual void add(const Edge& edge) {
deba@1866
   638
	Parent::add(edge);
deba@1866
   639
	Parent::operator[](edge).prevOut = edge;
deba@1866
   640
      }
deba@1866
   641
deba@1866
   642
      virtual void add(const std::vector<Edge>& edges) {
deba@1866
   643
	Parent::add(edges);
deba@1866
   644
	for (int i = 0; i < (int)edges.size(); ++i) {
deba@1866
   645
	  Parent::operator[](edges[i]).prevOut = edges[i];
deba@1866
   646
	}
deba@1866
   647
      }
deba@1866
   648
deba@1866
   649
      virtual void erase(const Edge& edge) {
deba@1866
   650
	adaptor.hide(edge);
deba@1866
   651
	Parent::erase(edge);
deba@1866
   652
      }
deba@1866
   653
deba@1866
   654
      virtual void erase(const std::vector<Edge>& edges) {
deba@1866
   655
	for (int i = 0; i < (int)edges.size(); ++i) {
deba@1866
   656
	  adaptor.hide(edges[i]);
deba@1866
   657
	}
deba@1964
   658
	Parent::erase(edges);
deba@1866
   659
      }
deba@1866
   660
deba@1866
   661
    private:
deba@1866
   662
      SubGraph& adaptor;
deba@1866
   663
    };
deba@1866
   664
deba@1866
   665
    NodesImpl* nodes;
deba@1866
   666
    EdgesImpl* edges;
deba@1866
   667
  };
deba@1866
   668
deba@1866
   669
  /// \ingroup semi_adaptors
deba@1866
   670
  ///
deba@1866
   671
  /// \brief Graph which uses a subset of an other graph's edges.
deba@1866
   672
  ///
deba@1866
   673
  /// Graph which uses a subset of an other graph's edges. This class
deba@1866
   674
  /// is an alternative to the EdgeSubGraphAdaptor which is created for the
deba@1866
   675
  /// same reason. The main difference between the two class that it
deba@1866
   676
  /// makes linked lists on the unhidden edges what cause that
deba@1866
   677
  /// on sparse subgraphs the algorithms can be more efficient and some times
deba@1866
   678
  /// provide better time complexity. On other way this implemetation is
deba@1866
   679
  /// less efficient in most case when the subgraph filters out only
deba@1866
   680
  /// a few edges.
deba@1866
   681
  /// 
deba@1866
   682
  /// \see EdgeSubGraphAdaptor
deba@1866
   683
  /// \see EdgeSubGraphBase
deba@1866
   684
  template <typename Graph>
deba@1866
   685
  class EdgeSubGraph 
deba@1979
   686
    : public GraphAdaptorExtender< EdgeSubGraphBase<Graph> > {
deba@1866
   687
  public:
deba@1979
   688
    typedef GraphAdaptorExtender< EdgeSubGraphBase<Graph> > Parent;
deba@1866
   689
  public:
deba@1866
   690
    /// \brief Constructor for sub-graph.
deba@1866
   691
    ///
deba@1866
   692
    /// Constructor for sub-graph. Initially all the edges are hidden in the 
deba@1866
   693
    /// graph.
deba@1866
   694
    EdgeSubGraph(const Graph& _graph) 
deba@1866
   695
      : Parent(), nodes(*this, _graph), edges(*this, _graph) { 
deba@1866
   696
      Parent::construct(_graph, nodes, edges);
deba@1866
   697
    }
deba@1866
   698
  private:
deba@1866
   699
    typename Parent::NodesImpl nodes;
deba@1866
   700
    typename Parent::EdgesImpl edges;
deba@1866
   701
  };
deba@1866
   702
deba@1866
   703
deba@1866
   704
//   template<typename Graph, typename Number, 
deba@1866
   705
// 	   typename CapacityMap, typename FlowMap>
deba@1866
   706
//   class ResGraph
deba@1866
   707
//     : public IterableGraphExtender<EdgeSubGraphBase<
klao@1909
   708
//     UGraphAdaptor<Graph> > > {
deba@1866
   709
//   public:
deba@1866
   710
//     typedef IterableGraphExtender<EdgeSubGraphBase<
klao@1909
   711
//       UGraphAdaptor<Graph> > > Parent;
deba@1866
   712
deba@1866
   713
//   protected:
klao@1909
   714
//     UGraphAdaptor<Graph> u;
deba@1866
   715
deba@1866
   716
//     const CapacityMap* capacity;
deba@1866
   717
//     FlowMap* flow;
deba@1866
   718
deba@1866
   719
//     typename Parent::NodesImpl nodes;
deba@1866
   720
//     typename Parent::EdgesImpl edges;
deba@1866
   721
deba@1866
   722
//     void setCapacityMap(const CapacityMap& _capacity) {
deba@1866
   723
//       capacity=&_capacity;
deba@1866
   724
//     }
deba@1866
   725
deba@1866
   726
//     void setFlowMap(FlowMap& _flow) {
deba@1866
   727
//       flow=&_flow;
deba@1866
   728
//     }
deba@1866
   729
deba@1866
   730
//   public:
deba@1866
   731
klao@1909
   732
//     typedef typename UGraphAdaptor<Graph>::Node Node;
klao@1909
   733
//     typedef typename UGraphAdaptor<Graph>::Edge Edge;
klao@1909
   734
//     typedef typename UGraphAdaptor<Graph>::UEdge UEdge;
deba@1866
   735
deba@1866
   736
//     ResGraphAdaptor(Graph& _graph, 
deba@1866
   737
// 		    const CapacityMap& _capacity, FlowMap& _flow) 
klao@1909
   738
//       : Parent(), u(_graph), capacity(&_capacity), flow(&_flow),
deba@1866
   739
// 	nodes(*this, _graph), edges(*this, _graph) {
klao@1909
   740
//       Parent::construct(u, nodes, edges);
deba@1866
   741
//       setFlowMap(_flow);
deba@1866
   742
//       setCapacityMap(_capacity);
deba@1866
   743
//       typename Graph::Edge edge; 
deba@1866
   744
//       for (_graph.first(edge); edge != INVALID; _graph.next(edge)) {
deba@1866
   745
// 	if ((*flow)[edge] != (*capacity)[edge]) {
deba@1866
   746
// 	  Parent::unHide(direct(edge, true));
deba@1866
   747
// 	}
deba@1866
   748
// 	if ((*flow)[edge] != 0) {
deba@1866
   749
// 	  Parent::unHide(direct(edge, false));
deba@1866
   750
// 	}
deba@1866
   751
//       }
deba@1866
   752
//     }
deba@1866
   753
deba@1866
   754
//     void augment(const Edge& e, Number a) {
deba@1866
   755
//       if (direction(e)) {
deba@1866
   756
// 	flow->set(e, (*flow)[e]+a);
deba@1866
   757
//       } else { 
deba@1866
   758
// 	flow->set(e, (*flow)[e]-a);
deba@1866
   759
//       }
deba@1866
   760
//       if ((*flow)[e] == (*capacity)[e]) {
deba@1866
   761
// 	Parent::hide(e);
deba@1866
   762
//       } else {
deba@1866
   763
// 	Parent::unHide(e);
deba@1866
   764
//       }
deba@1866
   765
//       if ((*flow)[e] == 0) {
deba@1866
   766
// 	Parent::hide(oppositeEdge(e));
deba@1866
   767
//       } else {
deba@1866
   768
// 	Parent::unHide(oppositeEdge(e));
deba@1866
   769
//       }
deba@1866
   770
//     }
deba@1866
   771
deba@1866
   772
//     Number resCap(const Edge& e) {
deba@1866
   773
//       if (direction(e)) { 
deba@1866
   774
// 	return (*capacity)[e]-(*flow)[e]; 
deba@1866
   775
//       } else { 
deba@1866
   776
// 	return (*flow)[e];
deba@1866
   777
//       }
deba@1866
   778
//     }
deba@1866
   779
    
deba@1866
   780
//     bool direction(const Edge& edge) const {
deba@1866
   781
//       return Parent::getGraph().direction(edge);
deba@1866
   782
//     }
deba@1866
   783
klao@1909
   784
//     Edge direct(const UEdge& edge, bool direction) const {
deba@1866
   785
//       return Parent::getGraph().direct(edge, direction);
deba@1866
   786
//     }
deba@1866
   787
klao@1909
   788
//     Edge direct(const UEdge& edge, const Node& node) const {
deba@1866
   789
//       return Parent::getGraph().direct(edge, node);
deba@1866
   790
//     }
deba@1866
   791
deba@1866
   792
//     Edge oppositeEdge(const Edge& edge) const {
deba@1866
   793
//       return Parent::getGraph().oppositeEdge(edge);
deba@1866
   794
//     }
deba@1866
   795
deba@1866
   796
//     /// \brief Residual capacity map.
deba@1866
   797
//     ///
deba@1866
   798
//     /// In generic residual graphs the residual capacity can be obtained 
deba@1866
   799
//     /// as a map. 
deba@1866
   800
//     class ResCap {
deba@1866
   801
//     protected:
deba@1866
   802
//       const ResGraphAdaptor* res_graph;
deba@1866
   803
//     public:
deba@1866
   804
//       typedef Number Value;
deba@1866
   805
//       typedef Edge Key;
deba@1866
   806
//       ResCap(const ResGraphAdaptor& _res_graph) 
deba@1866
   807
// 	: res_graph(&_res_graph) {}
deba@1866
   808
//       Number operator[](const Edge& e) const {
deba@1866
   809
// 	return res_graph->resCap(e);
deba@1866
   810
//       }
deba@1866
   811
//     };
deba@1866
   812
//   };
deba@1866
   813
deba@1866
   814
}
deba@1866
   815
deba@1866
   816
#endif