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