src/lemon/graph_utils.h
author klao
Mon, 08 Nov 2004 16:42:35 +0000
changeset 971 643d3192ebc8
parent 964 2c0c20e90116
child 977 48962802d168
permissions -rw-r--r--
remove an annoying warning
klao@946
     1
/* -*- C++ -*-
klao@946
     2
 * src/lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
klao@946
     3
 *
klao@946
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
klao@946
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
klao@946
     6
 *
klao@946
     7
 * Permission to use, modify and distribute this software is granted
klao@946
     8
 * provided that this copyright notice appears in all copies. For
klao@946
     9
 * precise terms see the accompanying LICENSE file.
klao@946
    10
 *
klao@946
    11
 * This software is provided "AS IS" with no warranty of any kind,
klao@946
    12
 * express or implied, and with no claim as to its suitability for any
klao@946
    13
 * purpose.
klao@946
    14
 *
klao@946
    15
 */
klao@946
    16
klao@946
    17
#ifndef LEMON_GRAPH_UTILS_H
klao@946
    18
#define LEMON_GRAPH_UTILS_H
klao@946
    19
klao@946
    20
#include <iterator>
klao@946
    21
klao@946
    22
#include <lemon/invalid.h>
klao@946
    23
alpar@947
    24
///\ingroup gutils
klao@946
    25
///\file
alpar@947
    26
///\brief Graph utilities.
klao@946
    27
///
alpar@964
    28
///\todo Please
alpar@964
    29
///revise the documentation.
alpar@964
    30
///
klao@946
    31
klao@946
    32
klao@946
    33
namespace lemon {
klao@946
    34
alpar@947
    35
/// \addtogroup gutils
alpar@947
    36
/// @{
alpar@947
    37
klao@946
    38
  // counters in the graph
klao@946
    39
  /// \brief Function to count the items in the graph.
klao@946
    40
  ///
klao@946
    41
  /// This function counts the items in the graph.
klao@946
    42
  /// The complexity of the function is O(n) because
klao@946
    43
  /// it iterates on all of the items.
klao@946
    44
klao@946
    45
  template <typename Graph, typename ItemIt>
klao@946
    46
  inline int countItems(const Graph& _g) {
klao@946
    47
    int num = 0;
klao@946
    48
    for (ItemIt it(_g); it != INVALID; ++it) {
klao@946
    49
      ++num;
klao@946
    50
    }
klao@946
    51
    return num;
klao@946
    52
  }
klao@946
    53
klao@946
    54
  /// \brief Function to count the nodes in the graph.
klao@946
    55
  ///
klao@946
    56
  /// This function counts the nodes in the graph.
klao@946
    57
  /// The complexity of the function is O(n) but for some
alpar@964
    58
  /// graph structure it is specialized to run in O(1).
klao@946
    59
klao@946
    60
  template <typename Graph>
klao@946
    61
  inline int countNodes(const Graph& _g) {
klao@946
    62
    return countItems<Graph, typename Graph::NodeIt>(_g);
klao@946
    63
  }
klao@946
    64
klao@946
    65
  /// \brief Function to count the edges in the graph.
klao@946
    66
  ///
klao@946
    67
  /// This function counts the edges in the graph.
klao@946
    68
  /// The complexity of the function is O(e) but for some
alpar@964
    69
  /// graph structure it is specialized to run in O(1).
klao@946
    70
  template <typename Graph>
klao@946
    71
  inline int countEdges(const Graph& _g) {
klao@946
    72
    return countItems<Graph, typename Graph::EdgeIt>(_g);
klao@946
    73
  }
klao@946
    74
klao@946
    75
  /// \brief Function to count the symmetric edges in the graph.
klao@946
    76
  ///
klao@946
    77
  /// This function counts the symmetric edges in the graph.
klao@946
    78
  /// The complexity of the function is O(e) but for some
alpar@964
    79
  /// graph structure it is specialized to run in O(1).
klao@946
    80
  template <typename Graph>
klao@946
    81
  inline int countSymEdges(const Graph& _g) {
klao@946
    82
    return countItems<Graph, typename Graph::SymEdgeIt>(_g);
klao@946
    83
  }
klao@946
    84
klao@946
    85
  template <typename Graph, typename DegIt>
klao@946
    86
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
klao@946
    87
    int num = 0;
klao@946
    88
    for (DegIt it(_g, _n); it != INVALID; ++it) {
klao@946
    89
      ++num;
klao@946
    90
    }
klao@946
    91
    return num;
klao@946
    92
  }
alpar@967
    93
alpar@967
    94
  /// Finds an edge between two nodes of a graph.
alpar@967
    95
alpar@967
    96
  /// Finds an edge from node \c u to node \c v in graph \c g.
alpar@967
    97
  ///
alpar@967
    98
  /// If \c prev is \ref INVALID (this is the default value), then
alpar@967
    99
  /// it finds the first edge from \c u to \c v. Otherwise it looks for
alpar@967
   100
  /// the next edge from \c u to \c v after \c prev.
alpar@967
   101
  /// \return The found edge or \ref INVALID if there is no such an edge.
alpar@967
   102
  ///
alpar@967
   103
  /// Thus you can iterate through each edge from \c u to \c v as it follows.
alpar@967
   104
  /// \code
alpar@967
   105
  /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
alpar@967
   106
  ///   ...
alpar@967
   107
  /// }
alpar@967
   108
  /// \endcode
alpar@967
   109
  /// \todo We may want to use the \ref concept::GraphBase "GraphBase"
alpar@967
   110
  /// interface here...
alpar@967
   111
  /// \bug Untested ...
alpar@967
   112
  template <typename Graph>
alpar@967
   113
  typename Graph::Edge findEdge(const Graph &g,
alpar@967
   114
		typename Graph::Node u, typename Graph::Node v,
alpar@967
   115
		typename Graph::Edge prev = INVALID) 
alpar@967
   116
  {
alpar@967
   117
    typename Graph::OutEdgeIt e(g,prev);
alpar@967
   118
    if(prev==INVALID) g.first(e,u);
alpar@967
   119
    else ++e;
alpar@967
   120
    while(e!=INVALID && g.tail(e)!=v) ++e;
alpar@967
   121
    return e;
alpar@967
   122
  }
alpar@964
   123
  
alpar@964
   124
  ///\e
klao@946
   125
alpar@964
   126
  ///\todo Please document.
alpar@964
   127
  ///
klao@946
   128
  template <typename Graph>
klao@946
   129
  inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
klao@946
   130
    return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
klao@946
   131
  }
klao@946
   132
alpar@964
   133
  ///\e
alpar@964
   134
alpar@964
   135
  ///\todo Please document.
alpar@964
   136
  ///
klao@946
   137
  template <typename Graph>
klao@946
   138
  inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
klao@946
   139
    return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
klao@946
   140
  }
klao@946
   141
klao@946
   142
  // graph copy
klao@946
   143
klao@946
   144
  template <
klao@946
   145
    typename DestinationGraph, 
klao@946
   146
    typename SourceGraph, 
klao@946
   147
    typename NodeBijection>
klao@946
   148
  void copyNodes(DestinationGraph& _d, const SourceGraph& _s, 
klao@946
   149
		 NodeBijection& _nb) {    
klao@946
   150
    for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
klao@946
   151
      _nb[it] = _d.addNode();
klao@946
   152
    }
klao@946
   153
  }
klao@946
   154
klao@946
   155
  template <
klao@946
   156
    typename DestinationGraph, 
klao@946
   157
    typename SourceGraph, 
klao@946
   158
    typename NodeBijection,
klao@946
   159
    typename EdgeBijection>
klao@946
   160
  void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
klao@946
   161
		 const NodeBijection& _nb, EdgeBijection& _eb) {    
klao@946
   162
    for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
klao@946
   163
      _eb[it] = _d.addEdge(_nb[_s.tail(it)], _nb[_s.head(it)]);
klao@946
   164
    }
klao@946
   165
  }
klao@946
   166
klao@946
   167
  template <
klao@946
   168
    typename DestinationGraph, 
klao@946
   169
    typename SourceGraph, 
klao@946
   170
    typename NodeBijection,
klao@946
   171
    typename EdgeBijection>
klao@946
   172
  void copyGraph(DestinationGraph& _d, const SourceGraph& _s, 
klao@946
   173
		 NodeBijection& _nb, EdgeBijection& _eb) {
klao@946
   174
    nodeCopy(_d, _s, _nb);
klao@946
   175
    edgeCopy(_d, _s, _nb, _eb);
klao@946
   176
  }
klao@946
   177
 
klao@946
   178
   template <
klao@946
   179
    typename _DestinationGraph, 
klao@946
   180
    typename _SourceGraph, 
klao@946
   181
    typename _NodeBijection 
klao@946
   182
    =typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
klao@946
   183
    typename _EdgeBijection 
klao@946
   184
    =typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
klao@946
   185
   >
klao@946
   186
   class GraphCopy {
klao@946
   187
   public:
klao@946
   188
klao@946
   189
     typedef _DestinationGraph DestinationGraph;
klao@946
   190
     typedef _SourceGraph SourceGraph;
klao@946
   191
klao@946
   192
     typedef _NodeBijection NodeBijection;
klao@946
   193
     typedef _EdgeBijection EdgeBijection;
klao@946
   194
klao@946
   195
   protected:          
klao@946
   196
klao@946
   197
     NodeBijection node_bijection;
klao@946
   198
     EdgeBijection edge_bijection;     
klao@946
   199
klao@946
   200
   public:
klao@946
   201
     
klao@946
   202
     GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
klao@946
   203
       copyGraph(_d, _s, node_bijection, edge_bijection);
klao@946
   204
     }
klao@946
   205
klao@946
   206
     const NodeBijection& getNodeBijection() const {
klao@946
   207
       return node_bijection;
klao@946
   208
     }
klao@946
   209
klao@946
   210
     const EdgeBijection& getEdgeBijection() const {
klao@946
   211
       return edge_bijection;
klao@946
   212
     }
klao@946
   213
     
klao@946
   214
   };
alpar@947
   215
alpar@947
   216
/// @}
alpar@947
   217
  
alpar@947
   218
} //END OF NAMESPACE LEMON
klao@946
   219
klao@946
   220
#endif