Several changes in doc.
2 * src/lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
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.
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
17 #ifndef LEMON_GRAPH_UTILS_H
18 #define LEMON_GRAPH_UTILS_H
22 #include <lemon/invalid.h>
26 ///\brief Graph utilities.
29 ///revise the documentation.
35 /// \addtogroup gutils
38 // counters in the graph
39 /// \brief Function to count the items in the graph.
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.
45 template <typename Graph, typename ItemIt>
46 inline int countItems(const Graph& _g) {
48 for (ItemIt it(_g); it != INVALID; ++it) {
54 /// \brief Function to count the nodes in the graph.
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).
60 template <typename Graph>
61 inline int countNodes(const Graph& _g) {
62 return countItems<Graph, typename Graph::NodeIt>(_g);
65 /// \brief Function to count the edges in the graph.
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);
75 /// \brief Function to count the symmetric edges in the graph.
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);
85 template <typename Graph, typename DegIt>
86 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
88 for (DegIt it(_g, _n); it != INVALID; ++it) {
94 /// Finds an edge between two nodes of a graph.
96 /// Finds an edge from node \c u to node \c v in graph \c g.
98 /// If \c prev is \ref INVALID (this is the default value), then
99 /// it finds the first edge from \c u to \c v. Otherwise it looks for
100 /// the next edge from \c u to \c v after \c prev.
101 /// \return The found edge or \ref INVALID if there is no such an edge.
103 /// Thus you can iterate through each edge from \c u to \c v as it follows.
105 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
109 /// \todo We may want to use the \ref concept::GraphBase "GraphBase"
110 /// interface here...
111 /// \bug Untested ...
112 template <typename Graph>
113 typename Graph::Edge findEdge(const Graph &g,
114 typename Graph::Node u, typename Graph::Node v,
115 typename Graph::Edge prev = INVALID)
117 typename Graph::OutEdgeIt e(g,prev);
118 if(prev==INVALID) g.first(e,u);
120 while(e!=INVALID && g.tail(e)!=v) ++e;
126 ///\todo Please document.
128 template <typename Graph>
129 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
130 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
135 ///\todo Please document.
137 template <typename Graph>
138 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
139 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
145 typename DestinationGraph,
146 typename SourceGraph,
147 typename NodeBijection>
148 void copyNodes(DestinationGraph& _d, const SourceGraph& _s,
149 NodeBijection& _nb) {
150 for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
151 _nb[it] = _d.addNode();
156 typename DestinationGraph,
157 typename SourceGraph,
158 typename NodeBijection,
159 typename EdgeBijection>
160 void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
161 const NodeBijection& _nb, EdgeBijection& _eb) {
162 for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
163 _eb[it] = _d.addEdge(_nb[_s.tail(it)], _nb[_s.head(it)]);
168 typename DestinationGraph,
169 typename SourceGraph,
170 typename NodeBijection,
171 typename EdgeBijection>
172 void copyGraph(DestinationGraph& _d, const SourceGraph& _s,
173 NodeBijection& _nb, EdgeBijection& _eb) {
174 nodeCopy(_d, _s, _nb);
175 edgeCopy(_d, _s, _nb, _eb);
179 typename _DestinationGraph,
180 typename _SourceGraph,
181 typename _NodeBijection
182 =typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
183 typename _EdgeBijection
184 =typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
189 typedef _DestinationGraph DestinationGraph;
190 typedef _SourceGraph SourceGraph;
192 typedef _NodeBijection NodeBijection;
193 typedef _EdgeBijection EdgeBijection;
197 NodeBijection node_bijection;
198 EdgeBijection edge_bijection;
202 GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
203 copyGraph(_d, _s, node_bijection, edge_bijection);
206 const NodeBijection& getNodeBijection() const {
207 return node_bijection;
210 const EdgeBijection& getEdgeBijection() const {
211 return edge_bijection;
218 } //END OF NAMESPACE LEMON