lemon/list_graph.h
author alpar
Mon, 06 Feb 2006 09:11:53 +0000
changeset 1960 a60b681d0825
parent 1909 2d806130e700
child 1979 c2992fd74dad
permissions -rw-r--r--
- Increased max. number of iteration
- Better tests.
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
klao@1909
    24
///\brief ListGraph, ListUGraph classes.
alpar@948
    25
deba@1307
    26
#include <lemon/bits/erasable_graph_extender.h>
deba@1307
    27
#include <lemon/bits/clearable_graph_extender.h>
deba@1307
    28
#include <lemon/bits/extendable_graph_extender.h>
deba@1307
    29
#include <lemon/bits/iterable_graph_extender.h>
deba@1307
    30
#include <lemon/bits/alteration_notifier.h>
deba@1307
    31
#include <lemon/bits/default_map.h>
deba@1791
    32
#include <lemon/bits/graph_extender.h>
deba@782
    33
deba@1774
    34
#include <lemon/error.h>
deba@1774
    35
alpar@1011
    36
#include <list>
deba@782
    37
alpar@921
    38
namespace lemon {
alpar@395
    39
klao@946
    40
  class ListGraphBase {
alpar@406
    41
alpar@949
    42
  protected:
klao@946
    43
    struct NodeT {
deba@1470
    44
      int first_in, first_out;
alpar@397
    45
      int prev, next;
alpar@395
    46
    };
klao@946
    47
 
klao@946
    48
    struct EdgeT {
alpar@986
    49
      int target, source;
alpar@397
    50
      int prev_in, prev_out;
alpar@397
    51
      int next_in, next_out;
alpar@395
    52
    };
alpar@395
    53
alpar@395
    54
    std::vector<NodeT> nodes;
klao@946
    55
alpar@397
    56
    int first_node;
klao@946
    57
alpar@397
    58
    int first_free_node;
klao@946
    59
alpar@395
    60
    std::vector<EdgeT> edges;
klao@946
    61
alpar@397
    62
    int first_free_edge;
alpar@395
    63
    
deba@782
    64
  public:
alpar@395
    65
    
klao@946
    66
    typedef ListGraphBase Graph;
alpar@397
    67
    
klao@946
    68
    class Node {
marci@975
    69
      friend class ListGraphBase;
klao@946
    70
    protected:
alpar@395
    71
klao@946
    72
      int id;
klao@946
    73
      Node(int pid) { id = pid;}
alpar@395
    74
klao@946
    75
    public:
klao@946
    76
      Node() {}
klao@946
    77
      Node (Invalid) { id = -1; }
klao@946
    78
      bool operator==(const Node& node) const {return id == node.id;}
klao@946
    79
      bool operator!=(const Node& node) const {return id != node.id;}
klao@946
    80
      bool operator<(const Node& node) const {return id < node.id;}
klao@946
    81
    };
deba@782
    82
klao@946
    83
    class Edge {
marci@975
    84
      friend class ListGraphBase;
klao@946
    85
    protected:
deba@782
    86
klao@946
    87
      int id;
klao@946
    88
      Edge(int pid) { id = pid;}
alpar@395
    89
klao@946
    90
    public:
klao@946
    91
      Edge() {}
klao@946
    92
      Edge (Invalid) { id = -1; }
klao@946
    93
      bool operator==(const Edge& edge) const {return id == edge.id;}
klao@946
    94
      bool operator!=(const Edge& edge) const {return id != edge.id;}
klao@946
    95
      bool operator<(const Edge& edge) const {return id < edge.id;}
klao@946
    96
    };
klao@946
    97
klao@946
    98
klao@946
    99
klao@946
   100
    ListGraphBase()
deba@782
   101
      : nodes(), first_node(-1),
deba@782
   102
	first_free_node(-1), edges(), first_free_edge(-1) {}
deba@782
   103
alpar@395
   104
    
alpar@813
   105
    /// Maximum node ID.
alpar@813
   106
    
alpar@813
   107
    /// Maximum node ID.
alpar@813
   108
    ///\sa id(Node)
deba@1791
   109
    int maxNodeId() const { return nodes.size()-1; } 
klao@946
   110
alpar@813
   111
    /// Maximum edge ID.
alpar@813
   112
    
alpar@813
   113
    /// Maximum edge ID.
alpar@813
   114
    ///\sa id(Edge)
deba@1791
   115
    int maxEdgeId() const { return edges.size()-1; }
alpar@395
   116
alpar@986
   117
    Node source(Edge e) const { return edges[e.id].source; }
alpar@986
   118
    Node target(Edge e) const { return edges[e.id].target; }
alpar@395
   119
alpar@395
   120
klao@946
   121
    void first(Node& node) const { 
klao@946
   122
      node.id = first_node;
klao@946
   123
    }
klao@946
   124
klao@946
   125
    void next(Node& node) const {
klao@946
   126
      node.id = nodes[node.id].next;
klao@946
   127
    }
klao@946
   128
klao@946
   129
klao@946
   130
    void first(Edge& e) const { 
klao@946
   131
      int n;
klao@946
   132
      for(n = first_node; 
klao@946
   133
	  n!=-1 && nodes[n].first_in == -1; 
klao@946
   134
	  n = nodes[n].next);
klao@946
   135
      e.id = (n == -1) ? -1 : nodes[n].first_in;
klao@946
   136
    }
klao@946
   137
klao@946
   138
    void next(Edge& edge) const {
klao@946
   139
      if (edges[edge.id].next_in != -1) {
klao@946
   140
	edge.id = edges[edge.id].next_in;
klao@946
   141
      } else {
klao@946
   142
	int n;
alpar@986
   143
	for(n = nodes[edges[edge.id].target].next;
klao@946
   144
	  n!=-1 && nodes[n].first_in == -1; 
klao@946
   145
	  n = nodes[n].next);
klao@946
   146
	edge.id = (n == -1) ? -1 : nodes[n].first_in;
klao@946
   147
      }      
klao@946
   148
    }
klao@946
   149
klao@946
   150
    void firstOut(Edge &e, const Node& v) const {
klao@946
   151
      e.id = nodes[v.id].first_out;
klao@946
   152
    }
klao@946
   153
    void nextOut(Edge &e) const {
klao@946
   154
      e.id=edges[e.id].next_out;
klao@946
   155
    }
klao@946
   156
klao@946
   157
    void firstIn(Edge &e, const Node& v) const {
klao@946
   158
      e.id = nodes[v.id].first_in;
klao@946
   159
    }
klao@946
   160
    void nextIn(Edge &e) const {
klao@946
   161
      e.id=edges[e.id].next_in;
klao@946
   162
    }
klao@946
   163
alpar@813
   164
    
klao@946
   165
    static int id(Node v) { return v.id; }
klao@946
   166
    static int id(Edge e) { return e.id; }
alpar@395
   167
deba@1791
   168
    static Node nodeFromId(int id) { return Node(id);}
deba@1791
   169
    static Edge edgeFromId(int id) { return Edge(id);}
deba@1106
   170
alpar@397
   171
    /// Adds a new node to the graph.
alpar@397
   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:
alpar@1546
   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
    }
alpar@1546
   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@1669
   314
  typedef ErasableGraphExtender<
deba@1669
   315
    ClearableGraphExtender<
deba@1669
   316
    ExtendableGraphExtender<
deba@1669
   317
    MappableGraphExtender<
deba@1669
   318
    IterableGraphExtender<
deba@1791
   319
    AlterableGraphExtender<
deba@1791
   320
    GraphExtender<ListGraphBase> > > > > > > ExtendedListGraphBase;
alpar@400
   321
deba@1718
   322
  /// \addtogroup graphs
deba@1718
   323
  /// @{
alpar@400
   324
alpar@948
   325
  ///A list graph class.
alpar@400
   326
alpar@948
   327
  ///This is a simple and fast erasable graph implementation.
alpar@948
   328
  ///
alpar@1010
   329
  ///It addition that it conforms to the
alpar@1010
   330
  ///\ref concept::ErasableGraph "ErasableGraph" concept,
alpar@1010
   331
  ///it also provides several additional useful extra functionalities.
klao@959
   332
  ///\sa concept::ErasableGraph.
deba@782
   333
deba@1669
   334
  class ListGraph : public ExtendedListGraphBase 
alpar@948
   335
  {
alpar@948
   336
  public:
alpar@1546
   337
    /// Changes the target of \c e to \c n
alpar@948
   338
alpar@1546
   339
    /// Changes the target of \c e to \c n
alpar@948
   340
    ///
alpar@1010
   341
    ///\note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
alpar@1546
   342
    ///referencing the changed edge remain
alpar@1010
   343
    ///valid. However <tt>InEdge</tt>'s are invalidated.
deba@1718
   344
    void changeTarget(Edge e, Node n) { 
deba@1718
   345
      _changeTarget(e,n); 
deba@1718
   346
    }
alpar@1546
   347
    /// Changes the source of \c e to \c n
alpar@948
   348
alpar@1546
   349
    /// Changes the source of \c e to \c n
alpar@948
   350
    ///
alpar@1010
   351
    ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
alpar@1546
   352
    ///referencing the changed edge remain
alpar@1010
   353
    ///valid. However <tt>OutEdge</tt>'s are invalidated.
deba@1718
   354
    void changeSource(Edge e, Node n) { 
deba@1718
   355
      _changeSource(e,n);
deba@1718
   356
    }
alpar@949
   357
alpar@1010
   358
    /// Invert the direction of an edge.
alpar@1010
   359
alpar@1010
   360
    ///\note The <tt>Edge</tt>'s
alpar@1546
   361
    ///referencing the changed edge remain
alpar@1010
   362
    ///valid. However <tt>OutEdge</tt>'s  and <tt>InEdge</tt>'s are invalidated.
alpar@1010
   363
    void reverseEdge(Edge e) {
alpar@1010
   364
      Node t=target(e);
alpar@1546
   365
      _changeTarget(e,source(e));
alpar@1546
   366
      _changeSource(e,t);
alpar@1010
   367
    }
alpar@1010
   368
alpar@1010
   369
    ///Using this it possible to avoid the superfluous memory allocation.
alpar@1010
   370
alpar@949
   371
    ///Using this it possible to avoid the superfluous memory allocation.
alpar@949
   372
    ///\todo more docs...
alpar@949
   373
    void reserveEdge(int n) { edges.reserve(n); };
alpar@1010
   374
alpar@1010
   375
    ///Contract two nodes.
alpar@1010
   376
alpar@1010
   377
    ///This function contracts two nodes.
alpar@1010
   378
    ///
alpar@1010
   379
    ///Node \p b will be removed but instead of deleting
alpar@1010
   380
    ///its neighboring edges, they will be joined to \p a.
alpar@1010
   381
    ///The last parameter \p r controls whether to remove loops. \c true
alpar@1010
   382
    ///means that loops will be removed.
alpar@1010
   383
    ///
alpar@1010
   384
    ///\note The <tt>Edge</tt>s
alpar@1281
   385
    ///referencing a moved edge remain
alpar@1010
   386
    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
alpar@1010
   387
    ///may be invalidated.
deba@1718
   388
    void contract(Node a, Node b, bool r = true) 
alpar@1010
   389
    {
alpar@1010
   390
      for(OutEdgeIt e(*this,b);e!=INVALID;) {
alpar@1010
   391
	OutEdgeIt f=e;
alpar@1010
   392
	++f;
alpar@1010
   393
	if(r && target(e)==a) erase(e);
alpar@1546
   394
	else changeSource(e,a);
alpar@1010
   395
	e=f;
alpar@1010
   396
      }
alpar@1010
   397
      for(InEdgeIt e(*this,b);e!=INVALID;) {
alpar@1010
   398
	InEdgeIt f=e;
alpar@1010
   399
	++f;
alpar@1010
   400
	if(r && source(e)==a) erase(e);
alpar@1546
   401
	else changeTarget(e,a);
alpar@1010
   402
	e=f;
alpar@1010
   403
      }
alpar@1010
   404
      erase(b);
alpar@1010
   405
    }
alpar@1011
   406
alpar@1281
   407
    ///Split a node.
alpar@1011
   408
alpar@1284
   409
    ///This function splits a node. First a new node is added to the graph,
alpar@1284
   410
    ///then the source of each outgoing edge of \c n is moved to this new node.
alpar@1281
   411
    ///If \c connect is \c true (this is the default value), then a new edge
alpar@1281
   412
    ///from \c n to the newly created node is also added.
alpar@1281
   413
    ///\return The newly created node.
alpar@1281
   414
    ///
alpar@1281
   415
    ///\note The <tt>Edge</tt>s
alpar@1281
   416
    ///referencing a moved edge remain
alpar@1281
   417
    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
alpar@1281
   418
    ///may be invalidated.
alpar@1770
   419
    ///\warning This functionality cannot be used together with the Snapshot
alpar@1284
   420
    ///feature.
alpar@1281
   421
    ///\todo It could be implemented in a bit faster way.
alpar@1281
   422
    Node split(Node n, bool connect = true) 
alpar@1281
   423
    {
alpar@1281
   424
      Node b = addNode();
alpar@1281
   425
      for(OutEdgeIt e(*this,n);e!=INVALID;) {
alpar@1281
   426
 	OutEdgeIt f=e;
alpar@1281
   427
	++f;
alpar@1546
   428
	changeSource(e,b);
alpar@1281
   429
	e=f;
alpar@1281
   430
      }
alpar@1281
   431
      if(connect) addEdge(n,b);
alpar@1281
   432
      return b;
alpar@1281
   433
    }
alpar@1281
   434
      
alpar@1812
   435
    ///Split an edge.
alpar@1812
   436
alpar@1812
   437
    ///This function splits an edge. First a new node \c b is added to the graph,
alpar@1812
   438
    ///then the original edge is re-targetes to \c b. Finally an edge
alpar@1812
   439
    ///from \c b to the original target is added.
alpar@1812
   440
    ///\return The newly created node.
alpar@1812
   441
    ///\warning This functionality cannot be used together with the Snapshot
alpar@1812
   442
    ///feature.
alpar@1812
   443
    Node split(Edge e) 
alpar@1812
   444
    {
alpar@1812
   445
      Node b = addNode();
alpar@1812
   446
      addEdge(b,target(e));
alpar@1812
   447
      changeTarget(e,b);
alpar@1812
   448
      return b;
alpar@1812
   449
    }
alpar@1812
   450
      
alpar@1011
   451
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   452
alpar@1011
   453
    ///Class to make a snapshot of the graph and to restrore to it later.
alpar@1011
   454
    ///
alpar@1011
   455
    ///The newly added nodes and edges can be removed using the
alpar@1011
   456
    ///restore() function.
alpar@1011
   457
    ///
alpar@1011
   458
    ///\warning Edge and node deletions cannot be restored.
alpar@1770
   459
    ///\warning Snapshots cannot be nested.
alpar@1770
   460
    class Snapshot : protected AlterationNotifier<Node>::ObserverBase,
deba@1039
   461
		     protected AlterationNotifier<Edge>::ObserverBase
alpar@1011
   462
    {
deba@1774
   463
    public:
deba@1774
   464
      
deba@1774
   465
      class UnsupportedOperation : public LogicError {
deba@1774
   466
      public:
deba@1774
   467
	virtual const char* exceptionName() const {
deba@1774
   468
	  return "lemon::ListGraph::Snapshot::UnsupportedOperation";
deba@1774
   469
	}
deba@1774
   470
      };
deba@1774
   471
            
deba@1774
   472
alpar@1011
   473
      protected:
alpar@1011
   474
      
alpar@1011
   475
      ListGraph *g;
alpar@1011
   476
      std::list<Node> added_nodes;
alpar@1011
   477
      std::list<Edge> added_edges;
alpar@1011
   478
      
alpar@1011
   479
      bool active;
alpar@1011
   480
      virtual void add(const Node& n) {
alpar@1011
   481
	added_nodes.push_back(n);
alpar@1011
   482
      };
alpar@1011
   483
      virtual void erase(const Node&) 
alpar@1011
   484
      {
deba@1774
   485
	throw UnsupportedOperation();
alpar@1011
   486
      }
alpar@1011
   487
      virtual void add(const Edge& n) {
alpar@1011
   488
	added_edges.push_back(n);
alpar@1011
   489
      };
alpar@1011
   490
      virtual void erase(const Edge&) 
alpar@1011
   491
      {
deba@1774
   492
	throw UnsupportedOperation();
alpar@1011
   493
      }
alpar@1011
   494
alpar@1457
   495
      ///\bug What is this used for?
alpar@1457
   496
      ///
alpar@1457
   497
      virtual void build() {}
alpar@1457
   498
      ///\bug What is this used for?
alpar@1457
   499
      ///
alpar@1457
   500
      virtual void clear() {}
alpar@1457
   501
alpar@1011
   502
      void regist(ListGraph &_g) {
alpar@1011
   503
	g=&_g;
deba@1039
   504
	AlterationNotifier<Node>::ObserverBase::
deba@1040
   505
	  attach(g->getNotifier(Node()));
deba@1039
   506
	AlterationNotifier<Edge>::ObserverBase::
deba@1040
   507
	  attach(g->getNotifier(Edge()));
alpar@1011
   508
      }
alpar@1011
   509
            
alpar@1011
   510
      void deregist() {
deba@1039
   511
	AlterationNotifier<Node>::ObserverBase::
alpar@1011
   512
	  detach();
deba@1039
   513
	AlterationNotifier<Edge>::ObserverBase::
alpar@1011
   514
	  detach();
alpar@1011
   515
	g=0;
alpar@1011
   516
      }
deba@1774
   517
alpar@1011
   518
    public:
alpar@1011
   519
      ///Default constructur.
alpar@1011
   520
      
alpar@1011
   521
      ///Default constructur.
alpar@1011
   522
      ///To actually make a snapshot you must call save().
alpar@1011
   523
      ///
alpar@1770
   524
      Snapshot() : g(0) {}
alpar@1011
   525
      ///Constructor that immediately makes a snapshot.
alpar@1011
   526
      
alpar@1011
   527
      ///This constructor immediately makes a snapshot of the graph.
alpar@1011
   528
      ///\param _g The graph we make a snapshot of.
alpar@1770
   529
      Snapshot(ListGraph &_g) {
alpar@1011
   530
	regist(_g);
alpar@1011
   531
      }
alpar@1011
   532
      ///\bug Is it necessary?
alpar@1011
   533
      ///
alpar@1770
   534
      ~Snapshot() 
alpar@1011
   535
      {
alpar@1011
   536
	if(g) deregist();
alpar@1011
   537
      }
alpar@1011
   538
      
alpar@1011
   539
      ///Make a snapshot.
alpar@1011
   540
alpar@1011
   541
      ///Make a snapshot of the graph.
alpar@1011
   542
      ///
alpar@1011
   543
      ///This function can be called more than once. In case of a repeated
alpar@1011
   544
      ///call, the previous snapshot gets lost.
alpar@1011
   545
      ///\param _g The graph we make the snapshot of.
alpar@1011
   546
      void save(ListGraph &_g) 
alpar@1011
   547
      {
alpar@1011
   548
	if(g!=&_g) {
alpar@1011
   549
	  if(g) deregist();
alpar@1011
   550
	  regist(_g);
alpar@1011
   551
	}
alpar@1011
   552
	added_nodes.clear();
alpar@1011
   553
	added_edges.clear();
alpar@1011
   554
      }
alpar@1011
   555
      
alpar@1011
   556
    ///Undo the changes until the last snapshot.
alpar@1011
   557
alpar@1011
   558
    ///Undo the changes until last snapshot created by save().
alpar@1011
   559
    ///
alpar@1011
   560
    ///\todo This function might be called undo().
alpar@1011
   561
      void restore() {
alpar@1457
   562
	ListGraph &old_g=*g;
alpar@1011
   563
	deregist();
alpar@1011
   564
	while(!added_edges.empty()) {
alpar@1457
   565
	  old_g.erase(added_edges.front());
alpar@1011
   566
	  added_edges.pop_front();
alpar@1011
   567
	}
alpar@1011
   568
 	while(!added_nodes.empty()) {
alpar@1457
   569
	  old_g.erase(added_nodes.front());
alpar@1011
   570
	  added_nodes.pop_front();
alpar@1011
   571
	}
alpar@1011
   572
      }
alpar@1011
   573
    };
alpar@1011
   574
    
alpar@949
   575
  };
klao@1034
   576
alpar@1555
   577
  ///@}
klao@1034
   578
klao@1034
   579
  /**************** Undirected List Graph ****************/
klao@1034
   580
klao@1909
   581
  typedef ErasableUGraphExtender<
klao@1909
   582
    ClearableUGraphExtender<
klao@1909
   583
    ExtendableUGraphExtender<
klao@1909
   584
    MappableUGraphExtender<
klao@1909
   585
    IterableUGraphExtender<
klao@1909
   586
    AlterableUGraphExtender<
klao@1909
   587
    UGraphExtender<ListGraphBase> > > > > > > ExtendedListUGraphBase;
klao@1034
   588
deba@1718
   589
  /// \addtogroup graphs
deba@1718
   590
  /// @{
alpar@1555
   591
alpar@1035
   592
  ///An undirected list graph class.
alpar@1035
   593
alpar@1035
   594
  ///This is a simple and fast erasable undirected graph implementation.
alpar@1035
   595
  ///
alpar@1035
   596
  ///It conforms to the
klao@1909
   597
  ///\ref concept::UGraph "UGraph" concept.
alpar@1035
   598
  ///
klao@1909
   599
  ///\sa concept::UGraph.
alpar@1035
   600
  ///
alpar@1770
   601
  ///\todo Snapshot, reverseEdge(), changeTarget(), changeSource(), contract()
alpar@1161
   602
  ///haven't been implemented yet.
alpar@1035
   603
  ///
klao@1909
   604
  class ListUGraph : public ExtendedListUGraphBase {
deba@1718
   605
  public:
klao@1909
   606
    typedef ExtendedListUGraphBase Parent;
deba@1718
   607
    /// \brief Changes the target of \c e to \c n
deba@1718
   608
    ///
deba@1718
   609
    /// Changes the target of \c e to \c n
deba@1718
   610
    ///
deba@1718
   611
    /// \note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
deba@1718
   612
    /// referencing the changed edge remain
deba@1718
   613
    /// valid. However <tt>InEdge</tt>'s are invalidated.
klao@1909
   614
    void changeTarget(UEdge e, Node n) { 
deba@1718
   615
      _changeTarget(e,n); 
deba@1718
   616
    }
deba@1718
   617
    /// Changes the source of \c e to \c n
deba@1718
   618
    ///
deba@1718
   619
    /// Changes the source of \c e to \c n
deba@1718
   620
    ///
deba@1718
   621
    ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
deba@1718
   622
    ///referencing the changed edge remain
deba@1718
   623
    ///valid. However <tt>OutEdge</tt>'s are invalidated.
klao@1909
   624
    void changeSource(UEdge e, Node n) { 
deba@1718
   625
      _changeSource(e,n); 
deba@1718
   626
    }
deba@1718
   627
    /// \brief Contract two nodes.
deba@1718
   628
    ///
deba@1718
   629
    /// This function contracts two nodes.
deba@1718
   630
    ///
deba@1718
   631
    /// Node \p b will be removed but instead of deleting
deba@1718
   632
    /// its neighboring edges, they will be joined to \p a.
deba@1718
   633
    /// The last parameter \p r controls whether to remove loops. \c true
deba@1718
   634
    /// means that loops will be removed.
deba@1718
   635
    ///
deba@1718
   636
    /// \note The <tt>Edge</tt>s
deba@1718
   637
    /// referencing a moved edge remain
deba@1718
   638
    /// valid.
deba@1718
   639
    void contract(Node a, Node b, bool r = true) {
deba@1718
   640
      for(IncEdgeIt e(*this, b); e!=INVALID;) {
deba@1718
   641
	IncEdgeIt f = e; ++f;
deba@1718
   642
	if (r && runningNode(e) == a) {
deba@1718
   643
	  erase(e);
deba@1718
   644
	} else if (source(e) == b) {
deba@1718
   645
	  changeSource(e, a);
deba@1718
   646
	} else {
deba@1718
   647
	  changeTarget(e, a);
deba@1718
   648
	}
deba@1718
   649
	e = f;
deba@1718
   650
      }
deba@1718
   651
      erase(b);
deba@1718
   652
    }
klao@1034
   653
  };
klao@1034
   654
alpar@949
   655
  
alpar@948
   656
  /// @}  
alpar@948
   657
} //namespace lemon
klao@946
   658
  
alpar@400
   659
klao@946
   660
#endif