COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/list_graph.h @ 1040:372f08e8f403

Last change on this file since 1040:372f08e8f403 was 1040:372f08e8f403, checked in by Balazs Dezso, 19 years ago

AlterationObserverRegistry? -> AlterationNotifier?
third step

File size: 13.4 KB
RevLine 
[948]1/* -*- C++ -*-
2 * src/lemon/list_graph.h - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
[395]16
[921]17#ifndef LEMON_LIST_GRAPH_H
18#define LEMON_LIST_GRAPH_H
[395]19
[948]20///\ingroup graphs
21///\file
22///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes.
23
[946]24#include <lemon/erasable_graph_extender.h>
25#include <lemon/clearable_graph_extender.h>
26#include <lemon/extendable_graph_extender.h>
[1034]27#include <lemon/iterable_graph_extender.h>
[1039]28#include <lemon/alteration_notifier.h>
[1034]29#include <lemon/default_map.h>
[395]30
[1034]31#include <lemon/undir_graph_extender.h>
[782]32
[1011]33#include <list>
[782]34
[921]35namespace lemon {
[395]36
[946]37  class ListGraphBase {
[406]38
[949]39  protected:
[946]40    struct NodeT {
[397]41      int first_in,first_out;
42      int prev, next;
[395]43    };
[946]44 
45    struct EdgeT {
[986]46      int target, source;
[397]47      int prev_in, prev_out;
48      int next_in, next_out;
[395]49    };
50
51    std::vector<NodeT> nodes;
[946]52
[397]53    int first_node;
[946]54
[397]55    int first_free_node;
[946]56
[395]57    std::vector<EdgeT> edges;
[946]58
[397]59    int first_free_edge;
[395]60   
[782]61  public:
[395]62   
[946]63    typedef ListGraphBase Graph;
[397]64   
[946]65    class Node {
[975]66      friend class ListGraphBase;
[946]67    protected:
[395]68
[946]69      int id;
70      Node(int pid) { id = pid;}
[395]71
[946]72    public:
73      Node() {}
74      Node (Invalid) { id = -1; }
75      bool operator==(const Node& node) const {return id == node.id;}
76      bool operator!=(const Node& node) const {return id != node.id;}
77      bool operator<(const Node& node) const {return id < node.id;}
78    };
[782]79
[946]80    class Edge {
[975]81      friend class ListGraphBase;
[946]82    protected:
[782]83
[946]84      int id;
85      Edge(int pid) { id = pid;}
[395]86
[946]87    public:
88      Edge() {}
89      Edge (Invalid) { id = -1; }
90      bool operator==(const Edge& edge) const {return id == edge.id;}
91      bool operator!=(const Edge& edge) const {return id != edge.id;}
92      bool operator<(const Edge& edge) const {return id < edge.id;}
93    };
94
95
96
97    ListGraphBase()
[782]98      : nodes(), first_node(-1),
99        first_free_node(-1), edges(), first_free_edge(-1) {}
100
[395]101   
[813]102    /// Maximum node ID.
103   
104    /// Maximum node ID.
105    ///\sa id(Node)
[980]106    int maxId(Node = INVALID) const { return nodes.size()-1; }
[946]107
[813]108    /// Maximum edge ID.
109   
110    /// Maximum edge ID.
111    ///\sa id(Edge)
[980]112    int maxId(Edge = INVALID) const { return edges.size()-1; }
[395]113
[986]114    Node source(Edge e) const { return edges[e.id].source; }
115    Node target(Edge e) const { return edges[e.id].target; }
[395]116
117
[946]118    void first(Node& node) const {
119      node.id = first_node;
120    }
121
122    void next(Node& node) const {
123      node.id = nodes[node.id].next;
124    }
125
126
127    void first(Edge& e) const {
128      int n;
129      for(n = first_node;
130          n!=-1 && nodes[n].first_in == -1;
131          n = nodes[n].next);
132      e.id = (n == -1) ? -1 : nodes[n].first_in;
133    }
134
135    void next(Edge& edge) const {
136      if (edges[edge.id].next_in != -1) {
137        edge.id = edges[edge.id].next_in;
138      } else {
139        int n;
[986]140        for(n = nodes[edges[edge.id].target].next;
[946]141          n!=-1 && nodes[n].first_in == -1;
142          n = nodes[n].next);
143        edge.id = (n == -1) ? -1 : nodes[n].first_in;
144      }     
145    }
146
147    void firstOut(Edge &e, const Node& v) const {
148      e.id = nodes[v.id].first_out;
149    }
150    void nextOut(Edge &e) const {
151      e.id=edges[e.id].next_out;
152    }
153
154    void firstIn(Edge &e, const Node& v) const {
155      e.id = nodes[v.id].first_in;
156    }
157    void nextIn(Edge &e) const {
158      e.id=edges[e.id].next_in;
159    }
160
[813]161   
[946]162    static int id(Node v) { return v.id; }
163    static int id(Edge e) { return e.id; }
[395]164
[397]165    /// Adds a new node to the graph.
166
[813]167    /// \warning It adds the new node to the front of the list.
[397]168    /// (i.e. the lastly added node becomes the first.)
[946]169    Node addNode() {     
[397]170      int n;
171     
[946]172      if(first_free_node==-1) {
173        n = nodes.size();
174        nodes.push_back(NodeT());
175      } else {
[397]176        n = first_free_node;
177        first_free_node = nodes[n].next;
178      }
179     
180      nodes[n].next = first_node;
181      if(first_node != -1) nodes[first_node].prev = n;
182      first_node = n;
183      nodes[n].prev = -1;
184     
185      nodes[n].first_in = nodes[n].first_out = -1;
186     
[946]187      return Node(n);
[395]188    }
189   
190    Edge addEdge(Node u, Node v) {
[946]191      int n;     
192
193      if (first_free_edge == -1) {
194        n = edges.size();
195        edges.push_back(EdgeT());
196      } else {
[397]197        n = first_free_edge;
198        first_free_edge = edges[n].next_in;
199      }
200     
[986]201      edges[n].source = u.id;
202      edges[n].target = v.id;
[395]203
[946]204      edges[n].next_out = nodes[u.id].first_out;
205      if(nodes[u.id].first_out != -1) {
206        edges[nodes[u.id].first_out].prev_out = n;
207      }
208     
209      edges[n].next_in = nodes[v.id].first_in;
210      if(nodes[v.id].first_in != -1) {
211        edges[nodes[v.id].first_in].prev_in = n;
212      }
213     
[397]214      edges[n].prev_in = edges[n].prev_out = -1;
215       
[946]216      nodes[u.id].first_out = nodes[v.id].first_in = n;
[397]217
[946]218      return Edge(n);
[395]219    }
[774]220   
[946]221    void erase(const Node& node) {
222      int n = node.id;
223     
224      if(nodes[n].next != -1) {
225        nodes[nodes[n].next].prev = nodes[n].prev;
226      }
227     
228      if(nodes[n].prev != -1) {
229        nodes[nodes[n].prev].next = nodes[n].next;
230      } else {
231        first_node = nodes[n].next;
232      }
233     
234      nodes[n].next = first_free_node;
235      first_free_node = n;
[395]236
[774]237    }
238   
[946]239    void erase(const Edge& edge) {
240      int n = edge.id;
[397]241     
[946]242      if(edges[n].next_in!=-1) {
[397]243        edges[edges[n].next_in].prev_in = edges[n].prev_in;
[946]244      }
245
246      if(edges[n].prev_in!=-1) {
[397]247        edges[edges[n].prev_in].next_in = edges[n].next_in;
[946]248      } else {
[986]249        nodes[edges[n].target].first_in = edges[n].next_in;
[946]250      }
251
[397]252     
[946]253      if(edges[n].next_out!=-1) {
[397]254        edges[edges[n].next_out].prev_out = edges[n].prev_out;
[946]255      }
256
257      if(edges[n].prev_out!=-1) {
[397]258        edges[edges[n].prev_out].next_out = edges[n].next_out;
[946]259      } else {
[986]260        nodes[edges[n].source].first_out = edges[n].next_out;
[946]261      }
[397]262     
263      edges[n].next_in = first_free_edge;
[695]264      first_free_edge = n;     
[397]265
266    }
267
268    void clear() {
[782]269      edges.clear();
270      nodes.clear();
[946]271      first_node = first_free_node = first_free_edge = -1;
[937]272    }
273
[949]274  protected:
[986]275    void _moveTarget(Edge e, Node n)
[949]276    {
277      if(edges[e.id].next_in != -1)
278        edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
279      if(edges[e.id].prev_in != -1)
280        edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
[986]281      else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
282      edges[e.id].target = n.id;
[949]283      edges[e.id].prev_in = -1;
284      edges[e.id].next_in = nodes[n.id].first_in;
285      nodes[n.id].first_in = e.id;
286    }
[986]287    void _moveSource(Edge e, Node n)
[949]288    {
289      if(edges[e.id].next_out != -1)
290        edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
291      if(edges[e.id].prev_out != -1)
292        edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
[986]293      else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
294      edges[e.id].source = n.id;
[949]295      edges[e.id].prev_out = -1;
296      edges[e.id].next_out = nodes[n.id].first_out;
297      nodes[n.id].first_out = e.id;
298    }
299
[919]300  };
[909]301
[946]302  typedef AlterableGraphExtender<ListGraphBase> AlterableListGraphBase;
303  typedef IterableGraphExtender<AlterableListGraphBase> IterableListGraphBase;
[980]304  typedef DefaultMappableGraphExtender<IterableListGraphBase> MappableListGraphBase;
[946]305  typedef ExtendableGraphExtender<MappableListGraphBase> ExtendableListGraphBase;
306  typedef ClearableGraphExtender<ExtendableListGraphBase> ClearableListGraphBase;
307  typedef ErasableGraphExtender<ClearableListGraphBase> ErasableListGraphBase;
[400]308
[948]309/// \addtogroup graphs
310/// @{
[400]311
[948]312  ///A list graph class.
[400]313
[948]314  ///This is a simple and fast erasable graph implementation.
315  ///
[1010]316  ///It addition that it conforms to the
317  ///\ref concept::ErasableGraph "ErasableGraph" concept,
318  ///it also provides several additional useful extra functionalities.
[959]319  ///\sa concept::ErasableGraph.
[782]320
[948]321  class ListGraph : public ErasableListGraphBase
322  {
323  public:
[986]324    /// Moves the target of \c e to \c n
[948]325
[986]326    /// Moves the target of \c e to \c n
[948]327    ///
[1010]328    ///\note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
329    ///referencing the moved edge remain
330    ///valid. However <tt>InEdge</tt>'s are invalidated.
[986]331    void moveTarget(Edge e, Node n) { _moveTarget(e,n); }
332    /// Moves the source of \c e to \c n
[948]333
[986]334    /// Moves the source of \c e to \c n
[948]335    ///
[1010]336    ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
337    ///referencing the moved edge remain
338    ///valid. However <tt>OutEdge</tt>'s are invalidated.
[986]339    void moveSource(Edge e, Node n) { _moveSource(e,n); }
[949]340
[1010]341    /// Invert the direction of an edge.
342
343    ///\note The <tt>Edge</tt>'s
344    ///referencing the moved edge remain
345    ///valid. However <tt>OutEdge</tt>'s  and <tt>InEdge</tt>'s are invalidated.
346    void reverseEdge(Edge e) {
347      Node t=target(e);
348      _moveTarget(e,source(e));
349      _moveSource(e,t);
350    }
351
352    ///Using this it possible to avoid the superfluous memory allocation.
353
[949]354    ///Using this it possible to avoid the superfluous memory allocation.
355    ///\todo more docs...
356    void reserveEdge(int n) { edges.reserve(n); };
[1010]357
358    ///Contract two nodes.
359
360    ///This function contracts two nodes.
361    ///
362    ///Node \p b will be removed but instead of deleting
363    ///its neighboring edges, they will be joined to \p a.
364    ///The last parameter \p r controls whether to remove loops. \c true
365    ///means that loops will be removed.
366    ///
367    ///\note The <tt>Edge</tt>s
368    ///referencing the moved edge remain
369    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
370    ///may be invalidated.
371    void contract(Node a,Node b,bool r=true)
372    {
373      for(OutEdgeIt e(*this,b);e!=INVALID;) {
374        OutEdgeIt f=e;
375        ++f;
376        if(r && target(e)==a) erase(e);
377        else moveSource(e,b);
378        e=f;
379      }
380      for(InEdgeIt e(*this,b);e!=INVALID;) {
381        InEdgeIt f=e;
382        ++f;
383        if(r && source(e)==a) erase(e);
384        else moveTarget(e,b);
385        e=f;
386      }
387      erase(b);
388    }
[1011]389
390
391    ///Class to make a snapshot of the graph and to restrore to it later.
392
393    ///Class to make a snapshot of the graph and to restrore to it later.
394    ///
395    ///The newly added nodes and edges can be removed using the
396    ///restore() function.
397    ///
398    ///\warning Edge and node deletions cannot be restored.
399    ///\warning SnapShots cannot be nested.
400    ///\ingroup graphs
[1035]401    ///\todo \c SnapShot or \c Snapshot?
[1039]402    class SnapShot : protected AlterationNotifier<Node>::ObserverBase,
403                     protected AlterationNotifier<Edge>::ObserverBase
[1011]404    {
405      protected:
406     
407      ListGraph *g;
408      std::list<Node> added_nodes;
409      std::list<Edge> added_edges;
410     
411      bool active;
412      virtual void add(const Node& n) {
413        added_nodes.push_back(n);
414      };
415      ///\bug Exception...
416      ///
417      virtual void erase(const Node&)
418      {
419        exit(1);
420      }
421      virtual void add(const Edge& n) {
422        added_edges.push_back(n);
423      };
424      ///\bug Exception...
425      ///
426      virtual void erase(const Edge&)
427      {
428        exit(1);
429      }
430
431      void regist(ListGraph &_g) {
432        g=&_g;
[1039]433        AlterationNotifier<Node>::ObserverBase::
[1040]434          attach(g->getNotifier(Node()));
[1039]435        AlterationNotifier<Edge>::ObserverBase::
[1040]436          attach(g->getNotifier(Edge()));
[1011]437      }
438           
439      void deregist() {
[1039]440        AlterationNotifier<Node>::ObserverBase::
[1011]441          detach();
[1039]442        AlterationNotifier<Edge>::ObserverBase::
[1011]443          detach();
444        g=0;
445      }
446           
447    public:
448      ///Default constructur.
449     
450      ///Default constructur.
451      ///To actually make a snapshot you must call save().
452      ///
453      SnapShot() : g(0) {}
454      ///Constructor that immediately makes a snapshot.
455     
456      ///This constructor immediately makes a snapshot of the graph.
457      ///\param _g The graph we make a snapshot of.
458      SnapShot(ListGraph &_g) {
459        regist(_g);
460      }
461      ///\bug Is it necessary?
462      ///
463      ~SnapShot()
464      {
465        if(g) deregist();
466      }
467     
468      ///Make a snapshot.
469
470      ///Make a snapshot of the graph.
471      ///
472      ///This function can be called more than once. In case of a repeated
473      ///call, the previous snapshot gets lost.
474      ///\param _g The graph we make the snapshot of.
475      void save(ListGraph &_g)
476      {
477        if(g!=&_g) {
478          if(g) deregist();
479          regist(_g);
480        }
481        added_nodes.clear();
482        added_edges.clear();
483      }
484     
485    ///Undo the changes until the last snapshot.
486
487    ///Undo the changes until last snapshot created by save().
488    ///
489    ///\todo This function might be called undo().
490      void restore() {
491        deregist();
492        while(!added_edges.empty()) {
493          g->erase(added_edges.front());
494          added_edges.pop_front();
495        }
496        while(!added_nodes.empty()) {
497          g->erase(added_nodes.front());
498          added_nodes.pop_front();
499        }
500      }
501    };
502   
[949]503  };
[1034]504
505
506  /**************** Undirected List Graph ****************/
507
508  typedef ErasableUndirGraphExtender<
509    ClearableUndirGraphExtender<
510    ExtendableUndirGraphExtender<
511    MappableUndirGraphExtender<
512    IterableUndirGraphExtender<
513    AlterableUndirGraphExtender<
514    UndirGraphExtender<ListGraphBase> > > > > > > ErasableUndirListGraphBase;
515
[1035]516  ///An undirected list graph class.
517
518  ///This is a simple and fast erasable undirected graph implementation.
519  ///
520  ///It conforms to the
521  ///\ref concept::UndirGraph "UndirGraph" concept.
522  ///
523  ///\sa concept::UndirGraph.
524  ///
525  ///\todo SnapShot hasn't been implemented yet.
526  ///
[1034]527  class UndirListGraph : public ErasableUndirListGraphBase {
528  };
529
[949]530 
[948]531  /// @} 
532} //namespace lemon
[946]533 
[400]534
[946]535#endif
Note: See TracBrowser for help on using the repository browser.