COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/list_graph.h @ 1790:c7dd9d8c770a

Last change on this file since 1790:c7dd9d8c770a was 1774:9fd56d75293e, checked in by Balazs Dezso, 19 years ago

UnsupportedException? on erase with Snapshot

File size: 16.2 KB
RevLine 
[948]1/* -*- C++ -*-
[1435]2 * lemon/list_graph.h - Part of LEMON, a generic C++ optimization library
[948]3 *
[1164]4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[948]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
[1692]22///\brief ListGraph, UndirListGraph classes.
[948]23
[1307]24#include <lemon/bits/erasable_graph_extender.h>
25#include <lemon/bits/clearable_graph_extender.h>
26#include <lemon/bits/extendable_graph_extender.h>
27#include <lemon/bits/iterable_graph_extender.h>
28#include <lemon/bits/alteration_notifier.h>
29#include <lemon/bits/default_map.h>
[395]30
[1307]31#include <lemon/bits/undir_graph_extender.h>
[782]32
[1774]33#include <lemon/error.h>
34
[1011]35#include <list>
[782]36
[921]37namespace lemon {
[395]38
[946]39  class ListGraphBase {
[406]40
[949]41  protected:
[946]42    struct NodeT {
[1470]43      int first_in, first_out;
[397]44      int prev, next;
[395]45    };
[946]46 
47    struct EdgeT {
[986]48      int target, source;
[397]49      int prev_in, prev_out;
50      int next_in, next_out;
[395]51    };
52
53    std::vector<NodeT> nodes;
[946]54
[397]55    int first_node;
[946]56
[397]57    int first_free_node;
[946]58
[395]59    std::vector<EdgeT> edges;
[946]60
[397]61    int first_free_edge;
[395]62   
[782]63  public:
[395]64   
[946]65    typedef ListGraphBase Graph;
[397]66   
[946]67    class Node {
[975]68      friend class ListGraphBase;
[946]69    protected:
[395]70
[946]71      int id;
72      Node(int pid) { id = pid;}
[395]73
[946]74    public:
75      Node() {}
76      Node (Invalid) { id = -1; }
77      bool operator==(const Node& node) const {return id == node.id;}
78      bool operator!=(const Node& node) const {return id != node.id;}
79      bool operator<(const Node& node) const {return id < node.id;}
80    };
[782]81
[946]82    class Edge {
[975]83      friend class ListGraphBase;
[946]84    protected:
[782]85
[946]86      int id;
87      Edge(int pid) { id = pid;}
[395]88
[946]89    public:
90      Edge() {}
91      Edge (Invalid) { id = -1; }
92      bool operator==(const Edge& edge) const {return id == edge.id;}
93      bool operator!=(const Edge& edge) const {return id != edge.id;}
94      bool operator<(const Edge& edge) const {return id < edge.id;}
95    };
96
97
98
99    ListGraphBase()
[782]100      : nodes(), first_node(-1),
101        first_free_node(-1), edges(), first_free_edge(-1) {}
102
[395]103   
[813]104    /// Maximum node ID.
105   
106    /// Maximum node ID.
107    ///\sa id(Node)
[980]108    int maxId(Node = INVALID) const { return nodes.size()-1; }
[946]109
[813]110    /// Maximum edge ID.
111   
112    /// Maximum edge ID.
113    ///\sa id(Edge)
[980]114    int maxId(Edge = INVALID) const { return edges.size()-1; }
[395]115
[986]116    Node source(Edge e) const { return edges[e.id].source; }
117    Node target(Edge e) const { return edges[e.id].target; }
[395]118
119
[946]120    void first(Node& node) const {
121      node.id = first_node;
122    }
123
124    void next(Node& node) const {
125      node.id = nodes[node.id].next;
126    }
127
128
129    void first(Edge& e) const {
130      int n;
131      for(n = first_node;
132          n!=-1 && nodes[n].first_in == -1;
133          n = nodes[n].next);
134      e.id = (n == -1) ? -1 : nodes[n].first_in;
135    }
136
137    void next(Edge& edge) const {
138      if (edges[edge.id].next_in != -1) {
139        edge.id = edges[edge.id].next_in;
140      } else {
141        int n;
[986]142        for(n = nodes[edges[edge.id].target].next;
[946]143          n!=-1 && nodes[n].first_in == -1;
144          n = nodes[n].next);
145        edge.id = (n == -1) ? -1 : nodes[n].first_in;
146      }     
147    }
148
149    void firstOut(Edge &e, const Node& v) const {
150      e.id = nodes[v.id].first_out;
151    }
152    void nextOut(Edge &e) const {
153      e.id=edges[e.id].next_out;
154    }
155
156    void firstIn(Edge &e, const Node& v) const {
157      e.id = nodes[v.id].first_in;
158    }
159    void nextIn(Edge &e) const {
160      e.id=edges[e.id].next_in;
161    }
162
[813]163   
[946]164    static int id(Node v) { return v.id; }
165    static int id(Edge e) { return e.id; }
[395]166
[1106]167    static Node fromId(int id, Node) { return Node(id);}
168    static Edge fromId(int id, Edge) { return Edge(id);}
169
[397]170    /// Adds a new node to the graph.
171
[813]172    /// \warning It adds the new node to the front of the list.
[397]173    /// (i.e. the lastly added node becomes the first.)
[946]174    Node addNode() {     
[397]175      int n;
176     
[946]177      if(first_free_node==-1) {
178        n = nodes.size();
179        nodes.push_back(NodeT());
180      } else {
[397]181        n = first_free_node;
182        first_free_node = nodes[n].next;
183      }
184     
185      nodes[n].next = first_node;
186      if(first_node != -1) nodes[first_node].prev = n;
187      first_node = n;
188      nodes[n].prev = -1;
189     
190      nodes[n].first_in = nodes[n].first_out = -1;
191     
[946]192      return Node(n);
[395]193    }
194   
195    Edge addEdge(Node u, Node v) {
[946]196      int n;     
197
198      if (first_free_edge == -1) {
199        n = edges.size();
200        edges.push_back(EdgeT());
201      } else {
[397]202        n = first_free_edge;
203        first_free_edge = edges[n].next_in;
204      }
205     
[986]206      edges[n].source = u.id;
207      edges[n].target = v.id;
[395]208
[946]209      edges[n].next_out = nodes[u.id].first_out;
210      if(nodes[u.id].first_out != -1) {
211        edges[nodes[u.id].first_out].prev_out = n;
212      }
213     
214      edges[n].next_in = nodes[v.id].first_in;
215      if(nodes[v.id].first_in != -1) {
216        edges[nodes[v.id].first_in].prev_in = n;
217      }
218     
[397]219      edges[n].prev_in = edges[n].prev_out = -1;
220       
[946]221      nodes[u.id].first_out = nodes[v.id].first_in = n;
[397]222
[946]223      return Edge(n);
[395]224    }
[774]225   
[946]226    void erase(const Node& node) {
227      int n = node.id;
228     
229      if(nodes[n].next != -1) {
230        nodes[nodes[n].next].prev = nodes[n].prev;
231      }
232     
233      if(nodes[n].prev != -1) {
234        nodes[nodes[n].prev].next = nodes[n].next;
235      } else {
236        first_node = nodes[n].next;
237      }
238     
239      nodes[n].next = first_free_node;
240      first_free_node = n;
[395]241
[774]242    }
243   
[946]244    void erase(const Edge& edge) {
245      int n = edge.id;
[397]246     
[946]247      if(edges[n].next_in!=-1) {
[397]248        edges[edges[n].next_in].prev_in = edges[n].prev_in;
[946]249      }
250
251      if(edges[n].prev_in!=-1) {
[397]252        edges[edges[n].prev_in].next_in = edges[n].next_in;
[946]253      } else {
[986]254        nodes[edges[n].target].first_in = edges[n].next_in;
[946]255      }
256
[397]257     
[946]258      if(edges[n].next_out!=-1) {
[397]259        edges[edges[n].next_out].prev_out = edges[n].prev_out;
[946]260      }
261
262      if(edges[n].prev_out!=-1) {
[397]263        edges[edges[n].prev_out].next_out = edges[n].next_out;
[946]264      } else {
[986]265        nodes[edges[n].source].first_out = edges[n].next_out;
[946]266      }
[397]267     
268      edges[n].next_in = first_free_edge;
[695]269      first_free_edge = n;     
[397]270
271    }
272
273    void clear() {
[782]274      edges.clear();
275      nodes.clear();
[946]276      first_node = first_free_node = first_free_edge = -1;
[937]277    }
278
[949]279  protected:
[1546]280    void _changeTarget(Edge e, Node n)
[949]281    {
282      if(edges[e.id].next_in != -1)
283        edges[edges[e.id].next_in].prev_in = edges[e.id].prev_in;
284      if(edges[e.id].prev_in != -1)
285        edges[edges[e.id].prev_in].next_in = edges[e.id].next_in;
[986]286      else nodes[edges[e.id].target].first_in = edges[e.id].next_in;
[1702]287      if (nodes[n.id].first_in != -1) {
288        edges[nodes[n.id].first_in].prev_in = e.id;
289      }
[986]290      edges[e.id].target = n.id;
[949]291      edges[e.id].prev_in = -1;
292      edges[e.id].next_in = nodes[n.id].first_in;
293      nodes[n.id].first_in = e.id;
294    }
[1546]295    void _changeSource(Edge e, Node n)
[949]296    {
297      if(edges[e.id].next_out != -1)
298        edges[edges[e.id].next_out].prev_out = edges[e.id].prev_out;
299      if(edges[e.id].prev_out != -1)
300        edges[edges[e.id].prev_out].next_out = edges[e.id].next_out;
[986]301      else nodes[edges[e.id].source].first_out = edges[e.id].next_out;
[1702]302      if (nodes[n.id].first_out != -1) {
303        edges[nodes[n.id].first_out].prev_out = e.id;
304      }
[986]305      edges[e.id].source = n.id;
[949]306      edges[e.id].prev_out = -1;
307      edges[e.id].next_out = nodes[n.id].first_out;
308      nodes[n.id].first_out = e.id;
309    }
310
[919]311  };
[909]312
[1669]313  typedef ErasableGraphExtender<
314    ClearableGraphExtender<
315    ExtendableGraphExtender<
316    MappableGraphExtender<
317    IterableGraphExtender<
318    AlterableGraphExtender<ListGraphBase> > > > > > ExtendedListGraphBase;
[400]319
[1718]320  /// \addtogroup graphs
321  /// @{
[400]322
[948]323  ///A list graph class.
[400]324
[948]325  ///This is a simple and fast erasable graph implementation.
326  ///
[1010]327  ///It addition that it conforms to the
328  ///\ref concept::ErasableGraph "ErasableGraph" concept,
329  ///it also provides several additional useful extra functionalities.
[959]330  ///\sa concept::ErasableGraph.
[782]331
[1669]332  class ListGraph : public ExtendedListGraphBase
[948]333  {
334  public:
[1546]335    /// Changes the target of \c e to \c n
[948]336
[1546]337    /// Changes the target of \c e to \c n
[948]338    ///
[1010]339    ///\note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
[1546]340    ///referencing the changed edge remain
[1010]341    ///valid. However <tt>InEdge</tt>'s are invalidated.
[1718]342    void changeTarget(Edge e, Node n) {
343      _changeTarget(e,n);
344    }
[1546]345    /// Changes the source of \c e to \c n
[948]346
[1546]347    /// Changes the source of \c e to \c n
[948]348    ///
[1010]349    ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
[1546]350    ///referencing the changed edge remain
[1010]351    ///valid. However <tt>OutEdge</tt>'s are invalidated.
[1718]352    void changeSource(Edge e, Node n) {
353      _changeSource(e,n);
354    }
[949]355
[1010]356    /// Invert the direction of an edge.
357
358    ///\note The <tt>Edge</tt>'s
[1546]359    ///referencing the changed edge remain
[1010]360    ///valid. However <tt>OutEdge</tt>'s  and <tt>InEdge</tt>'s are invalidated.
361    void reverseEdge(Edge e) {
362      Node t=target(e);
[1546]363      _changeTarget(e,source(e));
364      _changeSource(e,t);
[1010]365    }
366
367    ///Using this it possible to avoid the superfluous memory allocation.
368
[949]369    ///Using this it possible to avoid the superfluous memory allocation.
370    ///\todo more docs...
371    void reserveEdge(int n) { edges.reserve(n); };
[1010]372
373    ///Contract two nodes.
374
375    ///This function contracts two nodes.
376    ///
377    ///Node \p b will be removed but instead of deleting
378    ///its neighboring edges, they will be joined to \p a.
379    ///The last parameter \p r controls whether to remove loops. \c true
380    ///means that loops will be removed.
381    ///
382    ///\note The <tt>Edge</tt>s
[1281]383    ///referencing a moved edge remain
[1010]384    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
385    ///may be invalidated.
[1718]386    void contract(Node a, Node b, bool r = true)
[1010]387    {
388      for(OutEdgeIt e(*this,b);e!=INVALID;) {
389        OutEdgeIt f=e;
390        ++f;
391        if(r && target(e)==a) erase(e);
[1546]392        else changeSource(e,a);
[1010]393        e=f;
394      }
395      for(InEdgeIt e(*this,b);e!=INVALID;) {
396        InEdgeIt f=e;
397        ++f;
398        if(r && source(e)==a) erase(e);
[1546]399        else changeTarget(e,a);
[1010]400        e=f;
401      }
402      erase(b);
403    }
[1011]404
[1281]405    ///Split a node.
[1011]406
[1284]407    ///This function splits a node. First a new node is added to the graph,
408    ///then the source of each outgoing edge of \c n is moved to this new node.
[1281]409    ///If \c connect is \c true (this is the default value), then a new edge
410    ///from \c n to the newly created node is also added.
411    ///\return The newly created node.
412    ///
413    ///\note The <tt>Edge</tt>s
414    ///referencing a moved edge remain
415    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
416    ///may be invalidated.
[1770]417    ///\warning This functionality cannot be used together with the Snapshot
[1284]418    ///feature.
[1281]419    ///\todo It could be implemented in a bit faster way.
420    Node split(Node n, bool connect = true)
421    {
422      Node b = addNode();
423      for(OutEdgeIt e(*this,n);e!=INVALID;) {
424        OutEdgeIt f=e;
425        ++f;
[1546]426        changeSource(e,b);
[1281]427        e=f;
428      }
429      if(connect) addEdge(n,b);
430      return b;
431    }
432     
[1011]433    ///Class to make a snapshot of the graph and to restrore to it later.
434
435    ///Class to make a snapshot of the graph and to restrore to it later.
436    ///
437    ///The newly added nodes and edges can be removed using the
438    ///restore() function.
439    ///
440    ///\warning Edge and node deletions cannot be restored.
[1770]441    ///\warning Snapshots cannot be nested.
442    class Snapshot : protected AlterationNotifier<Node>::ObserverBase,
[1039]443                     protected AlterationNotifier<Edge>::ObserverBase
[1011]444    {
[1774]445    public:
446     
447      class UnsupportedOperation : public LogicError {
448      public:
449        virtual const char* exceptionName() const {
450          return "lemon::ListGraph::Snapshot::UnsupportedOperation";
451        }
452      };
453           
454
[1011]455      protected:
456     
457      ListGraph *g;
458      std::list<Node> added_nodes;
459      std::list<Edge> added_edges;
460     
461      bool active;
462      virtual void add(const Node& n) {
463        added_nodes.push_back(n);
464      };
465      virtual void erase(const Node&)
466      {
[1774]467        throw UnsupportedOperation();
[1011]468      }
469      virtual void add(const Edge& n) {
470        added_edges.push_back(n);
471      };
472      virtual void erase(const Edge&)
473      {
[1774]474        throw UnsupportedOperation();
[1011]475      }
476
[1457]477      ///\bug What is this used for?
478      ///
479      virtual void build() {}
480      ///\bug What is this used for?
481      ///
482      virtual void clear() {}
483
[1011]484      void regist(ListGraph &_g) {
485        g=&_g;
[1039]486        AlterationNotifier<Node>::ObserverBase::
[1040]487          attach(g->getNotifier(Node()));
[1039]488        AlterationNotifier<Edge>::ObserverBase::
[1040]489          attach(g->getNotifier(Edge()));
[1011]490      }
491           
492      void deregist() {
[1039]493        AlterationNotifier<Node>::ObserverBase::
[1011]494          detach();
[1039]495        AlterationNotifier<Edge>::ObserverBase::
[1011]496          detach();
497        g=0;
498      }
[1774]499
[1011]500    public:
501      ///Default constructur.
502     
503      ///Default constructur.
504      ///To actually make a snapshot you must call save().
505      ///
[1770]506      Snapshot() : g(0) {}
[1011]507      ///Constructor that immediately makes a snapshot.
508     
509      ///This constructor immediately makes a snapshot of the graph.
510      ///\param _g The graph we make a snapshot of.
[1770]511      Snapshot(ListGraph &_g) {
[1011]512        regist(_g);
513      }
514      ///\bug Is it necessary?
515      ///
[1770]516      ~Snapshot()
[1011]517      {
518        if(g) deregist();
519      }
520     
521      ///Make a snapshot.
522
523      ///Make a snapshot of the graph.
524      ///
525      ///This function can be called more than once. In case of a repeated
526      ///call, the previous snapshot gets lost.
527      ///\param _g The graph we make the snapshot of.
528      void save(ListGraph &_g)
529      {
530        if(g!=&_g) {
531          if(g) deregist();
532          regist(_g);
533        }
534        added_nodes.clear();
535        added_edges.clear();
536      }
537     
538    ///Undo the changes until the last snapshot.
539
540    ///Undo the changes until last snapshot created by save().
541    ///
542    ///\todo This function might be called undo().
543      void restore() {
[1457]544        ListGraph &old_g=*g;
[1011]545        deregist();
546        while(!added_edges.empty()) {
[1457]547          old_g.erase(added_edges.front());
[1011]548          added_edges.pop_front();
549        }
550        while(!added_nodes.empty()) {
[1457]551          old_g.erase(added_nodes.front());
[1011]552          added_nodes.pop_front();
553        }
554      }
555    };
556   
[949]557  };
[1034]558
[1555]559  ///@}
[1034]560
561  /**************** Undirected List Graph ****************/
562
563  typedef ErasableUndirGraphExtender<
564    ClearableUndirGraphExtender<
565    ExtendableUndirGraphExtender<
566    MappableUndirGraphExtender<
567    IterableUndirGraphExtender<
568    AlterableUndirGraphExtender<
[1669]569    UndirGraphExtender<ListGraphBase> > > > > > > ExtendedUndirListGraphBase;
[1034]570
[1718]571  /// \addtogroup graphs
572  /// @{
[1555]573
[1035]574  ///An undirected list graph class.
575
576  ///This is a simple and fast erasable undirected graph implementation.
577  ///
578  ///It conforms to the
579  ///\ref concept::UndirGraph "UndirGraph" concept.
580  ///
581  ///\sa concept::UndirGraph.
582  ///
[1770]583  ///\todo Snapshot, reverseEdge(), changeTarget(), changeSource(), contract()
[1161]584  ///haven't been implemented yet.
[1035]585  ///
[1669]586  class UndirListGraph : public ExtendedUndirListGraphBase {
[1718]587  public:
588    typedef ExtendedUndirListGraphBase Parent;
589    /// \brief Changes the target of \c e to \c n
590    ///
591    /// Changes the target of \c e to \c n
592    ///
593    /// \note The <tt>Edge</tt>'s and <tt>OutEdge</tt>'s
594    /// referencing the changed edge remain
595    /// valid. However <tt>InEdge</tt>'s are invalidated.
596    void changeTarget(UndirEdge e, Node n) {
597      _changeTarget(e,n);
598    }
599    /// Changes the source of \c e to \c n
600    ///
601    /// Changes the source of \c e to \c n
602    ///
603    ///\note The <tt>Edge</tt>'s and <tt>InEdge</tt>'s
604    ///referencing the changed edge remain
605    ///valid. However <tt>OutEdge</tt>'s are invalidated.
606    void changeSource(UndirEdge e, Node n) {
607      _changeSource(e,n);
608    }
609    /// \brief Contract two nodes.
610    ///
611    /// This function contracts two nodes.
612    ///
613    /// Node \p b will be removed but instead of deleting
614    /// its neighboring edges, they will be joined to \p a.
615    /// The last parameter \p r controls whether to remove loops. \c true
616    /// means that loops will be removed.
617    ///
618    /// \note The <tt>Edge</tt>s
619    /// referencing a moved edge remain
620    /// valid.
621    void contract(Node a, Node b, bool r = true) {
622      for(IncEdgeIt e(*this, b); e!=INVALID;) {
623        IncEdgeIt f = e; ++f;
624        if (r && runningNode(e) == a) {
625          erase(e);
626        } else if (source(e) == b) {
627          changeSource(e, a);
628        } else {
629          changeTarget(e, a);
630        }
631        e = f;
632      }
633      erase(b);
634    }
[1034]635  };
636
[949]637 
[948]638  /// @} 
639} //namespace lemon
[946]640 
[400]641
[946]642#endif
Note: See TracBrowser for help on using the repository browser.