COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/lemon/graph_utils.h @ 966:5e865c5c8a87

Last change on this file since 966:5e865c5c8a87 was 964:2c0c20e90116, checked in by Alpar Juttner, 19 years ago

Doc improvements

File size: 5.0 KB
Line 
1/* -*- C++ -*-
2 * src/lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
17#ifndef LEMON_GRAPH_UTILS_H
18#define LEMON_GRAPH_UTILS_H
19
20#include <iterator>
21
22#include <lemon/invalid.h>
23
24///\ingroup gutils
25///\file
26///\brief Graph utilities.
27///
28///\todo Please
29///revise the documentation.
30///
31
32
33namespace lemon {
34
35/// \addtogroup gutils
36/// @{
37
38  // counters in the graph
39  /// \brief Function to count the items in the graph.
40  ///
41  /// This function counts the items in the graph.
42  /// The complexity of the function is O(n) because
43  /// it iterates on all of the items.
44
45  template <typename Graph, typename ItemIt>
46  inline int countItems(const Graph& _g) {
47    int num = 0;
48    for (ItemIt it(_g); it != INVALID; ++it) {
49      ++num;
50    }
51    return num;
52  }
53
54  /// \brief Function to count the nodes in the graph.
55  ///
56  /// This function counts the nodes in the graph.
57  /// The complexity of the function is O(n) but for some
58  /// graph structure it is specialized to run in O(1).
59
60  template <typename Graph>
61  inline int countNodes(const Graph& _g) {
62    return countItems<Graph, typename Graph::NodeIt>(_g);
63  }
64
65  /// \brief Function to count the edges in the graph.
66  ///
67  /// This function counts the edges in the graph.
68  /// The complexity of the function is O(e) but for some
69  /// graph structure it is specialized to run in O(1).
70  template <typename Graph>
71  inline int countEdges(const Graph& _g) {
72    return countItems<Graph, typename Graph::EdgeIt>(_g);
73  }
74
75  /// \brief Function to count the symmetric edges in the graph.
76  ///
77  /// This function counts the symmetric edges in the graph.
78  /// The complexity of the function is O(e) but for some
79  /// graph structure it is specialized to run in O(1).
80  template <typename Graph>
81  inline int countSymEdges(const Graph& _g) {
82    return countItems<Graph, typename Graph::SymEdgeIt>(_g);
83  }
84
85  template <typename Graph, typename DegIt>
86  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
87    int num = 0;
88    for (DegIt it(_g, _n); it != INVALID; ++it) {
89      ++num;
90    }
91    return num;
92  }
93 
94  ///\e
95
96  ///\todo Please document.
97  ///
98  template <typename Graph>
99  inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
100    return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
101  }
102
103  ///\e
104
105  ///\todo Please document.
106  ///
107  template <typename Graph>
108  inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
109    return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
110  }
111
112  // graph copy
113
114  template <
115    typename DestinationGraph,
116    typename SourceGraph,
117    typename NodeBijection>
118  void copyNodes(DestinationGraph& _d, const SourceGraph& _s,
119                 NodeBijection& _nb) {   
120    for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
121      _nb[it] = _d.addNode();
122    }
123  }
124
125  template <
126    typename DestinationGraph,
127    typename SourceGraph,
128    typename NodeBijection,
129    typename EdgeBijection>
130  void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
131                 const NodeBijection& _nb, EdgeBijection& _eb) {   
132    for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
133      _eb[it] = _d.addEdge(_nb[_s.tail(it)], _nb[_s.head(it)]);
134    }
135  }
136
137  template <
138    typename DestinationGraph,
139    typename SourceGraph,
140    typename NodeBijection,
141    typename EdgeBijection>
142  void copyGraph(DestinationGraph& _d, const SourceGraph& _s,
143                 NodeBijection& _nb, EdgeBijection& _eb) {
144    nodeCopy(_d, _s, _nb);
145    edgeCopy(_d, _s, _nb, _eb);
146  }
147 
148   template <
149    typename _DestinationGraph,
150    typename _SourceGraph,
151    typename _NodeBijection
152    =typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
153    typename _EdgeBijection
154    =typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
155   >
156   class GraphCopy {
157   public:
158
159     typedef _DestinationGraph DestinationGraph;
160     typedef _SourceGraph SourceGraph;
161
162     typedef _NodeBijection NodeBijection;
163     typedef _EdgeBijection EdgeBijection;
164
165   protected:         
166
167     NodeBijection node_bijection;
168     EdgeBijection edge_bijection;     
169
170   public:
171     
172     GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
173       copyGraph(_d, _s, node_bijection, edge_bijection);
174     }
175
176     const NodeBijection& getNodeBijection() const {
177       return node_bijection;
178     }
179
180     const EdgeBijection& getEdgeBijection() const {
181       return edge_bijection;
182     }
183     
184   };
185
186/// @}
187 
188} //END OF NAMESPACE LEMON
189
190#endif
Note: See TracBrowser for help on using the repository browser.