src/lemon/full_graph.h
author deba
Thu, 11 Nov 2004 09:31:55 +0000
changeset 980 0f1044b7a3af
parent 977 48962802d168
child 983 3095ff2b5c18
permissions -rw-r--r--
maxNodeId() and maxEdgeId() changed to maxId(Node) and maxId(Edge)
getNodeObserverRegistry() and getEdgeObserverRegistry() changed to
getObserverRegistry(Node) and getObserverRegistry(Edge)

IdMappableGraphExtender erased
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/full_graph.h - Part of LEMON, 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@921
    17
#ifndef LEMON_FULL_GRAPH_H
alpar@921
    18
#define LEMON_FULL_GRAPH_H
alpar@591
    19
klao@946
    20
klao@946
    21
#include <lemon/iterable_graph_extender.h>
klao@946
    22
#include <lemon/alteration_observer_registry.h>
klao@946
    23
#include <lemon/default_map.h>
klao@946
    24
klao@977
    25
#include <lemon/invalid.h>
klao@977
    26
#include <lemon/utility.h>
klao@977
    27
klao@977
    28
alpar@591
    29
///\ingroup graphs
alpar@591
    30
///\file
alpar@591
    31
///\brief FullGraph and SymFullGraph classes.
alpar@591
    32
alpar@591
    33
alpar@921
    34
namespace lemon {
alpar@591
    35
alpar@591
    36
/// \addtogroup graphs
alpar@591
    37
/// @{
alpar@591
    38
klao@946
    39
  class FullGraphBase {
alpar@591
    40
    int NodeNum;
alpar@591
    41
    int EdgeNum;
alpar@591
    42
  public:
deba@782
    43
klao@946
    44
    typedef FullGraphBase Graph;
alpar@591
    45
alpar@591
    46
    class Node;
alpar@591
    47
    class Edge;
deba@782
    48
alpar@591
    49
  public:
alpar@591
    50
klao@946
    51
    FullGraphBase() {}
klao@946
    52
klao@946
    53
alpar@591
    54
    ///Creates a full graph with \c n nodes.
klao@946
    55
    void construct(int n) { NodeNum = n; EdgeNum = n * n; }
alpar@591
    56
    ///
klao@946
    57
    //    FullGraphBase(const FullGraphBase &_g)
klao@946
    58
    //      : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { }
alpar@591
    59
    
klao@977
    60
    typedef True NodeNumTag;
klao@977
    61
    typedef True EdgeNumTag;
klao@977
    62
alpar@813
    63
    ///Number of nodes.
alpar@813
    64
    int nodeNum() const { return NodeNum; }
alpar@813
    65
    ///Number of edges.
alpar@813
    66
    int edgeNum() const { return EdgeNum; }
alpar@591
    67
alpar@813
    68
    /// Maximum node ID.
alpar@813
    69
    
alpar@813
    70
    /// Maximum node ID.
alpar@813
    71
    ///\sa id(Node)
deba@980
    72
    int maxId(Node = INVALID) const { return NodeNum-1; }
alpar@813
    73
    /// Maximum edge ID.
alpar@813
    74
    
alpar@813
    75
    /// Maximum edge ID.
alpar@813
    76
    ///\sa id(Edge)
deba@980
    77
    int maxId(Edge = INVALID) const { return EdgeNum-1; }
alpar@591
    78
klao@946
    79
    Node tail(Edge e) const { return e.id % NodeNum; }
klao@946
    80
    Node head(Edge e) const { return e.id / NodeNum; }
alpar@591
    81
alpar@591
    82
alpar@813
    83
    /// Node ID.
alpar@813
    84
    
alpar@813
    85
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
    86
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
    87
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
    88
    ///
alpar@813
    89
    /// The ID of the \ref INVALID node is -1.
alpar@813
    90
    ///\return The ID of the node \c v. 
klao@946
    91
klao@946
    92
    static int id(Node v) { return v.id; }
alpar@813
    93
    /// Edge ID.
alpar@813
    94
    
alpar@813
    95
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
    96
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
    97
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
    98
    ///
alpar@813
    99
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   100
    ///\return The ID of the edge \c e. 
klao@946
   101
    static int id(Edge e) { return e.id; }
alpar@591
   102
alpar@774
   103
    /// Finds an edge between two nodes.
alpar@774
   104
    
alpar@774
   105
    /// Finds an edge from node \c u to node \c v.
alpar@774
   106
    ///
alpar@774
   107
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   108
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   109
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   110
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   111
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   112
    {
klao@946
   113
      return prev.id == -1 ? Edge(*this, u.id, v.id) : INVALID;
alpar@774
   114
    }
alpar@774
   115
    
alpar@774
   116
      
alpar@591
   117
    class Node {
klao@946
   118
      friend class FullGraphBase;
alpar@591
   119
alpar@591
   120
    protected:
klao@946
   121
      int id;
klao@946
   122
      Node(int _id) { id = _id;}
alpar@591
   123
    public:
alpar@591
   124
      Node() {}
klao@946
   125
      Node (Invalid) { id = -1; }
klao@946
   126
      bool operator==(const Node node) const {return id == node.id;}
klao@946
   127
      bool operator!=(const Node node) const {return id != node.id;}
klao@946
   128
      bool operator<(const Node node) const {return id < node.id;}
alpar@591
   129
    };
alpar@591
   130
    
klao@946
   131
klao@946
   132
klao@946
   133
    class Edge {
klao@946
   134
      friend class FullGraphBase;
klao@946
   135
      
klao@946
   136
    protected:
klao@946
   137
      int id;  // NodeNum * head + tail;
klao@946
   138
klao@946
   139
      Edge(int _id) : id(_id) {}
klao@946
   140
klao@946
   141
      Edge(const FullGraphBase& _graph, int tail, int head) 
klao@946
   142
	: id(_graph.NodeNum * head+tail) {}
alpar@591
   143
    public:
klao@946
   144
      Edge() { }
klao@946
   145
      Edge (Invalid) { id = -1; }
klao@946
   146
      bool operator==(const Edge edge) const {return id == edge.id;}
klao@946
   147
      bool operator!=(const Edge edge) const {return id != edge.id;}
klao@946
   148
      bool operator<(const Edge edge) const {return id < edge.id;}
alpar@591
   149
    };
alpar@591
   150
klao@946
   151
    void first(Node& node) const {
klao@946
   152
      node.id = NodeNum-1;
klao@946
   153
    }
alpar@591
   154
klao@946
   155
    static void next(Node& node) {
klao@946
   156
      --node.id;
klao@946
   157
    }
klao@946
   158
klao@946
   159
    void first(Edge& edge) const {
klao@946
   160
      edge.id = EdgeNum-1;
klao@946
   161
    }
klao@946
   162
klao@946
   163
    static void next(Edge& edge) {
klao@946
   164
      --edge.id;
klao@946
   165
    }
klao@946
   166
klao@946
   167
    void firstOut(Edge& edge, const Node& node) const {
klao@946
   168
      edge.id = EdgeNum + node.id - NodeNum;
klao@946
   169
    }
klao@946
   170
klao@946
   171
    void nextOut(Edge& edge) const {
klao@946
   172
      edge.id -= NodeNum;
klao@946
   173
      if (edge.id < 0) edge.id = -1;
klao@946
   174
    }
klao@946
   175
klao@946
   176
    void firstIn(Edge& edge, const Node& node) const {
klao@946
   177
      edge.id = node.id * NodeNum;
klao@946
   178
    }
alpar@591
   179
    
klao@946
   180
    void nextIn(Edge& edge) const {
klao@946
   181
      ++edge.id;
klao@946
   182
      if (edge.id % NodeNum == 0) edge.id = -1;
klao@946
   183
    }
alpar@591
   184
alpar@591
   185
  };
alpar@591
   186
klao@946
   187
klao@946
   188
  typedef AlterableGraphExtender<FullGraphBase> AlterableFullGraphBase;
klao@946
   189
  typedef IterableGraphExtender<AlterableFullGraphBase> IterableFullGraphBase;
deba@980
   190
  typedef DefaultMappableGraphExtender<IterableFullGraphBase> MappableFullGraphBase;
klao@946
   191
alpar@951
   192
  ///A full graph class.
alpar@951
   193
alpar@951
   194
  ///This is a simple and fast directed full graph implementation.
alpar@951
   195
  ///It is completely static, so you can neither add nor delete either
alpar@951
   196
  ///edges or nodes.
alpar@951
   197
  ///Thus it conforms to
klao@959
   198
  ///the \ref concept::StaticGraph "StaticGraph" concept
klao@959
   199
  ///\sa concept::StaticGraph.
alpar@951
   200
  ///\todo What about loops?
alpar@951
   201
  ///\todo Don't we need SymEdgeMap?
alpar@951
   202
  ///
alpar@951
   203
  ///\author Alpar Juttner
klao@946
   204
  class FullGraph : public MappableFullGraphBase {
klao@946
   205
  public:
klao@946
   206
klao@946
   207
    FullGraph(int n) { construct(n); }
klao@946
   208
  };
klao@946
   209
alpar@591
   210
  /// @}  
alpar@591
   211
alpar@921
   212
} //namespace lemon
alpar@591
   213
alpar@591
   214
alpar@921
   215
#endif //LEMON_FULL_GRAPH_H