COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/smart_graph.h @ 2091:c8ccc1f8fd51

Last change on this file since 2091:c8ccc1f8fd51 was 2076:10681ee9d8ae, checked in by Balazs Dezso, 14 years ago

Extenders modified

UGraphBaseExtender => UndirGraphExtender?
BpUGraphBaseExtender merged into BpUGraphExtender

File size: 15.5 KB
RevLine 
[906]1/* -*- C++ -*-
2 *
[1956]3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
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
[1909]24///\brief SmartGraph and SmartUGraph classes.
[242]25
[104]26#include <vector>
27
[1993]28#include <lemon/bits/invalid.h>
[157]29
[1999]30#include <lemon/bits/base_extender.h>
[1791]31#include <lemon/bits/graph_extender.h>
[1034]32
[1993]33#include <lemon/bits/utility.h>
[1820]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 {
[104]46
[973]47    friend class SmatGraph;
48
49  protected:
[104]50    struct NodeT
51    {
52      int first_in,first_out;     
[157]53      NodeT() : first_in(-1), first_out(-1) {}
[104]54    };
55    struct EdgeT
56    {
[986]57      int target, source, next_in, next_out;     
[104]58      //FIXME: is this necessary?
[157]59      EdgeT() : next_in(-1), next_out(-1) {} 
[104]60    };
61
62    std::vector<NodeT> nodes;
[129]63
[104]64    std::vector<EdgeT> edges;
65   
[185]66   
[104]67  public:
[782]68
[946]69    typedef SmartGraphBase Graph;
[104]70
[164]71    class Node;
72    class Edge;
[108]73
[104]74   
75  public:
76
[946]77    SmartGraphBase() : nodes(), edges() { }
[1718]78    SmartGraphBase(const SmartGraphBase &_g)
79      : nodes(_g.nodes), edges(_g.edges) { }
[104]80   
[977]81    typedef True NodeNumTag;
82    typedef True EdgeNumTag;
83
[813]84    ///Number of nodes.
85    int nodeNum() const { return nodes.size(); }
86    ///Number of edges.
87    int edgeNum() const { return edges.size(); }
[104]88
[813]89    /// Maximum node ID.
90   
91    /// Maximum node ID.
92    ///\sa id(Node)
[1791]93    int maxNodeId() const { return nodes.size()-1; }
[813]94    /// Maximum edge ID.
95   
96    /// Maximum edge ID.
97    ///\sa id(Edge)
[1791]98    int maxEdgeId() const { return edges.size()-1; }
[108]99
[986]100    Node source(Edge e) const { return edges[e.n].source; }
101    Node target(Edge e) const { return edges[e.n].target; }
[104]102
[813]103    /// Node ID.
104   
105    /// The ID of a valid Node is a nonnegative integer not greater than
[1791]106    /// \ref maxNodeId(). The range of the ID's is not surely continuous
107    /// and the greatest node ID can be actually less then \ref maxNodeId().
[813]108    ///
109    /// The ID of the \ref INVALID node is -1.
110    ///\return The ID of the node \c v.
[713]111    static int id(Node v) { return v.n; }
[813]112    /// Edge ID.
113   
114    /// The ID of a valid Edge is a nonnegative integer not greater than
[1791]115    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
116    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
[813]117    ///
118    /// The ID of the \ref INVALID edge is -1.
119    ///\return The ID of the edge \c e.
[713]120    static int id(Edge e) { return e.n; }
[104]121
[2076]122    /// \brief Returns the node from its \c id.
123    ///
124    /// Returns the node from its \c id. If there is not node
125    /// with the given id the effect of the function is undefinied.
[1791]126    static Node nodeFromId(int id) { return Node(id);}
[1106]127
[2076]128    /// \brief Returns the edge from its \c id.
129    ///
130    /// Returns the edge from its \c id. If there is not edge
131    /// with the given id the effect of the function is undefinied.
[1791]132    static Edge edgeFromId(int id) { return Edge(id);}
[1106]133
[164]134    Node addNode() {
135      Node n; n.n=nodes.size();
[104]136      nodes.push_back(NodeT()); //FIXME: Hmmm...
137      return n;
138    }
[108]139   
[164]140    Edge addEdge(Node u, Node v) {
141      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
[986]142      edges[e.n].source=u.n; edges[e.n].target=v.n;
[104]143      edges[e.n].next_out=nodes[u.n].first_out;
144      edges[e.n].next_in=nodes[v.n].first_in;
145      nodes[u.n].first_out=nodes[v.n].first_in=e.n;
[108]146
[104]147      return e;
148    }
149
[782]150    void clear() {
151      edges.clear();
152      nodes.clear();
153    }
[104]154
[946]155
[164]156    class Node {
[946]157      friend class SmartGraphBase;
[973]158      friend class SmartGraph;
[104]159
160    protected:
161      int n;
[164]162      Node(int nn) {n=nn;}
[104]163    public:
[164]164      Node() {}
[503]165      Node (Invalid) { n=-1; }
[164]166      bool operator==(const Node i) const {return n==i.n;}
167      bool operator!=(const Node i) const {return n!=i.n;}
168      bool operator<(const Node i) const {return n<i.n;}
[104]169    };
170   
171
[164]172    class Edge {
[946]173      friend class SmartGraphBase;
[973]174      friend class SmartGraph;
[185]175
[104]176    protected:
177      int n;
[905]178      Edge(int nn) {n=nn;}
[706]179    public:
[164]180      Edge() { }
[174]181      Edge (Invalid) { n=-1; }
[164]182      bool operator==(const Edge i) const {return n==i.n;}
183      bool operator!=(const Edge i) const {return n!=i.n;}
184      bool operator<(const Edge i) const {return n<i.n;}
[946]185    };
[905]186
[946]187    void first(Node& node) const {
188      node.n = nodes.size() - 1;
189    }
190
191    static void next(Node& node) {
192      --node.n;
193    }
194
195    void first(Edge& edge) const {
196      edge.n = edges.size() - 1;
197    }
198
199    static void next(Edge& edge) {
200      --edge.n;
201    }
202
203    void firstOut(Edge& edge, const Node& node) const {
204      edge.n = nodes[node.n].first_out;
205    }
206
207    void nextOut(Edge& edge) const {
208      edge.n = edges[edge.n].next_out;
209    }
210
211    void firstIn(Edge& edge, const Node& node) const {
212      edge.n = nodes[node.n].first_in;
213    }
[104]214   
[946]215    void nextIn(Edge& edge) const {
216      edge.n = edges[edge.n].next_in;
217    }
[105]218
[1284]219    Node _split(Node n, bool connect = true)
220    {
221      Node b = addNode();
222      nodes[b.n].first_out=nodes[n.n].first_out;
223      nodes[n.n].first_out=-1;
224      for(int i=nodes[b.n].first_out;i!=-1;i++) edges[i].source=b.n;
225      if(connect) addEdge(n,b);
226      return b;
227    }
228
[104]229  };
[185]230
[1979]231  typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase;
[937]232
[1791]233  /// \ingroup graphs
[1161]234
[950]235  ///A smart graph class.
[937]236
[950]237  ///This is a simple and fast graph implementation.
238  ///It is also quite memory efficient, but at the price
[974]239  ///that <b> it does support only limited (only stack-like)
240  ///node and edge deletions</b>.
[950]241  ///It conforms to
[959]242  ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
243  ///\sa concept::ExtendableGraph.
[950]244  ///
245  ///\author Alpar Juttner
[1669]246  class SmartGraph : public ExtendedSmartGraphBase {
[969]247  public:
[1979]248
249    typedef ExtendedSmartGraphBase Parent;
250
[1770]251    class Snapshot;
252    friend class Snapshot;
[973]253
[1011]254  protected:
[1770]255    void restoreSnapshot(const Snapshot &s)
[973]256    {
[1457]257      while(s.edge_num<edges.size()) {
[1040]258        Parent::getNotifier(Edge()).erase(Edge(edges.size()-1));
[986]259        nodes[edges.back().target].first_in=edges.back().next_in;
260        nodes[edges.back().source].first_out=edges.back().next_out;
[973]261        edges.pop_back();
262      }
263      //nodes.resize(s.nodes_num);
[1457]264      while(s.node_num<nodes.size()) {
[1040]265        Parent::getNotifier(Node()).erase(Node(nodes.size()-1));
[973]266        nodes.pop_back();
267      }
[1011]268    }   
269
270  public:
[1284]271
272    ///Split a node.
273   
274    ///This function splits a node. First a new node is added to the graph,
275    ///then the source of each outgoing edge of \c n is moved to this new node.
276    ///If \c connect is \c true (this is the default value), then a new edge
277    ///from \c n to the newly created node is also added.
278    ///\return The newly created node.
279    ///
280    ///\note The <tt>Edge</tt>s
281    ///referencing a moved edge remain
282    ///valid. However <tt>InEdge</tt>'s and <tt>OutEdge</tt>'s
283    ///may be invalidated.
[1770]284    ///\warning This functionality cannot be used together with the Snapshot
[1284]285    ///feature.
286    ///\todo It could be implemented in a bit faster way.
287    Node split(Node n, bool connect = true)
288    {
[1718]289      Node b = _split(n,connect);
290      return b;
[1284]291    }
292 
293
[1011]294    ///Class to make a snapshot of the graph and to restrore to it later.
295
296    ///Class to make a snapshot of the graph and to restrore to it later.
297    ///
298    ///The newly added nodes and edges can be removed using the
299    ///restore() function.
300    ///\note After you restore a state, you cannot restore
301    ///a later state, in other word you cannot add again the edges deleted
[1770]302    ///by restore() using another Snapshot instance.
[1011]303    ///
[1770]304    class Snapshot
[1011]305    {
306      SmartGraph *g;
307    protected:
308      friend class SmartGraph;
309      unsigned int node_num;
310      unsigned int edge_num;
311    public:
[1274]312      ///Default constructor.
[1011]313     
[1274]314      ///Default constructor.
[1011]315      ///To actually make a snapshot you must call save().
316      ///
[1770]317      Snapshot() : g(0) {}
[1011]318      ///Constructor that immediately makes a snapshot
319     
320      ///This constructor immediately makes a snapshot of the graph.
321      ///\param _g The graph we make a snapshot of.
[1770]322      Snapshot(SmartGraph &_g) :g(&_g) {
[1011]323        node_num=g->nodes.size();
324        edge_num=g->edges.size();
325      }
326
327      ///Make a snapshot.
328
329      ///Make a snapshot of the graph.
330      ///
331      ///This function can be called more than once. In case of a repeated
332      ///call, the previous snapshot gets lost.
333      ///\param _g The graph we make the snapshot of.
334      void save(SmartGraph &_g)
335      {
336        g=&_g;
337        node_num=g->nodes.size();
338        edge_num=g->edges.size();
339      }
340
341      ///Undo the changes until a snapshot.
342     
343      ///Undo the changes until a snapshot created by save().
344      ///
345      ///\note After you restored a state, you cannot restore
346      ///a later state, in other word you cannot add again the edges deleted
347      ///by restore().
348      ///
349      ///\todo This function might be called undo().
350     
351      void restore()
352      {
[1770]353        g->restoreSnapshot(*this);
[1011]354      }
355    };
[973]356  };
[1034]357
358
359  /**************** Undirected List Graph ****************/
360
[2076]361  typedef UGraphExtender<UndirGraphExtender<SmartGraphBase> >
[1979]362  ExtendedSmartUGraphBase;
[1034]363
[1909]364  /// \ingroup graphs
[1035]365  ///
[1909]366  /// \brief A smart undirected graph class.
[1035]367  ///
[1909]368  /// This is a simple and fast undirected graph implementation.
369  /// It is also quite memory efficient, but at the price
370  /// that <b> it does support only limited (only stack-like)
371  /// node and edge deletions</b>.
372  /// Except from this it conforms to
373  /// the \ref concept::UGraph "UGraph" concept.
374  /// \sa concept::UGraph.
375  ///
376  /// \todo Snapshot hasn't been implemented yet.
377  ///
378  class SmartUGraph : public ExtendedSmartUGraphBase {
[1034]379  };
380
[1820]381
[1910]382  class SmartBpUGraphBase {
[1820]383  public:
384
385    class NodeSetError : public LogicError {
386      virtual const char* exceptionName() const {
[1910]387        return "lemon::SmartBpUGraph::NodeSetError";
[1820]388      }
389    };
390
391  protected:
392
393    struct NodeT {
394      int first;
395      NodeT() {}
396      NodeT(int _first) : first(_first) {}
397    };
398
[2076]399    struct UEdgeT {
[1910]400      int aNode, next_out;
401      int bNode, next_in;
[1820]402    };
403
[1910]404    std::vector<NodeT> aNodes;
405    std::vector<NodeT> bNodes;
[1820]406
[2076]407    std::vector<UEdgeT> edges;
[1820]408
409  public:
410 
411    class Node {
[1910]412      friend class SmartBpUGraphBase;
[1820]413    protected:
414      int id;
415
416      Node(int _id) : id(_id) {}
417    public:
418      Node() {}
419      Node(Invalid) { id = -1; }
420      bool operator==(const Node i) const {return id==i.id;}
421      bool operator!=(const Node i) const {return id!=i.id;}
422      bool operator<(const Node i) const {return id<i.id;}
423    };
424
[2076]425    class UEdge {
[1910]426      friend class SmartBpUGraphBase;
[1820]427    protected:
428      int id;
429
[2076]430      UEdge(int _id) { id = _id;}
[1820]431    public:
[2076]432      UEdge() {}
433      UEdge (Invalid) { id = -1; }
434      bool operator==(const UEdge i) const {return id==i.id;}
435      bool operator!=(const UEdge i) const {return id!=i.id;}
436      bool operator<(const UEdge i) const {return id<i.id;}
[1820]437    };
438
[1910]439    void firstANode(Node& node) const {
440      node.id = 2 * aNodes.size() - 2;
[1820]441      if (node.id < 0) node.id = -1;
442    }
[1910]443    void nextANode(Node& node) const {
[1820]444      node.id -= 2;
445      if (node.id < 0) node.id = -1;
446    }
447
[1910]448    void firstBNode(Node& node) const {
449      node.id = 2 * bNodes.size() - 1;
[1820]450    }
[1910]451    void nextBNode(Node& node) const {
[1820]452      node.id -= 2;
453    }
454
455    void first(Node& node) const {
[1910]456      if (aNodes.size() > 0) {
457        node.id = 2 * aNodes.size() - 2;
[1820]458      } else {
[1910]459        node.id = 2 * bNodes.size() - 1;
[1820]460      }
461    }
462    void next(Node& node) const {
463      node.id -= 2;
464      if (node.id == -2) {
[1910]465        node.id = 2 * bNodes.size() - 1;
[1820]466      }
467    }
468 
[2076]469    void first(UEdge& edge) const {
[1820]470      edge.id = edges.size() - 1;
471    }
[2076]472    void next(UEdge& edge) const {
[1820]473      --edge.id;
474    }
475
[2076]476    void firstFromANode(UEdge& edge, const Node& node) const {
[1820]477      LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
[1910]478      edge.id = aNodes[node.id >> 1].first;
[1820]479    }
[2076]480    void nextFromANode(UEdge& edge) const {
[1910]481      edge.id = edges[edge.id].next_out;
[1820]482    }
483
[2076]484    void firstFromBNode(UEdge& edge, const Node& node) const {
[1820]485      LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
[1910]486      edge.id = bNodes[node.id >> 1].first;
[1820]487    }
[2076]488    void nextFromBNode(UEdge& edge) const {
[1910]489      edge.id = edges[edge.id].next_in;
[1820]490    }
491
492    static int id(const Node& node) {
493      return node.id;
494    }
495    static Node nodeFromId(int id) {
496      return Node(id);
497    }
498    int maxNodeId() const {
[1910]499      return aNodes.size() > bNodes.size() ?
500        aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
[1820]501    }
502 
[2076]503    static int id(const UEdge& edge) {
[1820]504      return edge.id;
505    }
[2076]506    static UEdge uEdgeFromId(int id) {
507      return UEdge(id);
[1820]508    }
[2076]509    int maxUEdgeId() const {
[1820]510      return edges.size();
511    }
512 
[1910]513    static int aNodeId(const Node& node) {
[1820]514      return node.id >> 1;
515    }
[1995]516    static Node fromANodeId(int id) {
[1820]517      return Node(id << 1);
518    }
[1910]519    int maxANodeId() const {
520      return aNodes.size();
[1820]521    }
522
[1910]523    static int bNodeId(const Node& node) {
[1820]524      return node.id >> 1;
525    }
[1910]526    static Node fromBNodeId(int id) {
[1820]527      return Node((id << 1) + 1);
528    }
[1910]529    int maxBNodeId() const {
530      return bNodes.size();
[1820]531    }
532
[2076]533    Node aNode(const UEdge& edge) const {
[1910]534      return Node(edges[edge.id].aNode);
[1820]535    }
[2076]536    Node bNode(const UEdge& edge) const {
[1910]537      return Node(edges[edge.id].bNode);
[1820]538    }
539
[1910]540    static bool aNode(const Node& node) {
[1820]541      return (node.id & 1) == 0;
542    }
543
[1910]544    static bool bNode(const Node& node) {
[1820]545      return (node.id & 1) == 1;
546    }
547
[1910]548    Node addANode() {
[1820]549      NodeT nodeT;
550      nodeT.first = -1;
[1910]551      aNodes.push_back(nodeT);
552      return Node(aNodes.size() * 2 - 2);
[1820]553    }
554
[1910]555    Node addBNode() {
[1820]556      NodeT nodeT;
557      nodeT.first = -1;
[1910]558      bNodes.push_back(nodeT);
559      return Node(bNodes.size() * 2 - 1);
[1820]560    }
561
[2076]562    UEdge addEdge(const Node& source, const Node& target) {
[1820]563      LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
[2076]564      UEdgeT edgeT;
[1820]565      if ((source.id & 1) == 0) {
[1910]566        edgeT.aNode = source.id;
567        edgeT.bNode = target.id;
[1820]568      } else {
[1910]569        edgeT.aNode = target.id;
570        edgeT.bNode = source.id;
[1820]571      }
[1910]572      edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
573      aNodes[edgeT.aNode >> 1].first = edges.size();
574      edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
575      bNodes[edgeT.bNode >> 1].first = edges.size();
[1820]576      edges.push_back(edgeT);
[2076]577      return UEdge(edges.size() - 1);
[1820]578    }
579
580    void clear() {
[1910]581      aNodes.clear();
582      bNodes.clear();
[1820]583      edges.clear();
584    }
585
[2031]586    typedef True NodeNumTag;
587    int nodeNum() const { return aNodes.size() + bNodes.size(); }
588    int aNodeNum() const { return aNodes.size(); }
589    int bNodeNum() const { return bNodes.size(); }
590
591    typedef True EdgeNumTag;
[2076]592    int uEdgeNum() const { return edges.size(); }
[2031]593
[1820]594  };
595
596
[2076]597  typedef BpUGraphExtender<SmartBpUGraphBase> ExtendedSmartBpUGraphBase;
[1820]598
[1910]599  /// \ingroup graphs
600  ///
601  /// \brief A smart bipartite undirected graph class.
602  ///
603  /// This is a simple and fast bipartite undirected graph implementation.
604  /// It is also quite memory efficient, but at the price
605  /// that <b> it does not support node and edge deletions</b>.
606  /// Except from this it conforms to
607  /// the \ref concept::BpUGraph "BpUGraph" concept.
608  /// \sa concept::BpUGraph.
609  ///
610  class SmartBpUGraph : public ExtendedSmartBpUGraphBase {};
[1820]611
[950]612 
[407]613  /// @} 
[921]614} //namespace lemon
[104]615
[157]616
[921]617#endif //LEMON_SMART_GRAPH_H
Note: See TracBrowser for help on using the repository browser.