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