src/lemon/smart_graph.h
author marci
Mon, 08 Nov 2004 16:33:53 +0000
changeset 970 09f9abe22df2
parent 959 c80ef5912903
child 973 6a6f3ac07b20
permissions -rw-r--r--
partial graph_wrapper changes with graph_factory
     1 /* -*- C++ -*-
     2  * src/lemon/smart_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  */
    16 
    17 #ifndef LEMON_SMART_GRAPH_H
    18 #define LEMON_SMART_GRAPH_H
    19 
    20 ///\ingroup graphs
    21 ///\file
    22 ///\brief SmartGraph and SymSmartGraph classes.
    23 
    24 #include <vector>
    25 
    26 #include <lemon/invalid.h>
    27 
    28 #include <lemon/erasable_graph_extender.h>
    29 #include <lemon/clearable_graph_extender.h>
    30 #include <lemon/extendable_graph_extender.h>
    31 
    32 #include <lemon/idmappable_graph_extender.h>
    33 
    34 #include <lemon/iterable_graph_extender.h>
    35 
    36 #include <lemon/alteration_observer_registry.h>
    37 #include <lemon/default_map.h>
    38 
    39 
    40 #include <lemon/graph_utils.h>
    41 
    42 
    43 namespace lemon {
    44 
    45   /// \addtogroup graphs
    46   /// @{
    47 
    48   ///Base of SmartGraph
    49 
    50   ///Base of SmartGraph
    51   ///
    52   class SmartGraphBase {
    53 
    54     struct NodeT 
    55     {
    56       int first_in,first_out;      
    57       NodeT() : first_in(-1), first_out(-1) {}
    58     };
    59     struct EdgeT 
    60     {
    61       int head, tail, next_in, next_out;      
    62       //FIXME: is this necessary?
    63       EdgeT() : next_in(-1), next_out(-1) {}  
    64     };
    65 
    66     std::vector<NodeT> nodes;
    67 
    68     std::vector<EdgeT> edges;
    69     
    70     
    71   public:
    72 
    73     typedef SmartGraphBase Graph;
    74 
    75     class Node;
    76     class Edge;
    77 
    78     
    79   public:
    80 
    81     SmartGraphBase() : nodes(), edges() { }
    82     SmartGraphBase(const SmartGraphBase &_g) : nodes(_g.nodes), edges(_g.edges) { }
    83     
    84     ///Number of nodes.
    85     int nodeNum() const { return nodes.size(); }
    86     ///Number of edges.
    87     int edgeNum() const { return edges.size(); }
    88 
    89     /// Maximum node ID.
    90     
    91     /// Maximum node ID.
    92     ///\sa id(Node)
    93     int maxNodeId() const { return nodes.size()-1; }
    94     /// Maximum edge ID.
    95     
    96     /// Maximum edge ID.
    97     ///\sa id(Edge)
    98     int maxEdgeId() const { return edges.size()-1; }
    99 
   100     Node tail(Edge e) const { return edges[e.n].tail; }
   101     Node head(Edge e) const { return edges[e.n].head; }
   102 
   103     /// Node ID.
   104     
   105     /// The ID of a valid Node is a nonnegative integer not greater than
   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().
   108     ///
   109     /// The ID of the \ref INVALID node is -1.
   110     ///\return The ID of the node \c v. 
   111     static int id(Node v) { return v.n; }
   112     /// Edge ID.
   113     
   114     /// The ID of a valid Edge is a nonnegative integer not greater than
   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().
   117     ///
   118     /// The ID of the \ref INVALID edge is -1.
   119     ///\return The ID of the edge \c e. 
   120     static int id(Edge e) { return e.n; }
   121 
   122     Node addNode() {
   123       Node n; n.n=nodes.size();
   124       nodes.push_back(NodeT()); //FIXME: Hmmm...
   125       return n;
   126     }
   127     
   128     Edge addEdge(Node u, Node v) {
   129       Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
   130       edges[e.n].tail=u.n; edges[e.n].head=v.n;
   131       edges[e.n].next_out=nodes[u.n].first_out;
   132       edges[e.n].next_in=nodes[v.n].first_in;
   133       nodes[u.n].first_out=nodes[v.n].first_in=e.n;
   134 
   135       return e;
   136     }
   137 
   138     void clear() {
   139       edges.clear();
   140       nodes.clear();
   141     }
   142 
   143 
   144     class Node {
   145       friend class SmartGraphBase;
   146 
   147     protected:
   148       int n;
   149       Node(int nn) {n=nn;}
   150     public:
   151       Node() {}
   152       Node (Invalid) { n=-1; }
   153       bool operator==(const Node i) const {return n==i.n;}
   154       bool operator!=(const Node i) const {return n!=i.n;}
   155       bool operator<(const Node i) const {return n<i.n;}
   156     };
   157     
   158 
   159     class Edge {
   160       friend class SmartGraphBase;
   161 
   162     protected:
   163       int n;
   164       Edge(int nn) {n=nn;}
   165     public:
   166       Edge() { }
   167       Edge (Invalid) { n=-1; }
   168       bool operator==(const Edge i) const {return n==i.n;}
   169       bool operator!=(const Edge i) const {return n!=i.n;}
   170       bool operator<(const Edge i) const {return n<i.n;}
   171     };
   172 
   173     void first(Node& node) const {
   174       node.n = nodes.size() - 1;
   175     }
   176 
   177     static void next(Node& node) {
   178       --node.n;
   179     }
   180 
   181     void first(Edge& edge) const {
   182       edge.n = edges.size() - 1;
   183     }
   184 
   185     static void next(Edge& edge) {
   186       --edge.n;
   187     }
   188 
   189     void firstOut(Edge& edge, const Node& node) const {
   190       edge.n = nodes[node.n].first_out;
   191     }
   192 
   193     void nextOut(Edge& edge) const {
   194       edge.n = edges[edge.n].next_out;
   195     }
   196 
   197     void firstIn(Edge& edge, const Node& node) const {
   198       edge.n = nodes[node.n].first_in;
   199     }
   200     
   201     void nextIn(Edge& edge) const {
   202       edge.n = edges[edge.n].next_in;
   203     }
   204 
   205     Edge _findEdge(Node u,Node v, Edge prev = INVALID) 
   206     {
   207       int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
   208       while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
   209       prev.n=e;
   210       return prev;
   211     }
   212   };
   213 
   214   typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
   215   typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase;
   216   typedef IdMappableGraphExtender<IterableSmartGraphBase> IdMappableSmartGraphBase;
   217   typedef DefaultMappableGraphExtender<IdMappableSmartGraphBase> MappableSmartGraphBase;
   218   typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase;
   219   typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase;
   220 
   221   ///A smart graph class.
   222 
   223   ///This is a simple and fast graph implementation.
   224   ///It is also quite memory efficient, but at the price
   225   ///that <b> it does not support node and edge deletion</b>.
   226   ///It conforms to 
   227   ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
   228   ///\sa concept::ExtendableGraph.
   229   ///
   230   ///\todo Some member functions could be \c static.
   231   ///
   232   ///\todo A possibly useful functionality: a function saveState()
   233   ///(or snapshot() ) would
   234   ///give back a data sturcture X and then the function restoreState(X)
   235   ///(or rollBack() )
   236   ///would remove the nodes and edges added after the call of saveState().
   237   ///Of course it should be used as a stack. (Maybe X is not necessary.)
   238   ///
   239   ///\author Alpar Juttner
   240   class SmartGraph :public ClearableSmartGraphBase {
   241   public:
   242     /// Finds an edge between two nodes.
   243 
   244     /// Finds an edge from node \c u to node \c v.
   245     ///
   246     /// If \c prev is \ref INVALID (this is the default value), then
   247     /// it finds the first edge from \c u to \c v. Otherwise it looks for
   248     /// the next edge from \c u to \c v after \c prev.
   249     /// \return The found edge or \ref INVALID if there is no such an edge.
   250     ///
   251     /// Thus you can iterate through each edge from \c u to \c v as it follows.
   252     /// \code
   253     /// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) {
   254     ///   ...
   255     /// }
   256     /// \endcode
   257     /// \todo Possibly it should be a global function.
   258     Edge findEdge(Node u,Node v, Edge prev = INVALID) 
   259     {
   260       return _findEdge(u,v,prev);
   261     }
   262 };
   263   
   264   template <>
   265   int countNodes<SmartGraph>(const SmartGraph& graph) {
   266     return graph.nodeNum();
   267   }
   268 
   269   template <>
   270   int countEdges<SmartGraph>(const SmartGraph& graph) {
   271     return graph.edgeNum();
   272   }
   273 
   274   /// @}  
   275 } //namespace lemon
   276 
   277 
   278 
   279 
   280 #endif //LEMON_SMART_GRAPH_H