lemon/list_graph.h
author alpar
Mon, 13 Nov 2006 18:46:19 +0000
changeset 2301 eb378706bd3d
parent 2260 4274224f8a7d
child 2302 d3c664c975ee
permissions -rw-r--r--
Test the automatic compilation checker 1/2: make a bug
alpar@948
     1
/* -*- C++ -*-
alpar@948
     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
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@948
     8
 *
alpar@948
     9
 * Permission to use, modify and distribute this software is granted
alpar@948
    10
 * provided that this copyright notice appears in all copies. For
alpar@948
    11
 * precise terms see the accompanying LICENSE file.
alpar@948
    12
 *
alpar@948
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@948
    14
 * express or implied, and with no claim as to its suitability for any
alpar@948
    15
 * purpose.
alpar@948
    16
 *
alpar@948
    17
 */
alpar@395
    18
alpar@921
    19
#ifndef LEMON_LIST_GRAPH_H
alpar@921
    20
#define LEMON_LIST_GRAPH_H
alpar@395
    21
alpar@948
    22
///\ingroup graphs
alpar@948
    23
///\file
deba@2116
    24
///\brief ListGraph, ListUGraph classes.
alpar@948
    25
deba@2116
    26
#include <lemon/bits/base_extender.h>
deba@1791
    27
#include <lemon/bits/graph_extender.h>
deba@782
    28
deba@2116
    29
#include <lemon/error.h>
deba@2116
    30
deba@1979
    31
#include <vector>
alpar@1011
    32
#include <list>
deba@782
    33
alpar@2301
    34
BAD CODE!!!!
alpar@2301
    35
alpar@921
    36
namespace lemon {
alpar@395
    37
klao@946
    38
  class ListGraphBase {
alpar@406
    39
alpar@949
    40
  protected:
klao@946
    41
    struct NodeT {
deba@1470
    42
      int first_in, first_out;
alpar@397
    43
      int prev, next;
alpar@395
    44
    };
klao@946
    45
 
klao@946
    46
    struct EdgeT {
alpar@986
    47
      int target, source;
alpar@397
    48
      int prev_in, prev_out;
alpar@397
    49
      int next_in, next_out;
alpar@395
    50
    };
alpar@395
    51
alpar@395
    52
    std::vector<NodeT> nodes;
klao@946
    53
alpar@397
    54
    int first_node;
klao@946
    55
alpar@397
    56
    int first_free_node;
klao@946
    57
alpar@395
    58
    std::vector<EdgeT> edges;
klao@946
    59
alpar@397
    60
    int first_free_edge;
alpar@395
    61
    
deba@782
    62
  public:
alpar@395
    63
    
klao@946
    64
    typedef ListGraphBase Graph;
alpar@397
    65
    
klao@946
    66
    class Node {
marci@975
    67
      friend class ListGraphBase;
klao@946
    68
    protected:
alpar@395
    69
klao@946
    70
      int id;
deba@2031
    71
      explicit Node(int pid) { id = pid;}
alpar@395
    72
klao@946
    73
    public:
klao@946
    74
      Node() {}
klao@946
    75
      Node (Invalid) { id = -1; }
klao@946
    76
      bool operator==(const Node& node) const {return id == node.id;}
klao@946
    77
      bool operator!=(const Node& node) const {return id != node.id;}
klao@946
    78
      bool operator<(const Node& node) const {return id < node.id;}
klao@946
    79
    };
deba@782
    80
klao@946
    81
    class Edge {
marci@975
    82
      friend class ListGraphBase;
klao@946
    83
    protected:
deba@782
    84
klao@946
    85
      int id;
deba@2031
    86
      explicit Edge(int pid) { id = pid;}
alpar@395
    87
klao@946
    88
    public:
klao@946
    89
      Edge() {}
klao@946
    90
      Edge (Invalid) { id = -1; }
klao@946
    91
      bool operator==(const Edge& edge) const {return id == edge.id;}
klao@946
    92
      bool operator!=(const Edge& edge) const {return id != edge.id;}
klao@946
    93
      bool operator<(const Edge& edge) const {return id < edge.id;}
klao@946
    94
    };
klao@946
    95
klao@946
    96
klao@946
    97
klao@946
    98
    ListGraphBase()
deba@782
    99
      : nodes(), first_node(-1),
deba@782
   100
	first_free_node(-1), edges(), first_free_edge(-1) {}
deba@782
   101
alpar@395
   102
    
alpar@813
   103
    /// Maximum node ID.
alpar@813
   104
    
alpar@813
   105
    /// Maximum node ID.
alpar@813
   106
    ///\sa id(Node)
deba@1791
   107
    int maxNodeId() const { return nodes.size()-1; } 
klao@946
   108
alpar@813
   109
    /// Maximum edge ID.
alpar@813
   110
    
alpar@813
   111
    /// Maximum edge ID.
alpar@813
   112
    ///\sa id(Edge)
deba@1791
   113
    int maxEdgeId() const { return edges.size()-1; }
alpar@395
   114
deba@2031
   115
    Node source(Edge e) const { return Node(edges[e.id].source); }
deba@2031
   116
    Node target(Edge e) const { return Node(edges[e.id].target); }
alpar@395
   117
alpar@395
   118
klao@946
   119
    void first(Node& node) const { 
klao@946
   120
      node.id = first_node;
klao@946
   121
    }
klao@946
   122
klao@946
   123
    void next(Node& node) const {
klao@946
   124
      node.id = nodes[node.id].next;
klao@946
   125
    }
klao@946
   126
klao@946
   127
klao@946
   128
    void first(Edge& e) const { 
klao@946
   129
      int n;
klao@946
   130
      for(n = first_node; 
klao@946
   131
	  n!=-1 && nodes[n].first_in == -1; 
klao@946
   132
	  n = nodes[n].next);
klao@946
   133
      e.id = (n == -1) ? -1 : nodes[n].first_in;
klao@946
   134
    }
klao@946
   135
klao@946
   136
    void next(Edge& edge) const {
klao@946
   137
      if (edges[edge.id].next_in != -1) {
klao@946
   138
	edge.id = edges[edge.id].next_in;
klao@946
   139
      } else {
klao@946
   140
	int n;
alpar@986
   141
	for(n = nodes[edges[edge.id].target].next;
klao@946
   142
	  n!=-1 && nodes[n].first_in == -1; 
klao@946
   143
	  n = nodes[n].next);
klao@946
   144
	edge.id = (n == -1) ? -1 : nodes[n].first_in;
klao@946
   145
      }      
klao@946
   146
    }
klao@946
   147
klao@946
   148
    void firstOut(Edge &e, const Node& v) const {
klao@946
   149
      e.id = nodes[v.id].first_out;
klao@946
   150
    }
klao@946
   151
    void nextOut(Edge &e) const {
klao@946
   152
      e.id=edges[e.id].next_out;
klao@946
   153
    }
klao@946
   154
klao@946
   155
    void firstIn(Edge &e, const Node& v) const {
klao@946
   156
      e.id = nodes[v.id].first_in;
klao@946
   157
    }
klao@946
   158
    void nextIn(Edge &e) const {
klao@946
   159
      e.id=edges[e.id].next_in;
klao@946
   160
    }
klao@946
   161
alpar@813
   162
    
klao@946
   163
    static int id(Node v) { return v.id; }
klao@946
   164
    static int id(Edge e) { return e.id; }
alpar@395
   165
deba@1791
   166
    static Node nodeFromId(int id) { return Node(id);}
deba@1791
   167
    static Edge edgeFromId(int id) { return Edge(id);}
deba@1106
   168
alpar@397
   169
    /// Adds a new node to the graph.
alpar@397
   170
alpar@2123
   171
    /// Adds a new node to the graph.
alpar@2123
   172
    ///
alpar@813
   173
    /// \warning It adds the new node to the front of the list.
alpar@397
   174
    /// (i.e. the lastly added node becomes the first.)
klao@946
   175
    Node addNode() {     
alpar@397
   176
      int n;
alpar@397
   177
      
klao@946
   178
      if(first_free_node==-1) {
klao@946
   179
	n = nodes.size();
klao@946
   180
	nodes.push_back(NodeT());
klao@946
   181
      } else {
alpar@397
   182
	n = first_free_node;
alpar@397
   183
	first_free_node = nodes[n].next;
alpar@397
   184
      }
alpar@397
   185
      
alpar@397
   186
      nodes[n].next = first_node;
alpar@397
   187
      if(first_node != -1) nodes[first_node].prev = n;
alpar@397
   188
      first_node = n;
alpar@397
   189
      nodes[n].prev = -1;
alpar@397
   190
      
alpar@397
   191
      nodes[n].first_in = nodes[n].first_out = -1;
alpar@397
   192
      
klao@946
   193
      return Node(n);
alpar@395
   194
    }
alpar@395
   195
    
alpar@395
   196
    Edge addEdge(Node u, Node v) {
klao@946
   197
      int n;      
klao@946
   198
klao@946
   199
      if (first_free_edge == -1) {
klao@946
   200
	n = edges.size();
klao@946
   201
	edges.push_back(EdgeT());
klao@946
   202
      } else {
alpar@397
   203
	n = first_free_edge;
alpar@397
   204
	first_free_edge = edges[n].next_in;
alpar@397
   205
      }
alpar@397
   206
      
alpar@986
   207
      edges[n].source = u.id; 
alpar@986
   208
      edges[n].target = v.id;
alpar@395
   209
klao@946
   210
      edges[n].next_out = nodes[u.id].first_out;
klao@946
   211
      if(nodes[u.id].first_out != -1) {
klao@946
   212
	edges[nodes[u.id].first_out].prev_out = n;
klao@946
   213
      }
klao@946
   214
      
klao@946
   215
      edges[n].next_in = nodes[v.id].first_in;
klao@946
   216
      if(nodes[v.id].first_in != -1) {
klao@946
   217
	edges[nodes[v.id].first_in].prev_in = n;
klao@946
   218
      }
klao@946
   219
      
alpar@397
   220
      edges[n].prev_in = edges[n].prev_out = -1;
alpar@397
   221
	
klao@946
   222
      nodes[u.id].first_out = nodes[v.id].first_in = n;
alpar@397
   223
klao@946
   224
      return Edge(n);
alpar@395
   225
    }
alpar@774
   226
    
klao@946
   227
    void erase(const Node& node) {
klao@946
   228
      int n = node.id;
klao@946
   229
      
klao@946
   230
      if(nodes[n].next != -1) {
klao@946
   231
	nodes[nodes[n].next].prev = nodes[n].prev;
klao@946
   232
      }
klao@946
   233
      
klao@946
   234
      if(nodes[n].prev != -1) {
klao@946
   235
	nodes[nodes[n].prev].next = nodes[n].next;
klao@946
   236
      } else {
klao@946
   237
	first_node = nodes[n].next;
klao@946
   238
      }
klao@946
   239
      
klao@946
   240
      nodes[n].next = first_free_node;
klao@946
   241
      first_free_node = n;
alpar@395
   242
alpar@774
   243
    }
alpar@774
   244
    
klao@946
   245
    void erase(const Edge& edge) {
klao@946
   246
      int n = edge.id;
alpar@397
   247
      
klao@946
   248
      if(edges[n].next_in!=-1) {
alpar@397
   249
	edges[edges[n].next_in].prev_in = edges[n].prev_in;
klao@946
   250
      }
klao@946
   251
klao@946
   252
      if(edges[n].prev_in!=-1) {
alpar@397
   253
	edges[edges[n].prev_in].next_in = edges[n].next_in;
klao@946
   254
      } else {
alpar@986
   255
	nodes[edges[n].target].first_in = edges[n].next_in;
klao@946
   256
      }
klao@946
   257
alpar@397
   258
      
klao@946
   259
      if(edges[n].next_out!=-1) {
alpar@397
   260
	edges[edges[n].next_out].prev_out = edges[n].prev_out;
klao@946
   261
      } 
klao@946
   262
klao@946
   263
      if(edges[n].prev_out!=-1) {
alpar@397
   264
	edges[edges[n].prev_out].next_out = edges[n].next_out;
klao@946
   265
      } else {
alpar@986
   266
	nodes[edges[n].source].first_out = edges[n].next_out;
klao@946
   267
      }
alpar@397
   268
      
alpar@397
   269
      edges[n].next_in = first_free_edge;
alpar@695
   270
      first_free_edge = n;      
alpar@397
   271
alpar@397
   272
    }
alpar@397
   273
alpar@397
   274
    void clear() {
deba@782
   275
      edges.clear();
deba@782
   276
      nodes.clear();
klao@946
   277
      first_node = first_free_node = first_free_edge = -1;
deba@937
   278
    }
deba@937
   279
alpar@949
   280
  protected:
deba@2111
   281
    void changeTarget(Edge e, Node n) 
alpar@949
   282
    {
alpar@949
   283
      if(edges[e.id].next_in != -1)
alpar@949
   284
	edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
alpar@949
   285
      if(edges[e.id].prev_in != -1)
alpar@949
   286
	edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
alpar@986
   287
      else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
deba@1702
   288
      if (nodes[n.id].first_in != -1) {
deba@1702
   289
	edges[nodes[n.id].first_in].prev_in = e.id;
deba@1702
   290
      }
alpar@986
   291
      edges[e.id].target = n.id;
alpar@949
   292
      edges[e.id].prev_in = -1;
alpar@949
   293
      edges[e.id].next_in = nodes[n.id].first_in;
alpar@949
   294
      nodes[n.id].first_in = e.id;
alpar@949
   295
    }
deba@2111
   296
    void changeSource(Edge e, Node n) 
alpar@949
   297
    {
alpar@949
   298
      if(edges[e.id].next_out != -1)
alpar@949
   299
	edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
alpar@949
   300
      if(edges[e.id].prev_out != -1)
alpar@949
   301
	edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
alpar@986
   302
      else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
deba@1702
   303
      if (nodes[n.id].first_out != -1) {
deba@1702
   304
	edges[nodes[n.id].first_out].prev_out = e.id;
deba@1702
   305
      }
alpar@986
   306
      edges[e.id].source = n.id;
alpar@949
   307
      edges[e.id].prev_out = -1;
alpar@949
   308
      edges[e.id].next_out = nodes[n.id].first_out;
alpar@949
   309
      nodes[n.id].first_out = e.id;
alpar@949
   310
    }
alpar@949
   311
alpar@919
   312
  };
deba@909
   313
deba@1979
   314
  typedef GraphExtender<ListGraphBase> ExtendedListGraphBase;
alpar@400
   315
deba@2116
   316
  /// \addtogroup graphs
deba@2116
   317
  /// @{
alpar@400
   318
alpar@948
   319
  ///A list graph class.
alpar@400
   320
alpar@2117
   321
  ///This is a simple and fast graph implementation.
alpar@948
   322
  ///
alpar@2260
   323
  ///It conforms to the \ref concepts::Graph "Graph concept" and it
deba@2111
   324
  ///also provides several additional useful extra functionalities.
deba@2111
   325
  ///The most of the member functions and nested classes are
deba@2111
   326
  ///documented only in the concept class.
alpar@2256
   327
  ///
alpar@2256
   328
  ///An important extra feature of this graph implementation is that
alpar@2260
   329
  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
alpar@2256
   330
  ///
alpar@2260
   331
  ///\sa concepts::Graph.
deba@782
   332
deba@1999
   333
  class ListGraph : public ExtendedListGraphBase {
alpar@2128
   334
  private:
alpar@2128
   335
    ///ListGraph is \e not copy constructible. Use GraphCopy() instead.
alpar@2128
   336
    
alpar@2128
   337
    ///ListGraph is \e not copy constructible. Use GraphCopy() instead.
alpar@2128
   338
    ///
alpar@2128
   339
    ListGraph(const ListGraph &) :ExtendedListGraphBase() {};
alpar@2132
   340
    ///\brief Assignment of ListGraph to another one is \e not allowed.
alpar@2128
   341
    ///Use GraphCopy() instead.
alpar@2128
   342
alpar@2132
   343
    ///Assignment of ListGraph to another one is \e not allowed.
alpar@2128
   344
    ///Use GraphCopy() instead.
alpar@2128
   345
    void operator=(const ListGraph &) {}
alpar@948
   346
  public:
deba@1999
   347
deba@1999
   348
    typedef ExtendedListGraphBase Parent;
deba@1999
   349
alpar@2128
   350
    /// Constructor
alpar@2128
   351
    
alpar@2128
   352
    /// Constructor.
alpar@2128
   353
    ///
alpar@2128
   354
    ListGraph() {}
alpar@2128
   355
deba@2111
   356
    ///Add a new node to the graph.
deba@2111
   357
    
deba@2111
   358
    /// \return the new node.
deba@2111
   359
    ///
deba@2111
   360
    Node addNode() { return Parent::addNode(); }
deba@2111
   361
deba@2111
   362
    ///Add a new edge to the graph.
deba@2111
   363
    
deba@2111
   364
    ///Add a new edge to the graph with source node \c s
deba@2111
   365
    ///and target node \c t.
deba@2111
   366
    ///\return the new edge.
deba@2111
   367
    Edge addEdge(const Node& s, const Node& t) { 
deba@2111
   368
      return Parent::addEdge(s, t); 
deba@2111
   369
    }
deba@2111
   370
alpar@1546
   371
    /// Changes the target of \c e to \c n
alpar@948
   372
alpar@1546
   373
    /// Changes the target of \c e to \c n
alpar@948
   374
    ///
deba@2160
   375
    ///\note The <tt>EdgeIt</tt>s and <tt>OutEdgeIt</tt>s referencing
deba@2114
   376
    ///the changed edge remain valid. However <tt>InEdgeIt</tt>s are
deba@2114
   377
    ///invalidated.
alpar@2123
   378
    ///\warning This functionality cannot be used together with the Snapshot
alpar@2123
   379
    ///feature.
deba@1718
   380
    void changeTarget(Edge e, Node n) { 
deba@2111
   381
      Parent::changeTarget(e,n); 
deba@1718
   382
    }
alpar@1546
   383
    /// Changes the source of \c e to \c n
alpar@948
   384
alpar@1546
   385
    /// Changes the source of \c e to \c n
alpar@948
   386
    ///
deba@2160
   387
    ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s referencing
deba@2114
   388
    ///the changed edge remain valid. However <tt>OutEdgeIt</tt>s are
deba@2114
   389
    ///invalidated.
alpar@2123
   390
    ///\warning This functionality cannot be used together with the Snapshot
alpar@2123
   391
    ///feature.
deba@1718
   392
    void changeSource(Edge e, Node n) { 
deba@2111
   393
      Parent::changeSource(e,n);
deba@1718
   394
    }
alpar@949
   395
alpar@1010
   396
    /// Invert the direction of an edge.
alpar@1010
   397
deba@2160
   398
    ///\note The <tt>EdgeIt</tt>s referencing the changed edge remain
deba@2114
   399
    ///valid. However <tt>OutEdgeIt</tt>s and <tt>InEdgeIt</tt>s are
deba@2114
   400
    ///invalidated.
alpar@2123
   401
    ///\warning This functionality cannot be used together with the Snapshot
alpar@2123
   402
    ///feature.
alpar@1010
   403
    void reverseEdge(Edge e) {
alpar@1010
   404
      Node t=target(e);
deba@2111
   405
      changeTarget(e,source(e));
deba@2111
   406
      changeSource(e,t);
alpar@1010
   407
    }
alpar@1010
   408
deba@2111
   409
    /// \brief Using this it is possible to avoid the superfluous memory
deba@2111
   410
    /// allocation.
alpar@1010
   411
deba@2107
   412
    ///Using this it is possible to avoid the superfluous memory
deba@2107
   413
    ///allocation: if you know that the graph you want to build will
alpar@2123
   414
    ///contain at least 10 million nodes then it is worth reserving
deba@2107
   415
    ///space for this amount before starting to build the graph.
deba@2107
   416
    void reserveNode(int n) { nodes.reserve(n); };
deba@2107
   417
deba@2111
   418
    /// \brief Using this it is possible to avoid the superfluous memory
deba@2111
   419
    /// allocation.
deba@2107
   420
deba@2107
   421
    ///Using this it is possible to avoid the superfluous memory
deba@2107
   422
    ///allocation: see the \ref reserveNode function.
alpar@949
   423
    void reserveEdge(int n) { edges.reserve(n); };
alpar@1010
   424
deba@2107
   425
alpar@1010
   426
    ///Contract two nodes.
alpar@1010
   427
alpar@1010
   428
    ///This function contracts two nodes.
alpar@1010
   429
    ///
alpar@1010
   430
    ///Node \p b will be removed but instead of deleting
athos@2102
   431
    ///incident edges, they will be joined to \p a.
alpar@1010
   432
    ///The last parameter \p r controls whether to remove loops. \c true
alpar@1010
   433
    ///means that loops will be removed.
alpar@1010
   434
    ///
deba@2160
   435
    ///\note The <tt>EdgeIt</tt>s
alpar@1281
   436
    ///referencing a moved edge remain
deba@2160
   437
    ///valid. However <tt>InEdgeIt</tt>s and <tt>OutEdgeIt</tt>s
alpar@1010
   438
    ///may be invalidated.
alpar@2123
   439
    ///\warning This functionality cannot be used together with the Snapshot
alpar@2123
   440
    ///feature.
deba@1718
   441
    void contract(Node a, Node b, bool r = true) 
alpar@1010
   442
    {
alpar@1010
   443
      for(OutEdgeIt e(*this,b);e!=INVALID;) {
alpar@1010
   444
	OutEdgeIt f=e;
alpar@1010
   445
	++f;
alpar@1010
   446
	if(r && target(e)==a) erase(e);
alpar@1546
   447
	else changeSource(e,a);
alpar@1010
   448
	e=f;
alpar@1010
   449
      }
alpar@1010
   450
      for(InEdgeIt e(*this,b);e!=INVALID;) {
alpar@1010
   451
	InEdgeIt f=e;
alpar@1010
   452
	++f;
alpar@1010
   453
	if(r && source(e)==a) erase(e);
alpar@1546
   454
	else changeTarget(e,a);
alpar@1010
   455
	e=f;
alpar@1010
   456
      }
alpar@1010
   457
      erase(b);
alpar@1010
   458
    }
alpar@1011
   459
alpar@1281
   460
    ///Split a node.
alpar@1011
   461
alpar@1284
   462
    ///This function splits a node. First a new node is added to the graph,
alpar@1284
   463
    ///then the source of each outgoing edge of \c n is moved to this new node.
alpar@1281
   464
    ///If \c connect is \c true (this is the default value), then a new edge
alpar@1281
   465
    ///from \c n to the newly created node is also added.
alpar@1281
   466
    ///\return The newly created node.
alpar@1281
   467
    ///
deba@2160
   468
    ///\note The <tt>EdgeIt</tt>s referencing a moved edge remain
deba@2160
   469
    ///valid. However <tt>InEdgeIt</tt>s and <tt>OutEdgeIt</tt>s may
deba@2160
   470
    ///be invalidated.  
deba@2160
   471
    ///
deba@2160
   472
    ///\warning This functionality cannot be used together with the
deba@2160
   473
    ///Snapshot feature.  \todo It could be implemented in a bit
deba@2160
   474
    ///faster way.
deba@2114
   475
    Node split(Node n, bool connect = true) {
alpar@1281
   476
      Node b = addNode();
alpar@1281
   477
      for(OutEdgeIt e(*this,n);e!=INVALID;) {
alpar@1281
   478
 	OutEdgeIt f=e;
alpar@1281
   479
	++f;
alpar@1546
   480
	changeSource(e,b);
alpar@1281
   481
	e=f;
alpar@1281
   482
      }
deba@2114
   483
      if (connect) addEdge(n,b);
alpar@1281
   484
      return b;
alpar@1281
   485
    }
alpar@1281
   486
      
alpar@1812
   487
    ///Split an edge.
alpar@1812
   488
athos@2102
   489
    ///This function splits an edge. First a new node \c b is added to
athos@2102
   490
    ///the graph, then the original edge is re-targeted to \c
athos@2102
   491
    ///b. Finally an edge from \c b to the original target is added.
athos@2102
   492
    ///\return The newly created node.  
athos@2102
   493
    ///\warning This functionality
athos@2102
   494
    ///cannot be used together with the Snapshot feature.
deba@2114
   495
    Node split(Edge e) {
alpar@1812
   496
      Node b = addNode();
alpar@1812
   497
      addEdge(b,target(e));
alpar@1812
   498
      changeTarget(e,b);
alpar@1812
   499
      return b;
alpar@1812
   500
    }
alpar@1812
   501
      
deba@2114
   502
    /// \brief Class to make a snapshot of the graph and restore
deba@2114
   503
    /// to it later.
alpar@1011
   504
    ///
deba@2114
   505
    /// Class to make a snapshot of the graph and to restore it
deba@2114
   506
    /// later.
alpar@1011
   507
    ///
deba@2114
   508
    /// The newly added nodes and edges can be removed using the
deba@2114
   509
    /// restore() function.
deba@2114
   510
    ///
deba@2189
   511
    /// \warning Edge and node deletions cannot be restored. This
deba@2189
   512
    /// events invalidate the snapshot. 
deba@2114
   513
    class Snapshot {
deba@1999
   514
    protected:
deba@2114
   515
deba@2114
   516
      typedef Parent::NodeNotifier NodeNotifier;
deba@2114
   517
deba@2114
   518
      class NodeObserverProxy : public NodeNotifier::ObserverBase {
deba@2114
   519
      public:
deba@2114
   520
deba@2114
   521
        NodeObserverProxy(Snapshot& _snapshot)
deba@2114
   522
          : snapshot(_snapshot) {}
deba@2114
   523
deba@2114
   524
        using NodeNotifier::ObserverBase::attach;
deba@2114
   525
        using NodeNotifier::ObserverBase::detach;
deba@2114
   526
        using NodeNotifier::ObserverBase::attached;
deba@2114
   527
        
deba@2114
   528
      protected:
deba@2114
   529
        
deba@2114
   530
        virtual void add(const Node& node) {
deba@2114
   531
          snapshot.addNode(node);
deba@2114
   532
        }
deba@2114
   533
        virtual void add(const std::vector<Node>& nodes) {
deba@2114
   534
          for (int i = nodes.size() - 1; i >= 0; ++i) {
deba@2114
   535
            snapshot.addNode(nodes[i]);
deba@2114
   536
          }
deba@2114
   537
        }
deba@2114
   538
        virtual void erase(const Node& node) {
deba@2114
   539
          snapshot.eraseNode(node);
deba@2114
   540
        }
deba@2114
   541
        virtual void erase(const std::vector<Node>& nodes) {
deba@2114
   542
          for (int i = 0; i < (int)nodes.size(); ++i) {
deba@2189
   543
            snapshot.eraseNode(nodes[i]);
deba@2114
   544
          }
deba@2114
   545
        }
deba@2114
   546
        virtual void build() {
deba@2114
   547
          NodeNotifier* notifier = getNotifier();
deba@2114
   548
          Node node;
deba@2114
   549
          std::vector<Node> nodes;
deba@2114
   550
          for (notifier->first(node); node != INVALID; notifier->next(node)) {
deba@2114
   551
            nodes.push_back(node);
deba@2114
   552
          }
deba@2114
   553
          for (int i = nodes.size() - 1; i >= 0; --i) {
deba@2114
   554
            snapshot.addNode(nodes[i]);
deba@2114
   555
          }
deba@2114
   556
        }
deba@2114
   557
        virtual void clear() {
deba@2114
   558
          NodeNotifier* notifier = getNotifier();
deba@2114
   559
          Node node;
deba@2114
   560
          for (notifier->first(node); node != INVALID; notifier->next(node)) {
deba@2189
   561
            snapshot.eraseNode(node);
deba@2114
   562
          }
deba@2114
   563
        }
deba@2114
   564
deba@2114
   565
        Snapshot& snapshot;
deba@2114
   566
      };
deba@2114
   567
deba@2114
   568
      class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
deba@2114
   569
      public:
deba@2114
   570
deba@2114
   571
        EdgeObserverProxy(Snapshot& _snapshot)
deba@2114
   572
          : snapshot(_snapshot) {}
deba@2114
   573
deba@2114
   574
        using EdgeNotifier::ObserverBase::attach;
deba@2114
   575
        using EdgeNotifier::ObserverBase::detach;
deba@2114
   576
        using EdgeNotifier::ObserverBase::attached;
deba@2114
   577
        
deba@2114
   578
      protected:
deba@2114
   579
deba@2114
   580
        virtual void add(const Edge& edge) {
deba@2114
   581
          snapshot.addEdge(edge);
deba@2114
   582
        }
deba@2114
   583
        virtual void add(const std::vector<Edge>& edges) {
deba@2114
   584
          for (int i = edges.size() - 1; i >= 0; ++i) {
deba@2114
   585
            snapshot.addEdge(edges[i]);
deba@2114
   586
          }
deba@2114
   587
        }
deba@2114
   588
        virtual void erase(const Edge& edge) {
deba@2114
   589
          snapshot.eraseEdge(edge);
deba@2114
   590
        }
deba@2114
   591
        virtual void erase(const std::vector<Edge>& edges) {
deba@2114
   592
          for (int i = 0; i < (int)edges.size(); ++i) {
deba@2189
   593
            snapshot.eraseEdge(edges[i]);
deba@2114
   594
          }
deba@2114
   595
        }
deba@2114
   596
        virtual void build() {
deba@2114
   597
          EdgeNotifier* notifier = getNotifier();
deba@2114
   598
          Edge edge;
deba@2114
   599
          std::vector<Edge> edges;
deba@2114
   600
          for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
deba@2114
   601
            edges.push_back(edge);
deba@2114
   602
          }
deba@2114
   603
          for (int i = edges.size() - 1; i >= 0; --i) {
deba@2114
   604
            snapshot.addEdge(edges[i]);
deba@2114
   605
          }
deba@2114
   606
        }
deba@2114
   607
        virtual void clear() {
deba@2114
   608
          EdgeNotifier* notifier = getNotifier();
deba@2114
   609
          Edge edge;
deba@2114
   610
          for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
deba@2189
   611
            snapshot.eraseEdge(edge);
deba@2114
   612
          }
deba@2114
   613
        }
deba@2114
   614
deba@2114
   615
        Snapshot& snapshot;
deba@2114
   616
      };
alpar@1011
   617
      
deba@2114
   618
      ListGraph *graph;
deba@2114
   619
deba@2114
   620
      NodeObserverProxy node_observer_proxy;
deba@2114
   621
      EdgeObserverProxy edge_observer_proxy;
deba@2114
   622
alpar@1011
   623
      std::list<Node> added_nodes;
alpar@1011
   624
      std::list<Edge> added_edges;
deba@2114
   625
deba@2114
   626
deba@2114
   627
      void addNode(const Node& node) {
deba@2114
   628
        added_nodes.push_front(node);        
alpar@1011
   629
      }
deba@2189
   630
      void eraseNode(const Node& node) {
deba@2114
   631
        std::list<Node>::iterator it = 
deba@2114
   632
          std::find(added_nodes.begin(), added_nodes.end(), node);
deba@2114
   633
        if (it == added_nodes.end()) {
deba@2114
   634
          clear();
deba@2189
   635
          edge_observer_proxy.detach();
deba@2189
   636
          throw NodeNotifier::ImmediateDetach();
deba@2114
   637
        } else {
deba@2114
   638
          added_nodes.erase(it);
deba@2114
   639
        }
alpar@1011
   640
      }
alpar@1011
   641
deba@2114
   642
      void addEdge(const Edge& edge) {
deba@2114
   643
        added_edges.push_front(edge);        
deba@2114
   644
      }
deba@2189
   645
      void eraseEdge(const Edge& edge) {
deba@2114
   646
        std::list<Edge>::iterator it = 
deba@2114
   647
          std::find(added_edges.begin(), added_edges.end(), edge);
deba@2114
   648
        if (it == added_edges.end()) {
deba@2114
   649
          clear();
deba@2189
   650
          node_observer_proxy.detach(); 
deba@2189
   651
          throw EdgeNotifier::ImmediateDetach();
deba@2114
   652
        } else {
deba@2114
   653
          added_edges.erase(it);
deba@2114
   654
        }        
deba@2114
   655
      }
alpar@1457
   656
deba@2114
   657
      void attach(ListGraph &_graph) {
deba@2114
   658
	graph = &_graph;
deba@2114
   659
	node_observer_proxy.attach(graph->getNotifier(Node()));
deba@2114
   660
        edge_observer_proxy.attach(graph->getNotifier(Edge()));
alpar@1011
   661
      }
alpar@1011
   662
            
deba@2114
   663
      void detach() {
deba@2114
   664
	node_observer_proxy.detach();
deba@2114
   665
	edge_observer_proxy.detach();
deba@2114
   666
      }
deba@2114
   667
deba@2189
   668
      bool attached() const {
deba@2189
   669
        return node_observer_proxy.attached();
deba@2189
   670
      }
deba@2189
   671
deba@2114
   672
      void clear() {
deba@2114
   673
        added_nodes.clear();
deba@2114
   674
        added_edges.clear();        
alpar@1011
   675
      }
deba@1774
   676
alpar@1011
   677
    public:
deba@2114
   678
deba@2160
   679
      /// \brief Default constructor.
deba@2114
   680
      ///
deba@2114
   681
      /// Default constructor.
deba@2114
   682
      /// To actually make a snapshot you must call save().
deba@2114
   683
      Snapshot() 
deba@2114
   684
        : graph(0), node_observer_proxy(*this), 
deba@2114
   685
          edge_observer_proxy(*this) {}
alpar@1011
   686
      
deba@2114
   687
      /// \brief Constructor that immediately makes a snapshot.
deba@2114
   688
      ///      
deba@2114
   689
      /// This constructor immediately makes a snapshot of the graph.
deba@2114
   690
      /// \param _graph The graph we make a snapshot of.
deba@2114
   691
      Snapshot(ListGraph &_graph) 
deba@2114
   692
        : node_observer_proxy(*this), 
deba@2114
   693
          edge_observer_proxy(*this) {
deba@2114
   694
	attach(_graph);
alpar@1011
   695
      }
alpar@1011
   696
      
deba@2114
   697
      /// \brief Make a snapshot.
alpar@1011
   698
      ///
deba@2114
   699
      /// Make a snapshot of the graph.
deba@2114
   700
      ///
deba@2114
   701
      /// This function can be called more than once. In case of a repeated
deba@2114
   702
      /// call, the previous snapshot gets lost.
deba@2114
   703
      /// \param _graph The graph we make the snapshot of.
deba@2114
   704
      void save(ListGraph &_graph) {
deba@2189
   705
        if (attached()) {
deba@2189
   706
          detach();
deba@2189
   707
          clear();
deba@2189
   708
        }
deba@2114
   709
        attach(_graph);
alpar@1011
   710
      }
alpar@1011
   711
      
deba@2114
   712
      /// \brief Undo the changes until the last snapshot.
deba@2114
   713
      // 
alpar@2123
   714
      /// Undo the changes until the last snapshot created by save().
alpar@1011
   715
      void restore() {
deba@2114
   716
	detach();
deba@2189
   717
	for(std::list<Edge>::iterator it = added_edges.begin(); 
deba@2189
   718
            it != added_edges.end(); ++it) {
deba@2189
   719
	  graph->erase(*it);
alpar@1011
   720
	}
deba@2189
   721
	for(std::list<Node>::iterator it = added_nodes.begin(); 
deba@2189
   722
            it != added_nodes.end(); ++it) {
deba@2189
   723
	  graph->erase(*it);
alpar@1011
   724
	}
deba@2189
   725
        clear();
alpar@1011
   726
      }
deba@2114
   727
deba@2114
   728
      /// \brief Gives back true when the snapshot is valid.
deba@2114
   729
      ///
deba@2114
   730
      /// Gives back true when the snapshot is valid.
deba@2114
   731
      bool valid() const {
deba@2189
   732
        return attached();
deba@2114
   733
      }
alpar@1011
   734
    };
alpar@1011
   735
    
alpar@949
   736
  };
klao@1034
   737
deba@2116
   738
  ///@}
deba@2116
   739
deba@2116
   740
  /**************** Undirected List Graph ****************/
deba@2116
   741
deba@2116
   742
  typedef UGraphExtender<UndirGraphExtender<ListGraphBase> > 
deba@2116
   743
  ExtendedListUGraphBase;
deba@2116
   744
deba@2116
   745
  /// \addtogroup graphs
deba@2116
   746
  /// @{
deba@2116
   747
deba@2116
   748
  ///An undirected list graph class.
deba@2116
   749
alpar@2117
   750
  ///This is a simple and fast undirected graph implementation.
deba@2116
   751
  ///
alpar@2256
   752
  ///An important extra feature of this graph implementation is that
alpar@2260
   753
  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
alpar@2256
   754
  ///
deba@2116
   755
  ///It conforms to the
alpar@2260
   756
  ///\ref concepts::UGraph "UGraph concept".
deba@2116
   757
  ///
alpar@2260
   758
  ///\sa concepts::UGraph.
deba@2116
   759
  ///
deba@2116
   760
  class ListUGraph : public ExtendedListUGraphBase {
alpar@2128
   761
  private:
alpar@2128
   762
    ///ListUGraph is \e not copy constructible. Use UGraphCopy() instead.
alpar@2128
   763
alpar@2128
   764
    ///ListUGraph is \e not copy constructible. Use UGraphCopy() instead.
alpar@2128
   765
    ///
alpar@2128
   766
    ListUGraph(const ListUGraph &) :ExtendedListUGraphBase()  {};
alpar@2132
   767
    ///\brief Assignment of ListUGraph to another one is \e not allowed.
alpar@2128
   768
    ///Use UGraphCopy() instead.
alpar@2128
   769
alpar@2132
   770
    ///Assignment of ListUGraph to another one is \e not allowed.
alpar@2128
   771
    ///Use UGraphCopy() instead.
alpar@2128
   772
    void operator=(const ListUGraph &) {}
deba@2116
   773
  public:
alpar@2128
   774
    /// Constructor
alpar@2128
   775
    
alpar@2128
   776
    /// Constructor.
alpar@2128
   777
    ///
alpar@2128
   778
    ListUGraph() {}
alpar@2128
   779
deba@2116
   780
    typedef ExtendedListUGraphBase Parent;
deba@2116
   781
    /// \brief Add a new node to the graph.
deba@2116
   782
    ///
deba@2116
   783
    /// \return the new node.
deba@2116
   784
    ///
deba@2116
   785
    Node addNode() { return Parent::addNode(); }
deba@2116
   786
deba@2116
   787
    /// \brief Add a new edge to the graph.
deba@2116
   788
    ///
deba@2116
   789
    /// Add a new edge to the graph with source node \c s
deba@2116
   790
    /// and target node \c t.
deba@2116
   791
    /// \return the new undirected edge.
deba@2116
   792
    UEdge addEdge(const Node& s, const Node& t) { 
deba@2116
   793
      return Parent::addEdge(s, t); 
deba@2116
   794
    }
deba@2160
   795
    /// \brief Changes the source of \c e to \c n
deba@2160
   796
    ///
deba@2160
   797
    /// Changes the source of \c e to \c n
deba@2160
   798
    ///
deba@2160
   799
    ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s
deba@2160
   800
    ///referencing the changed edge remain
deba@2160
   801
    ///valid. However <tt>OutEdgeIt</tt>s are invalidated.
deba@2160
   802
    void changeSource(UEdge e, Node n) { 
deba@2160
   803
      Parent::changeSource(e,n); 
deba@2160
   804
    }    
deba@2116
   805
    /// \brief Changes the target of \c e to \c n
deba@2116
   806
    ///
deba@2116
   807
    /// Changes the target of \c e to \c n
deba@2116
   808
    ///
deba@2160
   809
    /// \note The <tt>EdgeIt</tt>s referencing the changed edge remain
deba@2160
   810
    /// valid. However the other iterators may be invalidated.
deba@2116
   811
    void changeTarget(UEdge e, Node n) { 
deba@2116
   812
      Parent::changeTarget(e,n); 
deba@2116
   813
    }
deba@2160
   814
    /// \brief Changes the source of \c e to \c n
deba@2116
   815
    ///
deba@2160
   816
    /// Changes the source of \c e to \c n. It changes the proper
deba@2160
   817
    /// node of the represented undirected edge.
deba@2116
   818
    ///
deba@2160
   819
    ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s
deba@2116
   820
    ///referencing the changed edge remain
deba@2160
   821
    ///valid. However <tt>OutEdgeIt</tt>s are invalidated.
deba@2160
   822
    void changeSource(Edge e, Node n) { 
deba@2160
   823
      if (Parent::direction(e)) {
deba@2160
   824
        Parent::changeSource(e,n);
deba@2160
   825
      } else {
deba@2160
   826
        Parent::changeTarget(e,n);
deba@2160
   827
      } 
deba@2160
   828
    }
deba@2160
   829
    /// \brief Changes the target of \c e to \c n
deba@2160
   830
    ///
deba@2160
   831
    /// Changes the target of \c e to \c n. It changes the proper
deba@2160
   832
    /// node of the represented undirected edge.
deba@2160
   833
    ///
deba@2160
   834
    ///\note The <tt>EdgeIt</tt>s and <tt>OutEdgeIt</tt>s
deba@2160
   835
    ///referencing the changed edge remain
deba@2160
   836
    ///valid. However <tt>InEdgeIt</tt>s are invalidated.
deba@2160
   837
    void changeTarget(Edge e, Node n) { 
deba@2160
   838
      if (Parent::direction(e)) {
deba@2160
   839
        Parent::changeTarget(e,n);
deba@2160
   840
      } else {
deba@2160
   841
        Parent::changeSource(e,n);
deba@2160
   842
      } 
deba@2116
   843
    }
deba@2116
   844
    /// \brief Contract two nodes.
deba@2116
   845
    ///
deba@2116
   846
    /// This function contracts two nodes.
deba@2116
   847
    ///
deba@2116
   848
    /// Node \p b will be removed but instead of deleting
deba@2116
   849
    /// its neighboring edges, they will be joined to \p a.
deba@2116
   850
    /// The last parameter \p r controls whether to remove loops. \c true
deba@2116
   851
    /// means that loops will be removed.
deba@2116
   852
    ///
deba@2160
   853
    /// \note The <tt>EdgeIt</tt>s referencing a moved edge remain
deba@2116
   854
    /// valid.
deba@2116
   855
    void contract(Node a, Node b, bool r = true) {
deba@2116
   856
      for(IncEdgeIt e(*this, b); e!=INVALID;) {
deba@2116
   857
	IncEdgeIt f = e; ++f;
deba@2116
   858
	if (r && runningNode(e) == a) {
deba@2116
   859
	  erase(e);
deba@2116
   860
	} else if (source(e) == b) {
deba@2116
   861
	  changeSource(e, a);
deba@2116
   862
	} else {
deba@2116
   863
	  changeTarget(e, a);
deba@2116
   864
	}
deba@2116
   865
	e = f;
deba@2116
   866
      }
deba@2116
   867
      erase(b);
deba@2116
   868
    }
deba@2189
   869
deba@2189
   870
deba@2189
   871
    /// \brief Class to make a snapshot of the graph and restore
deba@2189
   872
    /// to it later.
deba@2189
   873
    ///
deba@2189
   874
    /// Class to make a snapshot of the graph and to restore it
deba@2189
   875
    /// later.
deba@2189
   876
    ///
deba@2189
   877
    /// The newly added nodes and undirected edges can be removed
deba@2189
   878
    /// using the restore() function.
deba@2189
   879
    ///
deba@2189
   880
    /// \warning Edge and node deletions cannot be restored. This
deba@2189
   881
    /// events invalidate the snapshot. 
deba@2189
   882
    class Snapshot {
deba@2189
   883
    protected:
deba@2189
   884
deba@2189
   885
      typedef Parent::NodeNotifier NodeNotifier;
deba@2189
   886
deba@2189
   887
      class NodeObserverProxy : public NodeNotifier::ObserverBase {
deba@2189
   888
      public:
deba@2189
   889
deba@2189
   890
        NodeObserverProxy(Snapshot& _snapshot)
deba@2189
   891
          : snapshot(_snapshot) {}
deba@2189
   892
deba@2189
   893
        using NodeNotifier::ObserverBase::attach;
deba@2189
   894
        using NodeNotifier::ObserverBase::detach;
deba@2189
   895
        using NodeNotifier::ObserverBase::attached;
deba@2189
   896
        
deba@2189
   897
      protected:
deba@2189
   898
        
deba@2189
   899
        virtual void add(const Node& node) {
deba@2189
   900
          snapshot.addNode(node);
deba@2189
   901
        }
deba@2189
   902
        virtual void add(const std::vector<Node>& nodes) {
deba@2189
   903
          for (int i = nodes.size() - 1; i >= 0; ++i) {
deba@2189
   904
            snapshot.addNode(nodes[i]);
deba@2189
   905
          }
deba@2189
   906
        }
deba@2189
   907
        virtual void erase(const Node& node) {
deba@2189
   908
          snapshot.eraseNode(node);
deba@2189
   909
        }
deba@2189
   910
        virtual void erase(const std::vector<Node>& nodes) {
deba@2189
   911
          for (int i = 0; i < (int)nodes.size(); ++i) {
deba@2189
   912
            snapshot.eraseNode(nodes[i]);
deba@2189
   913
          }
deba@2189
   914
        }
deba@2189
   915
        virtual void build() {
deba@2189
   916
          NodeNotifier* notifier = getNotifier();
deba@2189
   917
          Node node;
deba@2189
   918
          std::vector<Node> nodes;
deba@2189
   919
          for (notifier->first(node); node != INVALID; notifier->next(node)) {
deba@2189
   920
            nodes.push_back(node);
deba@2189
   921
          }
deba@2189
   922
          for (int i = nodes.size() - 1; i >= 0; --i) {
deba@2189
   923
            snapshot.addNode(nodes[i]);
deba@2189
   924
          }
deba@2189
   925
        }
deba@2189
   926
        virtual void clear() {
deba@2189
   927
          NodeNotifier* notifier = getNotifier();
deba@2189
   928
          Node node;
deba@2189
   929
          for (notifier->first(node); node != INVALID; notifier->next(node)) {
deba@2189
   930
            snapshot.eraseNode(node);
deba@2189
   931
          }
deba@2189
   932
        }
deba@2189
   933
deba@2189
   934
        Snapshot& snapshot;
deba@2189
   935
      };
deba@2189
   936
deba@2189
   937
      class UEdgeObserverProxy : public UEdgeNotifier::ObserverBase {
deba@2189
   938
      public:
deba@2189
   939
deba@2189
   940
        UEdgeObserverProxy(Snapshot& _snapshot)
deba@2189
   941
          : snapshot(_snapshot) {}
deba@2189
   942
deba@2189
   943
        using UEdgeNotifier::ObserverBase::attach;
deba@2189
   944
        using UEdgeNotifier::ObserverBase::detach;
deba@2189
   945
        using UEdgeNotifier::ObserverBase::attached;
deba@2189
   946
        
deba@2189
   947
      protected:
deba@2189
   948
deba@2189
   949
        virtual void add(const UEdge& edge) {
deba@2189
   950
          snapshot.addUEdge(edge);
deba@2189
   951
        }
deba@2189
   952
        virtual void add(const std::vector<UEdge>& edges) {
deba@2189
   953
          for (int i = edges.size() - 1; i >= 0; ++i) {
deba@2189
   954
            snapshot.addUEdge(edges[i]);
deba@2189
   955
          }
deba@2189
   956
        }
deba@2189
   957
        virtual void erase(const UEdge& edge) {
deba@2189
   958
          snapshot.eraseUEdge(edge);
deba@2189
   959
        }
deba@2189
   960
        virtual void erase(const std::vector<UEdge>& edges) {
deba@2189
   961
          for (int i = 0; i < (int)edges.size(); ++i) {
deba@2189
   962
            snapshot.eraseUEdge(edges[i]);
deba@2189
   963
          }
deba@2189
   964
        }
deba@2189
   965
        virtual void build() {
deba@2189
   966
          UEdgeNotifier* notifier = getNotifier();
deba@2189
   967
          UEdge edge;
deba@2189
   968
          std::vector<UEdge> edges;
deba@2189
   969
          for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
deba@2189
   970
            edges.push_back(edge);
deba@2189
   971
          }
deba@2189
   972
          for (int i = edges.size() - 1; i >= 0; --i) {
deba@2189
   973
            snapshot.addUEdge(edges[i]);
deba@2189
   974
          }
deba@2189
   975
        }
deba@2189
   976
        virtual void clear() {
deba@2189
   977
          UEdgeNotifier* notifier = getNotifier();
deba@2189
   978
          UEdge edge;
deba@2189
   979
          for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
deba@2189
   980
            snapshot.eraseUEdge(edge);
deba@2189
   981
          }
deba@2189
   982
        }
deba@2189
   983
deba@2189
   984
        Snapshot& snapshot;
deba@2189
   985
      };
deba@2189
   986
      
deba@2189
   987
      ListUGraph *graph;
deba@2189
   988
deba@2189
   989
      NodeObserverProxy node_observer_proxy;
deba@2189
   990
      UEdgeObserverProxy edge_observer_proxy;
deba@2189
   991
deba@2189
   992
      std::list<Node> added_nodes;
deba@2189
   993
      std::list<UEdge> added_edges;
deba@2189
   994
deba@2189
   995
deba@2189
   996
      void addNode(const Node& node) {
deba@2189
   997
        added_nodes.push_front(node);        
deba@2189
   998
      }
deba@2189
   999
      void eraseNode(const Node& node) {
deba@2189
  1000
        std::list<Node>::iterator it = 
deba@2189
  1001
          std::find(added_nodes.begin(), added_nodes.end(), node);
deba@2189
  1002
        if (it == added_nodes.end()) {
deba@2189
  1003
          clear();
deba@2189
  1004
          edge_observer_proxy.detach();
deba@2189
  1005
          throw NodeNotifier::ImmediateDetach();
deba@2189
  1006
        } else {
deba@2189
  1007
          added_nodes.erase(it);
deba@2189
  1008
        }
deba@2189
  1009
      }
deba@2189
  1010
deba@2189
  1011
      void addUEdge(const UEdge& edge) {
deba@2189
  1012
        added_edges.push_front(edge);        
deba@2189
  1013
      }
deba@2189
  1014
      void eraseUEdge(const UEdge& edge) {
deba@2189
  1015
        std::list<UEdge>::iterator it = 
deba@2189
  1016
          std::find(added_edges.begin(), added_edges.end(), edge);
deba@2189
  1017
        if (it == added_edges.end()) {
deba@2189
  1018
          clear();
deba@2189
  1019
          node_observer_proxy.detach();
deba@2189
  1020
          throw UEdgeNotifier::ImmediateDetach();
deba@2189
  1021
        } else {
deba@2189
  1022
          added_edges.erase(it);
deba@2189
  1023
        }        
deba@2189
  1024
      }
deba@2189
  1025
deba@2189
  1026
      void attach(ListUGraph &_graph) {
deba@2189
  1027
	graph = &_graph;
deba@2189
  1028
	node_observer_proxy.attach(graph->getNotifier(Node()));
deba@2189
  1029
        edge_observer_proxy.attach(graph->getNotifier(UEdge()));
deba@2189
  1030
      }
deba@2189
  1031
            
deba@2189
  1032
      void detach() {
deba@2189
  1033
	node_observer_proxy.detach();
deba@2189
  1034
	edge_observer_proxy.detach();
deba@2189
  1035
      }
deba@2189
  1036
deba@2189
  1037
      bool attached() const {
deba@2189
  1038
        return node_observer_proxy.attached();
deba@2189
  1039
      }
deba@2189
  1040
deba@2189
  1041
      void clear() {
deba@2189
  1042
        added_nodes.clear();
deba@2189
  1043
        added_edges.clear();        
deba@2189
  1044
      }
deba@2189
  1045
deba@2189
  1046
    public:
deba@2189
  1047
deba@2189
  1048
      /// \brief Default constructor.
deba@2189
  1049
      ///
deba@2189
  1050
      /// Default constructor.
deba@2189
  1051
      /// To actually make a snapshot you must call save().
deba@2189
  1052
      Snapshot() 
deba@2189
  1053
        : graph(0), node_observer_proxy(*this), 
deba@2189
  1054
          edge_observer_proxy(*this) {}
deba@2189
  1055
      
deba@2189
  1056
      /// \brief Constructor that immediately makes a snapshot.
deba@2189
  1057
      ///      
deba@2189
  1058
      /// This constructor immediately makes a snapshot of the graph.
deba@2189
  1059
      /// \param _graph The graph we make a snapshot of.
deba@2189
  1060
      Snapshot(ListUGraph &_graph) 
deba@2189
  1061
        : node_observer_proxy(*this), 
deba@2189
  1062
          edge_observer_proxy(*this) {
deba@2189
  1063
	attach(_graph);
deba@2189
  1064
      }
deba@2189
  1065
      
deba@2189
  1066
      /// \brief Make a snapshot.
deba@2189
  1067
      ///
deba@2189
  1068
      /// Make a snapshot of the graph.
deba@2189
  1069
      ///
deba@2189
  1070
      /// This function can be called more than once. In case of a repeated
deba@2189
  1071
      /// call, the previous snapshot gets lost.
deba@2189
  1072
      /// \param _graph The graph we make the snapshot of.
deba@2189
  1073
      void save(ListUGraph &_graph) {
deba@2189
  1074
        if (attached()) {
deba@2189
  1075
          detach();
deba@2189
  1076
          clear();
deba@2189
  1077
        }
deba@2189
  1078
        attach(_graph);
deba@2189
  1079
      }
deba@2189
  1080
      
deba@2189
  1081
      /// \brief Undo the changes until the last snapshot.
deba@2189
  1082
      // 
deba@2189
  1083
      /// Undo the changes until the last snapshot created by save().
deba@2189
  1084
      void restore() {
deba@2189
  1085
	detach();
deba@2189
  1086
	for(std::list<UEdge>::iterator it = added_edges.begin(); 
deba@2189
  1087
            it != added_edges.end(); ++it) {
deba@2189
  1088
	  graph->erase(*it);
deba@2189
  1089
	}
deba@2189
  1090
	for(std::list<Node>::iterator it = added_nodes.begin(); 
deba@2189
  1091
            it != added_nodes.end(); ++it) {
deba@2189
  1092
	  graph->erase(*it);
deba@2189
  1093
	}
deba@2189
  1094
        clear();
deba@2189
  1095
      }
deba@2189
  1096
deba@2189
  1097
      /// \brief Gives back true when the snapshot is valid.
deba@2189
  1098
      ///
deba@2189
  1099
      /// Gives back true when the snapshot is valid.
deba@2189
  1100
      bool valid() const {
deba@2189
  1101
        return attached();
deba@2189
  1102
      }
deba@2189
  1103
    };
deba@2116
  1104
  };
deba@2116
  1105
deba@2116
  1106
deba@2116
  1107
  class ListBpUGraphBase {
deba@2116
  1108
  public:
deba@2116
  1109
deba@2116
  1110
    class NodeSetError : public LogicError {
deba@2160
  1111
    public:
alpar@2151
  1112
      virtual const char* what() const throw() { 
deba@2116
  1113
	return "lemon::ListBpUGraph::NodeSetError";
deba@2116
  1114
      }
deba@2116
  1115
    };
deba@2116
  1116
deba@2116
  1117
  protected:
deba@2116
  1118
deba@2116
  1119
    struct NodeT {
deba@2116
  1120
      int first_edge, prev, next;
deba@2116
  1121
    };
deba@2116
  1122
deba@2116
  1123
    struct UEdgeT {
deba@2116
  1124
      int aNode, prev_out, next_out;
deba@2116
  1125
      int bNode, prev_in, next_in;
deba@2116
  1126
    };
deba@2116
  1127
deba@2116
  1128
    std::vector<NodeT> aNodes;
deba@2116
  1129
    std::vector<NodeT> bNodes;
deba@2116
  1130
deba@2116
  1131
    std::vector<UEdgeT> edges;
deba@2116
  1132
deba@2116
  1133
    int first_anode;
deba@2116
  1134
    int first_free_anode;
deba@2116
  1135
deba@2116
  1136
    int first_bnode;
deba@2116
  1137
    int first_free_bnode;
deba@2116
  1138
deba@2116
  1139
    int first_free_edge;
deba@2116
  1140
deba@2116
  1141
  public:
deba@2116
  1142
  
deba@2116
  1143
    class Node {
deba@2116
  1144
      friend class ListBpUGraphBase;
deba@2116
  1145
    protected:
deba@2116
  1146
      int id;
deba@2116
  1147
deba@2116
  1148
      explicit Node(int _id) : id(_id) {}
deba@2116
  1149
    public:
deba@2116
  1150
      Node() {}
deba@2116
  1151
      Node(Invalid) { id = -1; }
deba@2116
  1152
      bool operator==(const Node i) const {return id==i.id;}
deba@2116
  1153
      bool operator!=(const Node i) const {return id!=i.id;}
deba@2116
  1154
      bool operator<(const Node i) const {return id<i.id;}
deba@2116
  1155
    };
deba@2116
  1156
deba@2116
  1157
    class UEdge {
deba@2116
  1158
      friend class ListBpUGraphBase;
deba@2116
  1159
    protected:
deba@2116
  1160
      int id;
deba@2116
  1161
deba@2116
  1162
      explicit UEdge(int _id) { id = _id;}
deba@2116
  1163
    public:
deba@2116
  1164
      UEdge() {}
deba@2116
  1165
      UEdge (Invalid) { id = -1; }
deba@2116
  1166
      bool operator==(const UEdge i) const {return id==i.id;}
deba@2116
  1167
      bool operator!=(const UEdge i) const {return id!=i.id;}
deba@2116
  1168
      bool operator<(const UEdge i) const {return id<i.id;}
deba@2116
  1169
    };
deba@2116
  1170
deba@2116
  1171
    ListBpUGraphBase()
deba@2116
  1172
      : first_anode(-1), first_free_anode(-1),
deba@2116
  1173
        first_bnode(-1), first_free_bnode(-1),
deba@2116
  1174
        first_free_edge(-1) {}
deba@2116
  1175
deba@2116
  1176
    void firstANode(Node& node) const {
deba@2116
  1177
      node.id = first_anode != -1 ? (first_anode << 1) : -1;
deba@2116
  1178
    }
deba@2116
  1179
    void nextANode(Node& node) const {
deba@2116
  1180
      node.id = aNodes[node.id >> 1].next;
deba@2116
  1181
    }
deba@2116
  1182
deba@2116
  1183
    void firstBNode(Node& node) const {
deba@2116
  1184
      node.id = first_bnode != -1 ? (first_bnode << 1) + 1 : -1;
deba@2116
  1185
    }
deba@2116
  1186
    void nextBNode(Node& node) const {
deba@2116
  1187
      node.id = bNodes[node.id >> 1].next;
deba@2116
  1188
    }
deba@2116
  1189
deba@2116
  1190
    void first(Node& node) const {
deba@2116
  1191
      if (first_anode != -1) {
deba@2116
  1192
        node.id = (first_anode << 1);
deba@2116
  1193
      } else if (first_bnode != -1) {
deba@2116
  1194
        node.id = (first_bnode << 1) + 1;
deba@2116
  1195
      } else {
deba@2116
  1196
        node.id = -1;
deba@2116
  1197
      }
deba@2116
  1198
    }
deba@2116
  1199
    void next(Node& node) const {
deba@2116
  1200
      if (aNode(node)) {
deba@2116
  1201
        node.id = aNodes[node.id >> 1].next;
deba@2116
  1202
        if (node.id == -1) {
deba@2116
  1203
          if (first_bnode != -1) {
deba@2116
  1204
            node.id = (first_bnode << 1) + 1;
deba@2116
  1205
          }
deba@2116
  1206
        }
deba@2116
  1207
      } else {
deba@2116
  1208
        node.id = bNodes[node.id >> 1].next;
deba@2116
  1209
      }
deba@2116
  1210
    }
deba@2116
  1211
  
deba@2116
  1212
    void first(UEdge& edge) const {
deba@2116
  1213
      int aNodeId = first_anode;
deba@2116
  1214
      while (aNodeId != -1 && aNodes[aNodeId].first_edge == -1) {
deba@2116
  1215
        aNodeId = aNodes[aNodeId].next != -1 ? 
deba@2116
  1216
          aNodes[aNodeId].next >> 1 : -1;
deba@2116
  1217
      }
deba@2116
  1218
      if (aNodeId != -1) {
deba@2116
  1219
        edge.id = aNodes[aNodeId].first_edge;
deba@2116
  1220
      } else {
deba@2116
  1221
        edge.id = -1;
deba@2116
  1222
      }
deba@2116
  1223
    }
deba@2116
  1224
    void next(UEdge& edge) const {
deba@2116
  1225
      int aNodeId = edges[edge.id].aNode >> 1;
deba@2116
  1226
      edge.id = edges[edge.id].next_out;
deba@2116
  1227
      if (edge.id == -1) {
deba@2116
  1228
        aNodeId = aNodes[aNodeId].next != -1 ? 
deba@2116
  1229
          aNodes[aNodeId].next >> 1 : -1;
deba@2116
  1230
        while (aNodeId != -1 && aNodes[aNodeId].first_edge == -1) {
deba@2116
  1231
          aNodeId = aNodes[aNodeId].next != -1 ? 
deba@2116
  1232
          aNodes[aNodeId].next >> 1 : -1;
deba@2116
  1233
        }
deba@2116
  1234
        if (aNodeId != -1) {
deba@2116
  1235
          edge.id = aNodes[aNodeId].first_edge;
deba@2116
  1236
        } else {
deba@2116
  1237
          edge.id = -1;
deba@2116
  1238
        }
deba@2116
  1239
      }
deba@2116
  1240
    }
deba@2116
  1241
deba@2116
  1242
    void firstFromANode(UEdge& edge, const Node& node) const {
deba@2116
  1243
      LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
deba@2116
  1244
      edge.id = aNodes[node.id >> 1].first_edge;
deba@2116
  1245
    }
deba@2116
  1246
    void nextFromANode(UEdge& edge) const {
deba@2116
  1247
      edge.id = edges[edge.id].next_out;
deba@2116
  1248
    }
deba@2116
  1249
deba@2116
  1250
    void firstFromBNode(UEdge& edge, const Node& node) const {
deba@2116
  1251
      LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
deba@2116
  1252
      edge.id = bNodes[node.id >> 1].first_edge;
deba@2116
  1253
    }
deba@2116
  1254
    void nextFromBNode(UEdge& edge) const {
deba@2116
  1255
      edge.id = edges[edge.id].next_in;
deba@2116
  1256
    }
deba@2116
  1257
deba@2116
  1258
    static int id(const Node& node) {
deba@2116
  1259
      return node.id;
deba@2116
  1260
    }
deba@2116
  1261
    static Node nodeFromId(int id) {
deba@2116
  1262
      return Node(id);
deba@2116
  1263
    }
deba@2116
  1264
    int maxNodeId() const {
deba@2116
  1265
      return aNodes.size() > bNodes.size() ?
deba@2116
  1266
	aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
deba@2116
  1267
    }
deba@2116
  1268
  
deba@2116
  1269
    static int id(const UEdge& edge) {
deba@2116
  1270
      return edge.id;
deba@2116
  1271
    }
deba@2116
  1272
    static UEdge uEdgeFromId(int id) {
deba@2116
  1273
      return UEdge(id);
deba@2116
  1274
    }
deba@2116
  1275
    int maxUEdgeId() const {
deba@2116
  1276
      return edges.size();
deba@2116
  1277
    }
deba@2116
  1278
  
deba@2116
  1279
    static int aNodeId(const Node& node) {
deba@2116
  1280
      return node.id >> 1;
deba@2116
  1281
    }
deba@2231
  1282
    static Node nodeFromANodeId(int id) {
deba@2116
  1283
      return Node(id << 1);
deba@2116
  1284
    }
deba@2116
  1285
    int maxANodeId() const {
deba@2116
  1286
      return aNodes.size();
deba@2116
  1287
    }
deba@2116
  1288
deba@2116
  1289
    static int bNodeId(const Node& node) {
deba@2116
  1290
      return node.id >> 1;
deba@2116
  1291
    }
deba@2231
  1292
    static Node nodeFromBNodeId(int id) {
deba@2116
  1293
      return Node((id << 1) + 1);
deba@2116
  1294
    }
deba@2116
  1295
    int maxBNodeId() const {
deba@2116
  1296
      return bNodes.size();
deba@2116
  1297
    }
deba@2116
  1298
deba@2116
  1299
    Node aNode(const UEdge& edge) const {
deba@2116
  1300
      return Node(edges[edge.id].aNode);
deba@2116
  1301
    }
deba@2116
  1302
    Node bNode(const UEdge& edge) const {
deba@2116
  1303
      return Node(edges[edge.id].bNode);
deba@2116
  1304
    }
deba@2116
  1305
deba@2116
  1306
    static bool aNode(const Node& node) {
deba@2116
  1307
      return (node.id & 1) == 0;
deba@2116
  1308
    }
deba@2116
  1309
deba@2116
  1310
    static bool bNode(const Node& node) {
deba@2116
  1311
      return (node.id & 1) == 1;
deba@2116
  1312
    }
deba@2116
  1313
deba@2116
  1314
    Node addANode() {
deba@2116
  1315
      int aNodeId;
deba@2116
  1316
      if (first_free_anode == -1) {
deba@2116
  1317
        aNodeId = aNodes.size();
deba@2116
  1318
        aNodes.push_back(NodeT());
deba@2116
  1319
      } else {
deba@2116
  1320
        aNodeId = first_free_anode;
deba@2116
  1321
        first_free_anode = aNodes[first_free_anode].next;
deba@2116
  1322
      }
deba@2116
  1323
      if (first_anode != -1) {
deba@2116
  1324
        aNodes[aNodeId].next = first_anode << 1;
deba@2116
  1325
        aNodes[first_anode].prev = aNodeId << 1;
deba@2116
  1326
      } else {
deba@2116
  1327
        aNodes[aNodeId].next = -1;
deba@2116
  1328
      }
deba@2116
  1329
      aNodes[aNodeId].prev = -1;
deba@2116
  1330
      first_anode = aNodeId;
deba@2116
  1331
      aNodes[aNodeId].first_edge = -1;
deba@2116
  1332
      return Node(aNodeId << 1);
deba@2116
  1333
    }
deba@2116
  1334
deba@2116
  1335
    Node addBNode() {
deba@2116
  1336
      int bNodeId;
deba@2116
  1337
      if (first_free_bnode == -1) {
deba@2116
  1338
        bNodeId = bNodes.size();
deba@2116
  1339
        bNodes.push_back(NodeT());
deba@2116
  1340
      } else {
deba@2116
  1341
        bNodeId = first_free_bnode;
deba@2116
  1342
        first_free_bnode = bNodes[first_free_bnode].next;
deba@2116
  1343
      }
deba@2116
  1344
      if (first_bnode != -1) {
deba@2116
  1345
        bNodes[bNodeId].next = (first_bnode << 1) + 1;
deba@2116
  1346
        bNodes[first_bnode].prev = (bNodeId << 1) + 1;
deba@2116
  1347
      } else {
deba@2116
  1348
        bNodes[bNodeId].next = -1;
deba@2116
  1349
      }
deba@2189
  1350
      bNodes[bNodeId].prev = -1;
deba@2116
  1351
      first_bnode = bNodeId;
deba@2116
  1352
      bNodes[bNodeId].first_edge = -1;
deba@2116
  1353
      return Node((bNodeId << 1) + 1);
deba@2116
  1354
    }
deba@2116
  1355
deba@2116
  1356
    UEdge addEdge(const Node& source, const Node& target) {
deba@2116
  1357
      LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
deba@2116
  1358
      int edgeId;
deba@2116
  1359
      if (first_free_edge != -1) {
deba@2116
  1360
        edgeId = first_free_edge;
deba@2116
  1361
        first_free_edge = edges[edgeId].next_out;
deba@2116
  1362
      } else {
deba@2116
  1363
        edgeId = edges.size();
deba@2116
  1364
        edges.push_back(UEdgeT());
deba@2116
  1365
      }
deba@2116
  1366
      if ((source.id & 1) == 0) {
deba@2116
  1367
	edges[edgeId].aNode = source.id;
deba@2116
  1368
	edges[edgeId].bNode = target.id;
deba@2116
  1369
      } else {
deba@2116
  1370
	edges[edgeId].aNode = target.id;
deba@2116
  1371
	edges[edgeId].bNode = source.id;
deba@2116
  1372
      }
deba@2116
  1373
      edges[edgeId].next_out = aNodes[edges[edgeId].aNode >> 1].first_edge;
deba@2116
  1374
      edges[edgeId].prev_out = -1;
deba@2116
  1375
      if (aNodes[edges[edgeId].aNode >> 1].first_edge != -1) {
deba@2116
  1376
        edges[aNodes[edges[edgeId].aNode >> 1].first_edge].prev_out = edgeId;
deba@2116
  1377
      }
deba@2116
  1378
      aNodes[edges[edgeId].aNode >> 1].first_edge = edgeId;
deba@2116
  1379
      edges[edgeId].next_in = bNodes[edges[edgeId].bNode >> 1].first_edge;
deba@2116
  1380
      edges[edgeId].prev_in = -1;
deba@2116
  1381
      if (bNodes[edges[edgeId].bNode >> 1].first_edge != -1) {
deba@2116
  1382
        edges[bNodes[edges[edgeId].bNode >> 1].first_edge].prev_in = edgeId;
deba@2116
  1383
      }
deba@2116
  1384
      bNodes[edges[edgeId].bNode >> 1].first_edge = edgeId;
deba@2116
  1385
      return UEdge(edgeId);
deba@2116
  1386
    }
deba@2116
  1387
deba@2116
  1388
    void erase(const Node& node) {
deba@2116
  1389
      if (aNode(node)) {
deba@2116
  1390
        int aNodeId = node.id >> 1;
deba@2116
  1391
        if (aNodes[aNodeId].prev != -1) {
deba@2116
  1392
          aNodes[aNodes[aNodeId].prev >> 1].next = aNodes[aNodeId].next;
deba@2116
  1393
        } else {
deba@2189
  1394
          first_anode = 
deba@2189
  1395
            aNodes[aNodeId].next != -1 ? aNodes[aNodeId].next >> 1 : -1;
deba@2116
  1396
        }
deba@2116
  1397
        if (aNodes[aNodeId].next != -1) {
deba@2116
  1398
          aNodes[aNodes[aNodeId].next >> 1].prev = aNodes[aNodeId].prev;
deba@2116
  1399
        }
deba@2116
  1400
        aNodes[aNodeId].next = first_free_anode;
deba@2116
  1401
        first_free_anode = aNodeId;
deba@2116
  1402
      } else {
deba@2116
  1403
        int bNodeId = node.id >> 1;
deba@2116
  1404
        if (bNodes[bNodeId].prev != -1) {
deba@2116
  1405
          bNodes[bNodes[bNodeId].prev >> 1].next = bNodes[bNodeId].next;
deba@2116
  1406
        } else {
deba@2189
  1407
          first_bnode = 
deba@2189
  1408
            bNodes[bNodeId].next != -1 ? bNodes[bNodeId].next >> 1 : -1;
deba@2116
  1409
        }
deba@2116
  1410
        if (bNodes[bNodeId].next != -1) {
deba@2116
  1411
          bNodes[bNodes[bNodeId].next >> 1].prev = bNodes[bNodeId].prev;
deba@2116
  1412
        }
deba@2116
  1413
        bNodes[bNodeId].next = first_free_bnode;
deba@2116
  1414
        first_free_bnode = bNodeId;
deba@2116
  1415
      }
deba@2116
  1416
    }
deba@2116
  1417
deba@2116
  1418
    void erase(const UEdge& edge) {
deba@2116
  1419
deba@2116
  1420
      if (edges[edge.id].prev_out != -1) {
deba@2116
  1421
        edges[edges[edge.id].prev_out].next_out = edges[edge.id].next_out;
deba@2116
  1422
      } else {
deba@2116
  1423
        aNodes[edges[edge.id].aNode >> 1].first_edge = edges[edge.id].next_out;
deba@2116
  1424
      }
deba@2116
  1425
      if (edges[edge.id].next_out != -1) {
deba@2116
  1426
        edges[edges[edge.id].next_out].prev_out = edges[edge.id].prev_out;
deba@2116
  1427
      }
deba@2116
  1428
deba@2116
  1429
      if (edges[edge.id].prev_in != -1) {
deba@2116
  1430
        edges[edges[edge.id].prev_in].next_in = edges[edge.id].next_in;
deba@2116
  1431
      } else {
deba@2116
  1432
        bNodes[edges[edge.id].bNode >> 1].first_edge = edges[edge.id].next_in;
deba@2116
  1433
      }
deba@2116
  1434
      if (edges[edge.id].next_in != -1) {
deba@2116
  1435
        edges[edges[edge.id].next_in].prev_in = edges[edge.id].prev_in;
deba@2116
  1436
      }
deba@2116
  1437
deba@2116
  1438
      edges[edge.id].next_out = first_free_edge;
deba@2116
  1439
      first_free_edge = edge.id;
deba@2116
  1440
    }
alpar@2128
  1441
 
deba@2116
  1442
    void clear() {
deba@2116
  1443
      aNodes.clear();
deba@2116
  1444
      bNodes.clear();
deba@2116
  1445
      edges.clear();
deba@2116
  1446
      first_anode = -1;
deba@2116
  1447
      first_free_anode = -1;
deba@2116
  1448
      first_bnode = -1;
deba@2116
  1449
      first_free_bnode = -1;
deba@2116
  1450
      first_free_edge = -1;
deba@2116
  1451
    }
deba@2116
  1452
deba@2160
  1453
    void changeANode(const UEdge& edge, const Node& node) {
deba@2160
  1454
      LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
deba@2160
  1455
      if (edges[edge.id].prev_out != -1) {
deba@2160
  1456
        edges[edges[edge.id].prev_out].next_out = edges[edge.id].next_out;
deba@2160
  1457
      } else {
deba@2160
  1458
        aNodes[edges[edge.id].aNode >> 1].first_edge = edges[edge.id].next_out;
deba@2160
  1459
      }
deba@2160
  1460
      if (edges[edge.id].next_out != -1) {
deba@2160
  1461
        edges[edges[edge.id].next_out].prev_out = edges[edge.id].prev_out;  
deba@2160
  1462
      }
deba@2160
  1463
      if (aNodes[node.id >> 1].first_edge != -1) {
deba@2160
  1464
        edges[aNodes[node.id >> 1].first_edge].prev_out = edge.id;
deba@2160
  1465
      }
deba@2160
  1466
      edges[edge.id].prev_out = -1;
deba@2160
  1467
      edges[edge.id].next_out = aNodes[node.id >> 1].first_edge;
deba@2160
  1468
      aNodes[node.id >> 1].first_edge = edge.id;
deba@2160
  1469
      edges[edge.id].aNode = node.id;
deba@2160
  1470
    } 
deba@2160
  1471
deba@2160
  1472
    void changeBNode(const UEdge& edge, const Node& node) {
deba@2160
  1473
      LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
deba@2160
  1474
      if (edges[edge.id].prev_in != -1) {
deba@2160
  1475
        edges[edges[edge.id].prev_in].next_in = edges[edge.id].next_in;
deba@2160
  1476
      } else {
deba@2160
  1477
        bNodes[edges[edge.id].bNode >> 1].first_edge = edges[edge.id].next_in;
deba@2160
  1478
      }
deba@2160
  1479
      if (edges[edge.id].next_in != -1) {
deba@2160
  1480
        edges[edges[edge.id].next_in].prev_in = edges[edge.id].prev_in;  
deba@2160
  1481
      }
deba@2160
  1482
      if (bNodes[node.id >> 1].first_edge != -1) {
deba@2160
  1483
        edges[bNodes[node.id >> 1].first_edge].prev_in = edge.id;
deba@2160
  1484
      }
deba@2160
  1485
      edges[edge.id].prev_in = -1;
deba@2160
  1486
      edges[edge.id].next_in = bNodes[node.id >> 1].first_edge;
deba@2160
  1487
      bNodes[node.id >> 1].first_edge = edge.id;
deba@2160
  1488
      edges[edge.id].bNode = node.id;
deba@2160
  1489
    } 
deba@2160
  1490
deba@2116
  1491
  };
deba@2116
  1492
deba@2116
  1493
deba@2231
  1494
  typedef BpUGraphExtender<BidirBpUGraphExtender<ListBpUGraphBase> > 
deba@2231
  1495
  ExtendedListBpUGraphBase;
deba@2116
  1496
deba@2116
  1497
  /// \ingroup graphs
deba@2116
  1498
  ///
deba@2116
  1499
  /// \brief A smart bipartite undirected graph class.
deba@2116
  1500
  ///
deba@2116
  1501
  /// This is a bipartite undirected graph implementation.
alpar@2260
  1502
  /// It is conforms to the \ref concepts::BpUGraph "BpUGraph concept".
alpar@2256
  1503
  ///
alpar@2256
  1504
  ///An important extra feature of this graph implementation is that
alpar@2260
  1505
  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
alpar@2256
  1506
  ///
alpar@2260
  1507
  /// \sa concepts::BpUGraph.
deba@2116
  1508
  ///
deba@2160
  1509
  class ListBpUGraph : public ExtendedListBpUGraphBase {
deba@2160
  1510
    /// \brief ListBpUGraph is \e not copy constructible.
deba@2160
  1511
    ///
deba@2160
  1512
    ///ListBpUGraph is \e not copy constructible.
deba@2160
  1513
    ListBpUGraph(const ListBpUGraph &) :ExtendedListBpUGraphBase()  {};
deba@2160
  1514
    /// \brief Assignment of ListBpUGraph to another one is \e not
deba@2160
  1515
    /// allowed.
deba@2160
  1516
    ///
deba@2160
  1517
    /// Assignment of ListBpUGraph to another one is \e not allowed.
deba@2160
  1518
    void operator=(const ListBpUGraph &) {}
deba@2160
  1519
  public:
deba@2160
  1520
    /// \brief Constructor
deba@2160
  1521
    ///    
deba@2160
  1522
    /// Constructor.
deba@2160
  1523
    ///
deba@2160
  1524
    ListBpUGraph() {}
deba@2160
  1525
deba@2160
  1526
    typedef ExtendedListBpUGraphBase Parent;
deba@2160
  1527
    /// \brief Add a new ANode to the graph.
deba@2160
  1528
    ///
deba@2160
  1529
    /// \return the new node.
deba@2160
  1530
    ///
deba@2160
  1531
    Node addANode() { return Parent::addANode(); }
deba@2160
  1532
deba@2160
  1533
    /// \brief Add a new BNode to the graph.
deba@2160
  1534
    ///
deba@2160
  1535
    /// \return the new node.
deba@2160
  1536
    ///
deba@2160
  1537
    Node addBNode() { return Parent::addBNode(); }
deba@2160
  1538
deba@2160
  1539
    /// \brief Add a new edge to the graph.
deba@2160
  1540
    ///
deba@2160
  1541
    /// Add a new edge to the graph with an ANode and a BNode.
deba@2160
  1542
    /// \return the new undirected edge.
deba@2160
  1543
    UEdge addEdge(const Node& s, const Node& t) { 
deba@2160
  1544
      return Parent::addEdge(s, t); 
deba@2160
  1545
    }
deba@2160
  1546
deba@2160
  1547
    /// \brief Changes the ANode of \c e to \c n
deba@2160
  1548
    ///
deba@2160
  1549
    /// Changes the ANode of \c e to \c n
deba@2160
  1550
    ///
deba@2160
  1551
    ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s referencing
deba@2160
  1552
    ///the changed edge remain valid. However <tt>OutEdgeIt</tt>s are
deba@2160
  1553
    ///invalidated.
deba@2160
  1554
    void changeANode(UEdge e, Node n) { 
deba@2160
  1555
      Parent::changeANode(e,n); 
deba@2160
  1556
    }
deba@2160
  1557
deba@2160
  1558
    /// \brief Changes the BNode of \c e to \c n
deba@2160
  1559
    ///
deba@2160
  1560
    /// Changes the BNode of \c e to \c n
deba@2160
  1561
    ///
deba@2160
  1562
    /// \note The <tt>EdgeIt</tt>s and <tt>OutEdgeIt</tt>s
deba@2160
  1563
    /// referencing the changed edge remain
deba@2160
  1564
    /// valid. However <tt>InEdgeIt</tt>s are invalidated.
deba@2160
  1565
    void changeBNode(UEdge e, Node n) { 
deba@2160
  1566
      Parent::changeBNode(e,n); 
deba@2160
  1567
    }
deba@2160
  1568
deba@2160
  1569
    /// \brief Changes the source(ANode) of \c e to \c n
deba@2160
  1570
    ///
deba@2160
  1571
    /// Changes the source(ANode) of \c e to \c n
deba@2160
  1572
    ///
deba@2160
  1573
    ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s referencing
deba@2160
  1574
    ///the changed edge remain valid. However <tt>OutEdgeIt</tt>s are
deba@2160
  1575
    ///invalidated.
deba@2160
  1576
    void changeSource(UEdge e, Node n) { 
deba@2160
  1577
      Parent::changeANode(e,n); 
deba@2160
  1578
    }
deba@2160
  1579
deba@2160
  1580
    /// \brief Changes the target(BNode) of \c e to \c n
deba@2160
  1581
    ///
deba@2160
  1582
    /// Changes the target(BNode) of \c e to \c n
deba@2160
  1583
    ///
deba@2160
  1584
    /// \note The <tt>EdgeIt</tt>s and <tt>OutEdgeIt</tt>s
deba@2160
  1585
    /// referencing the changed edge remain
deba@2160
  1586
    /// valid. However <tt>InEdgeIt</tt>s are invalidated.
deba@2160
  1587
    void changeTarget(UEdge e, Node n) { 
deba@2160
  1588
      Parent::changeBNode(e,n); 
deba@2160
  1589
    }
deba@2160
  1590
deba@2160
  1591
    /// \brief Changes the source of \c e to \c n
deba@2160
  1592
    ///
deba@2160
  1593
    /// Changes the source of \c e to \c n. It changes the proper
deba@2160
  1594
    /// node of the represented undirected edge.
deba@2160
  1595
    ///
deba@2160
  1596
    ///\note The <tt>EdgeIt</tt>s and <tt>InEdgeIt</tt>s
deba@2160
  1597
    ///referencing the changed edge remain
deba@2160
  1598
    ///valid. However <tt>OutEdgeIt</tt>s are invalidated.
deba@2160
  1599
    void changeSource(Edge e, Node n) { 
deba@2160
  1600
      if (Parent::direction(e)) {
deba@2160
  1601
        Parent::changeANode(e,n);
deba@2160
  1602
      } else {
deba@2160
  1603
        Parent::changeBNode(e,n);
deba@2160
  1604
      } 
deba@2160
  1605
    }
deba@2160
  1606
    /// \brief Changes the target of \c e to \c n
deba@2160
  1607
    ///
deba@2160
  1608
    /// Changes the target of \c e to \c n. It changes the proper
deba@2160
  1609
    /// node of the represented undirected edge.
deba@2160
  1610
    ///
deba@2160
  1611
    ///\note The <tt>EdgeIt</tt>s and <tt>OutEdgeIt</tt>s
deba@2160
  1612
    ///referencing the changed edge remain
deba@2160
  1613
    ///valid. However <tt>InEdgeIt</tt>s are invalidated.
deba@2160
  1614
    void changeTarget(Edge e, Node n) { 
deba@2160
  1615
      if (Parent::direction(e)) {
deba@2160
  1616
        Parent::changeBNode(e,n);
deba@2160
  1617
      } else {
deba@2160
  1618
        Parent::changeANode(e,n);
deba@2160
  1619
      } 
deba@2160
  1620
    }
deba@2160
  1621
    /// \brief Contract two nodes.
deba@2160
  1622
    ///
deba@2160
  1623
    /// This function contracts two nodes.
deba@2160
  1624
    ///
deba@2160
  1625
    /// Node \p b will be removed but instead of deleting its
deba@2160
  1626
    /// neighboring edges, they will be joined to \p a.  The two nodes
deba@2160
  1627
    /// should be from the same nodeset, of course.
deba@2160
  1628
    ///
deba@2160
  1629
    /// \note The <tt>EdgeIt</tt>s referencing a moved edge remain
deba@2160
  1630
    /// valid.
deba@2160
  1631
    void contract(const Node& a, const Node& b) {
deba@2160
  1632
      LEMON_ASSERT(Parent::aNode(a) == Parent::aNode(b), NodeSetError());
deba@2160
  1633
      if (Parent::aNode(a)) {
deba@2160
  1634
        for (IncEdgeIt e(*this, b); e!=INVALID;) {
deba@2160
  1635
          IncEdgeIt f = e; ++f;
deba@2160
  1636
          changeSource(e, a);
deba@2160
  1637
          e = f;
deba@2160
  1638
        }
deba@2160
  1639
      } else {
deba@2160
  1640
        for (IncEdgeIt e(*this, b); e!=INVALID;) {
deba@2160
  1641
          IncEdgeIt f = e; ++f;
deba@2160
  1642
          changeTarget(e, a);
deba@2160
  1643
          e = f;
deba@2160
  1644
        }
deba@2160
  1645
      }
deba@2160
  1646
      erase(b);
deba@2160
  1647
    }
deba@2160
  1648
deba@2189
  1649
    /// \brief Class to make a snapshot of the graph and restore
deba@2189
  1650
    /// to it later.
deba@2189
  1651
    ///
deba@2189
  1652
    /// Class to make a snapshot of the graph and to restore it
deba@2189
  1653
    /// later.
deba@2189
  1654
    ///
deba@2189
  1655
    /// The newly added nodes and undirected edges can be removed
deba@2189
  1656
    /// using the restore() function.
deba@2189
  1657
    ///
deba@2189
  1658
    /// \warning Edge and node deletions cannot be restored. This
deba@2189
  1659
    /// events invalidate the snapshot. 
deba@2189
  1660
    class Snapshot {
deba@2189
  1661
    protected:
deba@2189
  1662
deba@2189
  1663
      typedef Parent::NodeNotifier NodeNotifier;
deba@2189
  1664
deba@2189
  1665
      class NodeObserverProxy : public NodeNotifier::ObserverBase {
deba@2189
  1666
      public:
deba@2189
  1667
deba@2189
  1668
        NodeObserverProxy(Snapshot& _snapshot)
deba@2189
  1669
          : snapshot(_snapshot) {}
deba@2189
  1670
deba@2189
  1671
        using NodeNotifier::ObserverBase::attach;
deba@2189
  1672
        using NodeNotifier::ObserverBase::detach;
deba@2189
  1673
        using NodeNotifier::ObserverBase::attached;
deba@2189
  1674
        
deba@2189
  1675
      protected:
deba@2189
  1676
        
deba@2189
  1677
        virtual void add(const Node& node) {
deba@2189
  1678
          snapshot.addNode(node);
deba@2189
  1679
        }
deba@2189
  1680
        virtual void add(const std::vector<Node>& nodes) {
deba@2189
  1681
          for (int i = nodes.size() - 1; i >= 0; ++i) {
deba@2189
  1682
            snapshot.addNode(nodes[i]);
deba@2189
  1683
          }
deba@2189
  1684
        }
deba@2189
  1685
        virtual void erase(const Node& node) {
deba@2189
  1686
          snapshot.eraseNode(node);
deba@2189
  1687
        }
deba@2189
  1688
        virtual void erase(const std::vector<Node>& nodes) {
deba@2189
  1689
          for (int i = 0; i < (int)nodes.size(); ++i) {
deba@2189
  1690
            snapshot.eraseNode(nodes[i]);
deba@2189
  1691
          }
deba@2189
  1692
        }
deba@2189
  1693
        virtual void build() {
deba@2189
  1694
          NodeNotifier* notifier = getNotifier();
deba@2189
  1695
          Node node;
deba@2189
  1696
          std::vector<Node> nodes;
deba@2189
  1697
          for (notifier->first(node); node != INVALID; notifier->next(node)) {
deba@2189
  1698
            nodes.push_back(node);
deba@2189
  1699
          }
deba@2189
  1700
          for (int i = nodes.size() - 1; i >= 0; --i) {
deba@2189
  1701
            snapshot.addNode(nodes[i]);
deba@2189
  1702
          }
deba@2189
  1703
        }
deba@2189
  1704
        virtual void clear() {
deba@2189
  1705
          NodeNotifier* notifier = getNotifier();
deba@2189
  1706
          Node node;
deba@2189
  1707
          for (notifier->first(node); node != INVALID; notifier->next(node)) {
deba@2189
  1708
            snapshot.eraseNode(node);
deba@2189
  1709
          }
deba@2189
  1710
        }
deba@2189
  1711
deba@2189
  1712
        Snapshot& snapshot;
deba@2189
  1713
      };
deba@2189
  1714
deba@2189
  1715
      class UEdgeObserverProxy : public UEdgeNotifier::ObserverBase {
deba@2189
  1716
      public:
deba@2189
  1717
deba@2189
  1718
        UEdgeObserverProxy(Snapshot& _snapshot)
deba@2189
  1719
          : snapshot(_snapshot) {}
deba@2189
  1720
deba@2189
  1721
        using UEdgeNotifier::ObserverBase::attach;
deba@2189
  1722
        using UEdgeNotifier::ObserverBase::detach;
deba@2189
  1723
        using UEdgeNotifier::ObserverBase::attached;
deba@2189
  1724
        
deba@2189
  1725
      protected:
deba@2189
  1726
deba@2189
  1727
        virtual void add(const UEdge& edge) {
deba@2189
  1728
          snapshot.addUEdge(edge);
deba@2189
  1729
        }
deba@2189
  1730
        virtual void add(const std::vector<UEdge>& edges) {
deba@2189
  1731
          for (int i = edges.size() - 1; i >= 0; ++i) {
deba@2189
  1732
            snapshot.addUEdge(edges[i]);
deba@2189
  1733
          }
deba@2189
  1734
        }
deba@2189
  1735
        virtual void erase(const UEdge& edge) {
deba@2189
  1736
          snapshot.eraseUEdge(edge);
deba@2189
  1737
        }
deba@2189
  1738
        virtual void erase(const std::vector<UEdge>& edges) {
deba@2189
  1739
          for (int i = 0; i < (int)edges.size(); ++i) {
deba@2189
  1740
            snapshot.eraseUEdge(edges[i]);
deba@2189
  1741
          }
deba@2189
  1742
        }
deba@2189
  1743
        virtual void build() {
deba@2189
  1744
          UEdgeNotifier* notifier = getNotifier();
deba@2189
  1745
          UEdge edge;
deba@2189
  1746
          std::vector<UEdge> edges;
deba@2189
  1747
          for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
deba@2189
  1748
            edges.push_back(edge);
deba@2189
  1749
          }
deba@2189
  1750
          for (int i = edges.size() - 1; i >= 0; --i) {
deba@2189
  1751
            snapshot.addUEdge(edges[i]);
deba@2189
  1752
          }
deba@2189
  1753
        }
deba@2189
  1754
        virtual void clear() {
deba@2189
  1755
          UEdgeNotifier* notifier = getNotifier();
deba@2189
  1756
          UEdge edge;
deba@2189
  1757
          for (notifier->first(edge); edge != INVALID; notifier->next(edge)) {
deba@2189
  1758
            snapshot.eraseUEdge(edge);
deba@2189
  1759
          }
deba@2189
  1760
        }
deba@2189
  1761
deba@2189
  1762
        Snapshot& snapshot;
deba@2189
  1763
      };
deba@2189
  1764
      
deba@2189
  1765
      ListBpUGraph *graph;
deba@2189
  1766
deba@2189
  1767
      NodeObserverProxy node_observer_proxy;
deba@2189
  1768
      UEdgeObserverProxy edge_observer_proxy;
deba@2189
  1769
deba@2189
  1770
      std::list<Node> added_nodes;
deba@2189
  1771
      std::list<UEdge> added_edges;
deba@2189
  1772
deba@2189
  1773
deba@2189
  1774
      void addNode(const Node& node) {
deba@2189
  1775
        added_nodes.push_front(node);        
deba@2189
  1776
      }
deba@2189
  1777
      void eraseNode(const Node& node) {
deba@2189
  1778
        std::list<Node>::iterator it = 
deba@2189
  1779
          std::find(added_nodes.begin(), added_nodes.end(), node);
deba@2189
  1780
        if (it == added_nodes.end()) {
deba@2189
  1781
          clear();
deba@2189
  1782
          edge_observer_proxy.detach();
deba@2189
  1783
          throw NodeNotifier::ImmediateDetach();
deba@2189
  1784
        } else {
deba@2189
  1785
          added_nodes.erase(it);
deba@2189
  1786
        }
deba@2189
  1787
      }
deba@2189
  1788
deba@2189
  1789
      void addUEdge(const UEdge& edge) {
deba@2189
  1790
        added_edges.push_front(edge);        
deba@2189
  1791
      }
deba@2189
  1792
      void eraseUEdge(const UEdge& edge) {
deba@2189
  1793
        std::list<UEdge>::iterator it = 
deba@2189
  1794
          std::find(added_edges.begin(), added_edges.end(), edge);
deba@2189
  1795
        if (it == added_edges.end()) {
deba@2189
  1796
          clear();
deba@2189
  1797
          node_observer_proxy.detach();
deba@2189
  1798
          throw UEdgeNotifier::ImmediateDetach();
deba@2189
  1799
        } else {
deba@2189
  1800
          added_edges.erase(it);
deba@2189
  1801
        }        
deba@2189
  1802
      }
deba@2189
  1803
deba@2189
  1804
      void attach(ListBpUGraph &_graph) {
deba@2189
  1805
	graph = &_graph;
deba@2189
  1806
	node_observer_proxy.attach(graph->getNotifier(Node()));
deba@2189
  1807
        edge_observer_proxy.attach(graph->getNotifier(UEdge()));
deba@2189
  1808
      }
deba@2189
  1809
            
deba@2189
  1810
      void detach() {
deba@2189
  1811
	node_observer_proxy.detach();
deba@2189
  1812
	edge_observer_proxy.detach();
deba@2189
  1813
      }
deba@2189
  1814
deba@2189
  1815
      bool attached() const {
deba@2189
  1816
        return node_observer_proxy.attached();
deba@2189
  1817
      }
deba@2189
  1818
deba@2189
  1819
      void clear() {
deba@2189
  1820
        added_nodes.clear();
deba@2189
  1821
        added_edges.clear();        
deba@2189
  1822
      }
deba@2189
  1823
deba@2189
  1824
    public:
deba@2189
  1825
deba@2189
  1826
      /// \brief Default constructor.
deba@2189
  1827
      ///
deba@2189
  1828
      /// Default constructor.
deba@2189
  1829
      /// To actually make a snapshot you must call save().
deba@2189
  1830
      Snapshot() 
deba@2189
  1831
        : graph(0), node_observer_proxy(*this), 
deba@2189
  1832
          edge_observer_proxy(*this) {}
deba@2189
  1833
      
deba@2189
  1834
      /// \brief Constructor that immediately makes a snapshot.
deba@2189
  1835
      ///      
deba@2189
  1836
      /// This constructor immediately makes a snapshot of the graph.
deba@2189
  1837
      /// \param _graph The graph we make a snapshot of.
deba@2189
  1838
      Snapshot(ListBpUGraph &_graph) 
deba@2189
  1839
        : node_observer_proxy(*this), 
deba@2189
  1840
          edge_observer_proxy(*this) {
deba@2189
  1841
	attach(_graph);
deba@2189
  1842
      }
deba@2189
  1843
      
deba@2189
  1844
      /// \brief Make a snapshot.
deba@2189
  1845
      ///
deba@2189
  1846
      /// Make a snapshot of the graph.
deba@2189
  1847
      ///
deba@2189
  1848
      /// This function can be called more than once. In case of a repeated
deba@2189
  1849
      /// call, the previous snapshot gets lost.
deba@2189
  1850
      /// \param _graph The graph we make the snapshot of.
deba@2189
  1851
      void save(ListBpUGraph &_graph) {
deba@2189
  1852
        if (attached()) {
deba@2189
  1853
          detach();
deba@2189
  1854
          clear();
deba@2189
  1855
        }
deba@2189
  1856
        attach(_graph);
deba@2189
  1857
      }
deba@2189
  1858
      
deba@2189
  1859
      /// \brief Undo the changes until the last snapshot.
deba@2189
  1860
      // 
deba@2189
  1861
      /// Undo the changes until the last snapshot created by save().
deba@2189
  1862
      void restore() {
deba@2189
  1863
	detach();
deba@2189
  1864
	for(std::list<UEdge>::iterator it = added_edges.begin(); 
deba@2189
  1865
            it != added_edges.end(); ++it) {
deba@2189
  1866
	  graph->erase(*it);
deba@2189
  1867
	}
deba@2189
  1868
	for(std::list<Node>::iterator it = added_nodes.begin(); 
deba@2189
  1869
            it != added_nodes.end(); ++it) {
deba@2189
  1870
	  graph->erase(*it);
deba@2189
  1871
	}
deba@2189
  1872
        clear();
deba@2189
  1873
      }
deba@2189
  1874
deba@2189
  1875
      /// \brief Gives back true when the snapshot is valid.
deba@2189
  1876
      ///
deba@2189
  1877
      /// Gives back true when the snapshot is valid.
deba@2189
  1878
      bool valid() const {
deba@2189
  1879
        return attached();
deba@2189
  1880
      }
deba@2189
  1881
    };
deba@2160
  1882
  };
deba@2116
  1883
deba@2116
  1884
  
deba@2116
  1885
  /// @}  
alpar@948
  1886
} //namespace lemon
klao@946
  1887
  
alpar@400
  1888
klao@946
  1889
#endif