src/hugo/full_graph.h
author alpar
Thu, 23 Sep 2004 15:09:55 +0000
changeset 907 df8472ab5d4a
parent 904 b40afcf42a4d
permissions -rw-r--r--
I forgot to apply
for i in `ls *.h`; do rpl template.h $i $i; done
in src/hugo/attic
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 * src/hugo/full_graph.h - Part of HUGOlib, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@591
    16
alpar@591
    17
#ifndef HUGO_FULL_GRAPH_H
alpar@591
    18
#define HUGO_FULL_GRAPH_H
alpar@591
    19
alpar@591
    20
///\ingroup graphs
alpar@591
    21
///\file
alpar@591
    22
///\brief FullGraph and SymFullGraph classes.
alpar@591
    23
alpar@591
    24
#include <vector>
deba@782
    25
#include <climits>
alpar@591
    26
alpar@591
    27
#include <hugo/invalid.h>
alpar@591
    28
deba@782
    29
#include <hugo/map_registry.h>
deba@897
    30
#include <hugo/array_map.h>
deba@822
    31
deba@822
    32
#include <hugo/map_defines.h>
deba@782
    33
alpar@591
    34
namespace hugo {
alpar@591
    35
alpar@591
    36
/// \addtogroup graphs
alpar@591
    37
/// @{
alpar@591
    38
alpar@591
    39
  ///A full graph class.
alpar@591
    40
alpar@591
    41
  ///This is a simple and fast directed full graph implementation.
marci@606
    42
  ///It is completely static, so you can neither add nor delete either
alpar@591
    43
  ///edges or nodes.
alpar@880
    44
  ///Thus it conforms to
alpar@880
    45
  ///the \ref skeleton::StaticGraph "StaticGraph" concept
alpar@880
    46
  ///\sa skeleton::StaticGraph.
alpar@752
    47
  ///\todo What about loops?
alpar@752
    48
  ///\todo Don't we need SymEdgeMap?
alpar@591
    49
  ///
alpar@591
    50
  ///\author Alpar Juttner
alpar@591
    51
  class FullGraph {
alpar@591
    52
    int NodeNum;
alpar@591
    53
    int EdgeNum;
alpar@591
    54
  public:
deba@782
    55
deba@782
    56
    typedef FullGraph Graph;
alpar@591
    57
alpar@591
    58
    class Node;
alpar@591
    59
    class Edge;
deba@782
    60
alpar@591
    61
    class NodeIt;
alpar@591
    62
    class EdgeIt;
alpar@591
    63
    class OutEdgeIt;
alpar@591
    64
    class InEdgeIt;
alpar@591
    65
    
deba@822
    66
alpar@904
    67
    // Create map registries.
deba@782
    68
    CREATE_MAP_REGISTRIES;
alpar@904
    69
    // Create node and edge maps.
deba@897
    70
    CREATE_MAPS(ArrayMap);
alpar@591
    71
    
alpar@591
    72
  public:
alpar@591
    73
alpar@591
    74
    ///Creates a full graph with \c n nodes.
alpar@591
    75
    FullGraph(int n) : NodeNum(n), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    76
    ///
alpar@591
    77
    FullGraph(const FullGraph &_g)
alpar@591
    78
      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    79
    
alpar@813
    80
    ///Number of nodes.
alpar@813
    81
    int nodeNum() const { return NodeNum; }
alpar@813
    82
    ///Number of edges.
alpar@813
    83
    int edgeNum() const { return EdgeNum; }
alpar@591
    84
alpar@813
    85
    /// Maximum node ID.
alpar@813
    86
    
alpar@813
    87
    /// Maximum node ID.
alpar@813
    88
    ///\sa id(Node)
alpar@813
    89
    int maxNodeId() const { return NodeNum-1; }
alpar@813
    90
    /// Maximum edge ID.
alpar@813
    91
    
alpar@813
    92
    /// Maximum edge ID.
alpar@813
    93
    ///\sa id(Edge)
alpar@813
    94
    int maxEdgeId() const { return EdgeNum-1; }
alpar@591
    95
alpar@591
    96
    Node tail(Edge e) const { return e.n%NodeNum; }
alpar@591
    97
    Node head(Edge e) const { return e.n/NodeNum; }
alpar@591
    98
alpar@591
    99
    NodeIt& first(NodeIt& v) const {
alpar@591
   100
      v=NodeIt(*this); return v; }
alpar@591
   101
    EdgeIt& first(EdgeIt& e) const { 
alpar@591
   102
      e=EdgeIt(*this); return e; }
alpar@591
   103
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@591
   104
      e=OutEdgeIt(*this,v); return e; }
alpar@591
   105
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@591
   106
      e=InEdgeIt(*this,v); return e; }
alpar@591
   107
alpar@813
   108
    /// Node ID.
alpar@813
   109
    
alpar@813
   110
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   111
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   112
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   113
    ///
alpar@813
   114
    /// The ID of the \ref INVALID node is -1.
alpar@813
   115
    ///\return The ID of the node \c v. 
alpar@713
   116
    static int id(Node v) { return v.n; }
alpar@813
   117
    /// Edge ID.
alpar@813
   118
    
alpar@813
   119
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   120
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   121
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   122
    ///
alpar@813
   123
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   124
    ///\return The ID of the edge \c e. 
alpar@713
   125
    static int id(Edge e) { return e.n; }
alpar@591
   126
alpar@774
   127
    /// Finds an edge between two nodes.
alpar@774
   128
    
alpar@774
   129
    /// Finds an edge from node \c u to node \c v.
alpar@774
   130
    ///
alpar@774
   131
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   132
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   133
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   134
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   135
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   136
    {
deba@782
   137
      return prev.n == -1 ? Edge(*this, u.n, v.n) : INVALID;
alpar@774
   138
    }
alpar@774
   139
    
alpar@774
   140
      
alpar@591
   141
    class Node {
alpar@591
   142
      friend class FullGraph;
alpar@591
   143
      template <typename T> friend class NodeMap;
alpar@591
   144
alpar@591
   145
      friend class Edge;
alpar@591
   146
      friend class OutEdgeIt;
alpar@591
   147
      friend class InEdgeIt;
alpar@591
   148
      friend class SymEdge;
alpar@591
   149
alpar@591
   150
    protected:
alpar@591
   151
      int n;
alpar@722
   152
      friend int FullGraph::id(Node v); 
alpar@591
   153
      Node(int nn) {n=nn;}
alpar@591
   154
    public:
alpar@591
   155
      Node() {}
alpar@591
   156
      Node (Invalid) { n=-1; }
alpar@591
   157
      bool operator==(const Node i) const {return n==i.n;}
alpar@591
   158
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@591
   159
      bool operator<(const Node i) const {return n<i.n;}
alpar@591
   160
    };
alpar@591
   161
    
alpar@591
   162
    class NodeIt : public Node {
alpar@774
   163
      const FullGraph *G;
alpar@591
   164
      friend class FullGraph;
alpar@591
   165
    public:
alpar@591
   166
      NodeIt() : Node() { }
alpar@774
   167
      NodeIt(const FullGraph& _G,Node n) : Node(n), G(&_G) { }
alpar@591
   168
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   169
      NodeIt(const FullGraph& _G) : Node(_G.NodeNum?0:-1), G(&_G) { }
alpar@591
   170
      ///\todo Undocumented conversion Node -\> NodeIt.
alpar@774
   171
      NodeIt& operator++() { n=(n+2)%(G->NodeNum+1)-1;return *this; }
alpar@591
   172
    };
alpar@591
   173
alpar@591
   174
    class Edge {
alpar@591
   175
      friend class FullGraph;
alpar@591
   176
      template <typename T> friend class EdgeMap;
alpar@591
   177
      
alpar@591
   178
      friend class Node;
alpar@591
   179
      friend class NodeIt;
alpar@591
   180
    protected:
alpar@591
   181
      int n; //NodeNum*head+tail;
alpar@722
   182
      friend int FullGraph::id(Edge e);
alpar@591
   183
alpar@774
   184
      Edge(int nn) : n(nn) {}
alpar@774
   185
      Edge(const FullGraph &G, int tail, int head) : n(G.NodeNum*head+tail) {}
alpar@591
   186
    public:
alpar@591
   187
      Edge() { }
alpar@591
   188
      Edge (Invalid) { n=-1; }
alpar@591
   189
      bool operator==(const Edge i) const {return n==i.n;}
alpar@591
   190
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@591
   191
      bool operator<(const Edge i) const {return n<i.n;}
alpar@591
   192
      ///\bug This is a workaround until somebody tells me how to
alpar@591
   193
      ///make class \c SymFullGraph::SymEdgeMap friend of Edge
alpar@591
   194
      int &idref() {return n;}
alpar@591
   195
      const int &idref() const {return n;}
alpar@591
   196
    };
alpar@591
   197
    
alpar@591
   198
    class EdgeIt : public Edge {
alpar@591
   199
      friend class FullGraph;
alpar@591
   200
    public:
alpar@774
   201
      EdgeIt(const FullGraph& _G) : Edge(_G.EdgeNum-1) { }
alpar@774
   202
      EdgeIt(const FullGraph&, Edge e) : Edge(e) { }
alpar@591
   203
      EdgeIt (Invalid i) : Edge(i) { }
alpar@591
   204
      EdgeIt() : Edge() { }
alpar@774
   205
      EdgeIt& operator++() { --n; return *this; }
alpar@774
   206
alpar@591
   207
      ///\bug This is a workaround until somebody tells me how to
alpar@591
   208
      ///make class \c SymFullGraph::SymEdgeMap friend of Edge
alpar@591
   209
      int &idref() {return n;}
alpar@591
   210
    };
alpar@591
   211
    
alpar@591
   212
    class OutEdgeIt : public Edge {
alpar@774
   213
      const FullGraph *G;
alpar@591
   214
      friend class FullGraph;
alpar@591
   215
    public: 
alpar@591
   216
      OutEdgeIt() : Edge() { }
alpar@774
   217
      OutEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@591
   218
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@591
   219
alpar@774
   220
      OutEdgeIt(const FullGraph& _G,const Node v) : Edge(v.n), G(&_G) {}
alpar@774
   221
      
alpar@774
   222
      OutEdgeIt& operator++()
alpar@774
   223
      { n+=G->NodeNum; if(n>=G->EdgeNum) n=-1; return *this; }
alpar@774
   224
alpar@591
   225
    };
alpar@591
   226
    
alpar@591
   227
    class InEdgeIt : public Edge {
alpar@774
   228
      const FullGraph *G;
alpar@591
   229
      friend class FullGraph;
alpar@591
   230
    public: 
alpar@591
   231
      InEdgeIt() : Edge() { }
alpar@774
   232
      InEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@591
   233
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@774
   234
      InEdgeIt(const FullGraph& _G,Node v) : Edge(v.n*_G.NodeNum), G(&_G) {}
alpar@774
   235
      InEdgeIt& operator++()
alpar@774
   236
      { if(!((++n)%G->NodeNum)) n=-1; return *this; }
alpar@591
   237
    };
alpar@591
   238
alpar@591
   239
  };
alpar@591
   240
alpar@591
   241
  /// @}  
alpar@591
   242
alpar@591
   243
} //namespace hugo
alpar@591
   244
alpar@591
   245
alpar@591
   246
alpar@591
   247
alpar@591
   248
#endif //HUGO_FULL_GRAPH_H