src/lemon/smart_graph.h
author alpar
Wed, 10 Nov 2004 11:42:00 +0000
changeset 974 785062a83f8e
parent 973 6a6f3ac07b20
child 977 48962802d168
permissions -rw-r--r--
Changes in doc.
     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/clearable_graph_extender.h>
    29 #include <lemon/extendable_graph_extender.h>
    30 
    31 #include <lemon/idmappable_graph_extender.h>
    32 
    33 #include <lemon/iterable_graph_extender.h>
    34 
    35 #include <lemon/alteration_observer_registry.h>
    36 #include <lemon/default_map.h>
    37 
    38 
    39 #include <lemon/graph_utils.h>
    40 
    41 
    42 namespace lemon {
    43 
    44   /// \addtogroup graphs
    45   /// @{
    46 
    47   class SmartGraph;
    48   ///Base of SmartGraph
    49 
    50   ///Base of SmartGraph
    51   ///
    52   class SmartGraphBase {
    53 
    54     friend class SmatGraph;
    55 
    56   protected:
    57     struct NodeT 
    58     {
    59       int first_in,first_out;      
    60       NodeT() : first_in(-1), first_out(-1) {}
    61     };
    62     struct EdgeT 
    63     {
    64       int head, tail, next_in, next_out;      
    65       //FIXME: is this necessary?
    66       EdgeT() : next_in(-1), next_out(-1) {}  
    67     };
    68 
    69     std::vector<NodeT> nodes;
    70 
    71     std::vector<EdgeT> edges;
    72     
    73     
    74   public:
    75 
    76     typedef SmartGraphBase Graph;
    77 
    78     class Node;
    79     class Edge;
    80 
    81     
    82   public:
    83 
    84     SmartGraphBase() : nodes(), edges() { }
    85     SmartGraphBase(const SmartGraphBase &_g) : nodes(_g.nodes), edges(_g.edges) { }
    86     
    87     ///Number of nodes.
    88     int nodeNum() const { return nodes.size(); }
    89     ///Number of edges.
    90     int edgeNum() const { return edges.size(); }
    91 
    92     /// Maximum node ID.
    93     
    94     /// Maximum node ID.
    95     ///\sa id(Node)
    96     int maxNodeId() const { return nodes.size()-1; }
    97     /// Maximum edge ID.
    98     
    99     /// Maximum edge ID.
   100     ///\sa id(Edge)
   101     int maxEdgeId() const { return edges.size()-1; }
   102 
   103     Node tail(Edge e) const { return edges[e.n].tail; }
   104     Node head(Edge e) const { return edges[e.n].head; }
   105 
   106     /// Node ID.
   107     
   108     /// The ID of a valid Node is a nonnegative integer not greater than
   109     /// \ref maxNodeId(). The range of the ID's is not surely continuous
   110     /// and the greatest node ID can be actually less then \ref maxNodeId().
   111     ///
   112     /// The ID of the \ref INVALID node is -1.
   113     ///\return The ID of the node \c v. 
   114     static int id(Node v) { return v.n; }
   115     /// Edge ID.
   116     
   117     /// The ID of a valid Edge is a nonnegative integer not greater than
   118     /// \ref maxEdgeId(). The range of the ID's is not surely continuous
   119     /// and the greatest edge ID can be actually less then \ref maxEdgeId().
   120     ///
   121     /// The ID of the \ref INVALID edge is -1.
   122     ///\return The ID of the edge \c e. 
   123     static int id(Edge e) { return e.n; }
   124 
   125     Node addNode() {
   126       Node n; n.n=nodes.size();
   127       nodes.push_back(NodeT()); //FIXME: Hmmm...
   128       return n;
   129     }
   130     
   131     Edge addEdge(Node u, Node v) {
   132       Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
   133       edges[e.n].tail=u.n; edges[e.n].head=v.n;
   134       edges[e.n].next_out=nodes[u.n].first_out;
   135       edges[e.n].next_in=nodes[v.n].first_in;
   136       nodes[u.n].first_out=nodes[v.n].first_in=e.n;
   137 
   138       return e;
   139     }
   140 
   141     void clear() {
   142       edges.clear();
   143       nodes.clear();
   144     }
   145 
   146 
   147     class Node {
   148       friend class SmartGraphBase;
   149       friend class SmartGraph;
   150 
   151     protected:
   152       int n;
   153       ///\todo It should be removed (or at least define a setToId() instead).
   154       ///
   155       Node(int nn) {n=nn;}
   156     public:
   157       Node() {}
   158       Node (Invalid) { n=-1; }
   159       bool operator==(const Node i) const {return n==i.n;}
   160       bool operator!=(const Node i) const {return n!=i.n;}
   161       bool operator<(const Node i) const {return n<i.n;}
   162     };
   163     
   164 
   165     class Edge {
   166       friend class SmartGraphBase;
   167       friend class SmartGraph;
   168 
   169     protected:
   170       int n;
   171       ///\todo It should be removed (or at least define a setToId() instead).
   172       ///
   173       Edge(int nn) {n=nn;}
   174     public:
   175       Edge() { }
   176       Edge (Invalid) { n=-1; }
   177       bool operator==(const Edge i) const {return n==i.n;}
   178       bool operator!=(const Edge i) const {return n!=i.n;}
   179       bool operator<(const Edge i) const {return n<i.n;}
   180     };
   181 
   182     void first(Node& node) const {
   183       node.n = nodes.size() - 1;
   184     }
   185 
   186     static void next(Node& node) {
   187       --node.n;
   188     }
   189 
   190     void first(Edge& edge) const {
   191       edge.n = edges.size() - 1;
   192     }
   193 
   194     static void next(Edge& edge) {
   195       --edge.n;
   196     }
   197 
   198     void firstOut(Edge& edge, const Node& node) const {
   199       edge.n = nodes[node.n].first_out;
   200     }
   201 
   202     void nextOut(Edge& edge) const {
   203       edge.n = edges[edge.n].next_out;
   204     }
   205 
   206     void firstIn(Edge& edge, const Node& node) const {
   207       edge.n = nodes[node.n].first_in;
   208     }
   209     
   210     void nextIn(Edge& edge) const {
   211       edge.n = edges[edge.n].next_in;
   212     }
   213 
   214     Edge _findEdge(Node u,Node v, Edge prev = INVALID) 
   215     {
   216       int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
   217       while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
   218       prev.n=e;
   219       return prev;
   220     }
   221 
   222   };
   223 
   224   typedef AlterableGraphExtender<SmartGraphBase> AlterableSmartGraphBase;
   225   typedef IterableGraphExtender<AlterableSmartGraphBase> IterableSmartGraphBase;
   226   typedef IdMappableGraphExtender<IterableSmartGraphBase> IdMappableSmartGraphBase;
   227   typedef DefaultMappableGraphExtender<IdMappableSmartGraphBase> MappableSmartGraphBase;
   228   typedef ExtendableGraphExtender<MappableSmartGraphBase> ExtendableSmartGraphBase;
   229   typedef ClearableGraphExtender<ExtendableSmartGraphBase> ClearableSmartGraphBase;
   230 
   231   ///A smart graph class.
   232 
   233   ///This is a simple and fast graph implementation.
   234   ///It is also quite memory efficient, but at the price
   235   ///that <b> it does support only limited (only stack-like)
   236   ///node and edge deletions</b>.
   237   ///It conforms to 
   238   ///the \ref concept::ExtendableGraph "ExtendableGraph" concept.
   239   ///\sa concept::ExtendableGraph.
   240   ///
   241   ///\todo Some member functions could be \c static.
   242   ///
   243   ///\author Alpar Juttner
   244   class SmartGraph :public ClearableSmartGraphBase {
   245   public:
   246     /// Finds an edge between two nodes.
   247     
   248     /// Finds an edge from node \c u to node \c v.
   249     ///
   250     /// If \c prev is \ref INVALID (this is the default value), then
   251     /// it finds the first edge from \c u to \c v. Otherwise it looks for
   252     /// the next edge from \c u to \c v after \c prev.
   253     /// \return The found edge or \ref INVALID if there is no such an edge.
   254     ///
   255     /// Thus you can iterate through each edge from \c u to \c v as it follows.
   256     /// \code
   257     /// for(Edge e=G.findEdge(u,v);e!=INVALID;e=G.findEdge(u,v,e)) {
   258     ///   ...
   259     /// }
   260     /// \endcode
   261     /// \todo Possibly it should be a global function.
   262     Edge findEdge(Node u,Node v, Edge prev = INVALID) 
   263     {
   264       return _findEdge(u,v,prev);
   265     }
   266     
   267     ///Internal data structure to store snapshots
   268     
   269     ///\ingroup graphs
   270     ///\sa makeSnapShot()
   271     ///\sa rollBack()
   272     struct SnapShot 
   273     {
   274       unsigned int node_num;
   275       unsigned int edge_num;
   276     };
   277     
   278     ///Make a snapshot of the graph.
   279 
   280     ///Make a snapshot of the graph.
   281     ///
   282     ///The newly added nodes and edges can be removed using the
   283     ///rollBack() function.
   284     ///
   285     ///\return An stucture SnapShot describing the pesent state of the
   286     ///graph.
   287     ///\note After you rolled back to a state, you cannot roll "back" to
   288     ///a later state, in other word you cannot add again the edges deleted
   289     ///by rollBack().
   290     ///\todo This function might be called saveState() or getState().
   291     SnapShot makeSnapShot() 
   292     {
   293       SnapShot s;
   294       s.node_num=nodes.size();
   295       s.edge_num=edges.size();
   296       return s;
   297     }
   298     
   299     ///Undo the changes until a snapshot.
   300 
   301     ///Undo the changes until a snapshot created by makeSnapShot().
   302     ///
   303     ///\param s an internal stucture given back by makeSnapShot()
   304     ///\note After you rolled back to a state, you cannot "roll forward" to
   305     ///a later state, in other word you cannot add again the edges deleted
   306     ///by rollBack().
   307     ///
   308     ///\todo This function might be called undo().
   309     
   310     void rollBack(const SnapShot &s)
   311     {
   312       while(s.edge_num>edges.size()) {
   313 	edge_observers.erase(Edge(edges.size()-1));
   314 	nodes[edges.back().head].first_in=edges.back().next_in;
   315 	nodes[edges.back().tail].first_out=edges.back().next_out;
   316 	edges.pop_back();
   317       }
   318       //nodes.resize(s.nodes_num);
   319       while(s.node_num>nodes.size()) {
   320 	node_observers.erase(Node(nodes.size()-1));
   321 	nodes.pop_back();
   322       }
   323     }
   324   };
   325   
   326   template <>
   327   int countNodes<SmartGraph>(const SmartGraph& graph) {
   328     return graph.nodeNum();
   329   }
   330 
   331   template <>
   332   int countEdges<SmartGraph>(const SmartGraph& graph) {
   333     return graph.edgeNum();
   334   }
   335 
   336   /// @}  
   337 } //namespace lemon
   338 
   339 
   340 
   341 
   342 #endif //LEMON_SMART_GRAPH_H