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