COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/smart_graph.h @ 816:a39579c35dd7

Last change on this file since 816:a39579c35dd7 was 813:65144c52969c, checked in by Alpar Juttner, 20 years ago
  • maxEdgeId() and maxNodeId() now works as their names suggest.
  • maxEdgeId(), maxNodeId(), nodeNum() and edgeNum() are documented.
File size: 10.0 KB
RevLine 
[105]1// -*- mode:C++ -*-
2
[185]3#ifndef HUGO_SMART_GRAPH_H
4#define HUGO_SMART_GRAPH_H
[104]5
[491]6///\ingroup graphs
[242]7///\file
8///\brief SmartGraph and SymSmartGraph classes.
9
[104]10#include <vector>
[782]11#include <climits>
[104]12
[542]13#include <hugo/invalid.h>
[157]14
[798]15#include <hugo/default_map_factory.h>
[782]16#include <hugo/sym_map_factory.h>
17#include <hugo/map_registry.h>
18
19#include <hugo/map_defines.h>
20
[105]21namespace hugo {
[104]22
[407]23/// \addtogroup graphs
24/// @{
[782]25//  class SymSmartGraph;
[185]26
[186]27  ///A smart graph class.
28
29  ///This is a simple and fast graph implementation.
30  ///It is also quite memory efficient, but at the price
31  ///that <b> it does not support node and edge deletion</b>.
[242]32  ///It conforms to the graph interface documented under
[186]33  ///the description of \ref GraphSkeleton.
34  ///\sa \ref GraphSkeleton.
[402]35  ///
36  ///\todo Some member functions could be \c static.
[753]37  ///
38  ///\todo A possibly useful functionality: a function saveState() would
39  ///give back a data sturcture X and then the function restoreState(X)
40  ///would remove the nodes and edges added after the call of saveState().
41  ///Of course it should be used as a stack. (Maybe X is not necessary.)
42  ///
[456]43  ///\author Alpar Juttner
[104]44  class SmartGraph {
45
46    struct NodeT
47    {
48      int first_in,first_out;     
[157]49      NodeT() : first_in(-1), first_out(-1) {}
[104]50    };
51    struct EdgeT
52    {
53      int head, tail, next_in, next_out;     
54      //FIXME: is this necessary?
[157]55      EdgeT() : next_in(-1), next_out(-1) {} 
[104]56    };
57
58    std::vector<NodeT> nodes;
[129]59
[104]60    std::vector<EdgeT> edges;
61   
[185]62   
[104]63  public:
[782]64
65    typedef SmartGraph Graph;
[104]66
[164]67    class Node;
68    class Edge;
[108]69
[164]70    class NodeIt;
71    class EdgeIt;
[104]72    class OutEdgeIt;
73    class InEdgeIt;
74   
[782]75    CREATE_MAP_REGISTRIES;
[798]76    CREATE_MAPS(DefaultMapFactory);
[104]77   
78  public:
79
80    SmartGraph() : nodes(), edges() { }
[136]81    SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
[104]82   
[813]83    ///Number of nodes.
84    int nodeNum() const { return nodes.size(); }
85    ///Number of edges.
86    int edgeNum() const { return edges.size(); }
[104]87
[813]88    /// Maximum node ID.
89   
90    /// Maximum node ID.
91    ///\sa id(Node)
92    int maxNodeId() const { return nodes.size()-1; }
93    /// Maximum edge ID.
94   
95    /// Maximum edge ID.
96    ///\sa id(Edge)
97    int maxEdgeId() const { return edges.size()-1; }
[108]98
[164]99    Node tail(Edge e) const { return edges[e.n].tail; }
100    Node head(Edge e) const { return edges[e.n].head; }
[104]101
[164]102    NodeIt& first(NodeIt& v) const {
103      v=NodeIt(*this); return v; }
104    EdgeIt& first(EdgeIt& e) const {
105      e=EdgeIt(*this); return e; }
106    OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
[104]107      e=OutEdgeIt(*this,v); return e; }
[164]108    InEdgeIt& first(InEdgeIt& e, const Node v) const {
[104]109      e=InEdgeIt(*this,v); return e; }
110
[813]111    /// Node ID.
112   
113    /// The ID of a valid Node is a nonnegative integer not greater than
114    /// \ref maxNodeId(). The range of the ID's is not surely continuous
115    /// and the greatest node ID can be actually less then \ref maxNodeId().
116    ///
117    /// The ID of the \ref INVALID node is -1.
118    ///\return The ID of the node \c v.
[713]119    static int id(Node v) { return v.n; }
[813]120    /// Edge ID.
121   
122    /// The ID of a valid Edge is a nonnegative integer not greater than
123    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
124    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
125    ///
126    /// The ID of the \ref INVALID edge is -1.
127    ///\return The ID of the edge \c e.
[713]128    static int id(Edge e) { return e.n; }
[104]129
[164]130    Node addNode() {
131      Node n; n.n=nodes.size();
[104]132      nodes.push_back(NodeT()); //FIXME: Hmmm...
[108]133
[782]134     
135      node_maps.add(n);
[104]136      return n;
137    }
[108]138   
[164]139    Edge addEdge(Node u, Node v) {
140      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
[104]141      edges[e.n].tail=u.n; edges[e.n].head=v.n;
142      edges[e.n].next_out=nodes[u.n].first_out;
143      edges[e.n].next_in=nodes[v.n].first_in;
144      nodes[u.n].first_out=nodes[v.n].first_in=e.n;
[108]145
[782]146      edge_maps.add(e);
[108]147
[104]148      return e;
149    }
150
[774]151    /// Finds an edge between two nodes.
152
153    /// Finds an edge from node \c u to node \c v.
154    ///
155    /// If \c prev is \ref INVALID (this is the default value), then
156    /// It finds the first edge from \c u to \c v. Otherwise it looks for
157    /// the next edge from \c u to \c v after \c prev.
158    /// \return The found edge or INVALID if there is no such an edge.
159    Edge findEdge(Node u,Node v, Edge prev = INVALID)
160    {
161      int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
162      while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
163      prev.n=e;
164      return prev;
165    }
166   
[782]167    void clear() {
168      edge_maps.clear();
169      edges.clear();
170      node_maps.clear();
171      nodes.clear();
172    }
[104]173
[164]174    class Node {
[104]175      friend class SmartGraph;
176      template <typename T> friend class NodeMap;
177     
[164]178      friend class Edge;
[104]179      friend class OutEdgeIt;
180      friend class InEdgeIt;
[164]181      friend class SymEdge;
[104]182
183    protected:
184      int n;
[722]185      friend int SmartGraph::id(Node v);
[164]186      Node(int nn) {n=nn;}
[104]187    public:
[164]188      Node() {}
[503]189      Node (Invalid) { n=-1; }
[164]190      bool operator==(const Node i) const {return n==i.n;}
191      bool operator!=(const Node i) const {return n!=i.n;}
192      bool operator<(const Node i) const {return n<i.n;}
[774]193      //      ///Validity check
194      //      operator bool() { return n!=-1; }
[104]195    };
196   
[164]197    class NodeIt : public Node {
[774]198      const SmartGraph *G;
[104]199      friend class SmartGraph;
200    public:
[402]201      NodeIt() : Node() { }
[774]202      NodeIt(const SmartGraph& _G,Node n) : Node(n), G(&_G) { }
[402]203      NodeIt(Invalid i) : Node(i) { }
[774]204      NodeIt(const SmartGraph& _G) : Node(_G.nodes.size()?0:-1), G(&_G) { }
205      NodeIt &operator++() {
206        n=(n+2)%(G->nodes.size()+1)-1;
207        return *this;
208      }
209//       ///Validity check
210//       operator bool() { return Node::operator bool(); }     
[104]211    };
212
[164]213    class Edge {
[104]214      friend class SmartGraph;
215      template <typename T> friend class EdgeMap;
[185]216
217      //template <typename T> friend class SymSmartGraph::SymEdgeMap;     
218      //friend Edge SymSmartGraph::opposite(Edge) const;
[104]219     
[164]220      friend class Node;
[104]221      friend class NodeIt;
222    protected:
223      int n;
[722]224      friend int SmartGraph::id(Edge e);
[157]225
[706]226    public:
227      /// An Edge with id \c n.
228
229      /// \bug It should be
230      /// obtained by a member function of the Graph.
[164]231      Edge(int nn) {n=nn;}
232      Edge() { }
[174]233      Edge (Invalid) { n=-1; }
[164]234      bool operator==(const Edge i) const {return n==i.n;}
235      bool operator!=(const Edge i) const {return n!=i.n;}
236      bool operator<(const Edge i) const {return n<i.n;}
[185]237      ///\bug This is a workaround until somebody tells me how to
238      ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
239      int &idref() {return n;}
[774]240      const int &idref() const {return n;}
241//       ///Validity check
242//       operator bool() { return n!=-1; }
243   };
[104]244   
[164]245    class EdgeIt : public Edge {
[774]246      const SmartGraph *G;
[104]247      friend class SmartGraph;
248    public:
[774]249      EdgeIt(const SmartGraph& _G) : Edge(_G.edges.size()-1), G(&_G) { }
250      EdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
[164]251      EdgeIt (Invalid i) : Edge(i) { }
252      EdgeIt() : Edge() { }
[185]253      ///\bug This is a workaround until somebody tells me how to
254      ///make class \c SymSmartGraph::SymEdgeMap friend of Edge
255      int &idref() {return n;}
[774]256      EdgeIt &operator++() { --n; return *this; }
257//       ///Validity check
258//       operator bool() { return Edge::operator bool(); }     
[104]259    };
260   
[164]261    class OutEdgeIt : public Edge {
[774]262      const SmartGraph *G;
[104]263      friend class SmartGraph;
264    public:
[164]265      OutEdgeIt() : Edge() { }
[774]266      OutEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
[164]267      OutEdgeIt (Invalid i) : Edge(i) { }
[157]268
[774]269      OutEdgeIt(const SmartGraph& _G,const Node v)
270        : Edge(_G.nodes[v.n].first_out), G(&_G) {}
271      OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
272//       ///Validity check
273//       operator bool() { return Edge::operator bool(); }     
[104]274    };
275   
[164]276    class InEdgeIt : public Edge {
[774]277      const SmartGraph *G;
[104]278      friend class SmartGraph;
279    public:
[164]280      InEdgeIt() : Edge() { }
[774]281      InEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
[164]282      InEdgeIt (Invalid i) : Edge(i) { }
[774]283      InEdgeIt(const SmartGraph& _G,Node v)
284        : Edge(_G.nodes[v.n].first_in), G(&_G) { }
285      InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
286//       ///Validity check
287//       operator bool() { return Edge::operator bool(); }     
[104]288    };
[105]289
[104]290  };
[185]291
292  ///Graph for bidirectional edges.
293
294  ///The purpose of this graph structure is to handle graphs
295  ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
[186]296  ///of oppositely directed edges.
297  ///There is a new edge map type called
298  ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
299  ///that complements this
300  ///feature by
301  ///storing shared values for the edge pairs. The usual
302  ///\ref GraphSkeleton::EdgeMap "EdgeMap"
303  ///can be used
[185]304  ///as well.
305  ///
[186]306  ///The oppositely directed edge can also be obtained easily
307  ///using \ref opposite.
308  ///\warning It shares the similarity with \ref SmartGraph that
309  ///it is not possible to delete edges or nodes from the graph.
310  //\sa \ref SmartGraph.
[185]311
312  class SymSmartGraph : public SmartGraph
313  {
314  public:
[782]315    typedef SymSmartGraph Graph;
316
317    KEEP_NODE_MAP(SmartGraph);
318    KEEP_EDGE_MAP(SmartGraph);
319
320    CREATE_SYM_EDGE_MAP_REGISTRY;
[798]321    CREATE_SYM_EDGE_MAP_FACTORY(DefaultMapFactory);
[782]322    IMPORT_SYM_EDGE_MAP(SymEdgeMapFactory);
[186]323
[185]324    SymSmartGraph() : SmartGraph() { }
325    SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
[398]326    ///Adds a pair of oppositely directed edges to the graph.
[185]327    Edge addEdge(Node u, Node v)
328    {
329      Edge e = SmartGraph::addEdge(u,v);
[798]330      Edge f = SmartGraph::addEdge(v,u);
331      sym_edge_maps.add(e);
332      sym_edge_maps.add(f);
[185]333      return e;
334    }
335
[186]336    ///The oppositely directed edge.
337
338    ///Returns the oppositely directed
339    ///pair of the edge \c e.
[713]340    static Edge opposite(Edge e)
[185]341    {
342      Edge f;
343      f.idref() = e.idref() - 2*(e.idref()%2) + 1;
344      return f;
345    }
346   
347
348  };
349 
[407]350  /// @} 
[105]351} //namespace hugo
[104]352
[157]353
354
355
[590]356#endif //HUGO_SMART_GRAPH_H
Note: See TracBrowser for help on using the repository browser.