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