src/lemon/graph_utils.h
author ladanyi
Thu, 04 Nov 2004 13:32:44 +0000
changeset 956 0ff924405d21
parent 946 c94ef40a22ce
child 964 2c0c20e90116
permissions -rw-r--r--
Added the SimpleController class, and removed the first version of SimAnn in favour of the second.
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
///
klao@946
    28
klao@946
    29
klao@946
    30
namespace lemon {
klao@946
    31
alpar@947
    32
/// \addtogroup gutils
alpar@947
    33
/// @{
alpar@947
    34
klao@946
    35
  // counters in the graph
klao@946
    36
  /// \brief Function to count the items in the graph.
klao@946
    37
  ///
klao@946
    38
  /// This function counts the items in the graph.
klao@946
    39
  /// The complexity of the function is O(n) because
klao@946
    40
  /// it iterates on all of the items.
klao@946
    41
klao@946
    42
  template <typename Graph, typename ItemIt>
klao@946
    43
  inline int countItems(const Graph& _g) {
klao@946
    44
    int num = 0;
klao@946
    45
    for (ItemIt it(_g); it != INVALID; ++it) {
klao@946
    46
      ++num;
klao@946
    47
    }
klao@946
    48
    return num;
klao@946
    49
  }
klao@946
    50
klao@946
    51
  /// \brief Function to count the nodes in the graph.
klao@946
    52
  ///
klao@946
    53
  /// This function counts the nodes in the graph.
klao@946
    54
  /// The complexity of the function is O(n) but for some
klao@946
    55
  /// graph structure it is specialized to O(1).
klao@946
    56
klao@946
    57
  template <typename Graph>
klao@946
    58
  inline int countNodes(const Graph& _g) {
klao@946
    59
    return countItems<Graph, typename Graph::NodeIt>(_g);
klao@946
    60
  }
klao@946
    61
klao@946
    62
  /// \brief Function to count the edges in the graph.
klao@946
    63
  ///
klao@946
    64
  /// This function counts the edges in the graph.
klao@946
    65
  /// The complexity of the function is O(e) but for some
klao@946
    66
  /// graph structure it is specialized to O(1).
klao@946
    67
  template <typename Graph>
klao@946
    68
  inline int countEdges(const Graph& _g) {
klao@946
    69
    return countItems<Graph, typename Graph::EdgeIt>(_g);
klao@946
    70
  }
klao@946
    71
klao@946
    72
  /// \brief Function to count the symmetric edges in the graph.
klao@946
    73
  ///
klao@946
    74
  /// This function counts the symmetric edges in the graph.
klao@946
    75
  /// The complexity of the function is O(e) but for some
klao@946
    76
  /// graph structure it is specialized to O(1).
klao@946
    77
  template <typename Graph>
klao@946
    78
  inline int countSymEdges(const Graph& _g) {
klao@946
    79
    return countItems<Graph, typename Graph::SymEdgeIt>(_g);
klao@946
    80
  }
klao@946
    81
klao@946
    82
  template <typename Graph, typename DegIt>
klao@946
    83
  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
klao@946
    84
    int num = 0;
klao@946
    85
    for (DegIt it(_g, _n); it != INVALID; ++it) {
klao@946
    86
      ++num;
klao@946
    87
    }
klao@946
    88
    return num;
klao@946
    89
  }
klao@946
    90
klao@946
    91
  template <typename Graph>
klao@946
    92
  inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
klao@946
    93
    return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
klao@946
    94
  }
klao@946
    95
klao@946
    96
  template <typename Graph>
klao@946
    97
  inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
klao@946
    98
    return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
klao@946
    99
  }
klao@946
   100
klao@946
   101
  // graph copy
klao@946
   102
klao@946
   103
  template <
klao@946
   104
    typename DestinationGraph, 
klao@946
   105
    typename SourceGraph, 
klao@946
   106
    typename NodeBijection>
klao@946
   107
  void copyNodes(DestinationGraph& _d, const SourceGraph& _s, 
klao@946
   108
		 NodeBijection& _nb) {    
klao@946
   109
    for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
klao@946
   110
      _nb[it] = _d.addNode();
klao@946
   111
    }
klao@946
   112
  }
klao@946
   113
klao@946
   114
  template <
klao@946
   115
    typename DestinationGraph, 
klao@946
   116
    typename SourceGraph, 
klao@946
   117
    typename NodeBijection,
klao@946
   118
    typename EdgeBijection>
klao@946
   119
  void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
klao@946
   120
		 const NodeBijection& _nb, EdgeBijection& _eb) {    
klao@946
   121
    for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
klao@946
   122
      _eb[it] = _d.addEdge(_nb[_s.tail(it)], _nb[_s.head(it)]);
klao@946
   123
    }
klao@946
   124
  }
klao@946
   125
klao@946
   126
  template <
klao@946
   127
    typename DestinationGraph, 
klao@946
   128
    typename SourceGraph, 
klao@946
   129
    typename NodeBijection,
klao@946
   130
    typename EdgeBijection>
klao@946
   131
  void copyGraph(DestinationGraph& _d, const SourceGraph& _s, 
klao@946
   132
		 NodeBijection& _nb, EdgeBijection& _eb) {
klao@946
   133
    nodeCopy(_d, _s, _nb);
klao@946
   134
    edgeCopy(_d, _s, _nb, _eb);
klao@946
   135
  }
klao@946
   136
 
klao@946
   137
   template <
klao@946
   138
    typename _DestinationGraph, 
klao@946
   139
    typename _SourceGraph, 
klao@946
   140
    typename _NodeBijection 
klao@946
   141
    =typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
klao@946
   142
    typename _EdgeBijection 
klao@946
   143
    =typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
klao@946
   144
   >
klao@946
   145
   class GraphCopy {
klao@946
   146
   public:
klao@946
   147
klao@946
   148
     typedef _DestinationGraph DestinationGraph;
klao@946
   149
     typedef _SourceGraph SourceGraph;
klao@946
   150
klao@946
   151
     typedef _NodeBijection NodeBijection;
klao@946
   152
     typedef _EdgeBijection EdgeBijection;
klao@946
   153
klao@946
   154
   protected:          
klao@946
   155
klao@946
   156
     NodeBijection node_bijection;
klao@946
   157
     EdgeBijection edge_bijection;     
klao@946
   158
klao@946
   159
   public:
klao@946
   160
     
klao@946
   161
     GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
klao@946
   162
       copyGraph(_d, _s, node_bijection, edge_bijection);
klao@946
   163
     }
klao@946
   164
klao@946
   165
     const NodeBijection& getNodeBijection() const {
klao@946
   166
       return node_bijection;
klao@946
   167
     }
klao@946
   168
klao@946
   169
     const EdgeBijection& getEdgeBijection() const {
klao@946
   170
       return edge_bijection;
klao@946
   171
     }
klao@946
   172
     
klao@946
   173
   };
alpar@947
   174
alpar@947
   175
/// @}
alpar@947
   176
  
alpar@947
   177
} //END OF NAMESPACE LEMON
klao@946
   178
klao@946
   179
#endif