The graph_factory branch (@ 1321) has been merged to trunk.
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 utils.
32 // counters in the graph
33 /// \brief Function to count the items in the graph.
35 /// This function counts the items in the graph.
36 /// The complexity of the function is O(n) because
37 /// it iterates on all of the items.
39 template <typename Graph, typename ItemIt>
40 inline int countItems(const Graph& _g) {
42 for (ItemIt it(_g); it != INVALID; ++it) {
48 /// \brief Function to count the nodes in the graph.
50 /// This function counts the nodes in the graph.
51 /// The complexity of the function is O(n) but for some
52 /// graph structure it is specialized to O(1).
54 template <typename Graph>
55 inline int countNodes(const Graph& _g) {
56 return countItems<Graph, typename Graph::NodeIt>(_g);
59 /// \brief Function to count the edges in the graph.
61 /// This function counts the edges in the graph.
62 /// The complexity of the function is O(e) but for some
63 /// graph structure it is specialized to O(1).
64 template <typename Graph>
65 inline int countEdges(const Graph& _g) {
66 return countItems<Graph, typename Graph::EdgeIt>(_g);
69 /// \brief Function to count the symmetric edges in the graph.
71 /// This function counts the symmetric edges in the graph.
72 /// The complexity of the function is O(e) but for some
73 /// graph structure it is specialized to O(1).
74 template <typename Graph>
75 inline int countSymEdges(const Graph& _g) {
76 return countItems<Graph, typename Graph::SymEdgeIt>(_g);
79 template <typename Graph, typename DegIt>
80 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
82 for (DegIt it(_g, _n); it != INVALID; ++it) {
88 template <typename Graph>
89 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
90 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
93 template <typename Graph>
94 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
95 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
101 typename DestinationGraph,
102 typename SourceGraph,
103 typename NodeBijection>
104 void copyNodes(DestinationGraph& _d, const SourceGraph& _s,
105 NodeBijection& _nb) {
106 for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
107 _nb[it] = _d.addNode();
112 typename DestinationGraph,
113 typename SourceGraph,
114 typename NodeBijection,
115 typename EdgeBijection>
116 void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
117 const NodeBijection& _nb, EdgeBijection& _eb) {
118 for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
119 _eb[it] = _d.addEdge(_nb[_s.tail(it)], _nb[_s.head(it)]);
124 typename DestinationGraph,
125 typename SourceGraph,
126 typename NodeBijection,
127 typename EdgeBijection>
128 void copyGraph(DestinationGraph& _d, const SourceGraph& _s,
129 NodeBijection& _nb, EdgeBijection& _eb) {
130 nodeCopy(_d, _s, _nb);
131 edgeCopy(_d, _s, _nb, _eb);
135 typename _DestinationGraph,
136 typename _SourceGraph,
137 typename _NodeBijection
138 =typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
139 typename _EdgeBijection
140 =typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
145 typedef _DestinationGraph DestinationGraph;
146 typedef _SourceGraph SourceGraph;
148 typedef _NodeBijection NodeBijection;
149 typedef _EdgeBijection EdgeBijection;
153 NodeBijection node_bijection;
154 EdgeBijection edge_bijection;
158 GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
159 copyGraph(_d, _s, node_bijection, edge_bijection);
162 const NodeBijection& getNodeBijection() const {
163 return node_bijection;
166 const EdgeBijection& getEdgeBijection() const {
167 return edge_bijection;