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);
150 namespace _graph_utils_bits {
152 template <typename Graph, typename Enable = void>
153 struct CountANodesSelector {
154 static int count(const Graph &g) {
155 return countItems<Graph, typename Graph::ANode>(g);
159 template <typename Graph>
160 struct CountANodesSelector<
162 enable_if<typename Graph::NodeNumTag, void>::type>
164 static int count(const Graph &g) {
170 /// \brief Function to count the anodes in the graph.
172 /// This function counts the anodes in the graph.
173 /// The complexity of the function is O(an) but for some
174 /// graph structures it is specialized to run in O(1).
176 /// \todo refer how to specialize it
178 template <typename Graph>
179 inline int countANodes(const Graph& g) {
180 return _graph_utils_bits::CountANodesSelector<Graph>::count(g);
183 namespace _graph_utils_bits {
185 template <typename Graph, typename Enable = void>
186 struct CountBNodesSelector {
187 static int count(const Graph &g) {
188 return countItems<Graph, typename Graph::BNode>(g);
192 template <typename Graph>
193 struct CountBNodesSelector<
195 enable_if<typename Graph::NodeNumTag, void>::type>
197 static int count(const Graph &g) {
203 /// \brief Function to count the bnodes in the graph.
205 /// This function counts the bnodes in the graph.
206 /// The complexity of the function is O(bn) but for some
207 /// graph structures it is specialized to run in O(1).
209 /// \todo refer how to specialize it
211 template <typename Graph>
212 inline int countBNodes(const Graph& g) {
213 return _graph_utils_bits::CountBNodesSelector<Graph>::count(g);
219 namespace _graph_utils_bits {
221 template <typename Graph, typename Enable = void>
222 struct CountEdgesSelector {
223 static int count(const Graph &g) {
224 return countItems<Graph, typename Graph::Edge>(g);
228 template <typename Graph>
229 struct CountEdgesSelector<
231 typename enable_if<typename Graph::EdgeNumTag, void>::type>
233 static int count(const Graph &g) {
239 /// \brief Function to count the edges in the graph.
241 /// This function counts the edges in the graph.
242 /// The complexity of the function is O(e) but for some
243 /// graph structures it is specialized to run in O(1).
245 template <typename Graph>
246 inline int countEdges(const Graph& g) {
247 return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
250 // Undirected edge counting:
251 namespace _graph_utils_bits {
253 template <typename Graph, typename Enable = void>
254 struct CountUEdgesSelector {
255 static int count(const Graph &g) {
256 return countItems<Graph, typename Graph::UEdge>(g);
260 template <typename Graph>
261 struct CountUEdgesSelector<
263 typename enable_if<typename Graph::EdgeNumTag, void>::type>
265 static int count(const Graph &g) {
271 /// \brief Function to count the undirected edges in the graph.
273 /// This function counts the undirected edges in the graph.
274 /// The complexity of the function is O(e) but for some
275 /// graph structures it is specialized to run in O(1).
277 template <typename Graph>
278 inline int countUEdges(const Graph& g) {
279 return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
284 template <typename Graph, typename DegIt>
285 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
287 for (DegIt it(_g, _n); it != INVALID; ++it) {
293 /// \brief Function to count the number of the out-edges from node \c n.
295 /// This function counts the number of the out-edges from node \c n
297 template <typename Graph>
298 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
299 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
302 /// \brief Function to count the number of the in-edges to node \c n.
304 /// This function counts the number of the in-edges to node \c n
306 template <typename Graph>
307 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
308 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
311 /// \brief Function to count the number of the inc-edges to node \c n.
313 /// This function counts the number of the inc-edges to node \c n
315 template <typename Graph>
316 inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
317 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
320 namespace _graph_utils_bits {
322 template <typename Graph, typename Enable = void>
323 struct FindEdgeSelector {
324 typedef typename Graph::Node Node;
325 typedef typename Graph::Edge Edge;
326 static Edge find(const Graph &g, Node u, Node v, Edge e) {
332 while (e != INVALID && g.target(e) != v) {
339 template <typename Graph>
340 struct FindEdgeSelector<
342 typename enable_if<typename Graph::FindEdgeTag, void>::type>
344 typedef typename Graph::Node Node;
345 typedef typename Graph::Edge Edge;
346 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
347 return g.findEdge(u, v, prev);
352 /// \brief Finds an edge between two nodes of a graph.
354 /// Finds an edge from node \c u to node \c v in graph \c g.
356 /// If \c prev is \ref INVALID (this is the default value), then
357 /// it finds the first edge from \c u to \c v. Otherwise it looks for
358 /// the next edge from \c u to \c v after \c prev.
359 /// \return The found edge or \ref INVALID if there is no such an edge.
361 /// Thus you can iterate through each edge from \c u to \c v as it follows.
363 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
367 template <typename Graph>
368 inline typename Graph::Edge findEdge(const Graph &g,
369 typename Graph::Node u,
370 typename Graph::Node v,
371 typename Graph::Edge prev = INVALID) {
372 return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
375 /// \brief Iterator for iterating on edges connected the same nodes.
377 /// Iterator for iterating on edges connected the same nodes. It is
378 /// higher level interface for the findEdge() function. You can
379 /// use it the following way:
381 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
386 /// \author Balazs Dezso
387 template <typename _Graph>
388 class ConEdgeIt : public _Graph::Edge {
391 typedef _Graph Graph;
392 typedef typename Graph::Edge Parent;
394 typedef typename Graph::Edge Edge;
395 typedef typename Graph::Node Node;
397 /// \brief Constructor.
399 /// Construct a new ConEdgeIt iterating on the edges which
400 /// connects the \c u and \c v node.
401 ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
402 Parent::operator=(findEdge(graph, u, v));
405 /// \brief Constructor.
407 /// Construct a new ConEdgeIt which continues the iterating from
409 ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
411 /// \brief Increment operator.
413 /// It increments the iterator and gives back the next edge.
414 ConEdgeIt& operator++() {
415 Parent::operator=(findEdge(graph, graph.source(*this),
416 graph.target(*this), *this));
423 namespace _graph_utils_bits {
425 template <typename Graph, typename Enable = void>
426 struct FindUEdgeSelector {
427 typedef typename Graph::Node Node;
428 typedef typename Graph::UEdge UEdge;
429 static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
435 b = g.source(e) == u;
438 while (e != INVALID && g.target(e) != v) {
448 while (e != INVALID && (!b || g.target(e) != v)) {
456 template <typename Graph>
457 struct FindUEdgeSelector<
459 typename enable_if<typename Graph::FindEdgeTag, void>::type>
461 typedef typename Graph::Node Node;
462 typedef typename Graph::UEdge UEdge;
463 static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
464 return g.findUEdge(u, v, prev);
469 /// \brief Finds an uedge between two nodes of a graph.
471 /// Finds an uedge from node \c u to node \c v in graph \c g.
472 /// If the node \c u and node \c v is equal then each loop edge
473 /// will be enumerated.
475 /// If \c prev is \ref INVALID (this is the default value), then
476 /// it finds the first edge from \c u to \c v. Otherwise it looks for
477 /// the next edge from \c u to \c v after \c prev.
478 /// \return The found edge or \ref INVALID if there is no such an edge.
480 /// Thus you can iterate through each edge from \c u to \c v as it follows.
482 /// for(UEdge e = findUEdge(g,u,v); e != INVALID;
483 /// e = findUEdge(g,u,v,e)) {
487 template <typename Graph>
488 inline typename Graph::UEdge findEdge(const Graph &g,
489 typename Graph::Node u,
490 typename Graph::Node v,
491 typename Graph::UEdge prev = INVALID) {
492 return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, prev);
495 /// \brief Iterator for iterating on uedges connected the same nodes.
497 /// Iterator for iterating on uedges connected the same nodes. It is
498 /// higher level interface for the findUEdge() function. You can
499 /// use it the following way:
501 /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
506 /// \author Balazs Dezso
507 template <typename _Graph>
508 class ConUEdgeIt : public _Graph::UEdge {
511 typedef _Graph Graph;
512 typedef typename Graph::UEdge Parent;
514 typedef typename Graph::UEdge UEdge;
515 typedef typename Graph::Node Node;
517 /// \brief Constructor.
519 /// Construct a new ConUEdgeIt iterating on the edges which
520 /// connects the \c u and \c v node.
521 ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
522 Parent::operator=(findUEdge(graph, u, v));
525 /// \brief Constructor.
527 /// Construct a new ConUEdgeIt which continues the iterating from
529 ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
531 /// \brief Increment operator.
533 /// It increments the iterator and gives back the next edge.
534 ConUEdgeIt& operator++() {
535 Parent::operator=(findUEdge(graph, graph.source(*this),
536 graph.target(*this), *this));
543 /// \brief Copy a map.
545 /// This function copies the \c source map to the \c target map. It uses the
546 /// given iterator to iterate on the data structure and it uses the \c ref
547 /// mapping to convert the source's keys to the target's keys.
548 template <typename Target, typename Source,
549 typename ItemIt, typename Ref>
550 void copyMap(Target& target, const Source& source,
551 ItemIt it, const Ref& ref) {
552 for (; it != INVALID; ++it) {
553 target[ref[it]] = source[it];
557 /// \brief Copy the source map to the target map.
559 /// Copy the \c source map to the \c target map. It uses the given iterator
560 /// to iterate on the data structure.
561 template <typename Target, typename Source, typename ItemIt>
562 void copyMap(Target& target, const Source& source, ItemIt it) {
563 for (; it != INVALID; ++it) {
564 target[it] = source[it];
568 /// \brief Class to copy a graph.
570 /// Class to copy a graph to another graph (duplicate a graph). The
571 /// simplest way of using it is through the \c copyGraph() function.
572 template <typename Target, typename Source>
575 typedef typename Source::Node Node;
576 typedef typename Source::NodeIt NodeIt;
577 typedef typename Source::Edge Edge;
578 typedef typename Source::EdgeIt EdgeIt;
580 typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
581 typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
583 /// \brief Constructor for the GraphCopy.
585 /// It copies the content of the \c _source graph into the
586 /// \c _target graph. It creates also two references, one beetween
587 /// the two nodeset and one beetween the two edgesets.
588 GraphCopy(Target& _target, const Source& _source)
589 : source(_source), target(_target),
590 nodeRefMap(_source), edgeRefMap(_source) {
591 for (NodeIt it(source); it != INVALID; ++it) {
592 nodeRefMap[it] = target.addNode();
594 for (EdgeIt it(source); it != INVALID; ++it) {
595 edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
596 nodeRefMap[source.target(it)]);
600 /// \brief Copies the node references into the given map.
602 /// Copies the node references into the given map.
603 template <typename NodeRef>
604 const GraphCopy& nodeRef(NodeRef& map) const {
605 for (NodeIt it(source); it != INVALID; ++it) {
606 map.set(it, nodeRefMap[it]);
611 /// \brief Reverse and copies the node references into the given map.
613 /// Reverse and copies the node references into the given map.
614 template <typename NodeRef>
615 const GraphCopy& nodeCrossRef(NodeRef& map) const {
616 for (NodeIt it(source); it != INVALID; ++it) {
617 map.set(nodeRefMap[it], it);
622 /// \brief Copies the edge references into the given map.
624 /// Copies the edge references into the given map.
625 template <typename EdgeRef>
626 const GraphCopy& edgeRef(EdgeRef& map) const {
627 for (EdgeIt it(source); it != INVALID; ++it) {
628 map.set(it, edgeRefMap[it]);
633 /// \brief Reverse and copies the edge references into the given map.
635 /// Reverse and copies the edge references into the given map.
636 template <typename EdgeRef>
637 const GraphCopy& edgeCrossRef(EdgeRef& map) const {
638 for (EdgeIt it(source); it != INVALID; ++it) {
639 map.set(edgeRefMap[it], it);
644 /// \brief Make copy of the given map.
646 /// Makes copy of the given map for the newly created graph.
647 /// The new map's key type is the target graph's node type,
648 /// and the copied map's key type is the source graph's node
650 template <typename TargetMap, typename SourceMap>
651 const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
652 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
656 /// \brief Make copy of the given map.
658 /// Makes copy of the given map for the newly created graph.
659 /// The new map's key type is the target graph's edge type,
660 /// and the copied map's key type is the source graph's edge
662 template <typename TargetMap, typename SourceMap>
663 const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
664 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
668 /// \brief Gives back the stored node references.
670 /// Gives back the stored node references.
671 const NodeRefMap& nodeRef() const {
675 /// \brief Gives back the stored edge references.
677 /// Gives back the stored edge references.
678 const EdgeRefMap& edgeRef() const {
686 const Source& source;
689 NodeRefMap nodeRefMap;
690 EdgeRefMap edgeRefMap;
693 /// \brief Copy a graph to another graph.
695 /// Copy a graph to another graph.
696 /// The usage of the function:
699 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
702 /// After the copy the \c nr map will contain the mapping from the
703 /// source graph's nodes to the target graph's nodes and the \c ecr will
704 /// contain the mapping from the target graph's edges to the source's
706 template <typename Target, typename Source>
707 GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
708 return GraphCopy<Target, Source>(target, source);
711 /// \brief Class to copy an undirected graph.
713 /// Class to copy an undirected graph to another graph (duplicate a graph).
714 /// The simplest way of using it is through the \c copyUGraph() function.
715 template <typename Target, typename Source>
718 typedef typename Source::Node Node;
719 typedef typename Source::NodeIt NodeIt;
720 typedef typename Source::Edge Edge;
721 typedef typename Source::EdgeIt EdgeIt;
722 typedef typename Source::UEdge UEdge;
723 typedef typename Source::UEdgeIt UEdgeIt;
725 typedef typename Source::
726 template NodeMap<typename Target::Node> NodeRefMap;
728 typedef typename Source::
729 template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
734 EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
735 typedef typename Source::Edge Key;
736 typedef typename Target::Edge Value;
738 Value operator[](const Key& key) {
739 return gc.target.direct(gc.uEdgeRef[key],
740 gc.target.direction(key));
748 /// \brief Constructor for the UGraphCopy.
750 /// It copies the content of the \c _source graph into the
751 /// \c _target graph. It creates also two references, one beetween
752 /// the two nodeset and one beetween the two edgesets.
753 UGraphCopy(Target& _target, const Source& _source)
754 : source(_source), target(_target),
755 nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
756 for (NodeIt it(source); it != INVALID; ++it) {
757 nodeRefMap[it] = target.addNode();
759 for (UEdgeIt it(source); it != INVALID; ++it) {
760 uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
761 nodeRefMap[source.target(it)]);
765 /// \brief Copies the node references into the given map.
767 /// Copies the node references into the given map.
768 template <typename NodeRef>
769 const UGraphCopy& nodeRef(NodeRef& map) const {
770 for (NodeIt it(source); it != INVALID; ++it) {
771 map.set(it, nodeRefMap[it]);
776 /// \brief Reverse and copies the node references into the given map.
778 /// Reverse and copies the node references into the given map.
779 template <typename NodeRef>
780 const UGraphCopy& nodeCrossRef(NodeRef& map) const {
781 for (NodeIt it(source); it != INVALID; ++it) {
782 map.set(nodeRefMap[it], it);
787 /// \brief Copies the edge references into the given map.
789 /// Copies the edge references into the given map.
790 template <typename EdgeRef>
791 const UGraphCopy& edgeRef(EdgeRef& map) const {
792 for (EdgeIt it(source); it != INVALID; ++it) {
793 map.set(edgeRefMap[it], it);
798 /// \brief Reverse and copies the undirected edge references into the
801 /// Reverse and copies the undirected edge references into the given map.
802 template <typename EdgeRef>
803 const UGraphCopy& edgeCrossRef(EdgeRef& map) const {
804 for (EdgeIt it(source); it != INVALID; ++it) {
805 map.set(it, edgeRefMap[it]);
810 /// \brief Copies the undirected edge references into the given map.
812 /// Copies the undirected edge references into the given map.
813 template <typename EdgeRef>
814 const UGraphCopy& uEdgeRef(EdgeRef& map) const {
815 for (UEdgeIt it(source); it != INVALID; ++it) {
816 map.set(it, uEdgeRefMap[it]);
821 /// \brief Reverse and copies the undirected edge references into the
824 /// Reverse and copies the undirected edge references into the given map.
825 template <typename EdgeRef>
826 const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
827 for (UEdgeIt it(source); it != INVALID; ++it) {
828 map.set(uEdgeRefMap[it], it);
833 /// \brief Make copy of the given map.
835 /// Makes copy of the given map for the newly created graph.
836 /// The new map's key type is the target graph's node type,
837 /// and the copied map's key type is the source graph's node
839 template <typename TargetMap, typename SourceMap>
840 const UGraphCopy& nodeMap(TargetMap& tMap,
841 const SourceMap& sMap) const {
842 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
846 /// \brief Make copy of the given map.
848 /// Makes copy of the given map for the newly created graph.
849 /// The new map's key type is the target graph's edge type,
850 /// and the copied map's key type is the source graph's edge
852 template <typename TargetMap, typename SourceMap>
853 const UGraphCopy& edgeMap(TargetMap& tMap,
854 const SourceMap& sMap) const {
855 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
859 /// \brief Make copy of the given map.
861 /// Makes copy of the given map for the newly created graph.
862 /// The new map's key type is the target graph's edge type,
863 /// and the copied map's key type is the source graph's edge
865 template <typename TargetMap, typename SourceMap>
866 const UGraphCopy& uEdgeMap(TargetMap& tMap,
867 const SourceMap& sMap) const {
868 copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
872 /// \brief Gives back the stored node references.
874 /// Gives back the stored node references.
875 const NodeRefMap& nodeRef() const {
879 /// \brief Gives back the stored edge references.
881 /// Gives back the stored edge references.
882 const EdgeRefMap& edgeRef() const {
886 /// \brief Gives back the stored uedge references.
888 /// Gives back the stored uedge references.
889 const UEdgeRefMap& uEdgeRef() const {
897 const Source& source;
900 NodeRefMap nodeRefMap;
901 EdgeRefMap edgeRefMap;
902 UEdgeRefMap uEdgeRefMap;
905 /// \brief Copy a graph to another graph.
907 /// Copy a graph to another graph.
908 /// The usage of the function:
911 /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
914 /// After the copy the \c nr map will contain the mapping from the
915 /// source graph's nodes to the target graph's nodes and the \c ecr will
916 /// contain the mapping from the target graph's edges to the source's
918 template <typename Target, typename Source>
919 UGraphCopy<Target, Source>
920 copyUGraph(Target& target, const Source& source) {
921 return UGraphCopy<Target, Source>(target, source);
927 /// \addtogroup graph_maps
930 /// Provides an immutable and unique id for each item in the graph.
932 /// The IdMap class provides a unique and immutable id for each item of the
933 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
934 /// different items (nodes) get different ids <li>\b immutable: the id of an
935 /// item (node) does not change (even if you delete other nodes). </ul>
936 /// Through this map you get access (i.e. can read) the inner id values of
937 /// the items stored in the graph. This map can be inverted with its member
938 /// class \c InverseMap.
940 template <typename _Graph, typename _Item>
943 typedef _Graph Graph;
948 /// \brief Constructor.
950 /// Constructor for creating id map.
951 IdMap(const Graph& _graph) : graph(&_graph) {}
953 /// \brief Gives back the \e id of the item.
955 /// Gives back the immutable and unique \e id of the map.
956 int operator[](const Item& item) const { return graph->id(item);}
964 /// \brief The class represents the inverse of its owner (IdMap).
966 /// The class represents the inverse of its owner (IdMap).
971 /// \brief Constructor.
973 /// Constructor for creating an id-to-item map.
974 InverseMap(const Graph& _graph) : graph(&_graph) {}
976 /// \brief Constructor.
978 /// Constructor for creating an id-to-item map.
979 InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
981 /// \brief Gives back the given item from its id.
983 /// Gives back the given item from its id.
985 Item operator[](int id) const { return graph->fromId(id, Item());}
990 /// \brief Gives back the inverse of the map.
992 /// Gives back the inverse of the IdMap.
993 InverseMap inverse() const { return InverseMap(*graph);}
998 /// \brief General invertable graph-map type.
1000 /// This type provides simple invertable graph-maps.
1001 /// The InvertableMap wraps an arbitrary ReadWriteMap
1002 /// and if a key is set to a new value then store it
1003 /// in the inverse map.
1005 /// The values of the map can be accessed
1006 /// with stl compatible forward iterator.
1008 /// \param _Graph The graph type.
1009 /// \param _Item The item type of the graph.
1010 /// \param _Value The value type of the map.
1012 /// \see IterableValueMap
1014 /// \param _Map A ReadWriteMap mapping from the item type to integer.
1016 typename _Graph, typename _Item, typename _Value,
1017 typename _Map = DefaultMap<_Graph, _Item, _Value>
1020 template <typename _Graph, typename _Item, typename _Value>
1022 class InvertableMap : protected _Map {
1025 /// The key type of InvertableMap (Node, Edge, UEdge).
1026 typedef typename _Map::Key Key;
1027 /// The value type of the InvertableMap.
1028 typedef typename _Map::Value Value;
1033 typedef _Graph Graph;
1035 typedef std::map<Value, Key> Container;
1042 /// \brief Constructor.
1044 /// Construct a new InvertableMap for the graph.
1046 InvertableMap(const Graph& graph) : Map(graph) {}
1048 /// \brief Forward iterator for values.
1050 /// This iterator is an stl compatible forward
1051 /// iterator on the values of the map. The values can
1052 /// be accessed in the [beginValue, endValue) range.
1055 : public std::iterator<std::forward_iterator_tag, Value> {
1056 friend class InvertableMap;
1058 ValueIterator(typename Container::const_iterator _it)
1064 ValueIterator& operator++() { ++it; return *this; }
1065 ValueIterator operator++(int) {
1066 ValueIterator tmp(*this);
1071 const Value& operator*() const { return it->first; }
1072 const Value* operator->() const { return &(it->first); }
1074 bool operator==(ValueIterator jt) const { return it == jt.it; }
1075 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1078 typename Container::const_iterator it;
1081 /// \brief Returns an iterator to the first value.
1083 /// Returns an stl compatible iterator to the
1084 /// first value of the map. The values of the
1085 /// map can be accessed in the [beginValue, endValue)
1087 ValueIterator beginValue() const {
1088 return ValueIterator(invMap.begin());
1091 /// \brief Returns an iterator after the last value.
1093 /// Returns an stl compatible iterator after the
1094 /// last value of the map. The values of the
1095 /// map can be accessed in the [beginValue, endValue)
1097 ValueIterator endValue() const {
1098 return ValueIterator(invMap.end());
1101 /// \brief The setter function of the map.
1103 /// Sets the mapped value.
1104 void set(const Key& key, const Value& val) {
1105 Value oldval = Map::operator[](key);
1106 typename Container::iterator it = invMap.find(oldval);
1107 if (it != invMap.end() && it->second == key) {
1110 invMap.insert(make_pair(val, key));
1114 /// \brief The getter function of the map.
1116 /// It gives back the value associated with the key.
1117 typename MapTraits<Map>::ConstReturnValue
1118 operator[](const Key& key) const {
1119 return Map::operator[](key);
1124 /// \brief Erase the key from the map.
1126 /// Erase the key to the map. It is called by the
1127 /// \c AlterationNotifier.
1128 virtual void erase(const Key& key) {
1129 Value val = Map::operator[](key);
1130 typename Container::iterator it = invMap.find(val);
1131 if (it != invMap.end() && it->second == key) {
1137 /// \brief Erase more keys from the map.
1139 /// Erase more keys from the map. It is called by the
1140 /// \c AlterationNotifier.
1141 virtual void erase(const std::vector<Key>& keys) {
1142 for (int i = 0; i < (int)keys.size(); ++i) {
1143 Value val = Map::operator[](keys[i]);
1144 typename Container::iterator it = invMap.find(val);
1145 if (it != invMap.end() && it->second == keys[i]) {
1152 /// \brief Clear the keys from the map and inverse map.
1154 /// Clear the keys from the map and inverse map. It is called by the
1155 /// \c AlterationNotifier.
1156 virtual void clear() {
1163 /// \brief The inverse map type.
1165 /// The inverse of this map. The subscript operator of the map
1166 /// gives back always the item what was last assigned to the value.
1169 /// \brief Constructor of the InverseMap.
1171 /// Constructor of the InverseMap.
1172 InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
1174 /// The value type of the InverseMap.
1175 typedef typename InvertableMap::Key Value;
1176 /// The key type of the InverseMap.
1177 typedef typename InvertableMap::Value Key;
1179 /// \brief Subscript operator.
1181 /// Subscript operator. It gives back always the item
1182 /// what was last assigned to the value.
1183 Value operator[](const Key& key) const {
1184 typename Container::const_iterator it = inverted.invMap.find(key);
1189 const InvertableMap& inverted;
1192 /// \brief It gives back the just readeable inverse map.
1194 /// It gives back the just readeable inverse map.
1195 InverseMap inverse() const {
1196 return InverseMap(*this);
1203 /// \brief Provides a mutable, continuous and unique descriptor for each
1204 /// item in the graph.
1206 /// The DescriptorMap class provides a unique and continuous (but mutable)
1207 /// descriptor (id) for each item of the same type (e.g. node) in the
1208 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1209 /// different ids <li>\b continuous: the range of the ids is the set of
1210 /// integers between 0 and \c n-1, where \c n is the number of the items of
1211 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1212 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1213 /// with its member class \c InverseMap.
1215 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1216 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
1219 /// \param _Map A ReadWriteMap mapping from the item type to integer.
1221 typename _Graph, typename _Item,
1222 typename _Map = DefaultMap<_Graph, _Item, int>
1225 template <typename _Graph, typename _Item>
1227 class DescriptorMap : protected _Map {
1233 /// The graph class of DescriptorMap.
1234 typedef _Graph Graph;
1236 /// The key type of DescriptorMap (Node, Edge, UEdge).
1237 typedef typename _Map::Key Key;
1238 /// The value type of DescriptorMap.
1239 typedef typename _Map::Value Value;
1241 /// \brief Constructor.
1243 /// Constructor for descriptor map.
1244 DescriptorMap(const Graph& _graph) : Map(_graph) {
1250 /// \brief Add a new key to the map.
1252 /// Add a new key to the map. It is called by the
1253 /// \c AlterationNotifier.
1254 virtual void add(const Item& item) {
1256 Map::set(item, invMap.size());
1257 invMap.push_back(item);
1260 /// \brief Add more new keys to the map.
1262 /// Add more new keys to the map. It is called by the
1263 /// \c AlterationNotifier.
1264 virtual void add(const std::vector<Item>& items) {
1266 for (int i = 0; i < (int)items.size(); ++i) {
1267 Map::set(items[i], invMap.size());
1268 invMap.push_back(items[i]);
1272 /// \brief Erase the key from the map.
1274 /// Erase the key from the map. It is called by the
1275 /// \c AlterationNotifier.
1276 virtual void erase(const Item& item) {
1277 Map::set(invMap.back(), Map::operator[](item));
1278 invMap[Map::operator[](item)] = invMap.back();
1283 /// \brief Erase more keys from the map.
1285 /// Erase more keys from the map. It is called by the
1286 /// \c AlterationNotifier.
1287 virtual void erase(const std::vector<Item>& items) {
1288 for (int i = 0; i < (int)items.size(); ++i) {
1289 Map::set(invMap.back(), Map::operator[](items[i]));
1290 invMap[Map::operator[](items[i])] = invMap.back();
1296 /// \brief Build the unique map.
1298 /// Build the unique map. It is called by the
1299 /// \c AlterationNotifier.
1300 virtual void build() {
1303 const typename Map::Notifier* notifier = Map::getNotifier();
1304 for (notifier->first(it); it != INVALID; notifier->next(it)) {
1305 Map::set(it, invMap.size());
1306 invMap.push_back(it);
1310 /// \brief Clear the keys from the map.
1312 /// Clear the keys from the map. It is called by the
1313 /// \c AlterationNotifier.
1314 virtual void clear() {
1321 /// \brief Returns the maximal value plus one.
1323 /// Returns the maximal value plus one in the map.
1324 unsigned int size() const {
1325 return invMap.size();
1328 /// \brief Swaps the position of the two items in the map.
1330 /// Swaps the position of the two items in the map.
1331 void swap(const Item& p, const Item& q) {
1332 int pi = Map::operator[](p);
1333 int qi = Map::operator[](q);
1340 /// \brief Gives back the \e descriptor of the item.
1342 /// Gives back the mutable and unique \e descriptor of the map.
1343 int operator[](const Item& item) const {
1344 return Map::operator[](item);
1349 typedef std::vector<Item> Container;
1353 /// \brief The inverse map type of DescriptorMap.
1355 /// The inverse map type of DescriptorMap.
1358 /// \brief Constructor of the InverseMap.
1360 /// Constructor of the InverseMap.
1361 InverseMap(const DescriptorMap& _inverted)
1362 : inverted(_inverted) {}
1365 /// The value type of the InverseMap.
1366 typedef typename DescriptorMap::Key Value;
1367 /// The key type of the InverseMap.
1368 typedef typename DescriptorMap::Value Key;
1370 /// \brief Subscript operator.
1372 /// Subscript operator. It gives back the item
1373 /// that the descriptor belongs to currently.
1374 Value operator[](const Key& key) const {
1375 return inverted.invMap[key];
1378 /// \brief Size of the map.
1380 /// Returns the size of the map.
1381 unsigned int size() const {
1382 return inverted.invMap.size();
1386 const DescriptorMap& inverted;
1389 /// \brief Gives back the inverse of the map.
1391 /// Gives back the inverse of the map.
1392 const InverseMap inverse() const {
1393 return InverseMap(*this);
1397 /// \brief Returns the source of the given edge.
1399 /// The SourceMap gives back the source Node of the given edge.
1400 /// \author Balazs Dezso
1401 template <typename Graph>
1405 typedef typename Graph::Node Value;
1406 typedef typename Graph::Edge Key;
1408 /// \brief Constructor
1411 /// \param _graph The graph that the map belongs to.
1412 SourceMap(const Graph& _graph) : graph(_graph) {}
1414 /// \brief The subscript operator.
1416 /// The subscript operator.
1417 /// \param edge The edge
1418 /// \return The source of the edge
1419 Value operator[](const Key& edge) const {
1420 return graph.source(edge);
1427 /// \brief Returns a \ref SourceMap class
1429 /// This function just returns an \ref SourceMap class.
1430 /// \relates SourceMap
1431 template <typename Graph>
1432 inline SourceMap<Graph> sourceMap(const Graph& graph) {
1433 return SourceMap<Graph>(graph);
1436 /// \brief Returns the target of the given edge.
1438 /// The TargetMap gives back the target Node of the given edge.
1439 /// \author Balazs Dezso
1440 template <typename Graph>
1444 typedef typename Graph::Node Value;
1445 typedef typename Graph::Edge Key;
1447 /// \brief Constructor
1450 /// \param _graph The graph that the map belongs to.
1451 TargetMap(const Graph& _graph) : graph(_graph) {}
1453 /// \brief The subscript operator.
1455 /// The subscript operator.
1456 /// \param e The edge
1457 /// \return The target of the edge
1458 Value operator[](const Key& e) const {
1459 return graph.target(e);
1466 /// \brief Returns a \ref TargetMap class
1468 /// This function just returns a \ref TargetMap class.
1469 /// \relates TargetMap
1470 template <typename Graph>
1471 inline TargetMap<Graph> targetMap(const Graph& graph) {
1472 return TargetMap<Graph>(graph);
1475 /// \brief Returns the "forward" directed edge view of an undirected edge.
1477 /// Returns the "forward" directed edge view of an undirected edge.
1478 /// \author Balazs Dezso
1479 template <typename Graph>
1483 typedef typename Graph::Edge Value;
1484 typedef typename Graph::UEdge Key;
1486 /// \brief Constructor
1489 /// \param _graph The graph that the map belongs to.
1490 ForwardMap(const Graph& _graph) : graph(_graph) {}
1492 /// \brief The subscript operator.
1494 /// The subscript operator.
1495 /// \param key An undirected edge
1496 /// \return The "forward" directed edge view of undirected edge
1497 Value operator[](const Key& key) const {
1498 return graph.direct(key, true);
1505 /// \brief Returns a \ref ForwardMap class
1507 /// This function just returns an \ref ForwardMap class.
1508 /// \relates ForwardMap
1509 template <typename Graph>
1510 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
1511 return ForwardMap<Graph>(graph);
1514 /// \brief Returns the "backward" directed edge view of an undirected edge.
1516 /// Returns the "backward" directed edge view of an undirected edge.
1517 /// \author Balazs Dezso
1518 template <typename Graph>
1522 typedef typename Graph::Edge Value;
1523 typedef typename Graph::UEdge Key;
1525 /// \brief Constructor
1528 /// \param _graph The graph that the map belongs to.
1529 BackwardMap(const Graph& _graph) : graph(_graph) {}
1531 /// \brief The subscript operator.
1533 /// The subscript operator.
1534 /// \param key An undirected edge
1535 /// \return The "backward" directed edge view of undirected edge
1536 Value operator[](const Key& key) const {
1537 return graph.direct(key, false);
1544 /// \brief Returns a \ref BackwardMap class
1546 /// This function just returns a \ref BackwardMap class.
1547 /// \relates BackwardMap
1548 template <typename Graph>
1549 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
1550 return BackwardMap<Graph>(graph);
1553 /// \brief Potential difference map
1555 /// If there is an potential map on the nodes then we
1556 /// can get an edge map as we get the substraction of the
1557 /// values of the target and source.
1558 template <typename Graph, typename NodeMap>
1559 class PotentialDifferenceMap {
1561 typedef typename Graph::Edge Key;
1562 typedef typename NodeMap::Value Value;
1564 /// \brief Constructor
1566 /// Contructor of the map
1567 PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential)
1568 : graph(_graph), potential(_potential) {}
1570 /// \brief Const subscription operator
1572 /// Const subscription operator
1573 Value operator[](const Key& edge) const {
1574 return potential[graph.target(edge)] - potential[graph.source(edge)];
1579 const NodeMap& potential;
1582 /// \brief Just returns a PotentialDifferenceMap
1584 /// Just returns a PotentialDifferenceMap
1585 /// \relates PotentialDifferenceMap
1586 template <typename Graph, typename NodeMap>
1587 PotentialDifferenceMap<Graph, NodeMap>
1588 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
1589 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
1592 /// \brief Map of the node in-degrees.
1594 /// This map returns the in-degree of a node. Once it is constructed,
1595 /// the degrees are stored in a standard NodeMap, so each query is done
1596 /// in constant time. On the other hand, the values are updated automatically
1597 /// whenever the graph changes.
1599 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1600 /// alternative ways to modify the graph. The correct behavior of InDegMap
1601 /// is not guarantied if these additional features are used. For example
1602 /// the functions \ref ListGraph::changeSource() "changeSource()",
1603 /// \ref ListGraph::changeTarget() "changeTarget()" and
1604 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1605 /// of \ref ListGraph will \e not update the degree values correctly.
1609 template <typename _Graph>
1611 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
1612 ::ItemNotifier::ObserverBase {
1616 typedef _Graph Graph;
1618 typedef typename Graph::Node Key;
1620 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
1621 ::ItemNotifier::ObserverBase Parent;
1625 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
1628 typedef DefaultMap<_Graph, Key, int> Parent;
1629 typedef typename Parent::Graph Graph;
1631 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1633 virtual void add(const Key& key) {
1635 Parent::set(key, 0);
1638 virtual void add(const std::vector<Key>& keys) {
1640 for (int i = 0; i < (int)keys.size(); ++i) {
1641 Parent::set(keys[i], 0);
1648 /// \brief Constructor.
1650 /// Constructor for creating in-degree map.
1651 InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1652 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
1654 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1655 deg[it] = countInEdges(graph, it);
1659 /// Gives back the in-degree of a Node.
1660 int operator[](const Key& key) const {
1666 typedef typename Graph::Edge Edge;
1668 virtual void add(const Edge& edge) {
1669 ++deg[graph.target(edge)];
1672 virtual void add(const std::vector<Edge>& edges) {
1673 for (int i = 0; i < (int)edges.size(); ++i) {
1674 ++deg[graph.target(edges[i])];
1678 virtual void erase(const Edge& edge) {
1679 --deg[graph.target(edge)];
1682 virtual void erase(const std::vector<Edge>& edges) {
1683 for (int i = 0; i < (int)edges.size(); ++i) {
1684 --deg[graph.target(edges[i])];
1688 virtual void build() {
1689 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1690 deg[it] = countInEdges(graph, it);
1694 virtual void clear() {
1695 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1701 const _Graph& graph;
1705 /// \brief Map of the node out-degrees.
1707 /// This map returns the out-degree of a node. Once it is constructed,
1708 /// the degrees are stored in a standard NodeMap, so each query is done
1709 /// in constant time. On the other hand, the values are updated automatically
1710 /// whenever the graph changes.
1712 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1713 /// alternative ways to modify the graph. The correct behavior of OutDegMap
1714 /// is not guarantied if these additional features are used. For example
1715 /// the functions \ref ListGraph::changeSource() "changeSource()",
1716 /// \ref ListGraph::changeTarget() "changeTarget()" and
1717 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1718 /// of \ref ListGraph will \e not update the degree values correctly.
1722 template <typename _Graph>
1724 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
1725 ::ItemNotifier::ObserverBase {
1729 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
1730 ::ItemNotifier::ObserverBase Parent;
1732 typedef _Graph Graph;
1734 typedef typename Graph::Node Key;
1738 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
1741 typedef DefaultMap<_Graph, Key, int> Parent;
1742 typedef typename Parent::Graph Graph;
1744 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1746 virtual void add(const Key& key) {
1748 Parent::set(key, 0);
1750 virtual void add(const std::vector<Key>& keys) {
1752 for (int i = 0; i < (int)keys.size(); ++i) {
1753 Parent::set(keys[i], 0);
1760 /// \brief Constructor.
1762 /// Constructor for creating out-degree map.
1763 OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1764 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
1766 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1767 deg[it] = countOutEdges(graph, it);
1771 /// Gives back the out-degree of a Node.
1772 int operator[](const Key& key) const {
1778 typedef typename Graph::Edge Edge;
1780 virtual void add(const Edge& edge) {
1781 ++deg[graph.source(edge)];
1784 virtual void add(const std::vector<Edge>& edges) {
1785 for (int i = 0; i < (int)edges.size(); ++i) {
1786 ++deg[graph.source(edges[i])];
1790 virtual void erase(const Edge& edge) {
1791 --deg[graph.source(edge)];
1794 virtual void erase(const std::vector<Edge>& edges) {
1795 for (int i = 0; i < (int)edges.size(); ++i) {
1796 --deg[graph.source(edges[i])];
1800 virtual void build() {
1801 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1802 deg[it] = countOutEdges(graph, it);
1806 virtual void clear() {
1807 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1813 const _Graph& graph;
1820 } //END OF NAMESPACE LEMON