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