src/lemon/full_graph.h
author klao
Thu, 03 Feb 2005 19:24:42 +0000
changeset 1120 5d8d64bde9c5
parent 1039 bd01c5a3f989
child 1161 1c9658d51c8d
permissions -rw-r--r--
Latest LEMON exception and assert concepts
     1 /* -*- C++ -*-
     2  * src/lemon/full_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_FULL_GRAPH_H
    18 #define LEMON_FULL_GRAPH_H
    19 
    20 #include <cmath>
    21 
    22 
    23 #include <lemon/iterable_graph_extender.h>
    24 #include <lemon/alteration_notifier.h>
    25 #include <lemon/default_map.h>
    26 
    27 #include <lemon/invalid.h>
    28 #include <lemon/utility.h>
    29 
    30 
    31 ///\ingroup graphs
    32 ///\file
    33 ///\brief FullGraph and SymFullGraph classes.
    34 
    35 
    36 namespace lemon {
    37 
    38 /// \addtogroup graphs
    39 /// @{
    40 
    41   class FullGraphBase {
    42     int NodeNum;
    43     int EdgeNum;
    44   public:
    45 
    46     typedef FullGraphBase Graph;
    47 
    48     class Node;
    49     class Edge;
    50 
    51   public:
    52 
    53     FullGraphBase() {}
    54 
    55 
    56     ///Creates a full graph with \c n nodes.
    57     void construct(int n) { NodeNum = n; EdgeNum = n * n; }
    58     ///
    59     //    FullGraphBase(const FullGraphBase &_g)
    60     //      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
    61     
    62     typedef True NodeNumTag;
    63     typedef True EdgeNumTag;
    64 
    65     ///Number of nodes.
    66     int nodeNum() const { return NodeNum; }
    67     ///Number of edges.
    68     int edgeNum() const { return EdgeNum; }
    69 
    70     /// Maximum node ID.
    71     
    72     /// Maximum node ID.
    73     ///\sa id(Node)
    74     int maxId(Node = INVALID) const { return NodeNum-1; }
    75     /// Maximum edge ID.
    76     
    77     /// Maximum edge ID.
    78     ///\sa id(Edge)
    79     int maxId(Edge = INVALID) const { return EdgeNum-1; }
    80 
    81     Node source(Edge e) const { return e.id % NodeNum; }
    82     Node target(Edge e) const { return e.id / NodeNum; }
    83 
    84 
    85     /// Node ID.
    86     
    87     /// The ID of a valid Node is a nonnegative integer not greater than
    88     /// \ref maxNodeId(). The range of the ID's is not surely continuous
    89     /// and the greatest node ID can be actually less then \ref maxNodeId().
    90     ///
    91     /// The ID of the \ref INVALID node is -1.
    92     ///\return The ID of the node \c v. 
    93 
    94     static int id(Node v) { return v.id; }
    95     /// Edge ID.
    96     
    97     /// The ID of a valid Edge is a nonnegative integer not greater than
    98     /// \ref maxEdgeId(). The range of the ID's is not surely continuous
    99     /// and the greatest edge ID can be actually less then \ref maxEdgeId().
   100     ///
   101     /// The ID of the \ref INVALID edge is -1.
   102     ///\return The ID of the edge \c e. 
   103     static int id(Edge e) { return e.id; }
   104 
   105     static Node fromId(int id, Node) { return Node(id);}
   106     
   107     static Edge fromId(int id, Edge) { return Edge(id);}
   108 
   109     /// Finds an edge between two nodes.
   110     
   111     /// Finds an edge from node \c u to node \c v.
   112     ///
   113     /// If \c prev is \ref INVALID (this is the default value), then
   114     /// It finds the first edge from \c u to \c v. Otherwise it looks for
   115     /// the next edge from \c u to \c v after \c prev.
   116     /// \return The found edge or INVALID if there is no such an edge.
   117     Edge findEdge(Node u,Node v, Edge prev = INVALID) 
   118     {
   119       return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
   120     }
   121     
   122       
   123     class Node {
   124       friend class FullGraphBase;
   125 
   126     protected:
   127       int id;
   128       Node(int _id) { id = _id;}
   129     public:
   130       Node() {}
   131       Node (Invalid) { id = -1; }
   132       bool operator==(const Node node) const {return id == node.id;}
   133       bool operator!=(const Node node) const {return id != node.id;}
   134       bool operator<(const Node node) const {return id < node.id;}
   135     };
   136     
   137 
   138 
   139     class Edge {
   140       friend class FullGraphBase;
   141       
   142     protected:
   143       int id;  // NodeNum * target + source;
   144 
   145       Edge(int _id) : id(_id) {}
   146 
   147       Edge(const FullGraphBase& _graph, int source, int target) 
   148 	: id(_graph.NodeNum * target+source) {}
   149     public:
   150       Edge() { }
   151       Edge (Invalid) { id = -1; }
   152       bool operator==(const Edge edge) const {return id == edge.id;}
   153       bool operator!=(const Edge edge) const {return id != edge.id;}
   154       bool operator<(const Edge edge) const {return id < edge.id;}
   155     };
   156 
   157     void first(Node& node) const {
   158       node.id = NodeNum-1;
   159     }
   160 
   161     static void next(Node& node) {
   162       --node.id;
   163     }
   164 
   165     void first(Edge& edge) const {
   166       edge.id = EdgeNum-1;
   167     }
   168 
   169     static void next(Edge& edge) {
   170       --edge.id;
   171     }
   172 
   173     void firstOut(Edge& edge, const Node& node) const {
   174       edge.id = EdgeNum + node.id - NodeNum;
   175     }
   176 
   177     void nextOut(Edge& edge) const {
   178       edge.id -= NodeNum;
   179       if (edge.id < 0) edge.id = -1;
   180     }
   181 
   182     void firstIn(Edge& edge, const Node& node) const {
   183       edge.id = node.id * NodeNum;
   184     }
   185     
   186     void nextIn(Edge& edge) const {
   187       ++edge.id;
   188       if (edge.id % NodeNum == 0) edge.id = -1;
   189     }
   190 
   191   };
   192 
   193 
   194   typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase;
   195   typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase;
   196   typedef DefaultMappableGraphExtender<IterableFullGraphBase> MappableFullGraphBase;
   197 
   198   ///A full graph class.
   199 
   200   ///This is a simple and fast directed full graph implementation.
   201   ///It is completely static, so you can neither add nor delete either
   202   ///edges or nodes.
   203   ///Thus it conforms to
   204   ///the \ref concept::StaticGraph "StaticGraph" concept
   205   ///\sa concept::StaticGraph.
   206   ///
   207   ///\author Alpar Juttner
   208   class FullGraph : public MappableFullGraphBase {
   209   public:
   210 
   211     FullGraph(int n) { construct(n); }
   212   };
   213 
   214 
   215   /// Base graph class for UndirFullGraph.
   216 
   217   class UndirFullGraphBase {
   218     int NodeNum;
   219     int EdgeNum;
   220   public:
   221 
   222     typedef UndirFullGraphBase Graph;
   223 
   224     class Node;
   225     class Edge;
   226 
   227   public:
   228 
   229     UndirFullGraphBase() {}
   230 
   231 
   232     ///Creates a full graph with \c n nodes.
   233     void construct(int n) { NodeNum = n; EdgeNum = n * (n - 1) / 2; }
   234     ///
   235     //    FullGraphBase(const FullGraphBase &_g)
   236     //      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
   237     
   238     typedef True NodeNumTag;
   239     typedef True EdgeNumTag;
   240 
   241     ///Number of nodes.
   242     int nodeNum() const { return NodeNum; }
   243     ///Number of edges.
   244     int edgeNum() const { return EdgeNum; }
   245 
   246     /// Maximum node ID.
   247     
   248     /// Maximum node ID.
   249     ///\sa id(Node)
   250     int maxId(Node = INVALID) const { return NodeNum-1; }
   251     /// Maximum edge ID.
   252     
   253     /// Maximum edge ID.
   254     ///\sa id(Edge)
   255     int maxId(Edge = INVALID) const { return EdgeNum-1; }
   256 
   257     Node source(Edge e) const { 
   258       /// \todo we may do it faster
   259       return ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2; 
   260     }
   261 
   262     Node target(Edge e) const { 
   263       int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
   264       return e.id - (source) * (source - 1) / 2; 
   265     }
   266 
   267 
   268     /// Node ID.
   269     
   270     /// The ID of a valid Node is a nonnegative integer not greater than
   271     /// \ref maxNodeId(). The range of the ID's is not surely continuous
   272     /// and the greatest node ID can be actually less then \ref maxNodeId().
   273     ///
   274     /// The ID of the \ref INVALID node is -1.
   275     ///\return The ID of the node \c v. 
   276 
   277     static int id(Node v) { return v.id; }
   278     /// Edge ID.
   279     
   280     /// The ID of a valid Edge is a nonnegative integer not greater than
   281     /// \ref maxEdgeId(). The range of the ID's is not surely continuous
   282     /// and the greatest edge ID can be actually less then \ref maxEdgeId().
   283     ///
   284     /// The ID of the \ref INVALID edge is -1.
   285     ///\return The ID of the edge \c e. 
   286     static int id(Edge e) { return e.id; }
   287 
   288     /// Finds an edge between two nodes.
   289     
   290     /// Finds an edge from node \c u to node \c v.
   291     ///
   292     /// If \c prev is \ref INVALID (this is the default value), then
   293     /// It finds the first edge from \c u to \c v. Otherwise it looks for
   294     /// the next edge from \c u to \c v after \c prev.
   295     /// \return The found edge or INVALID if there is no such an edge.
   296     Edge findEdge(Node u,Node v, Edge prev = INVALID) 
   297     {
   298       return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
   299     }
   300     
   301       
   302     class Node {
   303       friend class UndirFullGraphBase;
   304 
   305     protected:
   306       int id;
   307       Node(int _id) { id = _id;}
   308     public:
   309       Node() {}
   310       Node (Invalid) { id = -1; }
   311       bool operator==(const Node node) const {return id == node.id;}
   312       bool operator!=(const Node node) const {return id != node.id;}
   313       bool operator<(const Node node) const {return id < node.id;}
   314     };
   315     
   316 
   317 
   318     class Edge {
   319       friend class UndirFullGraphBase;
   320       
   321     protected:
   322       int id;  // NodeNum * target + source;
   323 
   324       Edge(int _id) : id(_id) {}
   325 
   326       Edge(const UndirFullGraphBase& _graph, int source, int target) 
   327 	: id(_graph.NodeNum * target+source) {}
   328     public:
   329       Edge() { }
   330       Edge (Invalid) { id = -1; }
   331       bool operator==(const Edge edge) const {return id == edge.id;}
   332       bool operator!=(const Edge edge) const {return id != edge.id;}
   333       bool operator<(const Edge edge) const {return id < edge.id;}
   334     };
   335 
   336     void first(Node& node) const {
   337       node.id = NodeNum-1;
   338     }
   339 
   340     static void next(Node& node) {
   341       --node.id;
   342     }
   343 
   344     void first(Edge& edge) const {
   345       edge.id = EdgeNum-1;
   346     }
   347 
   348     static void next(Edge& edge) {
   349       --edge.id;
   350     }
   351 
   352     void firstOut(Edge& edge, const Node& node) const {      
   353       edge.id = node.id != 0 ? node.id * (node.id - 1) / 2 : -1;
   354     }
   355 
   356     /// \todo with specialized iterators we can make faster iterating
   357     void nextOut(Edge& e) const {
   358       int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
   359       int target = e.id - (source) * (source - 1) / 2; 
   360       ++target;
   361       e.id = target < source ? source * (source - 1) / 2 + target : -1;
   362     }
   363 
   364     void firstIn(Edge& edge, const Node& node) const {
   365       edge.id = node.id * (node.id + 1) / 2 - 1;
   366     }
   367     
   368     void nextIn(Edge& e) const {
   369       int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
   370       int target = e.id - (source) * (source - 1) / 2; ++target;
   371       ++source;
   372       e.id = source < NodeNum ? source * (source - 1) / 2 + target : -1;
   373     }
   374 
   375   };
   376 
   377   /// \todo UndirFullGraph from the UndirFullGraphBase
   378 
   379   
   380 
   381   /// @}  
   382 
   383 } //namespace lemon
   384 
   385 
   386 #endif //LEMON_FULL_GRAPH_H