2 * lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2006 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
25 #include <lemon/invalid.h>
26 #include <lemon/utility.h>
27 #include <lemon/maps.h>
28 #include <lemon/traits.h>
29 #include <lemon/bits/alteration_notifier.h>
33 ///\brief Graph utilities.
40 /// \addtogroup gutils
43 ///Creates convenience typedefs for the graph types and iterators
45 ///This \c \#define creates convenience typedefs for the following types
46 ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
47 ///\c OutEdgeIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
48 ///\c BoolEdgeMap, \c IntEdgeMap, \c DoubleEdgeMap.
49 ///\note If \c G it a template parameter, it should be used in this way.
51 /// GRAPH_TYPEDEFS(typename G)
54 ///\warning There are no typedefs for the graph maps because of the lack of
55 ///template typedefs in C++.
56 #define GRAPH_TYPEDEFS(Graph) \
57 typedef Graph:: Node Node; \
58 typedef Graph:: NodeIt NodeIt; \
59 typedef Graph:: Edge Edge; \
60 typedef Graph:: EdgeIt EdgeIt; \
61 typedef Graph:: InEdgeIt InEdgeIt; \
62 typedef Graph::OutEdgeIt OutEdgeIt;
63 // typedef Graph::template NodeMap<bool> BoolNodeMap;
64 // typedef Graph::template NodeMap<int> IntNodeMap;
65 // typedef Graph::template NodeMap<double> DoubleNodeMap;
66 // typedef Graph::template EdgeMap<bool> BoolEdgeMap;
67 // typedef Graph::template EdgeMap<int> IntEdgeMap;
68 // typedef Graph::template EdgeMap<double> DoubleEdgeMap;
70 ///Creates convenience typedefs for the undirected graph types and iterators
72 ///This \c \#define creates the same convenience typedefs as defined by
73 ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
74 ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
75 ///\c BoolUEdgeMap, \c IntUEdgeMap, \c DoubleUEdgeMap.
77 ///\note If \c G it a template parameter, it should be used in this way.
79 /// UNDIRGRAPH_TYPEDEFS(typename G)
82 ///\warning There are no typedefs for the graph maps because of the lack of
83 ///template typedefs in C++.
84 #define UNDIRGRAPH_TYPEDEFS(Graph) \
85 GRAPH_TYPEDEFS(Graph) \
86 typedef Graph:: UEdge UEdge; \
87 typedef Graph:: UEdgeIt UEdgeIt; \
88 typedef Graph:: IncEdgeIt IncEdgeIt;
89 // typedef Graph::template UEdgeMap<bool> BoolUEdgeMap;
90 // typedef Graph::template UEdgeMap<int> IntUEdgeMap;
91 // typedef Graph::template UEdgeMap<double> DoubleUEdgeMap;
95 /// \brief Function to count the items in the graph.
97 /// This function counts the items (nodes, edges etc) in the graph.
98 /// The complexity of the function is O(n) because
99 /// it iterates on all of the items.
101 template <typename Graph, typename ItemIt>
102 inline int countItems(const Graph& g) {
104 for (ItemIt it(g); it != INVALID; ++it) {
112 template <typename Graph>
113 inline typename enable_if<typename Graph::NodeNumTag, int>::type
114 _countNodes(const Graph &g) {
118 template <typename Graph>
119 inline int _countNodes(Wrap<Graph> w) {
120 return countItems<Graph, typename Graph::NodeIt>(w.value);
123 /// \brief Function to count the nodes in the graph.
125 /// This function counts the nodes in the graph.
126 /// The complexity of the function is O(n) but for some
127 /// graph structures it is specialized to run in O(1).
129 /// \todo refer how to specialize it
131 template <typename Graph>
132 inline int countNodes(const Graph& g) {
133 return _countNodes<Graph>(g);
138 template <typename Graph>
139 inline typename enable_if<typename Graph::EdgeNumTag, int>::type
140 _countEdges(const Graph &g) {
144 template <typename Graph>
145 inline int _countEdges(Wrap<Graph> w) {
146 return countItems<Graph, typename Graph::EdgeIt>(w.value);
149 /// \brief Function to count the edges in the graph.
151 /// This function counts the edges in the graph.
152 /// The complexity of the function is O(e) but for some
153 /// graph structures it is specialized to run in O(1).
155 template <typename Graph>
156 inline int countEdges(const Graph& g) {
157 return _countEdges<Graph>(g);
160 // Undirected edge counting:
162 template <typename Graph>
164 typename enable_if<typename Graph::EdgeNumTag, int>::type
165 _countUEdges(const Graph &g) {
169 template <typename Graph>
170 inline int _countUEdges(Wrap<Graph> w) {
171 return countItems<Graph, typename Graph::UEdgeIt>(w.value);
174 /// \brief Function to count the undirected edges in the graph.
176 /// This function counts the undirected edges in the graph.
177 /// The complexity of the function is O(e) but for some
178 /// graph structures it is specialized to run in O(1).
180 template <typename Graph>
181 inline int countUEdges(const Graph& g) {
182 return _countUEdges<Graph>(g);
187 template <typename Graph, typename DegIt>
188 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
190 for (DegIt it(_g, _n); it != INVALID; ++it) {
196 /// \brief Function to count the number of the out-edges from node \c n.
198 /// This function counts the number of the out-edges from node \c n
200 template <typename Graph>
201 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
202 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
205 /// \brief Function to count the number of the in-edges to node \c n.
207 /// This function counts the number of the in-edges to node \c n
209 template <typename Graph>
210 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
211 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
214 /// \brief Function to count the number of the inc-edges to node \c n.
216 /// This function counts the number of the inc-edges to node \c n
218 template <typename Graph>
219 inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
220 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
224 template <typename Graph>
226 typename enable_if<typename Graph::FindEdgeTag, typename Graph::Edge>::type
227 _findEdge(const Graph &g,
228 typename Graph::Node u, typename Graph::Node v,
229 typename Graph::Edge prev = INVALID) {
230 return g.findEdge(u, v, prev);
233 template <typename Graph>
234 inline typename Graph::Edge
235 _findEdge(Wrap<Graph> w,
236 typename Graph::Node u,
237 typename Graph::Node v,
238 typename Graph::Edge prev = INVALID) {
239 const Graph& g = w.value;
240 if (prev == INVALID) {
241 typename Graph::OutEdgeIt e(g, u);
242 while (e != INVALID && g.target(e) != v) ++e;
245 typename Graph::OutEdgeIt e(g, prev); ++e;
246 while (e != INVALID && g.target(e) != v) ++e;
251 /// \brief Finds an edge between two nodes of a graph.
253 /// Finds an edge from node \c u to node \c v in graph \c g.
255 /// If \c prev is \ref INVALID (this is the default value), then
256 /// it finds the first edge from \c u to \c v. Otherwise it looks for
257 /// the next edge from \c u to \c v after \c prev.
258 /// \return The found edge or \ref INVALID if there is no such an edge.
260 /// Thus you can iterate through each edge from \c u to \c v as it follows.
262 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
266 // /// \todo We may want to use the "GraphBase"
267 // /// interface here...
268 template <typename Graph>
269 inline typename Graph::Edge findEdge(const Graph &g,
270 typename Graph::Node u,
271 typename Graph::Node v,
272 typename Graph::Edge prev = INVALID) {
273 return _findEdge<Graph>(g, u, v, prev);
276 /// \brief Iterator for iterating on edges connected the same nodes.
278 /// Iterator for iterating on edges connected the same nodes. It is
279 /// higher level interface for the findEdge() function. You can
280 /// use it the following way:
282 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
287 /// \author Balazs Dezso
288 template <typename _Graph>
289 class ConEdgeIt : public _Graph::Edge {
292 typedef _Graph Graph;
293 typedef typename Graph::Edge Parent;
295 typedef typename Graph::Edge Edge;
296 typedef typename Graph::Node Node;
298 /// \brief Constructor.
300 /// Construct a new ConEdgeIt iterating on the edges which
301 /// connects the \c u and \c v node.
302 ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
303 Parent::operator=(findEdge(graph, u, v));
306 /// \brief Constructor.
308 /// Construct a new ConEdgeIt which continues the iterating from
310 ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
312 /// \brief Increment operator.
314 /// It increments the iterator and gives back the next edge.
315 ConEdgeIt& operator++() {
316 Parent::operator=(findEdge(graph, graph.source(*this),
317 graph.target(*this), *this));
324 template <typename Graph>
327 typename Graph::FindEdgeTag,
328 typename Graph::UEdge>::type
329 _findUEdge(const Graph &g,
330 typename Graph::Node u, typename Graph::Node v,
331 typename Graph::UEdge prev = INVALID) {
332 return g.findUEdge(u, v, prev);
335 template <typename Graph>
336 inline typename Graph::UEdge
337 _findUEdge(Wrap<Graph> w,
338 typename Graph::Node u,
339 typename Graph::Node v,
340 typename Graph::UEdge prev = INVALID) {
341 const Graph& g = w.value;
342 if (prev == INVALID) {
343 typename Graph::OutEdgeIt e(g, u);
344 while (e != INVALID && g.target(e) != v) ++e;
347 typename Graph::OutEdgeIt e(g, g.direct(prev, u)); ++e;
348 while (e != INVALID && g.target(e) != v) ++e;
353 /// \brief Finds an uedge between two nodes of a graph.
355 /// Finds an uedge from node \c u to node \c v in graph \c g.
357 /// If \c prev is \ref INVALID (this is the default value), then
358 /// it finds the first edge from \c u to \c v. Otherwise it looks for
359 /// the next edge from \c u to \c v after \c prev.
360 /// \return The found edge or \ref INVALID if there is no such an edge.
362 /// Thus you can iterate through each edge from \c u to \c v as it follows.
364 /// for(UEdge e = findUEdge(g,u,v); e != INVALID;
365 /// e = findUEdge(g,u,v,e)) {
369 // /// \todo We may want to use the "GraphBase"
370 // /// interface here...
371 template <typename Graph>
372 inline typename Graph::UEdge
373 findUEdge(const Graph &g,
374 typename Graph::Node u,
375 typename Graph::Node v,
376 typename Graph::UEdge prev = INVALID) {
377 return _findUEdge<Graph>(g, u, v, prev);
380 /// \brief Iterator for iterating on uedges connected the same nodes.
382 /// Iterator for iterating on uedges connected the same nodes. It is
383 /// higher level interface for the findUEdge() function. You can
384 /// use it the following way:
386 /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
391 /// \author Balazs Dezso
392 template <typename _Graph>
393 class ConUEdgeIt : public _Graph::UEdge {
396 typedef _Graph Graph;
397 typedef typename Graph::UEdge Parent;
399 typedef typename Graph::UEdge UEdge;
400 typedef typename Graph::Node Node;
402 /// \brief Constructor.
404 /// Construct a new ConUEdgeIt iterating on the edges which
405 /// connects the \c u and \c v node.
406 ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
407 Parent::operator=(findUEdge(graph, u, v));
410 /// \brief Constructor.
412 /// Construct a new ConUEdgeIt which continues the iterating from
414 ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
416 /// \brief Increment operator.
418 /// It increments the iterator and gives back the next edge.
419 ConUEdgeIt& operator++() {
420 Parent::operator=(findUEdge(graph, graph.source(*this),
421 graph.target(*this), *this));
428 /// \brief Copy a map.
430 /// This function copies the \c source map to the \c target map. It uses the
431 /// given iterator to iterate on the data structure and it uses the \c ref
432 /// mapping to convert the source's keys to the target's keys.
433 template <typename Target, typename Source,
434 typename ItemIt, typename Ref>
435 void copyMap(Target& target, const Source& source,
436 ItemIt it, const Ref& ref) {
437 for (; it != INVALID; ++it) {
438 target[ref[it]] = source[it];
442 /// \brief Copy the source map to the target map.
444 /// Copy the \c source map to the \c target map. It uses the given iterator
445 /// to iterate on the data structure.
446 template <typename Target, typename Source, typename ItemIt>
447 void copyMap(Target& target, const Source& source, ItemIt it) {
448 for (; it != INVALID; ++it) {
449 target[it] = source[it];
453 /// \brief Class to copy a graph.
455 /// Class to copy a graph to an other graph (duplicate a graph). The
456 /// simplest way of using it is through the \c copyGraph() function.
457 template <typename Target, typename Source>
460 typedef typename Source::Node Node;
461 typedef typename Source::NodeIt NodeIt;
462 typedef typename Source::Edge Edge;
463 typedef typename Source::EdgeIt EdgeIt;
465 typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
466 typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
468 /// \brief Constructor for the GraphCopy.
470 /// It copies the content of the \c _source graph into the
471 /// \c _target graph. It creates also two references, one beetween
472 /// the two nodeset and one beetween the two edgesets.
473 GraphCopy(Target& _target, const Source& _source)
474 : source(_source), target(_target),
475 nodeRefMap(_source), edgeRefMap(_source) {
476 for (NodeIt it(source); it != INVALID; ++it) {
477 nodeRefMap[it] = target.addNode();
479 for (EdgeIt it(source); it != INVALID; ++it) {
480 edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
481 nodeRefMap[source.target(it)]);
485 /// \brief Copies the node references into the given map.
487 /// Copies the node references into the given map.
488 template <typename NodeRef>
489 const GraphCopy& nodeRef(NodeRef& map) const {
490 for (NodeIt it(source); it != INVALID; ++it) {
491 map.set(it, nodeRefMap[it]);
496 /// \brief Reverse and copies the node references into the given map.
498 /// Reverse and copies the node references into the given map.
499 template <typename NodeRef>
500 const GraphCopy& nodeCrossRef(NodeRef& map) const {
501 for (NodeIt it(source); it != INVALID; ++it) {
502 map.set(nodeRefMap[it], it);
507 /// \brief Copies the edge references into the given map.
509 /// Copies the edge references into the given map.
510 template <typename EdgeRef>
511 const GraphCopy& edgeRef(EdgeRef& map) const {
512 for (EdgeIt it(source); it != INVALID; ++it) {
513 map.set(it, edgeRefMap[it]);
518 /// \brief Reverse and copies the edge references into the given map.
520 /// Reverse and copies the edge references into the given map.
521 template <typename EdgeRef>
522 const GraphCopy& edgeCrossRef(EdgeRef& map) const {
523 for (EdgeIt it(source); it != INVALID; ++it) {
524 map.set(edgeRefMap[it], it);
529 /// \brief Make copy of the given map.
531 /// Makes copy of the given map for the newly created graph.
532 /// The new map's key type is the target graph's node type,
533 /// and the copied map's key type is the source graph's node
535 template <typename TargetMap, typename SourceMap>
536 const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
537 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
541 /// \brief Make copy of the given map.
543 /// Makes copy of the given map for the newly created graph.
544 /// The new map's key type is the target graph's edge type,
545 /// and the copied map's key type is the source graph's edge
547 template <typename TargetMap, typename SourceMap>
548 const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
549 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
553 /// \brief Gives back the stored node references.
555 /// Gives back the stored node references.
556 const NodeRefMap& nodeRef() const {
560 /// \brief Gives back the stored edge references.
562 /// Gives back the stored edge references.
563 const EdgeRefMap& edgeRef() const {
571 const Source& source;
574 NodeRefMap nodeRefMap;
575 EdgeRefMap edgeRefMap;
578 /// \brief Copy a graph to an other graph.
580 /// Copy a graph to an other graph.
581 /// The usage of the function:
584 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
587 /// After the copy the \c nr map will contain the mapping from the
588 /// source graph's nodes to the target graph's nodes and the \c ecr will
589 /// contain the mapping from the target graph's edges to the source's
591 template <typename Target, typename Source>
592 GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
593 return GraphCopy<Target, Source>(target, source);
596 /// \brief Class to copy an undirected graph.
598 /// Class to copy an undirected graph to an other graph (duplicate a graph).
599 /// The simplest way of using it is through the \c copyUGraph() function.
600 template <typename Target, typename Source>
603 typedef typename Source::Node Node;
604 typedef typename Source::NodeIt NodeIt;
605 typedef typename Source::Edge Edge;
606 typedef typename Source::EdgeIt EdgeIt;
607 typedef typename Source::UEdge UEdge;
608 typedef typename Source::UEdgeIt UEdgeIt;
610 typedef typename Source::
611 template NodeMap<typename Target::Node> NodeRefMap;
613 typedef typename Source::
614 template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
619 EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
620 typedef typename Source::Edge Key;
621 typedef typename Target::Edge Value;
623 Value operator[](const Key& key) {
624 return gc.target.direct(gc.uEdgeRef[key],
625 gc.target.direction(key));
633 /// \brief Constructor for the UGraphCopy.
635 /// It copies the content of the \c _source graph into the
636 /// \c _target graph. It creates also two references, one beetween
637 /// the two nodeset and one beetween the two edgesets.
638 UGraphCopy(Target& _target, const Source& _source)
639 : source(_source), target(_target),
640 nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
641 for (NodeIt it(source); it != INVALID; ++it) {
642 nodeRefMap[it] = target.addNode();
644 for (UEdgeIt it(source); it != INVALID; ++it) {
645 uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
646 nodeRefMap[source.target(it)]);
650 /// \brief Copies the node references into the given map.
652 /// Copies the node references into the given map.
653 template <typename NodeRef>
654 const UGraphCopy& nodeRef(NodeRef& map) const {
655 for (NodeIt it(source); it != INVALID; ++it) {
656 map.set(it, nodeRefMap[it]);
661 /// \brief Reverse and copies the node references into the given map.
663 /// Reverse and copies the node references into the given map.
664 template <typename NodeRef>
665 const UGraphCopy& nodeCrossRef(NodeRef& map) const {
666 for (NodeIt it(source); it != INVALID; ++it) {
667 map.set(nodeRefMap[it], it);
672 /// \brief Copies the edge references into the given map.
674 /// Copies the edge references into the given map.
675 template <typename EdgeRef>
676 const UGraphCopy& edgeRef(EdgeRef& map) const {
677 for (EdgeIt it(source); it != INVALID; ++it) {
678 map.set(edgeRefMap[it], it);
683 /// \brief Reverse and copies the undirected edge references into the
686 /// Reverse and copies the undirected edge references into the given map.
687 template <typename EdgeRef>
688 const UGraphCopy& edgeCrossRef(EdgeRef& map) const {
689 for (EdgeIt it(source); it != INVALID; ++it) {
690 map.set(it, edgeRefMap[it]);
695 /// \brief Copies the undirected edge references into the given map.
697 /// Copies the undirected edge references into the given map.
698 template <typename EdgeRef>
699 const UGraphCopy& uEdgeRef(EdgeRef& map) const {
700 for (UEdgeIt it(source); it != INVALID; ++it) {
701 map.set(it, uEdgeRefMap[it]);
706 /// \brief Reverse and copies the undirected edge references into the
709 /// Reverse and copies the undirected edge references into the given map.
710 template <typename EdgeRef>
711 const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
712 for (UEdgeIt it(source); it != INVALID; ++it) {
713 map.set(uEdgeRefMap[it], it);
718 /// \brief Make copy of the given map.
720 /// Makes copy of the given map for the newly created graph.
721 /// The new map's key type is the target graph's node type,
722 /// and the copied map's key type is the source graph's node
724 template <typename TargetMap, typename SourceMap>
725 const UGraphCopy& nodeMap(TargetMap& tMap,
726 const SourceMap& sMap) const {
727 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
731 /// \brief Make copy of the given map.
733 /// Makes copy of the given map for the newly created graph.
734 /// The new map's key type is the target graph's edge type,
735 /// and the copied map's key type is the source graph's edge
737 template <typename TargetMap, typename SourceMap>
738 const UGraphCopy& edgeMap(TargetMap& tMap,
739 const SourceMap& sMap) const {
740 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
744 /// \brief Make copy of the given map.
746 /// Makes copy of the given map for the newly created graph.
747 /// The new map's key type is the target graph's edge type,
748 /// and the copied map's key type is the source graph's edge
750 template <typename TargetMap, typename SourceMap>
751 const UGraphCopy& uEdgeMap(TargetMap& tMap,
752 const SourceMap& sMap) const {
753 copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
757 /// \brief Gives back the stored node references.
759 /// Gives back the stored node references.
760 const NodeRefMap& nodeRef() const {
764 /// \brief Gives back the stored edge references.
766 /// Gives back the stored edge references.
767 const EdgeRefMap& edgeRef() const {
771 /// \brief Gives back the stored uedge references.
773 /// Gives back the stored uedge references.
774 const UEdgeRefMap& uEdgeRef() const {
782 const Source& source;
785 NodeRefMap nodeRefMap;
786 EdgeRefMap edgeRefMap;
787 UEdgeRefMap uEdgeRefMap;
790 /// \brief Copy a graph to an other graph.
792 /// Copy a graph to an other graph.
793 /// The usage of the function:
796 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
799 /// After the copy the \c nr map will contain the mapping from the
800 /// source graph's nodes to the target graph's nodes and the \c ecr will
801 /// contain the mapping from the target graph's edges to the source's
803 template <typename Target, typename Source>
804 UGraphCopy<Target, Source>
805 copyUGraph(Target& target, const Source& source) {
806 return UGraphCopy<Target, Source>(target, source);
812 /// \addtogroup graph_maps
815 /// Provides an immutable and unique id for each item in the graph.
817 /// The IdMap class provides a unique and immutable id for each item of the
818 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
819 /// different items (nodes) get different ids <li>\b immutable: the id of an
820 /// item (node) does not change (even if you delete other nodes). </ul>
821 /// Through this map you get access (i.e. can read) the inner id values of
822 /// the items stored in the graph. This map can be inverted with its member
823 /// class \c InverseMap.
825 template <typename _Graph, typename _Item>
828 typedef _Graph Graph;
833 /// \brief Constructor.
835 /// Constructor for creating id map.
836 IdMap(const Graph& _graph) : graph(&_graph) {}
838 /// \brief Gives back the \e id of the item.
840 /// Gives back the immutable and unique \e id of the map.
841 int operator[](const Item& item) const { return graph->id(item);}
849 /// \brief The class represents the inverse of its owner (IdMap).
851 /// The class represents the inverse of its owner (IdMap).
856 /// \brief Constructor.
858 /// Constructor for creating an id-to-item map.
859 InverseMap(const Graph& _graph) : graph(&_graph) {}
861 /// \brief Constructor.
863 /// Constructor for creating an id-to-item map.
864 InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
866 /// \brief Gives back the given item from its id.
868 /// Gives back the given item from its id.
870 Item operator[](int id) const { return graph->fromId(id, Item());}
875 /// \brief Gives back the inverse of the map.
877 /// Gives back the inverse of the IdMap.
878 InverseMap inverse() const { return InverseMap(*graph);}
883 /// \brief General invertable graph-map type.
885 /// This type provides simple invertable graph-maps.
886 /// The InvertableMap wraps an arbitrary ReadWriteMap
887 /// and if a key is set to a new value then store it
888 /// in the inverse map.
890 /// The values of the map can be accessed
891 /// with stl compatible forward iterator.
893 /// \param _Graph The graph type.
894 /// \param _Item The item type of the graph.
895 /// \param _Value The value type of the map.
897 /// \see IterableValueMap
899 /// \param _Map A ReadWriteMap mapping from the item type to integer.
901 typename _Graph, typename _Item, typename _Value, typename _Map
902 = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent
905 template <typename _Graph, typename _Item, typename _Value>
907 class InvertableMap : protected _Map {
910 /// The key type of InvertableMap (Node, Edge, UEdge).
911 typedef typename _Map::Key Key;
912 /// The value type of the InvertableMap.
913 typedef typename _Map::Value Value;
918 typedef _Graph Graph;
920 typedef std::map<Value, Key> Container;
927 /// \brief Constructor.
929 /// Construct a new InvertableMap for the graph.
931 InvertableMap(const Graph& graph) : Map(graph) {}
933 /// \brief Forward iterator for values.
935 /// This iterator is an stl compatible forward
936 /// iterator on the values of the map. The values can
937 /// be accessed in the [beginValue, endValue) range.
940 : public std::iterator<std::forward_iterator_tag, Value> {
941 friend class InvertableMap;
943 ValueIterator(typename Container::const_iterator _it)
949 ValueIterator& operator++() { ++it; return *this; }
950 ValueIterator operator++(int) {
951 ValueIterator tmp(*this);
956 const Value& operator*() const { return it->first; }
957 const Value* operator->() const { return &(it->first); }
959 bool operator==(ValueIterator jt) const { return it == jt.it; }
960 bool operator!=(ValueIterator jt) const { return it != jt.it; }
963 typename Container::const_iterator it;
966 /// \brief Returns an iterator to the first value.
968 /// Returns an stl compatible iterator to the
969 /// first value of the map. The values of the
970 /// map can be accessed in the [beginValue, endValue)
972 ValueIterator beginValue() const {
973 return ValueIterator(invMap.begin());
976 /// \brief Returns an iterator after the last value.
978 /// Returns an stl compatible iterator after the
979 /// last value of the map. The values of the
980 /// map can be accessed in the [beginValue, endValue)
982 ValueIterator endValue() const {
983 return ValueIterator(invMap.end());
986 /// \brief The setter function of the map.
988 /// Sets the mapped value.
989 void set(const Key& key, const Value& val) {
990 Value oldval = Map::operator[](key);
991 typename Container::iterator it = invMap.find(oldval);
992 if (it != invMap.end() && it->second == key) {
995 invMap.insert(make_pair(val, key));
999 /// \brief The getter function of the map.
1001 /// It gives back the value associated with the key.
1002 typename MapTraits<Map>::ConstReturnValue
1003 operator[](const Key& key) const {
1004 return Map::operator[](key);
1009 /// \brief Erase the key from the map.
1011 /// Erase the key to the map. It is called by the
1012 /// \c AlterationNotifier.
1013 virtual void erase(const Key& key) {
1014 Value val = Map::operator[](key);
1015 typename Container::iterator it = invMap.find(val);
1016 if (it != invMap.end() && it->second == key) {
1022 /// \brief Erase more keys from the map.
1024 /// Erase more keys from the map. It is called by the
1025 /// \c AlterationNotifier.
1026 virtual void erase(const std::vector<Key>& keys) {
1027 for (int i = 0; i < (int)keys.size(); ++i) {
1028 Value val = Map::operator[](keys[i]);
1029 typename Container::iterator it = invMap.find(val);
1030 if (it != invMap.end() && it->second == keys[i]) {
1037 /// \brief Clear the keys from the map and inverse map.
1039 /// Clear the keys from the map and inverse map. It is called by the
1040 /// \c AlterationNotifier.
1041 virtual void clear() {
1048 /// \brief The inverse map type.
1050 /// The inverse of this map. The subscript operator of the map
1051 /// gives back always the item what was last assigned to the value.
1054 /// \brief Constructor of the InverseMap.
1056 /// Constructor of the InverseMap.
1057 InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
1059 /// The value type of the InverseMap.
1060 typedef typename InvertableMap::Key Value;
1061 /// The key type of the InverseMap.
1062 typedef typename InvertableMap::Value Key;
1064 /// \brief Subscript operator.
1066 /// Subscript operator. It gives back always the item
1067 /// what was last assigned to the value.
1068 Value operator[](const Key& key) const {
1069 typename Container::const_iterator it = inverted.invMap.find(key);
1074 const InvertableMap& inverted;
1077 /// \brief It gives back the just readeable inverse map.
1079 /// It gives back the just readeable inverse map.
1080 InverseMap inverse() const {
1081 return InverseMap(*this);
1088 /// \brief Provides a mutable, continuous and unique descriptor for each
1089 /// item in the graph.
1091 /// The DescriptorMap class provides a unique and continuous (but mutable)
1092 /// descriptor (id) for each item of the same type (e.g. node) in the
1093 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1094 /// different ids <li>\b continuous: the range of the ids is the set of
1095 /// integers between 0 and \c n-1, where \c n is the number of the items of
1096 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1097 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1098 /// with its member class \c InverseMap.
1100 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1101 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
1104 /// \param _Map A ReadWriteMap mapping from the item type to integer.
1106 typename _Graph, typename _Item, typename _Map
1107 = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
1110 template <typename _Graph, typename _Item>
1112 class DescriptorMap : protected _Map {
1118 /// The graph class of DescriptorMap.
1119 typedef _Graph Graph;
1121 /// The key type of DescriptorMap (Node, Edge, UEdge).
1122 typedef typename _Map::Key Key;
1123 /// The value type of DescriptorMap.
1124 typedef typename _Map::Value Value;
1126 /// \brief Constructor.
1128 /// Constructor for descriptor map.
1129 DescriptorMap(const Graph& _graph) : Map(_graph) {
1135 /// \brief Add a new key to the map.
1137 /// Add a new key to the map. It is called by the
1138 /// \c AlterationNotifier.
1139 virtual void add(const Item& item) {
1141 Map::set(item, invMap.size());
1142 invMap.push_back(item);
1145 /// \brief Add more new keys to the map.
1147 /// Add more new keys to the map. It is called by the
1148 /// \c AlterationNotifier.
1149 virtual void add(const std::vector<Item>& items) {
1151 for (int i = 0; i < (int)items.size(); ++i) {
1152 Map::set(items[i], invMap.size());
1153 invMap.push_back(items[i]);
1157 /// \brief Erase the key from the map.
1159 /// Erase the key from the map. It is called by the
1160 /// \c AlterationNotifier.
1161 virtual void erase(const Item& item) {
1162 Map::set(invMap.back(), Map::operator[](item));
1163 invMap[Map::operator[](item)] = invMap.back();
1168 /// \brief Erase more keys from the map.
1170 /// Erase more keys from the map. It is called by the
1171 /// \c AlterationNotifier.
1172 virtual void erase(const std::vector<Item>& items) {
1173 for (int i = 0; i < (int)items.size(); ++i) {
1174 Map::set(invMap.back(), Map::operator[](items[i]));
1175 invMap[Map::operator[](items[i])] = invMap.back();
1181 /// \brief Build the unique map.
1183 /// Build the unique map. It is called by the
1184 /// \c AlterationNotifier.
1185 virtual void build() {
1188 const typename Map::Graph* graph = Map::getGraph();
1189 for (graph->first(it); it != INVALID; graph->next(it)) {
1190 Map::set(it, invMap.size());
1191 invMap.push_back(it);
1195 /// \brief Clear the keys from the map.
1197 /// Clear the keys from the map. It is called by the
1198 /// \c AlterationNotifier.
1199 virtual void clear() {
1206 /// \brief Returns the maximal value plus one.
1208 /// Returns the maximal value plus one in the map.
1209 unsigned int size() const {
1210 return invMap.size();
1213 /// \brief Swaps the position of the two items in the map.
1215 /// Swaps the position of the two items in the map.
1216 void swap(const Item& p, const Item& q) {
1217 int pi = Map::operator[](p);
1218 int qi = Map::operator[](q);
1225 /// \brief Gives back the \e descriptor of the item.
1227 /// Gives back the mutable and unique \e descriptor of the map.
1228 int operator[](const Item& item) const {
1229 return Map::operator[](item);
1234 typedef std::vector<Item> Container;
1238 /// \brief The inverse map type of DescriptorMap.
1240 /// The inverse map type of DescriptorMap.
1243 /// \brief Constructor of the InverseMap.
1245 /// Constructor of the InverseMap.
1246 InverseMap(const DescriptorMap& _inverted)
1247 : inverted(_inverted) {}
1250 /// The value type of the InverseMap.
1251 typedef typename DescriptorMap::Key Value;
1252 /// The key type of the InverseMap.
1253 typedef typename DescriptorMap::Value Key;
1255 /// \brief Subscript operator.
1257 /// Subscript operator. It gives back the item
1258 /// that the descriptor belongs to currently.
1259 Value operator[](const Key& key) const {
1260 return inverted.invMap[key];
1263 /// \brief Size of the map.
1265 /// Returns the size of the map.
1266 unsigned int size() const {
1267 return inverted.invMap.size();
1271 const DescriptorMap& inverted;
1274 /// \brief Gives back the inverse of the map.
1276 /// Gives back the inverse of the map.
1277 const InverseMap inverse() const {
1278 return InverseMap(*this);
1282 /// \brief Returns the source of the given edge.
1284 /// The SourceMap gives back the source Node of the given edge.
1285 /// \author Balazs Dezso
1286 template <typename Graph>
1290 typedef typename Graph::Node Value;
1291 typedef typename Graph::Edge Key;
1293 /// \brief Constructor
1296 /// \param _graph The graph that the map belongs to.
1297 SourceMap(const Graph& _graph) : graph(_graph) {}
1299 /// \brief The subscript operator.
1301 /// The subscript operator.
1302 /// \param edge The edge
1303 /// \return The source of the edge
1304 Value operator[](const Key& edge) const {
1305 return graph.source(edge);
1312 /// \brief Returns a \ref SourceMap class
1314 /// This function just returns an \ref SourceMap class.
1315 /// \relates SourceMap
1316 template <typename Graph>
1317 inline SourceMap<Graph> sourceMap(const Graph& graph) {
1318 return SourceMap<Graph>(graph);
1321 /// \brief Returns the target of the given edge.
1323 /// The TargetMap gives back the target Node of the given edge.
1324 /// \author Balazs Dezso
1325 template <typename Graph>
1329 typedef typename Graph::Node Value;
1330 typedef typename Graph::Edge Key;
1332 /// \brief Constructor
1335 /// \param _graph The graph that the map belongs to.
1336 TargetMap(const Graph& _graph) : graph(_graph) {}
1338 /// \brief The subscript operator.
1340 /// The subscript operator.
1341 /// \param e The edge
1342 /// \return The target of the edge
1343 Value operator[](const Key& e) const {
1344 return graph.target(e);
1351 /// \brief Returns a \ref TargetMap class
1353 /// This function just returns a \ref TargetMap class.
1354 /// \relates TargetMap
1355 template <typename Graph>
1356 inline TargetMap<Graph> targetMap(const Graph& graph) {
1357 return TargetMap<Graph>(graph);
1360 /// \brief Returns the "forward" directed edge view of an undirected edge.
1362 /// Returns the "forward" directed edge view of an undirected edge.
1363 /// \author Balazs Dezso
1364 template <typename Graph>
1368 typedef typename Graph::Edge Value;
1369 typedef typename Graph::UEdge Key;
1371 /// \brief Constructor
1374 /// \param _graph The graph that the map belongs to.
1375 ForwardMap(const Graph& _graph) : graph(_graph) {}
1377 /// \brief The subscript operator.
1379 /// The subscript operator.
1380 /// \param key An undirected edge
1381 /// \return The "forward" directed edge view of undirected edge
1382 Value operator[](const Key& key) const {
1383 return graph.direct(key, true);
1390 /// \brief Returns a \ref ForwardMap class
1392 /// This function just returns an \ref ForwardMap class.
1393 /// \relates ForwardMap
1394 template <typename Graph>
1395 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
1396 return ForwardMap<Graph>(graph);
1399 /// \brief Returns the "backward" directed edge view of an undirected edge.
1401 /// Returns the "backward" directed edge view of an undirected edge.
1402 /// \author Balazs Dezso
1403 template <typename Graph>
1407 typedef typename Graph::Edge Value;
1408 typedef typename Graph::UEdge Key;
1410 /// \brief Constructor
1413 /// \param _graph The graph that the map belongs to.
1414 BackwardMap(const Graph& _graph) : graph(_graph) {}
1416 /// \brief The subscript operator.
1418 /// The subscript operator.
1419 /// \param key An undirected edge
1420 /// \return The "backward" directed edge view of undirected edge
1421 Value operator[](const Key& key) const {
1422 return graph.direct(key, false);
1429 /// \brief Returns a \ref BackwardMap class
1431 /// This function just returns a \ref BackwardMap class.
1432 /// \relates BackwardMap
1433 template <typename Graph>
1434 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
1435 return BackwardMap<Graph>(graph);
1438 /// \brief Potential difference map
1440 /// If there is an potential map on the nodes then we
1441 /// can get an edge map as we get the substraction of the
1442 /// values of the target and source.
1443 template <typename Graph, typename NodeMap>
1444 class PotentialDifferenceMap {
1446 typedef typename Graph::Edge Key;
1447 typedef typename NodeMap::Value Value;
1449 /// \brief Constructor
1451 /// Contructor of the map
1452 PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential)
1453 : graph(_graph), potential(_potential) {}
1455 /// \brief Const subscription operator
1457 /// Const subscription operator
1458 Value operator[](const Key& edge) const {
1459 return potential[graph.target(edge)] - potential[graph.source(edge)];
1464 const NodeMap& potential;
1467 /// \brief Just returns a PotentialDifferenceMap
1469 /// Just returns a PotentialDifferenceMap
1470 /// \relates PotentialDifferenceMap
1471 template <typename Graph, typename NodeMap>
1472 PotentialDifferenceMap<Graph, NodeMap>
1473 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
1474 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
1477 /// \brief Map of the node in-degrees.
1479 /// This map returns the in-degree of a node. Once it is constructed,
1480 /// the degrees are stored in a standard NodeMap, so each query is done
1481 /// in constant time. On the other hand, the values are updated automatically
1482 /// whenever the graph changes.
1484 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1485 /// alternative ways to modify the graph. The correct behavior of InDegMap
1486 /// is not guarantied if these additional features are used. For example
1487 /// the functions \ref ListGraph::changeSource() "changeSource()",
1488 /// \ref ListGraph::changeTarget() "changeTarget()" and
1489 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1490 /// of \ref ListGraph will \e not update the degree values correctly.
1494 template <typename _Graph>
1496 : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
1500 typedef _Graph Graph;
1502 typedef typename Graph::Node Key;
1506 class AutoNodeMap : public Graph::template NodeMap<int> {
1509 typedef typename Graph::template NodeMap<int> Parent;
1511 typedef typename Parent::Key Key;
1512 typedef typename Parent::Value Value;
1514 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1516 virtual void add(const Key& key) {
1518 Parent::set(key, 0);
1521 virtual void add(const std::vector<Key>& keys) {
1523 for (int i = 0; i < (int)keys.size(); ++i) {
1524 Parent::set(keys[i], 0);
1531 /// \brief Constructor.
1533 /// Constructor for creating in-degree map.
1534 InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1535 AlterationNotifier<typename _Graph::Edge>
1536 ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
1538 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1539 deg[it] = countInEdges(graph, it);
1543 virtual ~InDegMap() {
1544 AlterationNotifier<typename _Graph::Edge>::
1545 ObserverBase::detach();
1548 /// Gives back the in-degree of a Node.
1549 int operator[](const Key& key) const {
1555 typedef typename Graph::Edge Edge;
1557 virtual void add(const Edge& edge) {
1558 ++deg[graph.target(edge)];
1561 virtual void add(const std::vector<Edge>& edges) {
1562 for (int i = 0; i < (int)edges.size(); ++i) {
1563 ++deg[graph.target(edges[i])];
1567 virtual void erase(const Edge& edge) {
1568 --deg[graph.target(edge)];
1571 virtual void erase(const std::vector<Edge>& edges) {
1572 for (int i = 0; i < (int)edges.size(); ++i) {
1573 --deg[graph.target(edges[i])];
1577 virtual void build() {
1578 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1579 deg[it] = countInEdges(graph, it);
1583 virtual void clear() {
1584 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1590 const _Graph& graph;
1594 /// \brief Map of the node out-degrees.
1596 /// This map returns the out-degree of a node. Once it is constructed,
1597 /// the degrees are stored in a standard NodeMap, so each query is done
1598 /// in constant time. On the other hand, the values are updated automatically
1599 /// whenever the graph changes.
1601 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1602 /// alternative ways to modify the graph. The correct behavior of OutDegMap
1603 /// is not guarantied if these additional features are used. For example
1604 /// the functions \ref ListGraph::changeSource() "changeSource()",
1605 /// \ref ListGraph::changeTarget() "changeTarget()" and
1606 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1607 /// of \ref ListGraph will \e not update the degree values correctly.
1611 template <typename _Graph>
1613 : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
1617 typedef _Graph Graph;
1619 typedef typename Graph::Node Key;
1623 class AutoNodeMap : public Graph::template NodeMap<int> {
1626 typedef typename Graph::template NodeMap<int> Parent;
1628 typedef typename Parent::Key Key;
1629 typedef typename Parent::Value Value;
1631 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1633 virtual void add(const Key& key) {
1635 Parent::set(key, 0);
1637 virtual void add(const std::vector<Key>& keys) {
1639 for (int i = 0; i < (int)keys.size(); ++i) {
1640 Parent::set(keys[i], 0);
1647 /// \brief Constructor.
1649 /// Constructor for creating out-degree map.
1650 OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1651 AlterationNotifier<typename _Graph::Edge>
1652 ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
1654 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1655 deg[it] = countOutEdges(graph, it);
1659 virtual ~OutDegMap() {
1660 AlterationNotifier<typename _Graph::Edge>::
1661 ObserverBase::detach();
1664 /// Gives back the in-degree of a Node.
1665 int operator[](const Key& key) const {
1671 typedef typename Graph::Edge Edge;
1673 virtual void add(const Edge& edge) {
1674 ++deg[graph.source(edge)];
1677 virtual void add(const std::vector<Edge>& edges) {
1678 for (int i = 0; i < (int)edges.size(); ++i) {
1679 ++deg[graph.source(edges[i])];
1683 virtual void erase(const Edge& edge) {
1684 --deg[graph.source(edge)];
1687 virtual void erase(const std::vector<Edge>& edges) {
1688 for (int i = 0; i < (int)edges.size(); ++i) {
1689 --deg[graph.source(edges[i])];
1693 virtual void build() {
1694 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1695 deg[it] = countOutEdges(graph, it);
1699 virtual void clear() {
1700 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1706 const _Graph& graph;
1713 } //END OF NAMESPACE LEMON