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) {
96 ///\todo Please document.
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);
105 ///\todo Please document.
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);
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();
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)]);
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);
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>
159 typedef _DestinationGraph DestinationGraph;
160 typedef _SourceGraph SourceGraph;
162 typedef _NodeBijection NodeBijection;
163 typedef _EdgeBijection EdgeBijection;
167 NodeBijection node_bijection;
168 EdgeBijection edge_bijection;
172 GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
173 copyGraph(_d, _s, node_bijection, edge_bijection);
176 const NodeBijection& getNodeBijection() const {
177 return node_bijection;
180 const EdgeBijection& getEdgeBijection() const {
181 return edge_bijection;
188 } //END OF NAMESPACE LEMON