Six-coloring in plan graphs.
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
24 #include <lemon/invalid.h>
25 #include <lemon/utility.h>
26 #include <lemon/maps.h>
30 ///\brief Graph utilities.
33 ///revise the documentation.
39 /// \addtogroup gutils
42 /// \brief Function to count the items in the graph.
44 /// This function counts the items in the graph.
45 /// The complexity of the function is O(n) because
46 /// it iterates on all of the items.
48 template <typename Graph, typename ItemIt>
49 inline int countItems(const Graph& g) {
51 for (ItemIt it(g); it != INVALID; ++it) {
59 template <typename Graph>
61 typename enable_if<typename Graph::NodeNumTag, int>::type
62 _countNodes(const Graph &g) {
66 template <typename Graph>
67 inline int _countNodes(Wrap<Graph> w) {
68 return countItems<Graph, typename Graph::NodeIt>(w.value);
71 /// \brief Function to count the nodes in the graph.
73 /// This function counts the nodes in the graph.
74 /// The complexity of the function is O(n) but for some
75 /// graph structure it is specialized to run in O(1).
77 /// \todo refer how to specialize it
79 template <typename Graph>
80 inline int countNodes(const Graph& g) {
81 return _countNodes<Graph>(g);
86 template <typename Graph>
88 typename enable_if<typename Graph::EdgeNumTag, int>::type
89 _countEdges(const Graph &g) {
93 template <typename Graph>
94 inline int _countEdges(Wrap<Graph> w) {
95 return countItems<Graph, typename Graph::EdgeIt>(w.value);
98 /// \brief Function to count the edges in the graph.
100 /// This function counts the edges in the graph.
101 /// The complexity of the function is O(e) but for some
102 /// graph structure it is specialized to run in O(1).
104 template <typename Graph>
105 inline int countEdges(const Graph& g) {
106 return _countEdges<Graph>(g);
109 // Undirected edge counting:
111 template <typename Graph>
113 typename enable_if<typename Graph::EdgeNumTag, int>::type
114 _countUndirEdges(const Graph &g) {
115 return g.undirEdgeNum();
118 template <typename Graph>
119 inline int _countUndirEdges(Wrap<Graph> w) {
120 return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
123 /// \brief Function to count the edges in the graph.
125 /// This function counts the edges in the graph.
126 /// The complexity of the function is O(e) but for some
127 /// graph structure it is specialized to run in O(1).
129 template <typename Graph>
130 inline int countUndirEdges(const Graph& g) {
131 return _countUndirEdges<Graph>(g);
136 template <typename Graph, typename DegIt>
137 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
139 for (DegIt it(_g, _n); it != INVALID; ++it) {
145 /// Finds an edge between two nodes of a graph.
147 /// Finds an edge from node \c u to node \c v in graph \c g.
149 /// If \c prev is \ref INVALID (this is the default value), then
150 /// it finds the first edge from \c u to \c v. Otherwise it looks for
151 /// the next edge from \c u to \c v after \c prev.
152 /// \return The found edge or \ref INVALID if there is no such an edge.
154 /// Thus you can iterate through each edge from \c u to \c v as it follows.
156 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
160 /// \todo We may want to use the \ref concept::GraphBase "GraphBase"
161 /// interface here...
162 /// \bug Untested ...
163 template <typename Graph>
164 typename Graph::Edge findEdge(const Graph &g,
165 typename Graph::Node u, typename Graph::Node v,
166 typename Graph::Edge prev = INVALID)
168 typename Graph::OutEdgeIt e(g,prev);
169 // if(prev==INVALID) g.first(e,u);
170 if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u);
172 while(e!=INVALID && g.target(e)!=v) ++e;
178 ///\todo Please document.
180 template <typename Graph>
181 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
182 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
187 ///\todo Please document.
189 template <typename Graph>
190 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
191 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
197 typename DestinationGraph,
198 typename SourceGraph,
199 typename NodeBijection>
200 void copyNodes(DestinationGraph& _d, const SourceGraph& _s,
201 NodeBijection& _nb) {
202 for (typename SourceGraph::NodeIt it(_s); it != INVALID; ++it) {
203 _nb[it] = _d.addNode();
208 typename DestinationGraph,
209 typename SourceGraph,
210 typename NodeBijection,
211 typename EdgeBijection>
212 void copyEdges(DestinationGraph& _d, const SourceGraph& _s,
213 const NodeBijection& _nb, EdgeBijection& _eb) {
214 for (typename SourceGraph::EdgeIt it(_s); it != INVALID; ++it) {
215 _eb[it] = _d.addEdge(_nb[_s.source(it)], _nb[_s.target(it)]);
220 typename DestinationGraph,
221 typename SourceGraph,
222 typename NodeBijection,
223 typename EdgeBijection>
224 void copyGraph(DestinationGraph& _d, const SourceGraph& _s,
225 NodeBijection& _nb, EdgeBijection& _eb) {
226 nodeCopy(_d, _s, _nb);
227 edgeCopy(_d, _s, _nb, _eb);
231 typename _DestinationGraph,
232 typename _SourceGraph,
233 typename _NodeBijection
234 =typename _SourceGraph::template NodeMap<typename _DestinationGraph::Node>,
235 typename _EdgeBijection
236 = typename _SourceGraph::template EdgeMap<typename _DestinationGraph::Edge>
241 typedef _DestinationGraph DestinationGraph;
242 typedef _SourceGraph SourceGraph;
244 typedef _NodeBijection NodeBijection;
245 typedef _EdgeBijection EdgeBijection;
249 NodeBijection node_bijection;
250 EdgeBijection edge_bijection;
254 GraphCopy(DestinationGraph& _d, const SourceGraph& _s) {
255 copyGraph(_d, _s, node_bijection, edge_bijection);
258 const NodeBijection& getNodeBijection() const {
259 return node_bijection;
262 const EdgeBijection& getEdgeBijection() const {
263 return edge_bijection;
269 template <typename _Graph, typename _Item>
270 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;
364 /// Provides an immutable and unique id for each item in the graph.
366 /// The IdMap class provides an unique and immutable mapping for each item
369 template <typename _Graph, typename _Item>
372 typedef _Graph Graph;
377 typedef True NeedCopy;
379 /// \brief Constructor.
381 /// Constructor for creating id map.
382 IdMap(const Graph& _graph) : graph(&_graph) {}
384 /// \brief Gives back the \e id of the item.
386 /// Gives back the immutable and unique \e id of the map.
387 int operator[](const Item& item) const { return graph->id(item);}
395 /// \brief The class represents the inverse of the map.
397 /// The class represents the inverse of the map.
402 typedef True NeedCopy;
404 /// \brief Constructor.
406 /// Constructor for creating an id-to-item map.
407 InverseMap(const Graph& _graph) : graph(&_graph) {}
409 /// \brief Constructor.
411 /// Constructor for creating an id-to-item map.
412 InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
414 /// \brief Gives back the given item from its id.
416 /// Gives back the given item from its id.
418 Item operator[](int id) const { return graph->fromId(id, Item());}
423 /// \brief Gives back the inverse of the map.
425 /// Gives back the inverse of the map.
426 InverseMap inverse() const { return InverseMap(*graph);}
431 /// \brief General inversable graph-map type.
433 /// This type provides simple inversable map functions.
434 /// The InversableMap wraps an arbitrary ReadWriteMap
435 /// and if a key is setted to a new value then store it
436 /// in the inverse map.
437 /// \param _Graph The graph type.
438 /// \param _Map The map to extend with inversable functionality.
444 = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent
446 class InvertableMap : protected _Map {
451 typedef _Graph Graph;
453 /// The key type of InvertableMap (Node, Edge, UndirEdge).
454 typedef typename _Map::Key Key;
455 /// The value type of the InvertableMap.
456 typedef typename _Map::Value Value;
458 /// \brief Constructor.
460 /// Construct a new InvertableMap for the graph.
462 InvertableMap(const Graph& graph) : Map(graph) {}
464 /// \brief The setter function of the map.
466 /// Sets the mapped value.
467 void set(const Key& key, const Value& val) {
468 Value oldval = Map::operator[](key);
469 typename Container::iterator it = invMap.find(oldval);
470 if (it != invMap.end() && it->second == key) {
473 invMap.insert(make_pair(val, key));
477 /// \brief The getter function of the map.
479 /// It gives back the value associated with the key.
480 const Value operator[](const Key& key) const {
481 return Map::operator[](key);
484 /// \brief Add a new key to the map.
486 /// Add a new key to the map. It is called by the
487 /// \c AlterationNotifier.
488 virtual void add(const Key& key) {
492 /// \brief Erase the key from the map.
494 /// Erase the key to the map. It is called by the
495 /// \c AlterationNotifier.
496 virtual void erase(const Key& key) {
497 Value val = Map::operator[](key);
498 typename Container::iterator it = invMap.find(val);
499 if (it != invMap.end() && it->second == key) {
505 /// \brief Clear the keys from the map and inverse map.
507 /// Clear the keys from the map and inverse map. It is called by the
508 /// \c AlterationNotifier.
509 virtual void clear() {
516 typedef std::map<Value, Key> Container;
521 /// \brief The inverse map type.
523 /// The inverse of this map. The subscript operator of the map
524 /// gives back always the item what was last assigned to the value.
527 /// \brief Constructor of the InverseMap.
529 /// Constructor of the InverseMap.
530 InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
532 /// The value type of the InverseMap.
533 typedef typename InvertableMap::Key Value;
534 /// The key type of the InverseMap.
535 typedef typename InvertableMap::Value Key;
537 /// \brief Subscript operator.
539 /// Subscript operator. It gives back always the item
540 /// what was last assigned to the value.
541 Value operator[](const Key& key) const {
542 typename Container::const_iterator it = inverted.invMap.find(key);
547 const InvertableMap& inverted;
550 /// \brief It gives back the just readeable inverse map.
552 /// It gives back the just readeable inverse map.
553 InverseMap inverse() const {
554 return InverseMap(*this);
561 /// \brief Provides a mutable, continuous and unique descriptor for each
562 /// item in the graph.
564 /// The DescriptorMap class provides a mutable, continuous and immutable
565 /// mapping for each item in the graph. The value for an item may mutated
566 /// on each operation when the an item erased or added to graph.
568 /// \param _Graph The graph class the \c DescriptorMap belongs to.
569 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
571 /// \param _Map A ReadWriteMap mapping from the item type to integer.
576 = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
578 class DescriptorMap : protected _Map {
584 /// The graph class of DescriptorMap.
585 typedef _Graph Graph;
587 /// The key type of DescriptorMap (Node, Edge, UndirEdge).
588 typedef typename _Map::Key Key;
589 /// The value type of DescriptorMap.
590 typedef typename _Map::Value Value;
592 /// \brief Constructor.
594 /// Constructor for descriptor map.
595 DescriptorMap(const Graph& _graph) : Map(_graph) {
599 /// \brief Add a new key to the map.
601 /// Add a new key to the map. It is called by the
602 /// \c AlterationNotifier.
603 virtual void add(const Item& item) {
605 Map::set(item, invMap.size());
606 invMap.push_back(item);
609 /// \brief Erase the key from the map.
611 /// Erase the key to the map. It is called by the
612 /// \c AlterationNotifier.
613 virtual void erase(const Item& item) {
614 Map::set(invMap.back(), Map::operator[](item));
615 invMap[Map::operator[](item)] = invMap.back();
620 /// \brief Build the unique map.
622 /// Build the unique map. It is called by the
623 /// \c AlterationNotifier.
624 virtual void build() {
627 const typename Map::Graph* graph = Map::getGraph();
628 for (graph->first(it); it != INVALID; graph->next(it)) {
629 Map::set(it, invMap.size());
630 invMap.push_back(it);
634 /// \brief Clear the keys from the map.
636 /// Clear the keys from the map. It is called by the
637 /// \c AlterationNotifier.
638 virtual void clear() {
643 /// \brief Gives back the \e descriptor of the item.
645 /// Gives back the mutable and unique \e descriptor of the map.
646 int operator[](const Item& item) const {
647 return Map::operator[](item);
652 typedef std::vector<Item> Container;
656 /// \brief The inverse map type.
658 /// The inverse map type.
661 /// \brief Constructor of the InverseMap.
663 /// Constructor of the InverseMap.
664 InverseMap(const DescriptorMap& _inverted)
665 : inverted(_inverted) {}
668 /// The value type of the InverseMap.
669 typedef typename DescriptorMap::Key Value;
670 /// The key type of the InverseMap.
671 typedef typename DescriptorMap::Value Key;
673 /// \brief Subscript operator.
675 /// Subscript operator. It gives back the item
676 /// that the descriptor belongs to currently.
677 Value operator[](const Key& key) const {
678 return inverted.invMap[key];
682 const DescriptorMap& inverted;
685 /// \brief Gives back the inverse of the map.
687 /// Gives back the inverse of the map.
688 const InverseMap inverse() const {
689 return InverseMap(*this);
693 /// \brief Returns the source of the given edge.
695 /// The SourceMap gives back the source Node of the given edge.
696 /// \author Balazs Dezso
697 template <typename Graph>
701 typedef True NeedCopy;
703 typedef typename Graph::Node Value;
704 typedef typename Graph::Edge Key;
706 /// \brief Constructor
709 /// \param _graph The graph that the map belongs to.
710 SourceMap(const Graph& _graph) : graph(_graph) {}
712 /// \brief The subscript operator.
714 /// The subscript operator.
715 /// \param edge The edge
716 /// \return The source of the edge
717 Value operator[](const Key& edge) {
718 return graph.source(edge);
725 /// \brief Returns a \ref SourceMap class
727 /// This function just returns an \ref SourceMap class.
728 /// \relates SourceMap
729 template <typename Graph>
730 inline SourceMap<Graph> sourceMap(const Graph& graph) {
731 return SourceMap<Graph>(graph);
734 /// \brief Returns the target of the given edge.
736 /// The TargetMap gives back the target Node of the given edge.
737 /// \author Balazs Dezso
738 template <typename Graph>
742 typedef True NeedCopy;
744 typedef typename Graph::Node Value;
745 typedef typename Graph::Edge Key;
747 /// \brief Constructor
750 /// \param _graph The graph that the map belongs to.
751 TargetMap(const Graph& _graph) : graph(_graph) {}
753 /// \brief The subscript operator.
755 /// The subscript operator.
756 /// \param edge The edge
757 /// \return The target of the edge
758 Value operator[](const Key& key) {
759 return graph.target(key);
766 /// \brief Returns a \ref TargetMap class
768 /// This function just returns an \ref TargetMap class.
769 /// \relates TargetMap
770 template <typename Graph>
771 inline TargetMap<Graph> targetMap(const Graph& graph) {
772 return TargetMap<Graph>(graph);
775 /// \brief Returns the "forward" directed edge view of undirected edge.
777 /// Returns the "forward" directed edge view of undirected edge.
778 /// \author Balazs Dezso
779 template <typename Graph>
783 typedef True NeedCopy;
785 typedef typename Graph::Edge Value;
786 typedef typename Graph::UndirEdge Key;
788 /// \brief Constructor
791 /// \param _graph The graph that the map belongs to.
792 ForwardMap(const Graph& _graph) : graph(_graph) {}
794 /// \brief The subscript operator.
796 /// The subscript operator.
797 /// \param key An undirected edge
798 /// \return The "forward" directed edge view of undirected edge
799 Value operator[](const Key& key) const {
800 return graph.edgeWithSource(key, graph.source(key));
807 /// \brief Returns a \ref ForwardMap class
809 /// This function just returns an \ref ForwardMap class.
810 /// \relates ForwardMap
811 template <typename Graph>
812 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
813 return ForwardMap<Graph>(graph);
816 /// \brief Returns the "backward" directed edge view of undirected edge.
818 /// Returns the "backward" directed edge view of undirected edge.
819 /// \author Balazs Dezso
820 template <typename Graph>
823 typedef True NeedCopy;
825 typedef typename Graph::Edge Value;
826 typedef typename Graph::UndirEdge Key;
828 /// \brief Constructor
831 /// \param _graph The graph that the map belongs to.
832 BackwardMap(const Graph& _graph) : graph(_graph) {}
834 /// \brief The subscript operator.
836 /// The subscript operator.
837 /// \param key An undirected edge
838 /// \return The "backward" directed edge view of undirected edge
839 Value operator[](const Key& key) const {
840 return graph.edgeWithSource(key, graph.target(key));
847 /// \brief Returns a \ref BackwardMap class
849 /// This function just returns an \ref BackwardMap class.
850 /// \relates BackwardMap
851 template <typename Graph>
852 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
853 return BackwardMap<Graph>(graph);
859 } //END OF NAMESPACE LEMON