lemon/full_graph.h
author alpar
Tue, 16 Aug 2005 16:42:04 +0000
changeset 1629 a245eff568a6
parent 1555 48769ac7ec32
child 1643 9285f3777553
permissions -rw-r--r--
lp.h added
     1 /* -*- C++ -*-
     2  * lemon/full_graph.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, 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/bits/iterable_graph_extender.h>
    24 #include <lemon/bits/alteration_notifier.h>
    25 #include <lemon/bits/default_map.h>
    26 
    27 #include <lemon/bits/undir_graph_extender.h>
    28 
    29 #include <lemon/invalid.h>
    30 #include <lemon/utility.h>
    31 
    32 
    33 ///\ingroup graphs
    34 ///\file
    35 ///\brief FullGraph and SymFullGraph classes.
    36 
    37 
    38 namespace lemon {
    39 
    40   class FullGraphBase {
    41     int _nodeNum;
    42     int _edgeNum;
    43   public:
    44 
    45     typedef FullGraphBase Graph;
    46 
    47     class Node;
    48     class Edge;
    49 
    50   public:
    51 
    52     FullGraphBase() {}
    53 
    54 
    55     ///Creates a full graph with \c n nodes.
    56     void construct(int n) { _nodeNum = n; _edgeNum = n * n; }
    57     ///
    58     //    FullGraphBase(const FullGraphBase &_g)
    59     //      : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
    60     
    61     typedef True NodeNumTag;
    62     typedef True EdgeNumTag;
    63 
    64     ///Number of nodes.
    65     int nodeNum() const { return _nodeNum; }
    66     ///Number of edges.
    67     int edgeNum() const { return _edgeNum; }
    68 
    69     /// Maximum node ID.
    70     
    71     /// Maximum node ID.
    72     ///\sa id(Node)
    73     int maxId(Node = INVALID) const { return _nodeNum-1; }
    74     /// Maximum edge ID.
    75     
    76     /// Maximum edge ID.
    77     ///\sa id(Edge)
    78     int maxId(Edge = INVALID) const { return _edgeNum-1; }
    79 
    80     Node source(Edge e) const { return e.id % _nodeNum; }
    81     Node target(Edge e) const { return e.id / _nodeNum; }
    82 
    83 
    84     /// Node ID.
    85     
    86     /// The ID of a valid Node is a nonnegative integer not greater than
    87     /// \ref maxNodeId(). The range of the ID's is not surely continuous
    88     /// and the greatest node ID can be actually less then \ref maxNodeId().
    89     ///
    90     /// The ID of the \ref INVALID node is -1.
    91     ///\return The ID of the node \c v. 
    92 
    93     static int id(Node v) { return v.id; }
    94     /// Edge ID.
    95     
    96     /// The ID of a valid Edge is a nonnegative integer not greater than
    97     /// \ref maxEdgeId(). The range of the ID's is not surely continuous
    98     /// and the greatest edge ID can be actually less then \ref maxEdgeId().
    99     ///
   100     /// The ID of the \ref INVALID edge is -1.
   101     ///\return The ID of the edge \c e. 
   102     static int id(Edge e) { return e.id; }
   103 
   104     static Node fromId(int id, Node) { return Node(id);}
   105     
   106     static Edge fromId(int id, Edge) { return Edge(id);}
   107 
   108     typedef True FindEdgeTag;
   109 
   110     /// Finds an edge between two nodes.
   111     
   112     /// Finds an edge from node \c u to node \c v.
   113     ///
   114     /// If \c prev is \ref INVALID (this is the default value), then
   115     /// It finds the first edge from \c u to \c v. Otherwise it looks for
   116     /// the next edge from \c u to \c v after \c prev.
   117     /// \return The found edge or INVALID if there is no such an edge.
   118     Edge findEdge(Node u,Node v, Edge prev = INVALID) const {
   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> 
   195   AlterableFullGraphBase;
   196   typedef IterableGraphExtender<AlterableFullGraphBase> 
   197   IterableFullGraphBase;
   198   typedef DefaultMappableGraphExtender<IterableFullGraphBase> 
   199   MappableFullGraphBase;
   200 
   201   /// \ingroup graphs
   202   ///
   203   /// \brief A full graph class.
   204   ///
   205   /// This is a simple and fast directed full graph implementation.
   206   /// It is completely static, so you can neither add nor delete either
   207   /// edges or nodes.
   208   /// Thus it conforms to
   209   /// the \ref concept::StaticGraph "StaticGraph" concept
   210   /// \sa concept::StaticGraph.
   211   ///
   212   /// \author Alpar Juttner
   213   class FullGraph : public MappableFullGraphBase {
   214   public:
   215 
   216     FullGraph(int n) { construct(n); }
   217   };
   218 
   219   ///@}
   220 
   221   class UndirFullGraphBase {
   222     int _nodeNum;
   223     int _edgeNum;
   224   public:
   225 
   226     typedef UndirFullGraphBase Graph;
   227 
   228     class Node;
   229     class Edge;
   230 
   231   public:
   232 
   233     UndirFullGraphBase() {}
   234 
   235 
   236     ///Creates a full graph with \c n nodes.
   237     void construct(int n) { _nodeNum = n; _edgeNum = n * (n - 1) / 2; }
   238     ///
   239     //    FullGraphBase(const FullGraphBase &_g)
   240     //      : _nodeNum(_g.nodeNum()), _edgeNum(_nodeNum*_nodeNum) { }
   241     
   242     typedef True NodeNumTag;
   243     typedef True EdgeNumTag;
   244 
   245     ///Number of nodes.
   246     int nodeNum() const { return _nodeNum; }
   247     ///Number of edges.
   248     int edgeNum() const { return _edgeNum; }
   249 
   250     /// Maximum node ID.
   251     
   252     /// Maximum node ID.
   253     ///\sa id(Node)
   254     int maxId(Node = INVALID) const { return _nodeNum-1; }
   255     /// Maximum edge ID.
   256     
   257     /// Maximum edge ID.
   258     ///\sa id(Edge)
   259     int maxId(Edge = INVALID) const { return _edgeNum-1; }
   260 
   261     Node source(Edge e) const { 
   262       /// \todo we may do it faster
   263       return ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2; 
   264     }
   265 
   266     Node target(Edge e) const { 
   267       int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
   268       return e.id - (source) * (source - 1) / 2; 
   269     }
   270 
   271 
   272     /// Node ID.
   273     
   274     /// The ID of a valid Node is a nonnegative integer not greater than
   275     /// \ref maxNodeId(). The range of the ID's is not surely continuous
   276     /// and the greatest node ID can be actually less then \ref maxNodeId().
   277     ///
   278     /// The ID of the \ref INVALID node is -1.
   279     ///\return The ID of the node \c v. 
   280 
   281     static int id(Node v) { return v.id; }
   282     /// Edge ID.
   283     
   284     /// The ID of a valid Edge is a nonnegative integer not greater than
   285     /// \ref maxEdgeId(). The range of the ID's is not surely continuous
   286     /// and the greatest edge ID can be actually less then \ref maxEdgeId().
   287     ///
   288     /// The ID of the \ref INVALID edge is -1.
   289     ///\return The ID of the edge \c e. 
   290     static int id(Edge e) { return e.id; }
   291 
   292     /// Finds an edge between two nodes.
   293     
   294     /// Finds an edge from node \c u to node \c v.
   295     ///
   296     /// If \c prev is \ref INVALID (this is the default value), then
   297     /// It finds the first edge from \c u to \c v. Otherwise it looks for
   298     /// the next edge from \c u to \c v after \c prev.
   299     /// \return The found edge or INVALID if there is no such an edge.
   300     Edge findEdge(Node u,Node v, Edge prev = INVALID) 
   301     {
   302       return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
   303     }
   304     
   305       
   306     class Node {
   307       friend class UndirFullGraphBase;
   308 
   309     protected:
   310       int id;
   311       Node(int _id) { id = _id;}
   312     public:
   313       Node() {}
   314       Node (Invalid) { id = -1; }
   315       bool operator==(const Node node) const {return id == node.id;}
   316       bool operator!=(const Node node) const {return id != node.id;}
   317       bool operator<(const Node node) const {return id < node.id;}
   318     };
   319     
   320 
   321 
   322     class Edge {
   323       friend class UndirFullGraphBase;
   324       
   325     protected:
   326       int id;  // _nodeNum * target + source;
   327 
   328       Edge(int _id) : id(_id) {}
   329 
   330       Edge(const UndirFullGraphBase& _graph, int source, int target) 
   331 	: id(_graph._nodeNum * target+source) {}
   332     public:
   333       Edge() { }
   334       Edge (Invalid) { id = -1; }
   335       bool operator==(const Edge edge) const {return id == edge.id;}
   336       bool operator!=(const Edge edge) const {return id != edge.id;}
   337       bool operator<(const Edge edge) const {return id < edge.id;}
   338     };
   339 
   340     void first(Node& node) const {
   341       node.id = _nodeNum-1;
   342     }
   343 
   344     static void next(Node& node) {
   345       --node.id;
   346     }
   347 
   348     void first(Edge& edge) const {
   349       edge.id = _edgeNum-1;
   350     }
   351 
   352     static void next(Edge& edge) {
   353       --edge.id;
   354     }
   355 
   356     void firstOut(Edge& edge, const Node& node) const {      
   357       edge.id = node.id != 0 ? node.id * (node.id - 1) / 2 : -1;
   358     }
   359 
   360     /// \todo with specialized iterators we can make faster iterating
   361     void nextOut(Edge& e) const {
   362       int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
   363       int target = e.id - (source) * (source - 1) / 2; 
   364       ++target;
   365       e.id = target < source ? source * (source - 1) / 2 + target : -1;
   366     }
   367 
   368     void firstIn(Edge& edge, const Node& node) const {
   369       edge.id = node.id * (node.id + 1) / 2 - 1;
   370     }
   371     
   372     void nextIn(Edge& e) const {
   373       int source = ((int)sqrt((double)(1 + 8 * e.id)) + 1) / 2;;
   374       int target = e.id - (source) * (source - 1) / 2; ++target;
   375       ++source;
   376       e.id = source < _nodeNum ? source * (source - 1) / 2 + target : -1;
   377     }
   378 
   379   };
   380 
   381   typedef UndirGraphExtender<UndirFullGraphBase>
   382   UndirUndirFullGraphBase;
   383   typedef AlterableUndirGraphExtender<UndirUndirFullGraphBase> 
   384   AlterableUndirFullGraphBase;
   385   typedef IterableUndirGraphExtender<AlterableUndirFullGraphBase> 
   386   IterableUndirFullGraphBase;
   387   typedef MappableUndirGraphExtender<IterableUndirFullGraphBase> 
   388   MappableUndirFullGraphBase;
   389 
   390   /// \ingroup graphs
   391   ///
   392   /// \brief An undirected full graph class.
   393   ///
   394   /// This is a simple and fast directed full graph implementation.
   395   /// It is completely static, so you can neither add nor delete either
   396   /// edges or nodes.
   397   ///
   398   /// The main difference beetween the \e FullGraph and \e UndirFullGraph class
   399   /// is that this class conforms to the undirected graph concept and
   400   /// it does not contain the hook edges.
   401   ///
   402   /// \sa FullGraph
   403   ///
   404   /// \author Balazs Dezso
   405   class UndirFullGraph : public MappableUndirFullGraphBase {
   406   public:
   407     UndirFullGraph(int n) { construct(n); }
   408   };
   409 
   410 } //namespace lemon
   411 
   412 
   413 #endif //LEMON_FULL_GRAPH_H