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>
25 #include <lemon/maps.h>
29 ///\brief Graph utilities.
32 ///revise the documentation.
38 /// \addtogroup gutils
41 /// \brief Function to count the items in the graph.
43 /// This function counts the items in the graph.
44 /// The complexity of the function is O(n) because
45 /// it iterates on all of the items.
47 template <typename Graph, typename ItemIt>
48 inline int countItems(const Graph& g) {
50 for (ItemIt it(g); it != INVALID; ++it) {
58 template <typename Graph>
60 typename enable_if<typename Graph::NodeNumTag, int>::type
61 _countNodes(const Graph &g) {
65 template <typename Graph>
66 inline int _countNodes(Wrap<Graph> w) {
67 return countItems<Graph, typename Graph::NodeIt>(w.value);
70 /// \brief Function to count the nodes in the graph.
72 /// This function counts the nodes in the graph.
73 /// The complexity of the function is O(n) but for some
74 /// graph structure it is specialized to run in O(1).
76 /// \todo refer how to specialize it
78 template <typename Graph>
79 inline int countNodes(const Graph& g) {
80 return _countNodes<Graph>(g);
85 template <typename Graph>
87 typename enable_if<typename Graph::EdgeNumTag, int>::type
88 _countEdges(const Graph &g) {
92 template <typename Graph>
93 inline int _countEdges(Wrap<Graph> w) {
94 return countItems<Graph, typename Graph::EdgeIt>(w.value);
97 /// \brief Function to count the edges in the graph.
99 /// This function counts the edges in the graph.
100 /// The complexity of the function is O(e) but for some
101 /// graph structure it is specialized to run in O(1).
103 template <typename Graph>
104 inline int countEdges(const Graph& g) {
105 return _countEdges<Graph>(g);
108 // Undirected edge counting:
110 template <typename Graph>
112 typename enable_if<typename Graph::EdgeNumTag, int>::type
113 _countUndirEdges(const Graph &g) {
114 return g.undirEdgeNum();
117 template <typename Graph>
118 inline int _countUndirEdges(Wrap<Graph> w) {
119 return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
122 /// \brief Function to count the edges in the graph.
124 /// This function counts the edges in the graph.
125 /// The complexity of the function is O(e) but for some
126 /// graph structure it is specialized to run in O(1).
128 template <typename Graph>
129 inline int countUndirEdges(const Graph& g) {
130 return _countUndirEdges<Graph>(g);
135 template <typename Graph, typename DegIt>
136 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
138 for (DegIt it(_g, _n); it != INVALID; ++it) {
144 /// Finds an edge between two nodes of a graph.
146 /// Finds an edge from node \c u to node \c v in graph \c g.
148 /// If \c prev is \ref INVALID (this is the default value), then
149 /// it finds the first edge from \c u to \c v. Otherwise it looks for
150 /// the next edge from \c u to \c v after \c prev.
151 /// \return The found edge or \ref INVALID if there is no such an edge.
153 /// Thus you can iterate through each edge from \c u to \c v as it follows.
155 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
159 /// \todo We may want to use the \ref concept::GraphBase "GraphBase"
160 /// interface here...
161 /// \bug Untested ...
162 template <typename Graph>
163 typename Graph::Edge findEdge(const Graph &g,
164 typename Graph::Node u, typename Graph::Node v,
165 typename Graph::Edge prev = INVALID)
167 typename Graph::OutEdgeIt e(g,prev);
168 // if(prev==INVALID) g.first(e,u);
169 if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u);
171 while(e!=INVALID && g.target(e)!=v) ++e;
177 ///\todo Please document.
179 template <typename Graph>
180 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
181 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
186 ///\todo Please document.
188 template <typename Graph>
189 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
190 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
196 typename DestinationGraph,
197 typename SourceGraph,
198 typename NodeBijection>
199 void copyNodes(DestinationGraph& _d, const SourceGraph& _s,
200 NodeBijection& _nb) {
201 for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
202 _nb[it] = _d.addNode();
207 typename DestinationGraph,
208 typename SourceGraph,
209 typename NodeBijection,
210 typename EdgeBijection>
211 void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
212 const NodeBijection& _nb, EdgeBijection& _eb) {
213 for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
214 _eb[it] = _d.addEdge(_nb[_s.source(it)], _nb[_s.target(it)]);
219 typename DestinationGraph,
220 typename SourceGraph,
221 typename NodeBijection,
222 typename EdgeBijection>
223 void copyGraph(DestinationGraph& _d, const SourceGraph& _s,
224 NodeBijection& _nb, EdgeBijection& _eb) {
225 nodeCopy(_d, _s, _nb);
226 edgeCopy(_d, _s, _nb, _eb);
230 typename _DestinationGraph,
231 typename _SourceGraph,
232 typename _NodeBijection
233 =typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
234 typename _EdgeBijection
235 = typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
240 typedef _DestinationGraph DestinationGraph;
241 typedef _SourceGraph SourceGraph;
243 typedef _NodeBijection NodeBijection;
244 typedef _EdgeBijection EdgeBijection;
248 NodeBijection node_bijection;
249 EdgeBijection edge_bijection;
253 GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
254 copyGraph(_d, _s, node_bijection, edge_bijection);
257 const NodeBijection& getNodeBijection() const {
258 return node_bijection;
261 const EdgeBijection& getEdgeBijection() const {
262 return edge_bijection;
268 template <typename _Graph, typename _Item>
269 class ItemSetTraits {
272 template <typename _Graph>
273 class ItemSetTraits<_Graph, typename _Graph::Node> {
276 typedef _Graph Graph;
278 typedef typename Graph::Node Item;
279 typedef typename Graph::NodeIt ItemIt;
281 template <typename _Value>
282 class Map : public Graph::template NodeMap<_Value> {
284 typedef typename Graph::template NodeMap<_Value> Parent;
285 typedef typename Parent::Value Value;
287 Map(const Graph& _graph) : Parent(_graph) {}
288 Map(const Graph& _graph, const Value& _value)
289 : Parent(_graph, _value) {}
294 template <typename _Graph>
295 class ItemSetTraits<_Graph, typename _Graph::Edge> {
298 typedef _Graph Graph;
300 typedef typename Graph::Edge Item;
301 typedef typename Graph::EdgeIt ItemIt;
303 template <typename _Value>
304 class Map : public Graph::template EdgeMap<_Value> {
306 typedef typename Graph::template EdgeMap<_Value> Parent;
307 typedef typename Parent::Value Value;
309 Map(const Graph& _graph) : Parent(_graph) {}
310 Map(const Graph& _graph, const Value& _value)
311 : Parent(_graph, _value) {}
316 template <typename _Graph>
317 class ItemSetTraits<_Graph, typename _Graph::UndirEdge> {
320 typedef _Graph Graph;
322 typedef typename Graph::UndirEdge Item;
323 typedef typename Graph::UndirEdgeIt ItemIt;
325 template <typename _Value>
326 class Map : public Graph::template UndirEdgeMap<_Value> {
328 typedef typename Graph::template UndirEdgeMap<_Value> Parent;
329 typedef typename Parent::Value Value;
331 Map(const Graph& _graph) : Parent(_graph) {}
332 Map(const Graph& _graph, const Value& _value)
333 : Parent(_graph, _value) {}
340 /// \addtogroup graph_maps
343 template <typename Map, typename Enable = void>
344 struct ReferenceMapTraits {
345 typedef typename Map::Value Value;
346 typedef typename Map::Value& Reference;
347 typedef const typename Map::Value& ConstReference;
348 typedef typename Map::Value* Pointer;
349 typedef const typename Map::Value* ConstPointer;
352 template <typename Map>
353 struct ReferenceMapTraits<
355 typename enable_if<typename Map::FullTypeTag, void>::type
357 typedef typename Map::Value Value;
358 typedef typename Map::Reference Reference;
359 typedef typename Map::ConstReference ConstReference;
360 typedef typename Map::Pointer Pointer;
361 typedef typename Map::ConstPointer ConstPointer;
365 /// Provides an immutable and unique id for each item in the graph.
367 /// The IdMap class provides an unique and immutable mapping for each item
370 template <typename _Graph, typename _Item>
373 typedef _Graph Graph;
378 /// \brief Constructor.
380 /// Constructor for creating id map.
381 IdMap(const Graph& _graph) : graph(&_graph) {}
383 /// \brief Gives back the \e id of the item.
385 /// Gives back the immutable and unique \e id of the map.
386 int operator[](const Item& item) const { return graph->id(item);}
394 /// \brief The class represents the inverse of the map.
396 /// The class represents the inverse of the map.
400 /// \brief Constructor.
402 /// Constructor for creating an id-to-item map.
403 InverseMap(const Graph& _graph) : graph(&_graph) {}
405 /// \brief Constructor.
407 /// Constructor for creating an id-to-item map.
408 InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
410 /// \brief Gives back the given item from its id.
412 /// Gives back the given item from its id.
414 Item operator[](int id) const { return graph->fromId(id, Item());}
419 /// \brief Gives back the inverse of the map.
421 /// Gives back the inverse of the map.
422 InverseMap inverse() const { return InverseMap(*graph);}
427 /// \brief General inversable graph-map type.
429 /// This type provides simple inversable map functions.
430 /// The InversableMap wraps an arbitrary ReadWriteMap
431 /// and if a key is setted to a new value then store it
432 /// in the inverse map.
433 /// \param _Graph The graph type.
434 /// \param _Map The map to extend with inversable functionality.
440 = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent
442 class InvertableMap : protected _Map {
447 typedef _Graph Graph;
449 /// The key type of InvertableMap (Node, Edge, UndirEdge).
450 typedef typename _Map::Key Key;
451 /// The value type of the InvertableMap.
452 typedef typename _Map::Value Value;
454 /// \brief Constructor.
456 /// Construct a new InvertableMap for the graph.
458 InvertableMap(const Graph& graph) : Map(graph) {}
460 /// \brief The setter function of the map.
462 /// Sets the mapped value.
463 void set(const Key& key, const Value& val) {
464 Value oldval = Map::operator[](key);
465 typename Container::iterator it = invMap.find(oldval);
466 if (it != invMap.end() && it->second == key) {
469 invMap.insert(make_pair(val, key));
473 /// \brief The getter function of the map.
475 /// It gives back the value associated with the key.
476 const Value operator[](const Key& key) const {
477 return Map::operator[](key);
480 /// \brief Add a new key to the map.
482 /// Add a new key to the map. It is called by the
483 /// \c AlterationNotifier.
484 virtual void add(const Key& key) {
488 /// \brief Erase the key from the map.
490 /// Erase the key to the map. It is called by the
491 /// \c AlterationNotifier.
492 virtual void erase(const Key& key) {
493 Value val = Map::operator[](key);
494 typename Container::iterator it = invMap.find(val);
495 if (it != invMap.end() && it->second == key) {
501 /// \brief Clear the keys from the map and inverse map.
503 /// Clear the keys from the map and inverse map. It is called by the
504 /// \c AlterationNotifier.
505 virtual void clear() {
512 typedef std::map<Value, Key> Container;
517 /// \brief The inverse map type.
519 /// The inverse of this map. The subscript operator of the map
520 /// gives back always the item what was last assigned to the value.
523 /// \brief Constructor of the InverseMap.
525 /// Constructor of the InverseMap.
526 InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
528 /// The value type of the InverseMap.
529 typedef typename InvertableMap::Key Value;
530 /// The key type of the InverseMap.
531 typedef typename InvertableMap::Value Key;
533 /// \brief Subscript operator.
535 /// Subscript operator. It gives back always the item
536 /// what was last assigned to the value.
537 Value operator[](const Key& key) const {
538 typename Container::const_iterator it = inverted.invMap.find(key);
543 const InvertableMap& inverted;
546 /// \brief It gives back the just readeable inverse map.
548 /// It gives back the just readeable inverse map.
549 InverseMap inverse() const {
550 return InverseMap(*this);
557 /// \brief Provides a mutable, continuous and unique descriptor for each
558 /// item in the graph.
560 /// The DescriptorMap class provides a mutable, continuous and immutable
561 /// mapping for each item in the graph. The value for an item may mutated
562 /// on each operation when the an item erased or added to graph.
564 /// \param _Graph The graph class the \c DescriptorMap belongs to.
565 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
567 /// \param _Map A ReadWriteMap mapping from the item type to integer.
572 = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
574 class DescriptorMap : protected _Map {
580 /// The graph class of DescriptorMap.
581 typedef _Graph Graph;
583 /// The key type of DescriptorMap (Node, Edge, UndirEdge).
584 typedef typename _Map::Key Key;
585 /// The value type of DescriptorMap.
586 typedef typename _Map::Value Value;
588 /// \brief Constructor.
590 /// Constructor for descriptor map.
591 DescriptorMap(const Graph& _graph) : Map(_graph) {
595 /// \brief Add a new key to the map.
597 /// Add a new key to the map. It is called by the
598 /// \c AlterationNotifier.
599 virtual void add(const Item& item) {
601 Map::set(item, invMap.size());
602 invMap.push_back(item);
605 /// \brief Erase the key from the map.
607 /// Erase the key to the map. It is called by the
608 /// \c AlterationNotifier.
609 virtual void erase(const Item& item) {
610 Map::set(invMap.back(), Map::operator[](item));
611 invMap[Map::operator[](item)] = invMap.back();
616 /// \brief Build the unique map.
618 /// Build the unique map. It is called by the
619 /// \c AlterationNotifier.
620 virtual void build() {
623 const typename Map::Graph* graph = Map::getGraph();
624 for (graph->first(it); it != INVALID; graph->next(it)) {
625 Map::set(it, invMap.size());
626 invMap.push_back(it);
630 /// \brief Clear the keys from the map.
632 /// Clear the keys from the map. It is called by the
633 /// \c AlterationNotifier.
634 virtual void clear() {
639 /// \brief Gives back the \e descriptor of the item.
641 /// Gives back the mutable and unique \e descriptor of the map.
642 int operator[](const Item& item) const {
643 return Map::operator[](item);
648 typedef std::vector<Item> Container;
652 /// \brief The inverse map type.
654 /// The inverse map type.
657 /// \brief Constructor of the InverseMap.
659 /// Constructor of the InverseMap.
660 InverseMap(const DescriptorMap& _inverted)
661 : inverted(_inverted) {}
664 /// The value type of the InverseMap.
665 typedef typename DescriptorMap::Key Value;
666 /// The key type of the InverseMap.
667 typedef typename DescriptorMap::Value Key;
669 /// \brief Subscript operator.
671 /// Subscript operator. It gives back the item
672 /// that the descriptor belongs to currently.
673 Value operator[](const Key& key) const {
674 return inverted.invMap[key];
678 const DescriptorMap& inverted;
681 /// \brief Gives back the inverse of the map.
683 /// Gives back the inverse of the map.
684 const InverseMap inverse() const {
685 return InverseMap(*this);
689 /// \brief Returns the source of the given edge.
691 /// The SourceMap gives back the source Node of the given edge.
692 /// \author Balazs Dezso
693 template <typename Graph>
696 typedef typename Graph::Node Value;
697 typedef typename Graph::Edge Key;
699 /// \brief Constructor
702 /// \param _graph The graph that the map belongs to.
703 SourceMap(const Graph& _graph) : graph(_graph) {}
705 /// \brief The subscript operator.
707 /// The subscript operator.
708 /// \param edge The edge
709 /// \return The source of the edge
710 Value operator[](const Key& edge) {
711 return graph.source(edge);
718 /// \brief Returns a \ref SourceMap class
720 /// This function just returns an \ref SourceMap class.
721 /// \relates SourceMap
722 template <typename Graph>
723 inline SourceMap<Graph> sourceMap(const Graph& graph) {
724 return SourceMap<Graph>(graph);
727 /// \brief Returns the target of the given edge.
729 /// The TargetMap gives back the target Node of the given edge.
730 /// \author Balazs Dezso
731 template <typename Graph>
734 typedef typename Graph::Node Value;
735 typedef typename Graph::Edge Key;
737 /// \brief Constructor
740 /// \param _graph The graph that the map belongs to.
741 TargetMap(const Graph& _graph) : graph(_graph) {}
743 /// \brief The subscript operator.
745 /// The subscript operator.
746 /// \param edge The edge
747 /// \return The target of the edge
748 Value operator[](const Key& key) {
749 return graph.target(key);
756 /// \brief Returns a \ref TargetMap class
758 /// This function just returns an \ref TargetMap class.
759 /// \relates TargetMap
760 template <typename Graph>
761 inline TargetMap<Graph> targetMap(const Graph& graph) {
762 return TargetMap<Graph>(graph);
768 } //END OF NAMESPACE LEMON