2 * src/lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, 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
23 #include <lemon/invalid.h>
24 #include <lemon/utility.h>
28 ///\brief Graph utilities.
31 ///revise the documentation.
37 /// \addtogroup gutils
40 /// \brief Function to count the items in the graph.
42 /// This function counts the items in the graph.
43 /// The complexity of the function is O(n) because
44 /// it iterates on all of the items.
46 template <typename Graph, typename ItemIt>
47 inline int countItems(const Graph& g) {
49 for (ItemIt it(g); it != INVALID; ++it) {
57 template <typename Graph>
59 typename enable_if<typename Graph::NodeNumTag, int>::type
60 _countNodes(const Graph &g) {
64 template <typename Graph>
65 inline int _countNodes(Wrap<Graph> w) {
66 return countItems<Graph, typename Graph::NodeIt>(w.value);
69 /// \brief Function to count the nodes in the graph.
71 /// This function counts the nodes in the graph.
72 /// The complexity of the function is O(n) but for some
73 /// graph structure it is specialized to run in O(1).
75 /// \todo refer how to specialize it
77 template <typename Graph>
78 inline int countNodes(const Graph& g) {
79 return _countNodes<Graph>(g);
84 template <typename Graph>
86 typename enable_if<typename Graph::EdgeNumTag, int>::type
87 _countEdges(const Graph &g) {
91 template <typename Graph>
92 inline int _countEdges(Wrap<Graph> w) {
93 return countItems<Graph, typename Graph::EdgeIt>(w.value);
96 /// \brief Function to count the edges in the graph.
98 /// This function counts the edges in the graph.
99 /// The complexity of the function is O(e) but for some
100 /// graph structure it is specialized to run in O(1).
102 template <typename Graph>
103 inline int countEdges(const Graph& g) {
104 return _countEdges<Graph>(g);
107 // Undirected edge counting:
109 template <typename Graph>
111 typename enable_if<typename Graph::EdgeNumTag, int>::type
112 _countUndirEdges(const Graph &g) {
113 return g.undirEdgeNum();
116 template <typename Graph>
117 inline int _countUndirEdges(Wrap<Graph> w) {
118 return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
121 /// \brief Function to count the edges in the graph.
123 /// This function counts the edges in the graph.
124 /// The complexity of the function is O(e) but for some
125 /// graph structure it is specialized to run in O(1).
127 template <typename Graph>
128 inline int countUndirEdges(const Graph& g) {
129 return _countUndirEdges<Graph>(g);
134 template <typename Graph, typename DegIt>
135 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
137 for (DegIt it(_g, _n); it != INVALID; ++it) {
143 /// Finds an edge between two nodes of a graph.
145 /// Finds an edge from node \c u to node \c v in graph \c g.
147 /// If \c prev is \ref INVALID (this is the default value), then
148 /// it finds the first edge from \c u to \c v. Otherwise it looks for
149 /// the next edge from \c u to \c v after \c prev.
150 /// \return The found edge or \ref INVALID if there is no such an edge.
152 /// Thus you can iterate through each edge from \c u to \c v as it follows.
154 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
158 /// \todo We may want to use the \ref concept::GraphBase "GraphBase"
159 /// interface here...
160 /// \bug Untested ...
161 template <typename Graph>
162 typename Graph::Edge findEdge(const Graph &g,
163 typename Graph::Node u, typename Graph::Node v,
164 typename Graph::Edge prev = INVALID)
166 typename Graph::OutEdgeIt e(g,prev);
167 // if(prev==INVALID) g.first(e,u);
168 if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u);
170 while(e!=INVALID && g.target(e)!=v) ++e;
176 ///\todo Please document.
178 template <typename Graph>
179 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
180 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
185 ///\todo Please document.
187 template <typename Graph>
188 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
189 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
195 typename DestinationGraph,
196 typename SourceGraph,
197 typename NodeBijection>
198 void copyNodes(DestinationGraph& _d, const SourceGraph& _s,
199 NodeBijection& _nb) {
200 for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
201 _nb[it] = _d.addNode();
206 typename DestinationGraph,
207 typename SourceGraph,
208 typename NodeBijection,
209 typename EdgeBijection>
210 void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
211 const NodeBijection& _nb, EdgeBijection& _eb) {
212 for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
213 _eb[it] = _d.addEdge(_nb[_s.source(it)], _nb[_s.target(it)]);
218 typename DestinationGraph,
219 typename SourceGraph,
220 typename NodeBijection,
221 typename EdgeBijection>
222 void copyGraph(DestinationGraph& _d, const SourceGraph& _s,
223 NodeBijection& _nb, EdgeBijection& _eb) {
224 nodeCopy(_d, _s, _nb);
225 edgeCopy(_d, _s, _nb, _eb);
229 typename _DestinationGraph,
230 typename _SourceGraph,
231 typename _NodeBijection
232 =typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
233 typename _EdgeBijection
234 = typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
239 typedef _DestinationGraph DestinationGraph;
240 typedef _SourceGraph SourceGraph;
242 typedef _NodeBijection NodeBijection;
243 typedef _EdgeBijection EdgeBijection;
247 NodeBijection node_bijection;
248 EdgeBijection edge_bijection;
252 GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
253 copyGraph(_d, _s, node_bijection, edge_bijection);
256 const NodeBijection& getNodeBijection() const {
257 return node_bijection;
260 const EdgeBijection& getEdgeBijection() const {
261 return edge_bijection;
267 template <typename _Graph, typename _Item>
268 class ItemSetTraits {
271 template <typename _Graph>
272 class ItemSetTraits<_Graph, typename _Graph::Node> {
275 typedef _Graph Graph;
277 typedef typename Graph::Node Item;
278 typedef typename Graph::NodeIt ItemIt;
280 template <typename _Value>
281 class Map : public Graph::template NodeMap<_Value> {
283 typedef typename Graph::template NodeMap<_Value> Parent;
284 typedef typename Parent::Value Value;
286 Map(const Graph& _graph) : Parent(_graph) {}
287 Map(const Graph& _graph, const Value& _value)
288 : Parent(_graph, _value) {}
293 template <typename _Graph>
294 class ItemSetTraits<_Graph, typename _Graph::Edge> {
297 typedef _Graph Graph;
299 typedef typename Graph::Edge Item;
300 typedef typename Graph::EdgeIt ItemIt;
302 template <typename _Value>
303 class Map : public Graph::template EdgeMap<_Value> {
305 typedef typename Graph::template EdgeMap<_Value> Parent;
306 typedef typename Parent::Value Value;
308 Map(const Graph& _graph) : Parent(_graph) {}
309 Map(const Graph& _graph, const Value& _value)
310 : Parent(_graph, _value) {}
315 template <typename _Graph>
316 class ItemSetTraits<_Graph, typename _Graph::UndirEdge> {
319 typedef _Graph Graph;
321 typedef typename Graph::UndirEdge Item;
322 typedef typename Graph::UndirEdgeIt ItemIt;
324 template <typename _Value>
325 class Map : public Graph::template UndirEdgeMap<_Value> {
327 typedef typename Graph::template UndirEdgeMap<_Value> Parent;
328 typedef typename Parent::Value Value;
330 Map(const Graph& _graph) : Parent(_graph) {}
331 Map(const Graph& _graph, const Value& _value)
332 : Parent(_graph, _value) {}
339 /// \addtogroup graph_maps
342 /// Provides an immutable and unique id for each item in the graph.
344 /// The IdMap class provides an unique and immutable mapping for each item
347 template <typename _Graph, typename _Item>
350 typedef _Graph Graph;
355 /// \brief The class represents the inverse of the map.
357 /// The class represents the inverse of the map.
361 /// \brief Constructor.
363 /// Constructor for creating an id-to-item map.
364 InverseMap(const Graph& _graph) : graph(&_graph) {}
365 /// \brief Gives back the given item from its id.
367 /// Gives back the given item from its id.
369 Item operator[](int id) const { return graph->fromId(id, Item());}
374 /// \brief Constructor.
376 /// Constructor for creating id map.
377 IdMap(const Graph& _graph) : graph(&_graph) {}
379 /// \brief Gives back the \e id of the item.
381 /// Gives back the immutable and unique \e id of the map.
382 int operator[](const Item& item) const { return graph->id(item);}
384 /// \brief Gives back the inverse of the map.
386 /// Gives back the inverse of the map.
387 InverseMap inverse() const { return InverseMap(*graph);}
395 template <typename Map, typename Enable = void>
396 struct ReferenceMapTraits {
397 typedef typename Map::Value Value;
398 typedef typename Map::Value& Reference;
399 typedef const typename Map::Value& ConstReference;
400 typedef typename Map::Value* Pointer;
401 typedef const typename Map::Value* ConstPointer;
404 template <typename Map>
405 struct ReferenceMapTraits<
407 typename enable_if<typename Map::FullTypeTag, void>::type
409 typedef typename Map::Value Value;
410 typedef typename Map::Reference Reference;
411 typedef typename Map::ConstReference ConstReference;
412 typedef typename Map::Pointer Pointer;
413 typedef typename Map::ConstPointer ConstPointer;
416 /// \brief General inversable graph-map type.
418 /// This type provides simple inversable map functions.
419 /// The InversableMap wraps an arbitrary ReadWriteMap
420 /// and if a key is setted to a new value then store it
421 /// in the inverse map.
422 /// \param _Graph The graph type.
423 /// \param _Map The map to extend with inversable functionality.
429 = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>
431 class InversableMap : protected _Map {
436 typedef _Graph Graph;
437 /// The key type of InversableMap (Node, Edge, UndirEdge).
438 typedef typename _Map::Key Key;
439 /// The value type of the InversableMap.
440 typedef typename _Map::Value Value;
442 typedef std::map<Value, Key> InverseMap;
444 typedef typename _Map::ConstReference ConstReference;
446 /// \brief Constructor.
448 /// Construct a new InversableMap for the graph.
450 InversableMap(const Graph& graph) : Map(graph) {}
452 /// \brief The setter function of the map.
455 void set(const Key& key, const Value& val) {
456 Value oldval = Map::operator[](key);
457 typename InverseMap::iterator it = invMap.find(oldval);
458 if (it != invMap.end() && it->second == key) {
461 invMap.insert(make_pair(val, key));
465 /// \brief The getter function of the map.
467 /// It gives back the value associated with the key.
468 ConstReference operator[](const Key& key) const {
469 return Map::operator[](key);
472 /// \brief Add a new key to the map.
474 /// Add a new key to the map. It is called by the
475 /// \c AlterationNotifier.
476 virtual void add(const Key& key) {
480 /// \brief Erase the key from the map.
482 /// Erase the key to the map. It is called by the
483 /// \c AlterationNotifier.
484 virtual void erase(const Key& key) {
485 Value val = Map::operator[](key);
486 typename InverseMap::iterator it = invMap.find(val);
487 if (it != invMap.end() && it->second == key) {
493 /// \brief Clear the keys from the map and inverse map.
495 /// Clear the keys from the map and inverse map. It is called by the
496 /// \c AlterationNotifier.
497 virtual void clear() {
502 /// \brief It gives back the just readeable inverse map.
504 /// It gives back the just readeable inverse map.
505 const InverseMap& inverse() const {
514 /// \brief Provides a mutable, continuous and unique descriptor for each
515 /// item in the graph.
517 /// The DescriptorMap class provides a mutable, continuous and immutable
518 /// mapping for each item in the graph.
520 /// \param _Graph The graph class the \c DescriptorMap belongs to.
521 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
523 /// \param _Map A ReadWriteMap mapping from the item type to integer.
528 typename _Map = typename ItemSetTraits<_Graph, _Item>::template Map<int>
530 class DescriptorMap : protected _Map {
536 /// The graph class of DescriptorMap.
537 typedef _Graph Graph;
539 /// The key type of DescriptorMap (Node, Edge, UndirEdge).
540 typedef typename _Map::Key Key;
541 /// The value type of DescriptorMap.
542 typedef typename _Map::Value Value;
544 typedef std::vector<Item> InverseMap;
546 /// \brief Constructor.
548 /// Constructor for creating descriptor map.
549 DescriptorMap(const Graph& _graph) : Map(_graph) {
553 /// \brief Add a new key to the map.
555 /// Add a new key to the map. It is called by the
556 /// \c AlterationNotifier.
557 virtual void add(const Item& item) {
559 Map::set(item, invMap.size());
560 invMap.push_back(item);
563 /// \brief Erase the key from the map.
565 /// Erase the key to the map. It is called by the
566 /// \c AlterationNotifier.
567 virtual void erase(const Item& item) {
568 Map::set(invMap.back(), Map::operator[](item));
569 invMap[Map::operator[](item)] = invMap.back();
573 /// \brief Build the unique map.
575 /// Build the unique map. It is called by the
576 /// \c AlterationNotifier.
577 virtual void build() {
580 const typename Map::Graph* graph = Map::getGraph();
581 for (graph->first(it); it != INVALID; graph->next(it)) {
582 Map::set(it, invMap.size());
583 invMap.push_back(it);
587 /// \brief Clear the keys from the map.
589 /// Clear the keys from the map. It is called by the
590 /// \c AlterationNotifier.
591 virtual void clear() {
596 /// \brief Gives back the \e descriptor of the item.
598 /// Gives back the mutable and unique \e descriptor of the map.
599 int operator[](const Item& item) const {
600 return Map::operator[](item);
603 /// \brief Gives back the inverse of the map.
605 /// Gives back the inverse of the map.
606 const InverseMap inverse() const {
611 std::vector<Item> invMap;
614 /// \brief Returns the source of the given edge.
616 /// The SourceMap gives back the source Node of the given edge.
617 /// \author Balazs Dezso
618 template <typename Graph>
621 typedef typename Graph::Node Value;
622 typedef typename Graph::Edge Key;
624 /// \brief Constructor
627 /// \param _graph The graph that the map belongs to.
628 SourceMap(const Graph& _graph) : graph(_graph) {}
630 /// \brief The subscript operator.
632 /// The subscript operator.
633 /// \param edge The edge
634 /// \return The source of the edge
635 Value operator[](const Key& edge) {
636 return graph.source(edge);
643 /// \brief Returns a \ref SourceMap class
645 /// This function just returns an \ref SourceMap class.
646 /// \relates SourceMap
647 template <typename Graph>
648 inline SourceMap<Graph> sourceMap(const Graph& graph) {
649 return SourceMap<Graph>(graph);
652 /// \brief Returns the target of the given edge.
654 /// The TargetMap gives back the target Node of the given edge.
655 /// \author Balazs Dezso
656 template <typename Graph>
659 typedef typename Graph::Node Value;
660 typedef typename Graph::Edge Key;
662 /// \brief Constructor
665 /// \param _graph The graph that the map belongs to.
666 TargetMap(const Graph& _graph) : graph(_graph) {}
668 /// \brief The subscript operator.
670 /// The subscript operator.
671 /// \param edge The edge
672 /// \return The target of the edge
673 Value operator[](const Key& key) {
674 return graph.target(key);
681 /// \brief Returns a \ref TargetMap class
683 /// This function just returns an \ref TargetMap class.
684 /// \relates TargetMap
685 template <typename Graph>
686 inline TargetMap<Graph> targetMap(const Graph& graph) {
687 return TargetMap<Graph>(graph);
693 } //END OF NAMESPACE LEMON