3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_GRAPH_UTILS_H
20 #define LEMON_GRAPH_UTILS_H
27 #include <lemon/invalid.h>
28 #include <lemon/utility.h>
29 #include <lemon/maps.h>
30 #include <lemon/traits.h>
31 #include <lemon/bits/alteration_notifier.h>
35 ///\brief Graph utilities.
42 /// \addtogroup gutils
45 ///Creates convenience typedefs for the graph types and iterators
47 ///This \c \#define creates convenience typedefs for the following types
48 ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
49 ///\c OutEdgeIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
50 ///\c BoolEdgeMap, \c IntEdgeMap, \c DoubleEdgeMap.
51 ///\note If \c G it a template parameter, it should be used in this way.
53 /// GRAPH_TYPEDEFS(typename G)
56 ///\warning There are no typedefs for the graph maps because of the lack of
57 ///template typedefs in C++.
58 #define GRAPH_TYPEDEFS(Graph) \
59 typedef Graph:: Node Node; \
60 typedef Graph:: NodeIt NodeIt; \
61 typedef Graph:: Edge Edge; \
62 typedef Graph:: EdgeIt EdgeIt; \
63 typedef Graph:: InEdgeIt InEdgeIt; \
64 typedef Graph::OutEdgeIt OutEdgeIt;
65 // typedef Graph::template NodeMap<bool> BoolNodeMap;
66 // typedef Graph::template NodeMap<int> IntNodeMap;
67 // typedef Graph::template NodeMap<double> DoubleNodeMap;
68 // typedef Graph::template EdgeMap<bool> BoolEdgeMap;
69 // typedef Graph::template EdgeMap<int> IntEdgeMap;
70 // typedef Graph::template EdgeMap<double> DoubleEdgeMap;
72 ///Creates convenience typedefs for the undirected graph types and iterators
74 ///This \c \#define creates the same convenience typedefs as defined by
75 ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
76 ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
77 ///\c BoolUEdgeMap, \c IntUEdgeMap, \c DoubleUEdgeMap.
79 ///\note If \c G it a template parameter, it should be used in this way.
81 /// UNDIRGRAPH_TYPEDEFS(typename G)
84 ///\warning There are no typedefs for the graph maps because of the lack of
85 ///template typedefs in C++.
86 #define UNDIRGRAPH_TYPEDEFS(Graph) \
87 GRAPH_TYPEDEFS(Graph) \
88 typedef Graph:: UEdge UEdge; \
89 typedef Graph:: UEdgeIt UEdgeIt; \
90 typedef Graph:: IncEdgeIt IncEdgeIt;
91 // typedef Graph::template UEdgeMap<bool> BoolUEdgeMap;
92 // typedef Graph::template UEdgeMap<int> IntUEdgeMap;
93 // typedef Graph::template UEdgeMap<double> DoubleUEdgeMap;
97 /// \brief Function to count the items in the graph.
99 /// This function counts the items (nodes, edges etc) in the graph.
100 /// The complexity of the function is O(n) because
101 /// it iterates on all of the items.
103 template <typename Graph, typename ItemIt>
104 inline int countItems(const Graph& g) {
106 for (ItemIt it(g); it != INVALID; ++it) {
114 template <typename Graph>
115 inline typename enable_if<typename Graph::NodeNumTag, int>::type
116 _countNodes(const Graph &g) {
120 template <typename Graph>
121 inline int _countNodes(Wrap<Graph> w) {
122 return countItems<Graph, typename Graph::NodeIt>(w.value);
125 /// \brief Function to count the nodes in the graph.
127 /// This function counts the nodes in the graph.
128 /// The complexity of the function is O(n) but for some
129 /// graph structures it is specialized to run in O(1).
131 /// \todo refer how to specialize it
133 template <typename Graph>
134 inline int countNodes(const Graph& g) {
135 return _countNodes<Graph>(g);
140 template <typename Graph>
141 inline typename enable_if<typename Graph::EdgeNumTag, int>::type
142 _countEdges(const Graph &g) {
146 template <typename Graph>
147 inline int _countEdges(Wrap<Graph> w) {
148 return countItems<Graph, typename Graph::EdgeIt>(w.value);
151 /// \brief Function to count the edges in the graph.
153 /// This function counts the edges in the graph.
154 /// The complexity of the function is O(e) but for some
155 /// graph structures it is specialized to run in O(1).
157 template <typename Graph>
158 inline int countEdges(const Graph& g) {
159 return _countEdges<Graph>(g);
162 // Undirected edge counting:
164 template <typename Graph>
166 typename enable_if<typename Graph::EdgeNumTag, int>::type
167 _countUEdges(const Graph &g) {
171 template <typename Graph>
172 inline int _countUEdges(Wrap<Graph> w) {
173 return countItems<Graph, typename Graph::UEdgeIt>(w.value);
176 /// \brief Function to count the undirected edges in the graph.
178 /// This function counts the undirected edges in the graph.
179 /// The complexity of the function is O(e) but for some
180 /// graph structures it is specialized to run in O(1).
182 template <typename Graph>
183 inline int countUEdges(const Graph& g) {
184 return _countUEdges<Graph>(g);
189 template <typename Graph, typename DegIt>
190 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
192 for (DegIt it(_g, _n); it != INVALID; ++it) {
198 /// \brief Function to count the number of the out-edges from node \c n.
200 /// This function counts the number of the out-edges from node \c n
202 template <typename Graph>
203 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
204 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
207 /// \brief Function to count the number of the in-edges to node \c n.
209 /// This function counts the number of the in-edges to node \c n
211 template <typename Graph>
212 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
213 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
216 /// \brief Function to count the number of the inc-edges to node \c n.
218 /// This function counts the number of the inc-edges to node \c n
220 template <typename Graph>
221 inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
222 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
226 template <typename Graph>
228 typename enable_if<typename Graph::FindEdgeTag, typename Graph::Edge>::type
229 _findEdge(const Graph &g,
230 typename Graph::Node u, typename Graph::Node v,
231 typename Graph::Edge prev = INVALID) {
232 return g.findEdge(u, v, prev);
235 template <typename Graph>
236 inline typename Graph::Edge
237 _findEdge(Wrap<Graph> w,
238 typename Graph::Node u,
239 typename Graph::Node v,
240 typename Graph::Edge prev = INVALID) {
241 const Graph& g = w.value;
242 if (prev == INVALID) {
243 typename Graph::OutEdgeIt e(g, u);
244 while (e != INVALID && g.target(e) != v) ++e;
247 typename Graph::OutEdgeIt e(g, prev); ++e;
248 while (e != INVALID && g.target(e) != v) ++e;
253 /// \brief Finds an edge between two nodes of a graph.
255 /// Finds an edge from node \c u to node \c v in graph \c g.
257 /// If \c prev is \ref INVALID (this is the default value), then
258 /// it finds the first edge from \c u to \c v. Otherwise it looks for
259 /// the next edge from \c u to \c v after \c prev.
260 /// \return The found edge or \ref INVALID if there is no such an edge.
262 /// Thus you can iterate through each edge from \c u to \c v as it follows.
264 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
268 // /// \todo We may want to use the "GraphBase"
269 // /// interface here...
270 template <typename Graph>
271 inline typename Graph::Edge findEdge(const Graph &g,
272 typename Graph::Node u,
273 typename Graph::Node v,
274 typename Graph::Edge prev = INVALID) {
275 return _findEdge<Graph>(g, u, v, prev);
278 /// \brief Iterator for iterating on edges connected the same nodes.
280 /// Iterator for iterating on edges connected the same nodes. It is
281 /// higher level interface for the findEdge() function. You can
282 /// use it the following way:
284 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
289 /// \author Balazs Dezso
290 template <typename _Graph>
291 class ConEdgeIt : public _Graph::Edge {
294 typedef _Graph Graph;
295 typedef typename Graph::Edge Parent;
297 typedef typename Graph::Edge Edge;
298 typedef typename Graph::Node Node;
300 /// \brief Constructor.
302 /// Construct a new ConEdgeIt iterating on the edges which
303 /// connects the \c u and \c v node.
304 ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
305 Parent::operator=(findEdge(graph, u, v));
308 /// \brief Constructor.
310 /// Construct a new ConEdgeIt which continues the iterating from
312 ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
314 /// \brief Increment operator.
316 /// It increments the iterator and gives back the next edge.
317 ConEdgeIt& operator++() {
318 Parent::operator=(findEdge(graph, graph.source(*this),
319 graph.target(*this), *this));
326 template <typename Graph>
329 typename Graph::FindEdgeTag,
330 typename Graph::UEdge>::type
331 _findUEdge(const Graph &g,
332 typename Graph::Node u, typename Graph::Node v,
333 typename Graph::UEdge prev = INVALID) {
334 return g.findUEdge(u, v, prev);
337 template <typename Graph>
338 inline typename Graph::UEdge
339 _findUEdge(Wrap<Graph> w,
340 typename Graph::Node u,
341 typename Graph::Node v,
342 typename Graph::UEdge prev = INVALID) {
343 const Graph& g = w.value;
344 if (prev == INVALID) {
345 typename Graph::OutEdgeIt e(g, u);
346 while (e != INVALID && g.target(e) != v) ++e;
349 typename Graph::OutEdgeIt e(g, g.direct(prev, u)); ++e;
350 while (e != INVALID && g.target(e) != v) ++e;
355 /// \brief Finds an uedge between two nodes of a graph.
357 /// Finds an uedge from node \c u to node \c v in graph \c g.
359 /// If \c prev is \ref INVALID (this is the default value), then
360 /// it finds the first edge from \c u to \c v. Otherwise it looks for
361 /// the next edge from \c u to \c v after \c prev.
362 /// \return The found edge or \ref INVALID if there is no such an edge.
364 /// Thus you can iterate through each edge from \c u to \c v as it follows.
366 /// for(UEdge e = findUEdge(g,u,v); e != INVALID;
367 /// e = findUEdge(g,u,v,e)) {
371 // /// \todo We may want to use the "GraphBase"
372 // /// interface here...
373 template <typename Graph>
374 inline typename Graph::UEdge
375 findUEdge(const Graph &g,
376 typename Graph::Node u,
377 typename Graph::Node v,
378 typename Graph::UEdge prev = INVALID) {
379 return _findUEdge<Graph>(g, u, v, prev);
382 /// \brief Iterator for iterating on uedges connected the same nodes.
384 /// Iterator for iterating on uedges connected the same nodes. It is
385 /// higher level interface for the findUEdge() function. You can
386 /// use it the following way:
388 /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
393 /// \author Balazs Dezso
394 template <typename _Graph>
395 class ConUEdgeIt : public _Graph::UEdge {
398 typedef _Graph Graph;
399 typedef typename Graph::UEdge Parent;
401 typedef typename Graph::UEdge UEdge;
402 typedef typename Graph::Node Node;
404 /// \brief Constructor.
406 /// Construct a new ConUEdgeIt iterating on the edges which
407 /// connects the \c u and \c v node.
408 ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
409 Parent::operator=(findUEdge(graph, u, v));
412 /// \brief Constructor.
414 /// Construct a new ConUEdgeIt which continues the iterating from
416 ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
418 /// \brief Increment operator.
420 /// It increments the iterator and gives back the next edge.
421 ConUEdgeIt& operator++() {
422 Parent::operator=(findUEdge(graph, graph.source(*this),
423 graph.target(*this), *this));
430 /// \brief Copy a map.
432 /// This function copies the \c source map to the \c target map. It uses the
433 /// given iterator to iterate on the data structure and it uses the \c ref
434 /// mapping to convert the source's keys to the target's keys.
435 template <typename Target, typename Source,
436 typename ItemIt, typename Ref>
437 void copyMap(Target& target, const Source& source,
438 ItemIt it, const Ref& ref) {
439 for (; it != INVALID; ++it) {
440 target[ref[it]] = source[it];
444 /// \brief Copy the source map to the target map.
446 /// Copy the \c source map to the \c target map. It uses the given iterator
447 /// to iterate on the data structure.
448 template <typename Target, typename Source, typename ItemIt>
449 void copyMap(Target& target, const Source& source, ItemIt it) {
450 for (; it != INVALID; ++it) {
451 target[it] = source[it];
455 /// \brief Class to copy a graph.
457 /// Class to copy a graph to an other graph (duplicate a graph). The
458 /// simplest way of using it is through the \c copyGraph() function.
459 template <typename Target, typename Source>
462 typedef typename Source::Node Node;
463 typedef typename Source::NodeIt NodeIt;
464 typedef typename Source::Edge Edge;
465 typedef typename Source::EdgeIt EdgeIt;
467 typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
468 typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
470 /// \brief Constructor for the GraphCopy.
472 /// It copies the content of the \c _source graph into the
473 /// \c _target graph. It creates also two references, one beetween
474 /// the two nodeset and one beetween the two edgesets.
475 GraphCopy(Target& _target, const Source& _source)
476 : source(_source), target(_target),
477 nodeRefMap(_source), edgeRefMap(_source) {
478 for (NodeIt it(source); it != INVALID; ++it) {
479 nodeRefMap[it] = target.addNode();
481 for (EdgeIt it(source); it != INVALID; ++it) {
482 edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
483 nodeRefMap[source.target(it)]);
487 /// \brief Copies the node references into the given map.
489 /// Copies the node references into the given map.
490 template <typename NodeRef>
491 const GraphCopy& nodeRef(NodeRef& map) const {
492 for (NodeIt it(source); it != INVALID; ++it) {
493 map.set(it, nodeRefMap[it]);
498 /// \brief Reverse and copies the node references into the given map.
500 /// Reverse and copies the node references into the given map.
501 template <typename NodeRef>
502 const GraphCopy& nodeCrossRef(NodeRef& map) const {
503 for (NodeIt it(source); it != INVALID; ++it) {
504 map.set(nodeRefMap[it], it);
509 /// \brief Copies the edge references into the given map.
511 /// Copies the edge references into the given map.
512 template <typename EdgeRef>
513 const GraphCopy& edgeRef(EdgeRef& map) const {
514 for (EdgeIt it(source); it != INVALID; ++it) {
515 map.set(it, edgeRefMap[it]);
520 /// \brief Reverse and copies the edge references into the given map.
522 /// Reverse and copies the edge references into the given map.
523 template <typename EdgeRef>
524 const GraphCopy& edgeCrossRef(EdgeRef& map) const {
525 for (EdgeIt it(source); it != INVALID; ++it) {
526 map.set(edgeRefMap[it], it);
531 /// \brief Make copy of the given map.
533 /// Makes copy of the given map for the newly created graph.
534 /// The new map's key type is the target graph's node type,
535 /// and the copied map's key type is the source graph's node
537 template <typename TargetMap, typename SourceMap>
538 const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
539 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
543 /// \brief Make copy of the given map.
545 /// Makes copy of the given map for the newly created graph.
546 /// The new map's key type is the target graph's edge type,
547 /// and the copied map's key type is the source graph's edge
549 template <typename TargetMap, typename SourceMap>
550 const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
551 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
555 /// \brief Gives back the stored node references.
557 /// Gives back the stored node references.
558 const NodeRefMap& nodeRef() const {
562 /// \brief Gives back the stored edge references.
564 /// Gives back the stored edge references.
565 const EdgeRefMap& edgeRef() const {
573 const Source& source;
576 NodeRefMap nodeRefMap;
577 EdgeRefMap edgeRefMap;
580 /// \brief Copy a graph to an other graph.
582 /// Copy a graph to an other graph.
583 /// The usage of the function:
586 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
589 /// After the copy the \c nr map will contain the mapping from the
590 /// source graph's nodes to the target graph's nodes and the \c ecr will
591 /// contain the mapping from the target graph's edges to the source's
593 template <typename Target, typename Source>
594 GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
595 return GraphCopy<Target, Source>(target, source);
598 /// \brief Class to copy an undirected graph.
600 /// Class to copy an undirected graph to an other graph (duplicate a graph).
601 /// The simplest way of using it is through the \c copyUGraph() function.
602 template <typename Target, typename Source>
605 typedef typename Source::Node Node;
606 typedef typename Source::NodeIt NodeIt;
607 typedef typename Source::Edge Edge;
608 typedef typename Source::EdgeIt EdgeIt;
609 typedef typename Source::UEdge UEdge;
610 typedef typename Source::UEdgeIt UEdgeIt;
612 typedef typename Source::
613 template NodeMap<typename Target::Node> NodeRefMap;
615 typedef typename Source::
616 template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
621 EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
622 typedef typename Source::Edge Key;
623 typedef typename Target::Edge Value;
625 Value operator[](const Key& key) {
626 return gc.target.direct(gc.uEdgeRef[key],
627 gc.target.direction(key));
635 /// \brief Constructor for the UGraphCopy.
637 /// It copies the content of the \c _source graph into the
638 /// \c _target graph. It creates also two references, one beetween
639 /// the two nodeset and one beetween the two edgesets.
640 UGraphCopy(Target& _target, const Source& _source)
641 : source(_source), target(_target),
642 nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
643 for (NodeIt it(source); it != INVALID; ++it) {
644 nodeRefMap[it] = target.addNode();
646 for (UEdgeIt it(source); it != INVALID; ++it) {
647 uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
648 nodeRefMap[source.target(it)]);
652 /// \brief Copies the node references into the given map.
654 /// Copies the node references into the given map.
655 template <typename NodeRef>
656 const UGraphCopy& nodeRef(NodeRef& map) const {
657 for (NodeIt it(source); it != INVALID; ++it) {
658 map.set(it, nodeRefMap[it]);
663 /// \brief Reverse and copies the node references into the given map.
665 /// Reverse and copies the node references into the given map.
666 template <typename NodeRef>
667 const UGraphCopy& nodeCrossRef(NodeRef& map) const {
668 for (NodeIt it(source); it != INVALID; ++it) {
669 map.set(nodeRefMap[it], it);
674 /// \brief Copies the edge references into the given map.
676 /// Copies the edge references into the given map.
677 template <typename EdgeRef>
678 const UGraphCopy& edgeRef(EdgeRef& map) const {
679 for (EdgeIt it(source); it != INVALID; ++it) {
680 map.set(edgeRefMap[it], it);
685 /// \brief Reverse and copies the undirected edge references into the
688 /// Reverse and copies the undirected edge references into the given map.
689 template <typename EdgeRef>
690 const UGraphCopy& edgeCrossRef(EdgeRef& map) const {
691 for (EdgeIt it(source); it != INVALID; ++it) {
692 map.set(it, edgeRefMap[it]);
697 /// \brief Copies the undirected edge references into the given map.
699 /// Copies the undirected edge references into the given map.
700 template <typename EdgeRef>
701 const UGraphCopy& uEdgeRef(EdgeRef& map) const {
702 for (UEdgeIt it(source); it != INVALID; ++it) {
703 map.set(it, uEdgeRefMap[it]);
708 /// \brief Reverse and copies the undirected edge references into the
711 /// Reverse and copies the undirected edge references into the given map.
712 template <typename EdgeRef>
713 const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
714 for (UEdgeIt it(source); it != INVALID; ++it) {
715 map.set(uEdgeRefMap[it], it);
720 /// \brief Make copy of the given map.
722 /// Makes copy of the given map for the newly created graph.
723 /// The new map's key type is the target graph's node type,
724 /// and the copied map's key type is the source graph's node
726 template <typename TargetMap, typename SourceMap>
727 const UGraphCopy& nodeMap(TargetMap& tMap,
728 const SourceMap& sMap) const {
729 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
733 /// \brief Make copy of the given map.
735 /// Makes copy of the given map for the newly created graph.
736 /// The new map's key type is the target graph's edge type,
737 /// and the copied map's key type is the source graph's edge
739 template <typename TargetMap, typename SourceMap>
740 const UGraphCopy& edgeMap(TargetMap& tMap,
741 const SourceMap& sMap) const {
742 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
746 /// \brief Make copy of the given map.
748 /// Makes copy of the given map for the newly created graph.
749 /// The new map's key type is the target graph's edge type,
750 /// and the copied map's key type is the source graph's edge
752 template <typename TargetMap, typename SourceMap>
753 const UGraphCopy& uEdgeMap(TargetMap& tMap,
754 const SourceMap& sMap) const {
755 copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
759 /// \brief Gives back the stored node references.
761 /// Gives back the stored node references.
762 const NodeRefMap& nodeRef() const {
766 /// \brief Gives back the stored edge references.
768 /// Gives back the stored edge references.
769 const EdgeRefMap& edgeRef() const {
773 /// \brief Gives back the stored uedge references.
775 /// Gives back the stored uedge references.
776 const UEdgeRefMap& uEdgeRef() const {
784 const Source& source;
787 NodeRefMap nodeRefMap;
788 EdgeRefMap edgeRefMap;
789 UEdgeRefMap uEdgeRefMap;
792 /// \brief Copy a graph to an other graph.
794 /// Copy a graph to an other graph.
795 /// The usage of the function:
798 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
801 /// After the copy the \c nr map will contain the mapping from the
802 /// source graph's nodes to the target graph's nodes and the \c ecr will
803 /// contain the mapping from the target graph's edges to the source's
805 template <typename Target, typename Source>
806 UGraphCopy<Target, Source>
807 copyUGraph(Target& target, const Source& source) {
808 return UGraphCopy<Target, Source>(target, source);
814 /// \addtogroup graph_maps
817 /// Provides an immutable and unique id for each item in the graph.
819 /// The IdMap class provides a unique and immutable id for each item of the
820 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
821 /// different items (nodes) get different ids <li>\b immutable: the id of an
822 /// item (node) does not change (even if you delete other nodes). </ul>
823 /// Through this map you get access (i.e. can read) the inner id values of
824 /// the items stored in the graph. This map can be inverted with its member
825 /// class \c InverseMap.
827 template <typename _Graph, typename _Item>
830 typedef _Graph Graph;
835 /// \brief Constructor.
837 /// Constructor for creating id map.
838 IdMap(const Graph& _graph) : graph(&_graph) {}
840 /// \brief Gives back the \e id of the item.
842 /// Gives back the immutable and unique \e id of the map.
843 int operator[](const Item& item) const { return graph->id(item);}
851 /// \brief The class represents the inverse of its owner (IdMap).
853 /// The class represents the inverse of its owner (IdMap).
858 /// \brief Constructor.
860 /// Constructor for creating an id-to-item map.
861 InverseMap(const Graph& _graph) : graph(&_graph) {}
863 /// \brief Constructor.
865 /// Constructor for creating an id-to-item map.
866 InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
868 /// \brief Gives back the given item from its id.
870 /// Gives back the given item from its id.
872 Item operator[](int id) const { return graph->fromId(id, Item());}
877 /// \brief Gives back the inverse of the map.
879 /// Gives back the inverse of the IdMap.
880 InverseMap inverse() const { return InverseMap(*graph);}
885 /// \brief General invertable graph-map type.
887 /// This type provides simple invertable graph-maps.
888 /// The InvertableMap wraps an arbitrary ReadWriteMap
889 /// and if a key is set to a new value then store it
890 /// in the inverse map.
892 /// The values of the map can be accessed
893 /// with stl compatible forward iterator.
895 /// \param _Graph The graph type.
896 /// \param _Item The item type of the graph.
897 /// \param _Value The value type of the map.
899 /// \see IterableValueMap
901 /// \param _Map A ReadWriteMap mapping from the item type to integer.
903 typename _Graph, typename _Item, typename _Value, typename _Map
904 = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent
907 template <typename _Graph, typename _Item, typename _Value>
909 class InvertableMap : protected _Map {
912 /// The key type of InvertableMap (Node, Edge, UEdge).
913 typedef typename _Map::Key Key;
914 /// The value type of the InvertableMap.
915 typedef typename _Map::Value Value;
920 typedef _Graph Graph;
922 typedef std::map<Value, Key> Container;
929 /// \brief Constructor.
931 /// Construct a new InvertableMap for the graph.
933 InvertableMap(const Graph& graph) : Map(graph) {}
935 /// \brief Forward iterator for values.
937 /// This iterator is an stl compatible forward
938 /// iterator on the values of the map. The values can
939 /// be accessed in the [beginValue, endValue) range.
942 : public std::iterator<std::forward_iterator_tag, Value> {
943 friend class InvertableMap;
945 ValueIterator(typename Container::const_iterator _it)
951 ValueIterator& operator++() { ++it; return *this; }
952 ValueIterator operator++(int) {
953 ValueIterator tmp(*this);
958 const Value& operator*() const { return it->first; }
959 const Value* operator->() const { return &(it->first); }
961 bool operator==(ValueIterator jt) const { return it == jt.it; }
962 bool operator!=(ValueIterator jt) const { return it != jt.it; }
965 typename Container::const_iterator it;
968 /// \brief Returns an iterator to the first value.
970 /// Returns an stl compatible iterator to the
971 /// first value of the map. The values of the
972 /// map can be accessed in the [beginValue, endValue)
974 ValueIterator beginValue() const {
975 return ValueIterator(invMap.begin());
978 /// \brief Returns an iterator after the last value.
980 /// Returns an stl compatible iterator after the
981 /// last value of the map. The values of the
982 /// map can be accessed in the [beginValue, endValue)
984 ValueIterator endValue() const {
985 return ValueIterator(invMap.end());
988 /// \brief The setter function of the map.
990 /// Sets the mapped value.
991 void set(const Key& key, const Value& val) {
992 Value oldval = Map::operator[](key);
993 typename Container::iterator it = invMap.find(oldval);
994 if (it != invMap.end() && it->second == key) {
997 invMap.insert(make_pair(val, key));
1001 /// \brief The getter function of the map.
1003 /// It gives back the value associated with the key.
1004 typename MapTraits<Map>::ConstReturnValue
1005 operator[](const Key& key) const {
1006 return Map::operator[](key);
1011 /// \brief Erase the key from the map.
1013 /// Erase the key to the map. It is called by the
1014 /// \c AlterationNotifier.
1015 virtual void erase(const Key& key) {
1016 Value val = Map::operator[](key);
1017 typename Container::iterator it = invMap.find(val);
1018 if (it != invMap.end() && it->second == key) {
1024 /// \brief Erase more keys from the map.
1026 /// Erase more keys from the map. It is called by the
1027 /// \c AlterationNotifier.
1028 virtual void erase(const std::vector<Key>& keys) {
1029 for (int i = 0; i < (int)keys.size(); ++i) {
1030 Value val = Map::operator[](keys[i]);
1031 typename Container::iterator it = invMap.find(val);
1032 if (it != invMap.end() && it->second == keys[i]) {
1039 /// \brief Clear the keys from the map and inverse map.
1041 /// Clear the keys from the map and inverse map. It is called by the
1042 /// \c AlterationNotifier.
1043 virtual void clear() {
1050 /// \brief The inverse map type.
1052 /// The inverse of this map. The subscript operator of the map
1053 /// gives back always the item what was last assigned to the value.
1056 /// \brief Constructor of the InverseMap.
1058 /// Constructor of the InverseMap.
1059 InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
1061 /// The value type of the InverseMap.
1062 typedef typename InvertableMap::Key Value;
1063 /// The key type of the InverseMap.
1064 typedef typename InvertableMap::Value Key;
1066 /// \brief Subscript operator.
1068 /// Subscript operator. It gives back always the item
1069 /// what was last assigned to the value.
1070 Value operator[](const Key& key) const {
1071 typename Container::const_iterator it = inverted.invMap.find(key);
1076 const InvertableMap& inverted;
1079 /// \brief It gives back the just readeable inverse map.
1081 /// It gives back the just readeable inverse map.
1082 InverseMap inverse() const {
1083 return InverseMap(*this);
1090 /// \brief Provides a mutable, continuous and unique descriptor for each
1091 /// item in the graph.
1093 /// The DescriptorMap class provides a unique and continuous (but mutable)
1094 /// descriptor (id) for each item of the same type (e.g. node) in the
1095 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1096 /// different ids <li>\b continuous: the range of the ids is the set of
1097 /// integers between 0 and \c n-1, where \c n is the number of the items of
1098 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1099 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1100 /// with its member class \c InverseMap.
1102 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1103 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
1106 /// \param _Map A ReadWriteMap mapping from the item type to integer.
1108 typename _Graph, typename _Item, typename _Map
1109 = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
1112 template <typename _Graph, typename _Item>
1114 class DescriptorMap : protected _Map {
1120 /// The graph class of DescriptorMap.
1121 typedef _Graph Graph;
1123 /// The key type of DescriptorMap (Node, Edge, UEdge).
1124 typedef typename _Map::Key Key;
1125 /// The value type of DescriptorMap.
1126 typedef typename _Map::Value Value;
1128 /// \brief Constructor.
1130 /// Constructor for descriptor map.
1131 DescriptorMap(const Graph& _graph) : Map(_graph) {
1137 /// \brief Add a new key to the map.
1139 /// Add a new key to the map. It is called by the
1140 /// \c AlterationNotifier.
1141 virtual void add(const Item& item) {
1143 Map::set(item, invMap.size());
1144 invMap.push_back(item);
1147 /// \brief Add more new keys to the map.
1149 /// Add more new keys to the map. It is called by the
1150 /// \c AlterationNotifier.
1151 virtual void add(const std::vector<Item>& items) {
1153 for (int i = 0; i < (int)items.size(); ++i) {
1154 Map::set(items[i], invMap.size());
1155 invMap.push_back(items[i]);
1159 /// \brief Erase the key from the map.
1161 /// Erase the key from the map. It is called by the
1162 /// \c AlterationNotifier.
1163 virtual void erase(const Item& item) {
1164 Map::set(invMap.back(), Map::operator[](item));
1165 invMap[Map::operator[](item)] = invMap.back();
1170 /// \brief Erase more keys from the map.
1172 /// Erase more keys from the map. It is called by the
1173 /// \c AlterationNotifier.
1174 virtual void erase(const std::vector<Item>& items) {
1175 for (int i = 0; i < (int)items.size(); ++i) {
1176 Map::set(invMap.back(), Map::operator[](items[i]));
1177 invMap[Map::operator[](items[i])] = invMap.back();
1183 /// \brief Build the unique map.
1185 /// Build the unique map. It is called by the
1186 /// \c AlterationNotifier.
1187 virtual void build() {
1190 const typename Map::Graph* graph = Map::getGraph();
1191 for (graph->first(it); it != INVALID; graph->next(it)) {
1192 Map::set(it, invMap.size());
1193 invMap.push_back(it);
1197 /// \brief Clear the keys from the map.
1199 /// Clear the keys from the map. It is called by the
1200 /// \c AlterationNotifier.
1201 virtual void clear() {
1208 /// \brief Returns the maximal value plus one.
1210 /// Returns the maximal value plus one in the map.
1211 unsigned int size() const {
1212 return invMap.size();
1215 /// \brief Swaps the position of the two items in the map.
1217 /// Swaps the position of the two items in the map.
1218 void swap(const Item& p, const Item& q) {
1219 int pi = Map::operator[](p);
1220 int qi = Map::operator[](q);
1227 /// \brief Gives back the \e descriptor of the item.
1229 /// Gives back the mutable and unique \e descriptor of the map.
1230 int operator[](const Item& item) const {
1231 return Map::operator[](item);
1236 typedef std::vector<Item> Container;
1240 /// \brief The inverse map type of DescriptorMap.
1242 /// The inverse map type of DescriptorMap.
1245 /// \brief Constructor of the InverseMap.
1247 /// Constructor of the InverseMap.
1248 InverseMap(const DescriptorMap& _inverted)
1249 : inverted(_inverted) {}
1252 /// The value type of the InverseMap.
1253 typedef typename DescriptorMap::Key Value;
1254 /// The key type of the InverseMap.
1255 typedef typename DescriptorMap::Value Key;
1257 /// \brief Subscript operator.
1259 /// Subscript operator. It gives back the item
1260 /// that the descriptor belongs to currently.
1261 Value operator[](const Key& key) const {
1262 return inverted.invMap[key];
1265 /// \brief Size of the map.
1267 /// Returns the size of the map.
1268 unsigned int size() const {
1269 return inverted.invMap.size();
1273 const DescriptorMap& inverted;
1276 /// \brief Gives back the inverse of the map.
1278 /// Gives back the inverse of the map.
1279 const InverseMap inverse() const {
1280 return InverseMap(*this);
1284 /// \brief Returns the source of the given edge.
1286 /// The SourceMap gives back the source Node of the given edge.
1287 /// \author Balazs Dezso
1288 template <typename Graph>
1292 typedef typename Graph::Node Value;
1293 typedef typename Graph::Edge Key;
1295 /// \brief Constructor
1298 /// \param _graph The graph that the map belongs to.
1299 SourceMap(const Graph& _graph) : graph(_graph) {}
1301 /// \brief The subscript operator.
1303 /// The subscript operator.
1304 /// \param edge The edge
1305 /// \return The source of the edge
1306 Value operator[](const Key& edge) const {
1307 return graph.source(edge);
1314 /// \brief Returns a \ref SourceMap class
1316 /// This function just returns an \ref SourceMap class.
1317 /// \relates SourceMap
1318 template <typename Graph>
1319 inline SourceMap<Graph> sourceMap(const Graph& graph) {
1320 return SourceMap<Graph>(graph);
1323 /// \brief Returns the target of the given edge.
1325 /// The TargetMap gives back the target Node of the given edge.
1326 /// \author Balazs Dezso
1327 template <typename Graph>
1331 typedef typename Graph::Node Value;
1332 typedef typename Graph::Edge Key;
1334 /// \brief Constructor
1337 /// \param _graph The graph that the map belongs to.
1338 TargetMap(const Graph& _graph) : graph(_graph) {}
1340 /// \brief The subscript operator.
1342 /// The subscript operator.
1343 /// \param e The edge
1344 /// \return The target of the edge
1345 Value operator[](const Key& e) const {
1346 return graph.target(e);
1353 /// \brief Returns a \ref TargetMap class
1355 /// This function just returns a \ref TargetMap class.
1356 /// \relates TargetMap
1357 template <typename Graph>
1358 inline TargetMap<Graph> targetMap(const Graph& graph) {
1359 return TargetMap<Graph>(graph);
1362 /// \brief Returns the "forward" directed edge view of an undirected edge.
1364 /// Returns the "forward" directed edge view of an undirected edge.
1365 /// \author Balazs Dezso
1366 template <typename Graph>
1370 typedef typename Graph::Edge Value;
1371 typedef typename Graph::UEdge Key;
1373 /// \brief Constructor
1376 /// \param _graph The graph that the map belongs to.
1377 ForwardMap(const Graph& _graph) : graph(_graph) {}
1379 /// \brief The subscript operator.
1381 /// The subscript operator.
1382 /// \param key An undirected edge
1383 /// \return The "forward" directed edge view of undirected edge
1384 Value operator[](const Key& key) const {
1385 return graph.direct(key, true);
1392 /// \brief Returns a \ref ForwardMap class
1394 /// This function just returns an \ref ForwardMap class.
1395 /// \relates ForwardMap
1396 template <typename Graph>
1397 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
1398 return ForwardMap<Graph>(graph);
1401 /// \brief Returns the "backward" directed edge view of an undirected edge.
1403 /// Returns the "backward" directed edge view of an undirected edge.
1404 /// \author Balazs Dezso
1405 template <typename Graph>
1409 typedef typename Graph::Edge Value;
1410 typedef typename Graph::UEdge Key;
1412 /// \brief Constructor
1415 /// \param _graph The graph that the map belongs to.
1416 BackwardMap(const Graph& _graph) : graph(_graph) {}
1418 /// \brief The subscript operator.
1420 /// The subscript operator.
1421 /// \param key An undirected edge
1422 /// \return The "backward" directed edge view of undirected edge
1423 Value operator[](const Key& key) const {
1424 return graph.direct(key, false);
1431 /// \brief Returns a \ref BackwardMap class
1433 /// This function just returns a \ref BackwardMap class.
1434 /// \relates BackwardMap
1435 template <typename Graph>
1436 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
1437 return BackwardMap<Graph>(graph);
1440 /// \brief Potential difference map
1442 /// If there is an potential map on the nodes then we
1443 /// can get an edge map as we get the substraction of the
1444 /// values of the target and source.
1445 template <typename Graph, typename NodeMap>
1446 class PotentialDifferenceMap {
1448 typedef typename Graph::Edge Key;
1449 typedef typename NodeMap::Value Value;
1451 /// \brief Constructor
1453 /// Contructor of the map
1454 PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential)
1455 : graph(_graph), potential(_potential) {}
1457 /// \brief Const subscription operator
1459 /// Const subscription operator
1460 Value operator[](const Key& edge) const {
1461 return potential[graph.target(edge)] - potential[graph.source(edge)];
1466 const NodeMap& potential;
1469 /// \brief Just returns a PotentialDifferenceMap
1471 /// Just returns a PotentialDifferenceMap
1472 /// \relates PotentialDifferenceMap
1473 template <typename Graph, typename NodeMap>
1474 PotentialDifferenceMap<Graph, NodeMap>
1475 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
1476 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
1479 /// \brief Map of the node in-degrees.
1481 /// This map returns the in-degree of a node. Once it is constructed,
1482 /// the degrees are stored in a standard NodeMap, so each query is done
1483 /// in constant time. On the other hand, the values are updated automatically
1484 /// whenever the graph changes.
1486 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1487 /// alternative ways to modify the graph. The correct behavior of InDegMap
1488 /// is not guarantied if these additional features are used. For example
1489 /// the functions \ref ListGraph::changeSource() "changeSource()",
1490 /// \ref ListGraph::changeTarget() "changeTarget()" and
1491 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1492 /// of \ref ListGraph will \e not update the degree values correctly.
1496 template <typename _Graph>
1498 : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
1502 typedef _Graph Graph;
1504 typedef typename Graph::Node Key;
1508 class AutoNodeMap : public Graph::template NodeMap<int> {
1511 typedef typename Graph::template NodeMap<int> Parent;
1513 typedef typename Parent::Key Key;
1514 typedef typename Parent::Value Value;
1516 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1518 virtual void add(const Key& key) {
1520 Parent::set(key, 0);
1523 virtual void add(const std::vector<Key>& keys) {
1525 for (int i = 0; i < (int)keys.size(); ++i) {
1526 Parent::set(keys[i], 0);
1533 /// \brief Constructor.
1535 /// Constructor for creating in-degree map.
1536 InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1537 AlterationNotifier<typename _Graph::Edge>
1538 ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
1540 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1541 deg[it] = countInEdges(graph, it);
1545 virtual ~InDegMap() {
1546 AlterationNotifier<typename _Graph::Edge>::
1547 ObserverBase::detach();
1550 /// Gives back the in-degree of a Node.
1551 int operator[](const Key& key) const {
1557 typedef typename Graph::Edge Edge;
1559 virtual void add(const Edge& edge) {
1560 ++deg[graph.target(edge)];
1563 virtual void add(const std::vector<Edge>& edges) {
1564 for (int i = 0; i < (int)edges.size(); ++i) {
1565 ++deg[graph.target(edges[i])];
1569 virtual void erase(const Edge& edge) {
1570 --deg[graph.target(edge)];
1573 virtual void erase(const std::vector<Edge>& edges) {
1574 for (int i = 0; i < (int)edges.size(); ++i) {
1575 --deg[graph.target(edges[i])];
1579 virtual void build() {
1580 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1581 deg[it] = countInEdges(graph, it);
1585 virtual void clear() {
1586 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1592 const _Graph& graph;
1596 /// \brief Map of the node out-degrees.
1598 /// This map returns the out-degree of a node. Once it is constructed,
1599 /// the degrees are stored in a standard NodeMap, so each query is done
1600 /// in constant time. On the other hand, the values are updated automatically
1601 /// whenever the graph changes.
1603 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1604 /// alternative ways to modify the graph. The correct behavior of OutDegMap
1605 /// is not guarantied if these additional features are used. For example
1606 /// the functions \ref ListGraph::changeSource() "changeSource()",
1607 /// \ref ListGraph::changeTarget() "changeTarget()" and
1608 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1609 /// of \ref ListGraph will \e not update the degree values correctly.
1613 template <typename _Graph>
1615 : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
1619 typedef _Graph Graph;
1621 typedef typename Graph::Node Key;
1625 class AutoNodeMap : public Graph::template NodeMap<int> {
1628 typedef typename Graph::template NodeMap<int> Parent;
1630 typedef typename Parent::Key Key;
1631 typedef typename Parent::Value Value;
1633 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1635 virtual void add(const Key& key) {
1637 Parent::set(key, 0);
1639 virtual void add(const std::vector<Key>& keys) {
1641 for (int i = 0; i < (int)keys.size(); ++i) {
1642 Parent::set(keys[i], 0);
1649 /// \brief Constructor.
1651 /// Constructor for creating out-degree map.
1652 OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1653 AlterationNotifier<typename _Graph::Edge>
1654 ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
1656 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1657 deg[it] = countOutEdges(graph, it);
1661 virtual ~OutDegMap() {
1662 AlterationNotifier<typename _Graph::Edge>::
1663 ObserverBase::detach();
1666 /// Gives back the in-degree of a Node.
1667 int operator[](const Key& key) const {
1673 typedef typename Graph::Edge Edge;
1675 virtual void add(const Edge& edge) {
1676 ++deg[graph.source(edge)];
1679 virtual void add(const std::vector<Edge>& edges) {
1680 for (int i = 0; i < (int)edges.size(); ++i) {
1681 ++deg[graph.source(edges[i])];
1685 virtual void erase(const Edge& edge) {
1686 --deg[graph.source(edge)];
1689 virtual void erase(const std::vector<Edge>& edges) {
1690 for (int i = 0; i < (int)edges.size(); ++i) {
1691 --deg[graph.source(edges[i])];
1695 virtual void build() {
1696 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1697 deg[it] = countOutEdges(graph, it);
1701 virtual void clear() {
1702 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1708 const _Graph& graph;
1715 } //END OF NAMESPACE LEMON