COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/smart_graph.h @ 2391:14a343be7a5a

Last change on this file since 2391:14a343be7a5a was 2391:14a343be7a5a, checked in by Alpar Juttner, 17 years ago

Happy New Year to all source files!

File size: 28.6 KB
RevLine 
[906]1/* -*- C++ -*-
2 *
[1956]3 * This file is a part of LEMON, a generic C++ optimization library
4 *
[2391]5 * Copyright (C) 2003-2007
[1956]6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[906]8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
[105]18
[921]19#ifndef LEMON_SMART_GRAPH_H
20#define LEMON_SMART_GRAPH_H
[104]21
[491]22///\ingroup graphs
[242]23///\file
[2116]24///\brief SmartGraph and SmartUGraph classes.
[242]25
[104]26#include <vector>
27
[1993]28#include <lemon/bits/invalid.h>
[157]29
[2116]30#include <lemon/bits/base_extender.h>
[1791]31#include <lemon/bits/graph_extender.h>
[1034]32
[1993]33#include <lemon/bits/utility.h>
[2116]34#include <lemon/error.h>
[782]35
[1979]36#include <lemon/bits/graph_extender.h>
37
[921]38namespace lemon {
[104]39
[973]40  class SmartGraph;
[969]41  ///Base of SmartGraph
42
43  ///Base of SmartGraph
44  ///
[946]45  class SmartGraphBase {
[2190]46  protected:
[104]47
48    struct NodeT
49    {
[2190]50      int first_in, first_out;     
51      NodeT() {}
[104]52    };
53    struct EdgeT
54    {
[986]55      int target, source, next_in, next_out;     
[2190]56      EdgeT() {} 
[104]57    };
58
59    std::vector<NodeT> nodes;
[129]60
[104]61    std::vector<EdgeT> edges;
62   
[185]63   
[104]64  public:
[782]65
[946]66    typedef SmartGraphBase Graph;
[104]67
[164]68    class Node;
69    class Edge;
[108]70
[104]71   
72  public:
73
[946]74    SmartGraphBase() : nodes(), edges() { }
[1718]75    SmartGraphBase(const SmartGraphBase &_g)
76      : nodes(_g.nodes), edges(_g.edges) { }
[104]77   
[977]78    typedef True NodeNumTag;
79    typedef True EdgeNumTag;
80
[813]81    int nodeNum() const { return nodes.size(); }
82    int edgeNum() const { return edges.size(); }
[104]83
[1791]84    int maxNodeId() const { return nodes.size()-1; }
85    int maxEdgeId() const { return edges.size()-1; }
[108]86
[2128]87    Node addNode() {
[2190]88      int n = nodes.size();     
89      nodes.push_back(NodeT());
90      nodes[n].first_in = -1;
91      nodes[n].first_out = -1;
92      return Node(n);
[2128]93    }
94   
95    Edge addEdge(Node u, Node v) {
[2190]96      int n = edges.size();
97      edges.push_back(EdgeT());
98      edges[n].source = u.id;
99      edges[n].target = v.id;
100      edges[n].next_out = nodes[u.id].first_out;
101      edges[n].next_in = nodes[v.id].first_in;
102      nodes[u.id].first_out = nodes[v.id].first_in = n;
[2128]103
[2190]104      return Edge(n);
[2128]105    }
106
[2190]107    void clear() {
108      edges.clear();
109      nodes.clear();
110    }
[2128]111
[2190]112    Node source(Edge e) const { return Node(edges[e.id].source); }
113    Node target(Edge e) const { return Node(edges[e.id].target); }
[104]114
[2190]115    static int id(Node v) { return v.id; }
116    static int id(Edge e) { return e.id; }
[104]117
[1791]118    static Node nodeFromId(int id) { return Node(id);}
119    static Edge edgeFromId(int id) { return Edge(id);}
[1106]120
[164]121    class Node {
[946]122      friend class SmartGraphBase;
[973]123      friend class SmartGraph;
[104]124
125    protected:
[2190]126      int id;
127      explicit Node(int _id) : id(_id) {}
[104]128    public:
[164]129      Node() {}
[2190]130      Node (Invalid) : id(-1) {}
131      bool operator==(const Node i) const {return id == i.id;}
132      bool operator!=(const Node i) const {return id != i.id;}
133      bool operator<(const Node i) const {return id < i.id;}
[104]134    };
135   
136
[164]137    class Edge {
[946]138      friend class SmartGraphBase;
[973]139      friend class SmartGraph;
[185]140
[104]141    protected:
[2190]142      int id;
143      explicit Edge(int _id) : id(_id) {}
[706]144    public:
[164]145      Edge() { }
[2190]146      Edge (Invalid) : id(-1) {}
147      bool operator==(const Edge i) const {return id == i.id;}
148      bool operator!=(const Edge i) const {return id != i.id;}
149      bool operator<(const Edge i) const {return id < i.id;}
[946]150    };
[905]151
[946]152    void first(Node& node) const {
[2190]153      node.id = nodes.size() - 1;
[946]154    }
155
156    static void next(Node& node) {
[2190]157      --node.id;
[946]158    }
159
160    void first(Edge& edge) const {
[2190]161      edge.id = edges.size() - 1;
[946]162    }
163
164    static void next(Edge& edge) {
[2190]165      --edge.id;
[946]166    }
167
168    void firstOut(Edge& edge, const Node& node) const {
[2190]169      edge.id = nodes[node.id].first_out;
[946]170    }
171
172    void nextOut(Edge& edge) const {
[2190]173      edge.id = edges[edge.id].next_out;
[946]174    }
175
176    void firstIn(Edge& edge, const Node& node) const {
[2190]177      edge.id = nodes[node.id].first_in;
[946]178    }
[104]179   
[946]180    void nextIn(Edge& edge) const {
[2190]181      edge.id = edges[edge.id].next_in;
[946]182    }
[105]183
[104]184  };
[185]185
[1979]186  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
[937]187
[1791]188  /// \ingroup graphs
[1161]189
[950]190  ///A smart graph class.
[937]191
[950]192  ///This is a simple and fast graph implementation.
193  ///It is also quite memory efficient, but at the price
[974]194  ///that <b> it does support only limited (only stack-like)
195  ///node and edge deletions</b>.
[950]196  ///It conforms to
[2260]197  ///the \ref concepts::Graph "Graph concept" with an
[2256]198  ///important extra feature that
[2260]199  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
[2256]200  ///
[2260]201  ///\sa concepts::Graph.
[950]202  ///
203  ///\author Alpar Juttner
[1669]204  class SmartGraph : public ExtendedSmartGraphBase {
[969]205  public:
[1979]206
207    typedef ExtendedSmartGraphBase Parent;
208
[2190]209  private:
[973]210
[2128]211    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
212
213    ///SmartGraph is \e not copy constructible. Use GraphCopy() instead.
214    ///
[2190]215    SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
[2132]216    ///\brief Assignment of SmartGraph to another one is \e not allowed.
[2128]217    ///Use GraphCopy() instead.
218
[2132]219    ///Assignment of SmartGraph to another one is \e not allowed.
[2128]220    ///Use GraphCopy() instead.
221    void operator=(const SmartGraph &) {}
[1011]222
223  public:
[2128]224   
225    /// Constructor
226   
227    /// Constructor.
228    ///
229    SmartGraph() {};
230   
231    ///Add a new node to the graph.
232   
233    /// \return the new node.
234    ///
235    Node addNode() { return Parent::addNode(); }
236   
237    ///Add a new edge to the graph.
238   
239    ///Add a new edge to the graph with source node \c s
240    ///and target node \c t.
241    ///\return the new edge.
242    Edge addEdge(const Node& s, const Node& t) {
243      return Parent::addEdge(s, t);
244    }
245
[2190]246    ///Clear the graph.
[2128]247   
[2190]248    ///Erase all the nodes and edges from the graph.
249    ///
[2128]250    void clear() {
[2190]251      Parent::clear();
[2128]252    }
[1284]253
254    ///Split a node.
255   
256    ///This function splits a node. First a new node is added to the graph,
257    ///then the source of each outgoing edge of \c n is moved to this new node.
258    ///If \c connect is \c true (this is the default value), then a new edge
259    ///from \c n to the newly created node is also added.
260    ///\return The newly created node.
261    ///
262    ///\note The <tt>Edge</tt>s
263    ///referencing a moved edge remain
264    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
265    ///may be invalidated.
[1770]266    ///\warning This functionality cannot be used together with the Snapshot
[1284]267    ///feature.
268    ///\todo It could be implemented in a bit faster way.
[2128]269    Node split(Node n, bool connect = true)
[1284]270    {
[2128]271      Node b = addNode();
[2190]272      nodes[b.id].first_out=nodes[n.id].first_out;
273      nodes[n.id].first_out=-1;
274      for(int i=nodes[b.id].first_out;i!=-1;i++) edges[i].source=b.id;
[2128]275      if(connect) addEdge(n,b);
[1718]276      return b;
[1284]277    }
278
[2190]279  public:
280   
281    class Snapshot;
282
283  protected:
284
285    void restoreSnapshot(const Snapshot &s)
286    {
287      while(s.edge_num<edges.size()) {
288        Edge edge = edgeFromId(edges.size()-1);
[2381]289        Parent::notifier(Edge()).erase(edge);
[2190]290        nodes[edges.back().source].first_out=edges.back().next_out;
291        nodes[edges.back().target].first_in=edges.back().next_in;
292        edges.pop_back();
293      }
294      while(s.node_num<nodes.size()) {
295        Node node = nodeFromId(nodes.size()-1);
[2381]296        Parent::notifier(Node()).erase(node);
[2190]297        nodes.pop_back();
298      }
299    }   
300
301  public:
302
[1011]303    ///Class to make a snapshot of the graph and to restrore to it later.
304
305    ///Class to make a snapshot of the graph and to restrore to it later.
306    ///
307    ///The newly added nodes and edges can be removed using the
308    ///restore() function.
309    ///\note After you restore a state, you cannot restore
310    ///a later state, in other word you cannot add again the edges deleted
[2132]311    ///by restore() using another one Snapshot instance.
[1011]312    ///
[2190]313    ///\warning If you do not use correctly the snapshot that can cause
314    ///either broken program, invalid state of the graph, valid but
315    ///not the restored graph or no change. Because the runtime performance
316    ///the validity of the snapshot is not stored.
[1770]317    class Snapshot
[1011]318    {
319      SmartGraph *g;
320    protected:
321      friend class SmartGraph;
322      unsigned int node_num;
323      unsigned int edge_num;
324    public:
[1274]325      ///Default constructor.
[1011]326     
[1274]327      ///Default constructor.
[1011]328      ///To actually make a snapshot you must call save().
329      ///
[1770]330      Snapshot() : g(0) {}
[1011]331      ///Constructor that immediately makes a snapshot
332     
333      ///This constructor immediately makes a snapshot of the graph.
334      ///\param _g The graph we make a snapshot of.
[1770]335      Snapshot(SmartGraph &_g) :g(&_g) {
[1011]336        node_num=g->nodes.size();
337        edge_num=g->edges.size();
338      }
339
340      ///Make a snapshot.
341
342      ///Make a snapshot of the graph.
343      ///
344      ///This function can be called more than once. In case of a repeated
345      ///call, the previous snapshot gets lost.
346      ///\param _g The graph we make the snapshot of.
347      void save(SmartGraph &_g)
348      {
349        g=&_g;
350        node_num=g->nodes.size();
351        edge_num=g->edges.size();
352      }
353
354      ///Undo the changes until a snapshot.
355     
356      ///Undo the changes until a snapshot created by save().
357      ///
358      ///\note After you restored a state, you cannot restore
359      ///a later state, in other word you cannot add again the edges deleted
360      ///by restore().
361      void restore()
362      {
[1770]363        g->restoreSnapshot(*this);
[1011]364      }
365    };
[973]366  };
[1034]367
368
[2338]369  class SmartUGraphBase {
[2116]370
[2338]371  protected:
372
373    struct NodeT {
374      int first_out;
375    };
376 
377    struct EdgeT {
378      int target;
379      int next_out;
380    };
381
382    std::vector<NodeT> nodes;
383    std::vector<EdgeT> edges;
384
385    int first_free_edge;
386   
387  public:
388   
389    typedef SmartUGraphBase Graph;
[2342]390
391    class Node;
392    class Edge;
393    class UEdge;
[2338]394   
395    class Node {
396      friend class SmartUGraphBase;
397    protected:
398
399      int id;
400      explicit Node(int pid) { id = pid;}
401
402    public:
403      Node() {}
404      Node (Invalid) { id = -1; }
405      bool operator==(const Node& node) const {return id == node.id;}
406      bool operator!=(const Node& node) const {return id != node.id;}
407      bool operator<(const Node& node) const {return id < node.id;}
408    };
409
410    class UEdge {
411      friend class SmartUGraphBase;
412    protected:
413
414      int id;
415      explicit UEdge(int pid) { id = pid;}
416
417    public:
418      UEdge() {}
419      UEdge (Invalid) { id = -1; }
420      bool operator==(const UEdge& edge) const {return id == edge.id;}
421      bool operator!=(const UEdge& edge) const {return id != edge.id;}
422      bool operator<(const UEdge& edge) const {return id < edge.id;}
423    };
424
425    class Edge {
426      friend class SmartUGraphBase;
427    protected:
428
429      int id;
430      explicit Edge(int pid) { id = pid;}
431
432    public:
[2343]433      operator UEdge() const { return uEdgeFromId(id / 2); }
[2338]434
435      Edge() {}
436      Edge (Invalid) { id = -1; }
437      bool operator==(const Edge& edge) const {return id == edge.id;}
438      bool operator!=(const Edge& edge) const {return id != edge.id;}
439      bool operator<(const Edge& edge) const {return id < edge.id;}
440    };
441
442
443
444    SmartUGraphBase()
445      : nodes(), edges() {}
446
447   
448    int maxNodeId() const { return nodes.size()-1; }
449    int maxUEdgeId() const { return edges.size() / 2 - 1; }
450    int maxEdgeId() const { return edges.size()-1; }
451
452    Node source(Edge e) const { return Node(edges[e.id ^ 1].target); }
453    Node target(Edge e) const { return Node(edges[e.id].target); }
454
455    Node source(UEdge e) const { return Node(edges[2 * e.id].target); }
456    Node target(UEdge e) const { return Node(edges[2 * e.id + 1].target); }
457
458    static bool direction(Edge e) {
459      return (e.id & 1) == 1;
460    }
461
462    static Edge direct(UEdge e, bool d) {
463      return Edge(e.id * 2 + (d ? 1 : 0));
464    }
465
466    void first(Node& node) const {
467      node.id = nodes.size() - 1;
468    }
469
470    void next(Node& node) const {
471      --node.id;
472    }
473
474    void first(Edge& edge) const {
475      edge.id = edges.size() - 1;
476    }
477
478    void next(Edge& edge) const {
479      --edge.id;
480    }
481
482    void first(UEdge& edge) const {
483      edge.id = edges.size() / 2 - 1;
484    }
485
486    void next(UEdge& edge) const {
487      --edge.id;
488    }
489
490    void firstOut(Edge &edge, const Node& v) const {
491      edge.id = nodes[v.id].first_out;
492    }
493    void nextOut(Edge &edge) const {
494      edge.id = edges[edge.id].next_out;
495    }
496
497    void firstIn(Edge &edge, const Node& v) const {
498      edge.id = ((nodes[v.id].first_out) ^ 1);
[2339]499      if (edge.id == -2) edge.id = -1;
[2338]500    }
501    void nextIn(Edge &edge) const {
502      edge.id = ((edges[edge.id ^ 1].next_out) ^ 1);
[2339]503      if (edge.id == -2) edge.id = -1;
[2338]504    }
505
506    void firstInc(UEdge &edge, bool& d, const Node& v) const {
507      int de = nodes[v.id].first_out;
[2381]508      if (de != -1) {
509        edge.id = de / 2;
510        d = ((de & 1) == 1);
511      } else {
512        edge.id = -1;
513        d = true;
514      }
[2338]515    }
516    void nextInc(UEdge &edge, bool& d) const {
517      int de = (edges[(edge.id * 2) | (d ? 1 : 0)].next_out);
[2381]518      if (de != -1) {
519        edge.id = de / 2;
520        d = ((de & 1) == 1);
521      } else {
522        edge.id = -1;
523        d = true;     
524      }
[2338]525    }
526   
527    static int id(Node v) { return v.id; }
528    static int id(Edge e) { return e.id; }
529    static int id(UEdge e) { return e.id; }
530
531    static Node nodeFromId(int id) { return Node(id);}
532    static Edge edgeFromId(int id) { return Edge(id);}
533    static UEdge uEdgeFromId(int id) { return UEdge(id);}
534
535    Node addNode() {     
536      int n = nodes.size();
537      nodes.push_back(NodeT());
538      nodes[n].first_out = -1;
539     
540      return Node(n);
541    }
542   
543    UEdge addEdge(Node u, Node v) {
544      int n = edges.size();
545      edges.push_back(EdgeT());
546      edges.push_back(EdgeT());
547     
548      edges[n].target = u.id;
549      edges[n | 1].target = v.id;
550
551      edges[n].next_out = nodes[v.id].first_out;
552      edges[n | 1].next_out = nodes[u.id].first_out;
553       
554      nodes[v.id].first_out = n;
555      nodes[u.id].first_out = (n | 1);
556
557      return UEdge(n / 2);
558    }
559   
560    void clear() {
561      edges.clear();
562      nodes.clear();
563    }
564
565  };
566
567  typedef UGraphExtender<SmartUGraphBase> ExtendedSmartUGraphBase;
[2116]568
569  /// \ingroup graphs
570  ///
571  /// \brief A smart undirected graph class.
572  ///
573  /// This is a simple and fast undirected graph implementation.
574  /// It is also quite memory efficient, but at the price
575  /// that <b> it does support only limited (only stack-like)
576  /// node and edge deletions</b>.
577  /// Except from this it conforms to
[2260]578  /// the \ref concepts::UGraph "UGraph concept".
[2256]579  ///
580  ///It also has an
581  ///important extra feature that
[2260]582  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
[2256]583  ///
[2260]584  /// \sa concepts::UGraph.
[2116]585  ///
586  class SmartUGraph : public ExtendedSmartUGraphBase {
[2128]587  private:
[2190]588
[2128]589    ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead.
590
591    ///SmartUGraph is \e not copy constructible. Use UGraphCopy() instead.
592    ///
593    SmartUGraph(const SmartUGraph &) : ExtendedSmartUGraphBase() {};
[2190]594
[2132]595    ///\brief Assignment of SmartUGraph to another one is \e not allowed.
[2128]596    ///Use UGraphCopy() instead.
597
[2132]598    ///Assignment of SmartUGraph to another one is \e not allowed.
[2128]599    ///Use UGraphCopy() instead.
600    void operator=(const SmartUGraph &) {}
[2190]601
[2128]602  public:
[2190]603
604    typedef ExtendedSmartUGraphBase Parent;
[2338]605    typedef Parent::OutEdgeIt IncEdgeIt;
[2190]606
[2128]607    /// Constructor
608   
609    /// Constructor.
610    ///
611    SmartUGraph() {}
[2190]612
613    ///Add a new node to the graph.
614   
615    /// \return the new node.
616    ///
617    Node addNode() { return Parent::addNode(); }
618   
619    ///Add a new undirected edge to the graph.
620   
621    ///Add a new undirected edge to the graph with node \c s
622    ///and \c t.
623    ///\return the new undirected edge.
624    UEdge addEdge(const Node& s, const Node& t) {
625      return Parent::addEdge(s, t);
626    }
627
628    ///Clear the graph.
629   
630    ///Erase all the nodes and edges from the graph.
631    ///
632    void clear() {
633      Parent::clear();
634    }
635
636  public:
637   
638    class Snapshot;
639
640  protected:
641
[2338]642    void saveSnapshot(Snapshot &s)
643    {
[2386]644      s.graph = this;
[2338]645      s.node_num = nodes.size();
646      s.edge_num = edges.size();
647    }
[2190]648
649    void restoreSnapshot(const Snapshot &s)
650    {
651      while(s.edge_num<edges.size()) {
[2338]652        int n=edges.size()-1;
653        UEdge edge=uEdgeFromId(n/2);
[2381]654        Parent::notifier(UEdge()).erase(edge);
[2190]655        std::vector<Edge> dir;
[2338]656        dir.push_back(edgeFromId(n));
657        dir.push_back(edgeFromId(n-1));
[2381]658        Parent::notifier(Edge()).erase(dir);
[2338]659        nodes[edges[n].target].first_out=edges[n].next_out;
660        nodes[edges[n-1].target].first_out=edges[n-1].next_out;
661        edges.pop_back();
[2190]662        edges.pop_back();
663      }
664      while(s.node_num<nodes.size()) {
[2338]665        int n=nodes.size()-1;
666        Node node = nodeFromId(n);
[2381]667        Parent::notifier(Node()).erase(node);
[2190]668        nodes.pop_back();
669      }
670    }   
671
672  public:
673
674    ///Class to make a snapshot of the graph and to restrore to it later.
675
676    ///Class to make a snapshot of the graph and to restrore to it later.
677    ///
678    ///The newly added nodes and edges can be removed using the
679    ///restore() function.
680    ///
681    ///\note After you restore a state, you cannot restore
682    ///a later state, in other word you cannot add again the edges deleted
683    ///by restore() using another one Snapshot instance.
684    ///
685    ///\warning If you do not use correctly the snapshot that can cause
686    ///either broken program, invalid state of the graph, valid but
687    ///not the restored graph or no change. Because the runtime performance
688    ///the validity of the snapshot is not stored.
689    class Snapshot
690    {
[2386]691      SmartUGraph *graph;
[2190]692    protected:
693      friend class SmartUGraph;
694      unsigned int node_num;
695      unsigned int edge_num;
696    public:
697      ///Default constructor.
698     
699      ///Default constructor.
700      ///To actually make a snapshot you must call save().
701      ///
[2386]702      Snapshot() : graph(0) {}
[2190]703      ///Constructor that immediately makes a snapshot
704     
705      ///This constructor immediately makes a snapshot of the graph.
[2350]706      ///\param g The graph we make a snapshot of.
[2338]707      Snapshot(SmartUGraph &g) {
708        g.saveSnapshot(*this);
[2190]709      }
710
711      ///Make a snapshot.
712
713      ///Make a snapshot of the graph.
714      ///
715      ///This function can be called more than once. In case of a repeated
716      ///call, the previous snapshot gets lost.
[2350]717      ///\param g The graph we make the snapshot of.
[2338]718      void save(SmartUGraph &g)
[2190]719      {
[2338]720        g.saveSnapshot(*this);
[2190]721      }
722
723      ///Undo the changes until a snapshot.
724     
725      ///Undo the changes until a snapshot created by save().
726      ///
727      ///\note After you restored a state, you cannot restore
728      ///a later state, in other word you cannot add again the edges deleted
729      ///by restore().
730      void restore()
731      {
[2386]732        graph->restoreSnapshot(*this);
[2190]733      }
734    };
[2116]735  };
736
737
738  class SmartBpUGraphBase {
739  public:
740
741    class NodeSetError : public LogicError {
[2162]742    public:
[2151]743      virtual const char* what() const throw() {
[2116]744        return "lemon::SmartBpUGraph::NodeSetError";
745      }
746    };
747
748  protected:
749
750    struct NodeT {
751      int first;
752      NodeT() {}
753      NodeT(int _first) : first(_first) {}
754    };
755
756    struct UEdgeT {
757      int aNode, next_out;
758      int bNode, next_in;
759    };
760
761    std::vector<NodeT> aNodes;
762    std::vector<NodeT> bNodes;
763
764    std::vector<UEdgeT> edges;
765
766  public:
767 
768    class Node {
769      friend class SmartBpUGraphBase;
770    protected:
771      int id;
772
[2190]773      explicit Node(int _id) : id(_id) {}
[2116]774    public:
775      Node() {}
[2190]776      Node(Invalid) : id(-1) {}
[2116]777      bool operator==(const Node i) const {return id==i.id;}
778      bool operator!=(const Node i) const {return id!=i.id;}
779      bool operator<(const Node i) const {return id<i.id;}
780    };
781
782    class UEdge {
783      friend class SmartBpUGraphBase;
784    protected:
785      int id;
786
[2190]787      UEdge(int _id) : id(_id) {}
[2116]788    public:
789      UEdge() {}
[2190]790      UEdge(Invalid) : id(-1) {}
[2116]791      bool operator==(const UEdge i) const {return id==i.id;}
792      bool operator!=(const UEdge i) const {return id!=i.id;}
793      bool operator<(const UEdge i) const {return id<i.id;}
794    };
795
796    void firstANode(Node& node) const {
797      node.id = 2 * aNodes.size() - 2;
798      if (node.id < 0) node.id = -1;
799    }
800    void nextANode(Node& node) const {
801      node.id -= 2;
802      if (node.id < 0) node.id = -1;
803    }
804
805    void firstBNode(Node& node) const {
806      node.id = 2 * bNodes.size() - 1;
807    }
808    void nextBNode(Node& node) const {
809      node.id -= 2;
810    }
811
812    void first(Node& node) const {
813      if (aNodes.size() > 0) {
814        node.id = 2 * aNodes.size() - 2;
815      } else {
816        node.id = 2 * bNodes.size() - 1;
817      }
818    }
819    void next(Node& node) const {
820      node.id -= 2;
821      if (node.id == -2) {
822        node.id = 2 * bNodes.size() - 1;
823      }
824    }
825 
826    void first(UEdge& edge) const {
827      edge.id = edges.size() - 1;
828    }
829    void next(UEdge& edge) const {
830      --edge.id;
831    }
832
833    void firstFromANode(UEdge& edge, const Node& node) const {
834      LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
835      edge.id = aNodes[node.id >> 1].first;
836    }
837    void nextFromANode(UEdge& edge) const {
838      edge.id = edges[edge.id].next_out;
839    }
840
841    void firstFromBNode(UEdge& edge, const Node& node) const {
842      LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
843      edge.id = bNodes[node.id >> 1].first;
844    }
845    void nextFromBNode(UEdge& edge) const {
846      edge.id = edges[edge.id].next_in;
847    }
848
849    static int id(const Node& node) {
850      return node.id;
851    }
852    static Node nodeFromId(int id) {
853      return Node(id);
854    }
855    int maxNodeId() const {
856      return aNodes.size() > bNodes.size() ?
857        aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
858    }
859 
860    static int id(const UEdge& edge) {
861      return edge.id;
862    }
863    static UEdge uEdgeFromId(int id) {
864      return UEdge(id);
865    }
866    int maxUEdgeId() const {
867      return edges.size();
868    }
869 
870    static int aNodeId(const Node& node) {
871      return node.id >> 1;
872    }
[2231]873    static Node nodeFromANodeId(int id) {
[2116]874      return Node(id << 1);
875    }
876    int maxANodeId() const {
877      return aNodes.size();
878    }
879
880    static int bNodeId(const Node& node) {
881      return node.id >> 1;
882    }
[2231]883    static Node nodeFromBNodeId(int id) {
[2116]884      return Node((id << 1) + 1);
885    }
886    int maxBNodeId() const {
887      return bNodes.size();
888    }
889
890    Node aNode(const UEdge& edge) const {
891      return Node(edges[edge.id].aNode);
892    }
893    Node bNode(const UEdge& edge) const {
894      return Node(edges[edge.id].bNode);
895    }
896
897    static bool aNode(const Node& node) {
898      return (node.id & 1) == 0;
899    }
900
901    static bool bNode(const Node& node) {
902      return (node.id & 1) == 1;
903    }
904
905    Node addANode() {
906      NodeT nodeT;
907      nodeT.first = -1;
908      aNodes.push_back(nodeT);
909      return Node(aNodes.size() * 2 - 2);
910    }
911
912    Node addBNode() {
913      NodeT nodeT;
914      nodeT.first = -1;
915      bNodes.push_back(nodeT);
916      return Node(bNodes.size() * 2 - 1);
917    }
918
919    UEdge addEdge(const Node& source, const Node& target) {
920      LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
921      UEdgeT edgeT;
922      if ((source.id & 1) == 0) {
923        edgeT.aNode = source.id;
924        edgeT.bNode = target.id;
925      } else {
926        edgeT.aNode = target.id;
927        edgeT.bNode = source.id;
928      }
929      edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
930      aNodes[edgeT.aNode >> 1].first = edges.size();
931      edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
932      bNodes[edgeT.bNode >> 1].first = edges.size();
933      edges.push_back(edgeT);
934      return UEdge(edges.size() - 1);
935    }
936
937    void clear() {
938      aNodes.clear();
939      bNodes.clear();
940      edges.clear();
941    }
942
943    typedef True NodeNumTag;
944    int nodeNum() const { return aNodes.size() + bNodes.size(); }
945    int aNodeNum() const { return aNodes.size(); }
946    int bNodeNum() const { return bNodes.size(); }
947
948    typedef True EdgeNumTag;
949    int uEdgeNum() const { return edges.size(); }
950
951  };
952
953
[2231]954  typedef BpUGraphExtender<BidirBpUGraphExtender<SmartBpUGraphBase> >
955  ExtendedSmartBpUGraphBase;
[2116]956
957  /// \ingroup graphs
958  ///
959  /// \brief A smart bipartite undirected graph class.
960  ///
961  /// This is a simple and fast bipartite undirected graph implementation.
962  /// It is also quite memory efficient, but at the price
963  /// that <b> it does not support node and edge deletions</b>.
964  /// Except from this it conforms to
[2260]965  /// the \ref concepts::BpUGraph "BpUGraph concept".
[2256]966  ///
967  ///It also has an
968  ///important extra feature that
[2260]969  ///its maps are real \ref concepts::ReferenceMap "reference map"s.
[2256]970  ///
[2260]971  /// \sa concepts::BpUGraph.
[2116]972  ///
[2190]973  class SmartBpUGraph : public ExtendedSmartBpUGraphBase {
974  private:
975
976    /// \brief SmartBpUGraph is \e not copy constructible.
977    ///
978    ///SmartBpUGraph is \e not copy constructible.
979    SmartBpUGraph(const SmartBpUGraph &) : ExtendedSmartBpUGraphBase() {};
980
981    /// \brief Assignment of SmartBpUGraph to another one is \e not
982    /// allowed.
983    ///
984    /// Assignment of SmartBpUGraph to another one is \e not allowed.
985    void operator=(const SmartBpUGraph &) {}
986
987  public:
988
989    typedef ExtendedSmartBpUGraphBase Parent;
990
991    ///Constructor
992   
993    ///Constructor.
994    ///
995    SmartBpUGraph() : ExtendedSmartBpUGraphBase() {}
996
997    ///Add a new ANode to the graph.
998   
999    /// \return the new node.
1000    ///
1001    Node addANode() { return Parent::addANode(); }
1002
1003    ///Add a new BNode to the graph.
1004   
1005    /// \return the new node.
1006    ///
1007    Node addBNode() { return Parent::addBNode(); }
1008   
1009    ///Add a new undirected edge to the graph.
1010   
1011    ///Add a new undirected edge to the graph with node \c s
1012    ///and \c t.
1013    ///\return the new undirected edge.
1014    UEdge addEdge(const Node& s, const Node& t) {
1015      return Parent::addEdge(s, t);
1016    }
1017
1018    ///Clear the graph.
1019   
1020    ///Erase all the nodes and edges from the graph.
1021    ///
1022    void clear() {
1023      Parent::clear();
1024    }
1025   
1026  public:
1027
1028    class Snapshot;
1029
1030  protected:
1031   
1032    void restoreSnapshot(const Snapshot &s)
1033    {
1034      while(s.edge_num<edges.size()) {
1035        UEdge edge = uEdgeFromId(edges.size()-1);
[2381]1036        Parent::notifier(UEdge()).erase(edge);
[2190]1037        std::vector<Edge> dir;
1038        dir.push_back(Parent::direct(edge, true));
1039        dir.push_back(Parent::direct(edge, false));
[2381]1040        Parent::notifier(Edge()).erase(dir);
[2190]1041        aNodes[edges.back().aNode >> 1].first=edges.back().next_out;
1042        bNodes[edges.back().bNode >> 1].first=edges.back().next_in;
1043        edges.pop_back();
1044      }
1045      while(s.anode_num<aNodes.size()) {
[2231]1046        Node node = nodeFromANodeId(aNodes.size() - 1);
[2381]1047        Parent::notifier(ANode()).erase(node);
1048        Parent::notifier(Node()).erase(node);
[2190]1049        aNodes.pop_back();
1050      }
1051      while(s.bnode_num<bNodes.size()) {
[2231]1052        Node node = nodeFromBNodeId(bNodes.size() - 1);
[2381]1053        Parent::notifier(BNode()).erase(node);
1054        Parent::notifier(Node()).erase(node);
[2190]1055        bNodes.pop_back();
1056      }
1057    }   
1058
1059  public:
1060
1061    ///Class to make a snapshot of the graph and to restrore to it later.
1062
1063    ///Class to make a snapshot of the graph and to restrore to it later.
1064    ///
1065    ///The newly added nodes and edges can be removed using the
1066    ///restore() function.
1067    ///
1068    ///\note After you restore a state, you cannot restore
1069    ///a later state, in other word you cannot add again the edges deleted
1070    ///by restore() using another one Snapshot instance.
1071    ///
1072    ///\warning If you do not use correctly the snapshot that can cause
1073    ///either broken program, invalid state of the graph, valid but
1074    ///not the restored graph or no change. Because the runtime performance
1075    ///the validity of the snapshot is not stored.
1076    class Snapshot
1077    {
1078      SmartBpUGraph *g;
1079    protected:
1080      friend class SmartBpUGraph;
1081      unsigned int anode_num;
1082      unsigned int bnode_num;
1083      unsigned int edge_num;
1084    public:
1085      ///Default constructor.
1086     
1087      ///Default constructor.
1088      ///To actually make a snapshot you must call save().
1089      ///
1090      Snapshot() : g(0) {}
1091
1092      ///Constructor that immediately makes a snapshot
1093     
1094      ///This constructor immediately makes a snapshot of the graph.
1095      ///\param _g The graph we make a snapshot of.
1096      Snapshot(SmartBpUGraph &_g) : g(&_g) {
1097        anode_num=g->aNodes.size();
1098        bnode_num=g->bNodes.size();
1099        edge_num=g->edges.size();
1100      }
1101
1102      ///Make a snapshot.
1103
1104      ///Make a snapshot of the graph.
1105      ///
1106      ///This function can be called more than once. In case of a repeated
1107      ///call, the previous snapshot gets lost.
1108      ///\param _g The graph we make the snapshot of.
1109      void save(SmartBpUGraph &_g)
1110      {
1111        g=&_g;
1112        anode_num=g->aNodes.size();
1113        bnode_num=g->bNodes.size();
1114        edge_num=g->edges.size();
1115      }
1116
1117      ///Undo the changes until a snapshot.
1118     
1119      ///Undo the changes until a snapshot created by save().
1120      ///
1121      ///\note After you restored a state, you cannot restore
1122      ///a later state, in other word you cannot add again the edges deleted
1123      ///by restore().
1124      void restore()
1125      {
1126        g->restoreSnapshot(*this);
1127      }
1128    };
1129  };
[2116]1130
1131 
1132  /// @} 
[921]1133} //namespace lemon
[104]1134
[157]1135
[921]1136#endif //LEMON_SMART_GRAPH_H
Note: See TracBrowser for help on using the repository browser.