Unionfind changes induced some bugs here. Also some augmentations made.
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/bits/invalid.h>
28 #include <lemon/bits/utility.h>
29 #include <lemon/maps.h>
30 #include <lemon/bits/traits.h>
32 #include <lemon/bits/alteration_notifier.h>
33 #include <lemon/bits/default_map.h>
37 ///\brief Graph utilities.
44 /// \addtogroup gutils
47 ///Creates convenience typedefs for the graph types and iterators
49 ///This \c \#define creates convenience typedefs for the following types
50 ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
51 ///\c OutEdgeIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
52 ///\c BoolEdgeMap, \c IntEdgeMap, \c DoubleEdgeMap.
53 ///\note If \c G it a template parameter, it should be used in this way.
55 /// GRAPH_TYPEDEFS(typename G)
58 ///\warning There are no typedefs for the graph maps because of the lack of
59 ///template typedefs in C++.
60 #define GRAPH_TYPEDEFS(Graph) \
61 typedef Graph:: Node Node; \
62 typedef Graph:: NodeIt NodeIt; \
63 typedef Graph:: Edge Edge; \
64 typedef Graph:: EdgeIt EdgeIt; \
65 typedef Graph:: InEdgeIt InEdgeIt; \
66 typedef Graph::OutEdgeIt OutEdgeIt;
67 // typedef Graph::template NodeMap<bool> BoolNodeMap;
68 // typedef Graph::template NodeMap<int> IntNodeMap;
69 // typedef Graph::template NodeMap<double> DoubleNodeMap;
70 // typedef Graph::template EdgeMap<bool> BoolEdgeMap;
71 // typedef Graph::template EdgeMap<int> IntEdgeMap;
72 // typedef Graph::template EdgeMap<double> DoubleEdgeMap;
74 ///Creates convenience typedefs for the undirected graph types and iterators
76 ///This \c \#define creates the same convenience typedefs as defined by
77 ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
78 ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
79 ///\c BoolUEdgeMap, \c IntUEdgeMap, \c DoubleUEdgeMap.
81 ///\note If \c G it a template parameter, it should be used in this way.
83 /// UGRAPH_TYPEDEFS(typename G)
86 ///\warning There are no typedefs for the graph maps because of the lack of
87 ///template typedefs in C++.
88 #define UGRAPH_TYPEDEFS(Graph) \
89 GRAPH_TYPEDEFS(Graph) \
90 typedef Graph:: UEdge UEdge; \
91 typedef Graph:: UEdgeIt UEdgeIt; \
92 typedef Graph:: IncEdgeIt IncEdgeIt;
93 // typedef Graph::template UEdgeMap<bool> BoolUEdgeMap;
94 // typedef Graph::template UEdgeMap<int> IntUEdgeMap;
95 // typedef Graph::template UEdgeMap<double> DoubleUEdgeMap;
99 /// \brief Function to count the items in the graph.
101 /// This function counts the items (nodes, edges etc) in the graph.
102 /// The complexity of the function is O(n) because
103 /// it iterates on all of the items.
105 template <typename Graph, typename Item>
106 inline int countItems(const Graph& g) {
107 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
109 for (ItemIt it(g); it != INVALID; ++it) {
117 namespace _graph_utils_bits {
119 template <typename Graph, typename Enable = void>
120 struct CountNodesSelector {
121 static int count(const Graph &g) {
122 return countItems<Graph, typename Graph::Node>(g);
126 template <typename Graph>
127 struct CountNodesSelector<
129 enable_if<typename Graph::NodeNumTag, void>::type>
131 static int count(const Graph &g) {
137 /// \brief Function to count the nodes in the graph.
139 /// This function counts the nodes in the graph.
140 /// The complexity of the function is O(n) but for some
141 /// graph structures it is specialized to run in O(1).
143 /// \todo refer how to specialize it
145 template <typename Graph>
146 inline int countNodes(const Graph& g) {
147 return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
153 namespace _graph_utils_bits {
155 template <typename Graph, typename Enable = void>
156 struct CountEdgesSelector {
157 static int count(const Graph &g) {
158 return countItems<Graph, typename Graph::Edge>(g);
162 template <typename Graph>
163 struct CountEdgesSelector<
165 typename enable_if<typename Graph::EdgeNumTag, void>::type>
167 static int count(const Graph &g) {
173 /// \brief Function to count the edges in the graph.
175 /// This function counts the edges in the graph.
176 /// The complexity of the function is O(e) but for some
177 /// graph structures it is specialized to run in O(1).
179 template <typename Graph>
180 inline int countEdges(const Graph& g) {
181 return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
184 // Undirected edge counting:
185 namespace _graph_utils_bits {
187 template <typename Graph, typename Enable = void>
188 struct CountUEdgesSelector {
189 static int count(const Graph &g) {
190 return countItems<Graph, typename Graph::UEdge>(g);
194 template <typename Graph>
195 struct CountUEdgesSelector<
197 typename enable_if<typename Graph::EdgeNumTag, void>::type>
199 static int count(const Graph &g) {
205 /// \brief Function to count the undirected edges in the graph.
207 /// This function counts the undirected edges in the graph.
208 /// The complexity of the function is O(e) but for some
209 /// graph structures it is specialized to run in O(1).
211 template <typename Graph>
212 inline int countUEdges(const Graph& g) {
213 return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
218 template <typename Graph, typename DegIt>
219 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
221 for (DegIt it(_g, _n); it != INVALID; ++it) {
227 /// \brief Function to count the number of the out-edges from node \c n.
229 /// This function counts the number of the out-edges from node \c n
231 template <typename Graph>
232 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
233 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
236 /// \brief Function to count the number of the in-edges to node \c n.
238 /// This function counts the number of the in-edges to node \c n
240 template <typename Graph>
241 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
242 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
245 /// \brief Function to count the number of the inc-edges to node \c n.
247 /// This function counts the number of the inc-edges to node \c n
249 template <typename Graph>
250 inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
251 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
254 namespace _graph_utils_bits {
256 template <typename Graph, typename Enable = void>
257 struct FindEdgeSelector {
258 typedef typename Graph::Node Node;
259 typedef typename Graph::Edge Edge;
260 static Edge find(const Graph &g, Node u, Node v, Edge e) {
266 while (e != INVALID && g.target(e) != v) {
273 template <typename Graph>
274 struct FindEdgeSelector<
276 typename enable_if<typename Graph::FindEdgeTag, void>::type>
278 typedef typename Graph::Node Node;
279 typedef typename Graph::Edge Edge;
280 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
281 return g.findEdge(u, v, prev);
286 /// \brief Finds an edge between two nodes of a graph.
288 /// Finds an edge from node \c u to node \c v in graph \c g.
290 /// If \c prev is \ref INVALID (this is the default value), then
291 /// it finds the first edge from \c u to \c v. Otherwise it looks for
292 /// the next edge from \c u to \c v after \c prev.
293 /// \return The found edge or \ref INVALID if there is no such an edge.
295 /// Thus you can iterate through each edge from \c u to \c v as it follows.
297 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
301 template <typename Graph>
302 inline typename Graph::Edge findEdge(const Graph &g,
303 typename Graph::Node u,
304 typename Graph::Node v,
305 typename Graph::Edge prev = INVALID) {
306 return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
309 /// \brief Iterator for iterating on edges connected the same nodes.
311 /// Iterator for iterating on edges connected the same nodes. It is
312 /// higher level interface for the findEdge() function. You can
313 /// use it the following way:
315 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
320 /// \author Balazs Dezso
321 template <typename _Graph>
322 class ConEdgeIt : public _Graph::Edge {
325 typedef _Graph Graph;
326 typedef typename Graph::Edge Parent;
328 typedef typename Graph::Edge Edge;
329 typedef typename Graph::Node Node;
331 /// \brief Constructor.
333 /// Construct a new ConEdgeIt iterating on the edges which
334 /// connects the \c u and \c v node.
335 ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
336 Parent::operator=(findEdge(graph, u, v));
339 /// \brief Constructor.
341 /// Construct a new ConEdgeIt which continues the iterating from
343 ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
345 /// \brief Increment operator.
347 /// It increments the iterator and gives back the next edge.
348 ConEdgeIt& operator++() {
349 Parent::operator=(findEdge(graph, graph.source(*this),
350 graph.target(*this), *this));
357 namespace _graph_utils_bits {
359 template <typename Graph, typename Enable = void>
360 struct FindUEdgeSelector {
361 typedef typename Graph::Node Node;
362 typedef typename Graph::UEdge UEdge;
363 static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
369 b = g.source(e) == u;
372 while (e != INVALID && g.target(e) != v) {
382 while (e != INVALID && (!b || g.target(e) != v)) {
390 template <typename Graph>
391 struct FindUEdgeSelector<
393 typename enable_if<typename Graph::FindEdgeTag, void>::type>
395 typedef typename Graph::Node Node;
396 typedef typename Graph::UEdge UEdge;
397 static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
398 return g.findUEdge(u, v, prev);
403 /// \brief Finds an uedge between two nodes of a graph.
405 /// Finds an uedge from node \c u to node \c v in graph \c g.
406 /// If the node \c u and node \c v is equal then each loop edge
407 /// will be enumerated.
409 /// If \c prev is \ref INVALID (this is the default value), then
410 /// it finds the first edge from \c u to \c v. Otherwise it looks for
411 /// the next edge from \c u to \c v after \c prev.
412 /// \return The found edge or \ref INVALID if there is no such an edge.
414 /// Thus you can iterate through each edge from \c u to \c v as it follows.
416 /// for(UEdge e = findUEdge(g,u,v); e != INVALID;
417 /// e = findUEdge(g,u,v,e)) {
421 template <typename Graph>
422 inline typename Graph::UEdge findEdge(const Graph &g,
423 typename Graph::Node u,
424 typename Graph::Node v,
425 typename Graph::UEdge prev = INVALID) {
426 return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, prev);
429 /// \brief Iterator for iterating on uedges connected the same nodes.
431 /// Iterator for iterating on uedges connected the same nodes. It is
432 /// higher level interface for the findUEdge() function. You can
433 /// use it the following way:
435 /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
440 /// \author Balazs Dezso
441 template <typename _Graph>
442 class ConUEdgeIt : public _Graph::UEdge {
445 typedef _Graph Graph;
446 typedef typename Graph::UEdge Parent;
448 typedef typename Graph::UEdge UEdge;
449 typedef typename Graph::Node Node;
451 /// \brief Constructor.
453 /// Construct a new ConUEdgeIt iterating on the edges which
454 /// connects the \c u and \c v node.
455 ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
456 Parent::operator=(findUEdge(graph, u, v));
459 /// \brief Constructor.
461 /// Construct a new ConUEdgeIt which continues the iterating from
463 ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
465 /// \brief Increment operator.
467 /// It increments the iterator and gives back the next edge.
468 ConUEdgeIt& operator++() {
469 Parent::operator=(findUEdge(graph, graph.source(*this),
470 graph.target(*this), *this));
477 /// \brief Copy a map.
479 /// This function copies the \c source map to the \c target map. It uses the
480 /// given iterator to iterate on the data structure and it uses the \c ref
481 /// mapping to convert the source's keys to the target's keys.
482 template <typename Target, typename Source,
483 typename ItemIt, typename Ref>
484 void copyMap(Target& target, const Source& source,
485 ItemIt it, const Ref& ref) {
486 for (; it != INVALID; ++it) {
487 target[ref[it]] = source[it];
491 /// \brief Copy the source map to the target map.
493 /// Copy the \c source map to the \c target map. It uses the given iterator
494 /// to iterate on the data structure.
495 template <typename Target, typename Source, typename ItemIt>
496 void copyMap(Target& target, const Source& source, ItemIt it) {
497 for (; it != INVALID; ++it) {
498 target[it] = source[it];
502 /// \brief Class to copy a graph.
504 /// Class to copy a graph to another graph (duplicate a graph). The
505 /// simplest way of using it is through the \c copyGraph() function.
506 template <typename Target, typename Source>
509 typedef typename Source::Node Node;
510 typedef typename Source::NodeIt NodeIt;
511 typedef typename Source::Edge Edge;
512 typedef typename Source::EdgeIt EdgeIt;
514 typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
515 typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
517 /// \brief Constructor for the GraphCopy.
519 /// It copies the content of the \c _source graph into the
520 /// \c _target graph. It creates also two references, one beetween
521 /// the two nodeset and one beetween the two edgesets.
522 GraphCopy(Target& _target, const Source& _source)
523 : source(_source), target(_target),
524 nodeRefMap(_source), edgeRefMap(_source) {
525 for (NodeIt it(source); it != INVALID; ++it) {
526 nodeRefMap[it] = target.addNode();
528 for (EdgeIt it(source); it != INVALID; ++it) {
529 edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
530 nodeRefMap[source.target(it)]);
534 /// \brief Copies the node references into the given map.
536 /// Copies the node references into the given map.
537 template <typename NodeRef>
538 const GraphCopy& nodeRef(NodeRef& map) const {
539 for (NodeIt it(source); it != INVALID; ++it) {
540 map.set(it, nodeRefMap[it]);
545 /// \brief Reverse and copies the node references into the given map.
547 /// Reverse and copies the node references into the given map.
548 template <typename NodeRef>
549 const GraphCopy& nodeCrossRef(NodeRef& map) const {
550 for (NodeIt it(source); it != INVALID; ++it) {
551 map.set(nodeRefMap[it], it);
556 /// \brief Copies the edge references into the given map.
558 /// Copies the edge references into the given map.
559 template <typename EdgeRef>
560 const GraphCopy& edgeRef(EdgeRef& map) const {
561 for (EdgeIt it(source); it != INVALID; ++it) {
562 map.set(it, edgeRefMap[it]);
567 /// \brief Reverse and copies the edge references into the given map.
569 /// Reverse and copies the edge references into the given map.
570 template <typename EdgeRef>
571 const GraphCopy& edgeCrossRef(EdgeRef& map) const {
572 for (EdgeIt it(source); it != INVALID; ++it) {
573 map.set(edgeRefMap[it], it);
578 /// \brief Make copy of the given map.
580 /// Makes copy of the given map for the newly created graph.
581 /// The new map's key type is the target graph's node type,
582 /// and the copied map's key type is the source graph's node
584 template <typename TargetMap, typename SourceMap>
585 const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
586 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
590 /// \brief Make copy of the given map.
592 /// Makes copy of the given map for the newly created graph.
593 /// The new map's key type is the target graph's edge type,
594 /// and the copied map's key type is the source graph's edge
596 template <typename TargetMap, typename SourceMap>
597 const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
598 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
602 /// \brief Gives back the stored node references.
604 /// Gives back the stored node references.
605 const NodeRefMap& nodeRef() const {
609 /// \brief Gives back the stored edge references.
611 /// Gives back the stored edge references.
612 const EdgeRefMap& edgeRef() const {
620 const Source& source;
623 NodeRefMap nodeRefMap;
624 EdgeRefMap edgeRefMap;
627 /// \brief Copy a graph to another graph.
629 /// Copy a graph to another graph.
630 /// The usage of the function:
633 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
636 /// After the copy the \c nr map will contain the mapping from the
637 /// source graph's nodes to the target graph's nodes and the \c ecr will
638 /// contain the mapping from the target graph's edges to the source's
640 template <typename Target, typename Source>
641 GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
642 return GraphCopy<Target, Source>(target, source);
645 /// \brief Class to copy an undirected graph.
647 /// Class to copy an undirected graph to another graph (duplicate a graph).
648 /// The simplest way of using it is through the \c copyUGraph() function.
649 template <typename Target, typename Source>
652 typedef typename Source::Node Node;
653 typedef typename Source::NodeIt NodeIt;
654 typedef typename Source::Edge Edge;
655 typedef typename Source::EdgeIt EdgeIt;
656 typedef typename Source::UEdge UEdge;
657 typedef typename Source::UEdgeIt UEdgeIt;
659 typedef typename Source::
660 template NodeMap<typename Target::Node> NodeRefMap;
662 typedef typename Source::
663 template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
668 EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
669 typedef typename Source::Edge Key;
670 typedef typename Target::Edge Value;
672 Value operator[](const Key& key) {
673 return gc.target.direct(gc.uEdgeRef[key],
674 gc.target.direction(key));
682 /// \brief Constructor for the UGraphCopy.
684 /// It copies the content of the \c _source graph into the
685 /// \c _target graph. It creates also two references, one beetween
686 /// the two nodeset and one beetween the two edgesets.
687 UGraphCopy(Target& _target, const Source& _source)
688 : source(_source), target(_target),
689 nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
690 for (NodeIt it(source); it != INVALID; ++it) {
691 nodeRefMap[it] = target.addNode();
693 for (UEdgeIt it(source); it != INVALID; ++it) {
694 uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
695 nodeRefMap[source.target(it)]);
699 /// \brief Copies the node references into the given map.
701 /// Copies the node references into the given map.
702 template <typename NodeRef>
703 const UGraphCopy& nodeRef(NodeRef& map) const {
704 for (NodeIt it(source); it != INVALID; ++it) {
705 map.set(it, nodeRefMap[it]);
710 /// \brief Reverse and copies the node references into the given map.
712 /// Reverse and copies the node references into the given map.
713 template <typename NodeRef>
714 const UGraphCopy& nodeCrossRef(NodeRef& map) const {
715 for (NodeIt it(source); it != INVALID; ++it) {
716 map.set(nodeRefMap[it], it);
721 /// \brief Copies the edge references into the given map.
723 /// Copies the edge references into the given map.
724 template <typename EdgeRef>
725 const UGraphCopy& edgeRef(EdgeRef& map) const {
726 for (EdgeIt it(source); it != INVALID; ++it) {
727 map.set(edgeRefMap[it], it);
732 /// \brief Reverse and copies the undirected edge references into the
735 /// Reverse and copies the undirected edge references into the given map.
736 template <typename EdgeRef>
737 const UGraphCopy& edgeCrossRef(EdgeRef& map) const {
738 for (EdgeIt it(source); it != INVALID; ++it) {
739 map.set(it, edgeRefMap[it]);
744 /// \brief Copies the undirected edge references into the given map.
746 /// Copies the undirected edge references into the given map.
747 template <typename EdgeRef>
748 const UGraphCopy& uEdgeRef(EdgeRef& map) const {
749 for (UEdgeIt it(source); it != INVALID; ++it) {
750 map.set(it, uEdgeRefMap[it]);
755 /// \brief Reverse and copies the undirected edge references into the
758 /// Reverse and copies the undirected edge references into the given map.
759 template <typename EdgeRef>
760 const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
761 for (UEdgeIt it(source); it != INVALID; ++it) {
762 map.set(uEdgeRefMap[it], it);
767 /// \brief Make copy of the given map.
769 /// Makes copy of the given map for the newly created graph.
770 /// The new map's key type is the target graph's node type,
771 /// and the copied map's key type is the source graph's node
773 template <typename TargetMap, typename SourceMap>
774 const UGraphCopy& nodeMap(TargetMap& tMap,
775 const SourceMap& sMap) const {
776 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
780 /// \brief Make copy of the given map.
782 /// Makes copy of the given map for the newly created graph.
783 /// The new map's key type is the target graph's edge type,
784 /// and the copied map's key type is the source graph's edge
786 template <typename TargetMap, typename SourceMap>
787 const UGraphCopy& edgeMap(TargetMap& tMap,
788 const SourceMap& sMap) const {
789 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
793 /// \brief Make copy of the given map.
795 /// Makes copy of the given map for the newly created graph.
796 /// The new map's key type is the target graph's edge type,
797 /// and the copied map's key type is the source graph's edge
799 template <typename TargetMap, typename SourceMap>
800 const UGraphCopy& uEdgeMap(TargetMap& tMap,
801 const SourceMap& sMap) const {
802 copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
806 /// \brief Gives back the stored node references.
808 /// Gives back the stored node references.
809 const NodeRefMap& nodeRef() const {
813 /// \brief Gives back the stored edge references.
815 /// Gives back the stored edge references.
816 const EdgeRefMap& edgeRef() const {
820 /// \brief Gives back the stored uedge references.
822 /// Gives back the stored uedge references.
823 const UEdgeRefMap& uEdgeRef() const {
831 const Source& source;
834 NodeRefMap nodeRefMap;
835 EdgeRefMap edgeRefMap;
836 UEdgeRefMap uEdgeRefMap;
839 /// \brief Copy a graph to another graph.
841 /// Copy a graph to another graph.
842 /// The usage of the function:
845 /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
848 /// After the copy the \c nr map will contain the mapping from the
849 /// source graph's nodes to the target graph's nodes and the \c ecr will
850 /// contain the mapping from the target graph's edges to the source's
852 template <typename Target, typename Source>
853 UGraphCopy<Target, Source>
854 copyUGraph(Target& target, const Source& source) {
855 return UGraphCopy<Target, Source>(target, source);
861 /// \addtogroup graph_maps
864 /// Provides an immutable and unique id for each item in the graph.
866 /// The IdMap class provides a unique and immutable id for each item of the
867 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
868 /// different items (nodes) get different ids <li>\b immutable: the id of an
869 /// item (node) does not change (even if you delete other nodes). </ul>
870 /// Through this map you get access (i.e. can read) the inner id values of
871 /// the items stored in the graph. This map can be inverted with its member
872 /// class \c InverseMap.
874 template <typename _Graph, typename _Item>
877 typedef _Graph Graph;
882 /// \brief Constructor.
884 /// Constructor for creating id map.
885 IdMap(const Graph& _graph) : graph(&_graph) {}
887 /// \brief Gives back the \e id of the item.
889 /// Gives back the immutable and unique \e id of the map.
890 int operator[](const Item& item) const { return graph->id(item);}
898 /// \brief The class represents the inverse of its owner (IdMap).
900 /// The class represents the inverse of its owner (IdMap).
905 /// \brief Constructor.
907 /// Constructor for creating an id-to-item map.
908 InverseMap(const Graph& _graph) : graph(&_graph) {}
910 /// \brief Constructor.
912 /// Constructor for creating an id-to-item map.
913 InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
915 /// \brief Gives back the given item from its id.
917 /// Gives back the given item from its id.
919 Item operator[](int id) const { return graph->fromId(id, Item());}
924 /// \brief Gives back the inverse of the map.
926 /// Gives back the inverse of the IdMap.
927 InverseMap inverse() const { return InverseMap(*graph);}
932 /// \brief General invertable graph-map type.
934 /// This type provides simple invertable graph-maps.
935 /// The InvertableMap wraps an arbitrary ReadWriteMap
936 /// and if a key is set to a new value then store it
937 /// in the inverse map.
939 /// The values of the map can be accessed
940 /// with stl compatible forward iterator.
942 /// \param _Graph The graph type.
943 /// \param _Item The item type of the graph.
944 /// \param _Value The value type of the map.
946 /// \see IterableValueMap
948 /// \param _Map A ReadWriteMap mapping from the item type to integer.
950 typename _Graph, typename _Item, typename _Value,
951 typename _Map = DefaultMap<_Graph, _Item, _Value>
954 template <typename _Graph, typename _Item, typename _Value>
956 class InvertableMap : protected _Map {
959 /// The key type of InvertableMap (Node, Edge, UEdge).
960 typedef typename _Map::Key Key;
961 /// The value type of the InvertableMap.
962 typedef typename _Map::Value Value;
967 typedef _Graph Graph;
969 typedef std::map<Value, Key> Container;
976 /// \brief Constructor.
978 /// Construct a new InvertableMap for the graph.
980 InvertableMap(const Graph& graph) : Map(graph) {}
982 /// \brief Forward iterator for values.
984 /// This iterator is an stl compatible forward
985 /// iterator on the values of the map. The values can
986 /// be accessed in the [beginValue, endValue) range.
989 : public std::iterator<std::forward_iterator_tag, Value> {
990 friend class InvertableMap;
992 ValueIterator(typename Container::const_iterator _it)
998 ValueIterator& operator++() { ++it; return *this; }
999 ValueIterator operator++(int) {
1000 ValueIterator tmp(*this);
1005 const Value& operator*() const { return it->first; }
1006 const Value* operator->() const { return &(it->first); }
1008 bool operator==(ValueIterator jt) const { return it == jt.it; }
1009 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1012 typename Container::const_iterator it;
1015 /// \brief Returns an iterator to the first value.
1017 /// Returns an stl compatible iterator to the
1018 /// first value of the map. The values of the
1019 /// map can be accessed in the [beginValue, endValue)
1021 ValueIterator beginValue() const {
1022 return ValueIterator(invMap.begin());
1025 /// \brief Returns an iterator after the last value.
1027 /// Returns an stl compatible iterator after the
1028 /// last value of the map. The values of the
1029 /// map can be accessed in the [beginValue, endValue)
1031 ValueIterator endValue() const {
1032 return ValueIterator(invMap.end());
1035 /// \brief The setter function of the map.
1037 /// Sets the mapped value.
1038 void set(const Key& key, const Value& val) {
1039 Value oldval = Map::operator[](key);
1040 typename Container::iterator it = invMap.find(oldval);
1041 if (it != invMap.end() && it->second == key) {
1044 invMap.insert(make_pair(val, key));
1048 /// \brief The getter function of the map.
1050 /// It gives back the value associated with the key.
1051 typename MapTraits<Map>::ConstReturnValue
1052 operator[](const Key& key) const {
1053 return Map::operator[](key);
1058 /// \brief Erase the key from the map.
1060 /// Erase the key to the map. It is called by the
1061 /// \c AlterationNotifier.
1062 virtual void erase(const Key& key) {
1063 Value val = Map::operator[](key);
1064 typename Container::iterator it = invMap.find(val);
1065 if (it != invMap.end() && it->second == key) {
1071 /// \brief Erase more keys from the map.
1073 /// Erase more keys from the map. It is called by the
1074 /// \c AlterationNotifier.
1075 virtual void erase(const std::vector<Key>& keys) {
1076 for (int i = 0; i < (int)keys.size(); ++i) {
1077 Value val = Map::operator[](keys[i]);
1078 typename Container::iterator it = invMap.find(val);
1079 if (it != invMap.end() && it->second == keys[i]) {
1086 /// \brief Clear the keys from the map and inverse map.
1088 /// Clear the keys from the map and inverse map. It is called by the
1089 /// \c AlterationNotifier.
1090 virtual void clear() {
1097 /// \brief The inverse map type.
1099 /// The inverse of this map. The subscript operator of the map
1100 /// gives back always the item what was last assigned to the value.
1103 /// \brief Constructor of the InverseMap.
1105 /// Constructor of the InverseMap.
1106 InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
1108 /// The value type of the InverseMap.
1109 typedef typename InvertableMap::Key Value;
1110 /// The key type of the InverseMap.
1111 typedef typename InvertableMap::Value Key;
1113 /// \brief Subscript operator.
1115 /// Subscript operator. It gives back always the item
1116 /// what was last assigned to the value.
1117 Value operator[](const Key& key) const {
1118 typename Container::const_iterator it = inverted.invMap.find(key);
1123 const InvertableMap& inverted;
1126 /// \brief It gives back the just readeable inverse map.
1128 /// It gives back the just readeable inverse map.
1129 InverseMap inverse() const {
1130 return InverseMap(*this);
1137 /// \brief Provides a mutable, continuous and unique descriptor for each
1138 /// item in the graph.
1140 /// The DescriptorMap class provides a unique and continuous (but mutable)
1141 /// descriptor (id) for each item of the same type (e.g. node) in the
1142 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1143 /// different ids <li>\b continuous: the range of the ids is the set of
1144 /// integers between 0 and \c n-1, where \c n is the number of the items of
1145 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1146 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1147 /// with its member class \c InverseMap.
1149 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1150 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
1153 /// \param _Map A ReadWriteMap mapping from the item type to integer.
1155 typename _Graph, typename _Item,
1156 typename _Map = DefaultMap<_Graph, _Item, int>
1159 template <typename _Graph, typename _Item>
1161 class DescriptorMap : protected _Map {
1167 /// The graph class of DescriptorMap.
1168 typedef _Graph Graph;
1170 /// The key type of DescriptorMap (Node, Edge, UEdge).
1171 typedef typename _Map::Key Key;
1172 /// The value type of DescriptorMap.
1173 typedef typename _Map::Value Value;
1175 /// \brief Constructor.
1177 /// Constructor for descriptor map.
1178 DescriptorMap(const Graph& _graph) : Map(_graph) {
1184 /// \brief Add a new key to the map.
1186 /// Add a new key to the map. It is called by the
1187 /// \c AlterationNotifier.
1188 virtual void add(const Item& item) {
1190 Map::set(item, invMap.size());
1191 invMap.push_back(item);
1194 /// \brief Add more new keys to the map.
1196 /// Add more new keys to the map. It is called by the
1197 /// \c AlterationNotifier.
1198 virtual void add(const std::vector<Item>& items) {
1200 for (int i = 0; i < (int)items.size(); ++i) {
1201 Map::set(items[i], invMap.size());
1202 invMap.push_back(items[i]);
1206 /// \brief Erase the key from the map.
1208 /// Erase the key from the map. It is called by the
1209 /// \c AlterationNotifier.
1210 virtual void erase(const Item& item) {
1211 Map::set(invMap.back(), Map::operator[](item));
1212 invMap[Map::operator[](item)] = invMap.back();
1217 /// \brief Erase more keys from the map.
1219 /// Erase more keys from the map. It is called by the
1220 /// \c AlterationNotifier.
1221 virtual void erase(const std::vector<Item>& items) {
1222 for (int i = 0; i < (int)items.size(); ++i) {
1223 Map::set(invMap.back(), Map::operator[](items[i]));
1224 invMap[Map::operator[](items[i])] = invMap.back();
1230 /// \brief Build the unique map.
1232 /// Build the unique map. It is called by the
1233 /// \c AlterationNotifier.
1234 virtual void build() {
1237 const typename Map::Notifier* notifier = Map::getNotifier();
1238 for (notifier->first(it); it != INVALID; notifier->next(it)) {
1239 Map::set(it, invMap.size());
1240 invMap.push_back(it);
1244 /// \brief Clear the keys from the map.
1246 /// Clear the keys from the map. It is called by the
1247 /// \c AlterationNotifier.
1248 virtual void clear() {
1255 /// \brief Returns the maximal value plus one.
1257 /// Returns the maximal value plus one in the map.
1258 unsigned int size() const {
1259 return invMap.size();
1262 /// \brief Swaps the position of the two items in the map.
1264 /// Swaps the position of the two items in the map.
1265 void swap(const Item& p, const Item& q) {
1266 int pi = Map::operator[](p);
1267 int qi = Map::operator[](q);
1274 /// \brief Gives back the \e descriptor of the item.
1276 /// Gives back the mutable and unique \e descriptor of the map.
1277 int operator[](const Item& item) const {
1278 return Map::operator[](item);
1283 typedef std::vector<Item> Container;
1287 /// \brief The inverse map type of DescriptorMap.
1289 /// The inverse map type of DescriptorMap.
1292 /// \brief Constructor of the InverseMap.
1294 /// Constructor of the InverseMap.
1295 InverseMap(const DescriptorMap& _inverted)
1296 : inverted(_inverted) {}
1299 /// The value type of the InverseMap.
1300 typedef typename DescriptorMap::Key Value;
1301 /// The key type of the InverseMap.
1302 typedef typename DescriptorMap::Value Key;
1304 /// \brief Subscript operator.
1306 /// Subscript operator. It gives back the item
1307 /// that the descriptor belongs to currently.
1308 Value operator[](const Key& key) const {
1309 return inverted.invMap[key];
1312 /// \brief Size of the map.
1314 /// Returns the size of the map.
1315 unsigned int size() const {
1316 return inverted.invMap.size();
1320 const DescriptorMap& inverted;
1323 /// \brief Gives back the inverse of the map.
1325 /// Gives back the inverse of the map.
1326 const InverseMap inverse() const {
1327 return InverseMap(*this);
1331 /// \brief Returns the source of the given edge.
1333 /// The SourceMap gives back the source Node of the given edge.
1334 /// \author Balazs Dezso
1335 template <typename Graph>
1339 typedef typename Graph::Node Value;
1340 typedef typename Graph::Edge Key;
1342 /// \brief Constructor
1345 /// \param _graph The graph that the map belongs to.
1346 SourceMap(const Graph& _graph) : graph(_graph) {}
1348 /// \brief The subscript operator.
1350 /// The subscript operator.
1351 /// \param edge The edge
1352 /// \return The source of the edge
1353 Value operator[](const Key& edge) const {
1354 return graph.source(edge);
1361 /// \brief Returns a \ref SourceMap class
1363 /// This function just returns an \ref SourceMap class.
1364 /// \relates SourceMap
1365 template <typename Graph>
1366 inline SourceMap<Graph> sourceMap(const Graph& graph) {
1367 return SourceMap<Graph>(graph);
1370 /// \brief Returns the target of the given edge.
1372 /// The TargetMap gives back the target Node of the given edge.
1373 /// \author Balazs Dezso
1374 template <typename Graph>
1378 typedef typename Graph::Node Value;
1379 typedef typename Graph::Edge Key;
1381 /// \brief Constructor
1384 /// \param _graph The graph that the map belongs to.
1385 TargetMap(const Graph& _graph) : graph(_graph) {}
1387 /// \brief The subscript operator.
1389 /// The subscript operator.
1390 /// \param e The edge
1391 /// \return The target of the edge
1392 Value operator[](const Key& e) const {
1393 return graph.target(e);
1400 /// \brief Returns a \ref TargetMap class
1402 /// This function just returns a \ref TargetMap class.
1403 /// \relates TargetMap
1404 template <typename Graph>
1405 inline TargetMap<Graph> targetMap(const Graph& graph) {
1406 return TargetMap<Graph>(graph);
1409 /// \brief Returns the "forward" directed edge view of an undirected edge.
1411 /// Returns the "forward" directed edge view of an undirected edge.
1412 /// \author Balazs Dezso
1413 template <typename Graph>
1417 typedef typename Graph::Edge Value;
1418 typedef typename Graph::UEdge Key;
1420 /// \brief Constructor
1423 /// \param _graph The graph that the map belongs to.
1424 ForwardMap(const Graph& _graph) : graph(_graph) {}
1426 /// \brief The subscript operator.
1428 /// The subscript operator.
1429 /// \param key An undirected edge
1430 /// \return The "forward" directed edge view of undirected edge
1431 Value operator[](const Key& key) const {
1432 return graph.direct(key, true);
1439 /// \brief Returns a \ref ForwardMap class
1441 /// This function just returns an \ref ForwardMap class.
1442 /// \relates ForwardMap
1443 template <typename Graph>
1444 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
1445 return ForwardMap<Graph>(graph);
1448 /// \brief Returns the "backward" directed edge view of an undirected edge.
1450 /// Returns the "backward" directed edge view of an undirected edge.
1451 /// \author Balazs Dezso
1452 template <typename Graph>
1456 typedef typename Graph::Edge Value;
1457 typedef typename Graph::UEdge Key;
1459 /// \brief Constructor
1462 /// \param _graph The graph that the map belongs to.
1463 BackwardMap(const Graph& _graph) : graph(_graph) {}
1465 /// \brief The subscript operator.
1467 /// The subscript operator.
1468 /// \param key An undirected edge
1469 /// \return The "backward" directed edge view of undirected edge
1470 Value operator[](const Key& key) const {
1471 return graph.direct(key, false);
1478 /// \brief Returns a \ref BackwardMap class
1480 /// This function just returns a \ref BackwardMap class.
1481 /// \relates BackwardMap
1482 template <typename Graph>
1483 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
1484 return BackwardMap<Graph>(graph);
1487 /// \brief Potential difference map
1489 /// If there is an potential map on the nodes then we
1490 /// can get an edge map as we get the substraction of the
1491 /// values of the target and source.
1492 template <typename Graph, typename NodeMap>
1493 class PotentialDifferenceMap {
1495 typedef typename Graph::Edge Key;
1496 typedef typename NodeMap::Value Value;
1498 /// \brief Constructor
1500 /// Contructor of the map
1501 PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential)
1502 : graph(_graph), potential(_potential) {}
1504 /// \brief Const subscription operator
1506 /// Const subscription operator
1507 Value operator[](const Key& edge) const {
1508 return potential[graph.target(edge)] - potential[graph.source(edge)];
1513 const NodeMap& potential;
1516 /// \brief Just returns a PotentialDifferenceMap
1518 /// Just returns a PotentialDifferenceMap
1519 /// \relates PotentialDifferenceMap
1520 template <typename Graph, typename NodeMap>
1521 PotentialDifferenceMap<Graph, NodeMap>
1522 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
1523 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
1526 /// \brief Map of the node in-degrees.
1528 /// This map returns the in-degree of a node. Once it is constructed,
1529 /// the degrees are stored in a standard NodeMap, so each query is done
1530 /// in constant time. On the other hand, the values are updated automatically
1531 /// whenever the graph changes.
1533 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1534 /// alternative ways to modify the graph. The correct behavior of InDegMap
1535 /// is not guarantied if these additional features are used. For example
1536 /// the functions \ref ListGraph::changeSource() "changeSource()",
1537 /// \ref ListGraph::changeTarget() "changeTarget()" and
1538 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1539 /// of \ref ListGraph will \e not update the degree values correctly.
1543 template <typename _Graph>
1545 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
1546 ::ItemNotifier::ObserverBase {
1550 typedef _Graph Graph;
1552 typedef typename Graph::Node Key;
1554 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
1555 ::ItemNotifier::ObserverBase Parent;
1559 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
1562 typedef DefaultMap<_Graph, Key, int> Parent;
1563 typedef typename Parent::Graph Graph;
1565 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1567 virtual void add(const Key& key) {
1569 Parent::set(key, 0);
1572 virtual void add(const std::vector<Key>& keys) {
1574 for (int i = 0; i < (int)keys.size(); ++i) {
1575 Parent::set(keys[i], 0);
1582 /// \brief Constructor.
1584 /// Constructor for creating in-degree map.
1585 InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1586 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
1588 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1589 deg[it] = countInEdges(graph, it);
1593 /// Gives back the in-degree of a Node.
1594 int operator[](const Key& key) const {
1600 typedef typename Graph::Edge Edge;
1602 virtual void add(const Edge& edge) {
1603 ++deg[graph.target(edge)];
1606 virtual void add(const std::vector<Edge>& edges) {
1607 for (int i = 0; i < (int)edges.size(); ++i) {
1608 ++deg[graph.target(edges[i])];
1612 virtual void erase(const Edge& edge) {
1613 --deg[graph.target(edge)];
1616 virtual void erase(const std::vector<Edge>& edges) {
1617 for (int i = 0; i < (int)edges.size(); ++i) {
1618 --deg[graph.target(edges[i])];
1622 virtual void build() {
1623 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1624 deg[it] = countInEdges(graph, it);
1628 virtual void clear() {
1629 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1635 const _Graph& graph;
1639 /// \brief Map of the node out-degrees.
1641 /// This map returns the out-degree of a node. Once it is constructed,
1642 /// the degrees are stored in a standard NodeMap, so each query is done
1643 /// in constant time. On the other hand, the values are updated automatically
1644 /// whenever the graph changes.
1646 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1647 /// alternative ways to modify the graph. The correct behavior of OutDegMap
1648 /// is not guarantied if these additional features are used. For example
1649 /// the functions \ref ListGraph::changeSource() "changeSource()",
1650 /// \ref ListGraph::changeTarget() "changeTarget()" and
1651 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1652 /// of \ref ListGraph will \e not update the degree values correctly.
1656 template <typename _Graph>
1658 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
1659 ::ItemNotifier::ObserverBase {
1663 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
1664 ::ItemNotifier::ObserverBase Parent;
1666 typedef _Graph Graph;
1668 typedef typename Graph::Node Key;
1672 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
1675 typedef DefaultMap<_Graph, Key, int> Parent;
1676 typedef typename Parent::Graph Graph;
1678 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1680 virtual void add(const Key& key) {
1682 Parent::set(key, 0);
1684 virtual void add(const std::vector<Key>& keys) {
1686 for (int i = 0; i < (int)keys.size(); ++i) {
1687 Parent::set(keys[i], 0);
1694 /// \brief Constructor.
1696 /// Constructor for creating out-degree map.
1697 OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1698 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
1700 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1701 deg[it] = countOutEdges(graph, it);
1705 /// Gives back the out-degree of a Node.
1706 int operator[](const Key& key) const {
1712 typedef typename Graph::Edge Edge;
1714 virtual void add(const Edge& edge) {
1715 ++deg[graph.source(edge)];
1718 virtual void add(const std::vector<Edge>& edges) {
1719 for (int i = 0; i < (int)edges.size(); ++i) {
1720 ++deg[graph.source(edges[i])];
1724 virtual void erase(const Edge& edge) {
1725 --deg[graph.source(edge)];
1728 virtual void erase(const std::vector<Edge>& edges) {
1729 for (int i = 0; i < (int)edges.size(); ++i) {
1730 --deg[graph.source(edges[i])];
1734 virtual void build() {
1735 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1736 deg[it] = countOutEdges(graph, it);
1740 virtual void clear() {
1741 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1747 const _Graph& graph;
1754 } //END OF NAMESPACE LEMON