Interface to the cplex MIP solver: it is little, a bit sour but it is ours.
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,
52 ///\note If \c G it a template parameter, it should be used in this way.
54 /// GRAPH_TYPEDEFS(typename G)
57 ///\warning There are no typedefs for the graph maps because of the lack of
58 ///template typedefs in C++.
59 #define GRAPH_TYPEDEFS(Graph) \
60 typedef Graph:: Node Node; \
61 typedef Graph:: NodeIt NodeIt; \
62 typedef Graph:: Edge Edge; \
63 typedef Graph:: EdgeIt EdgeIt; \
64 typedef Graph:: InEdgeIt InEdgeIt; \
65 typedef Graph::OutEdgeIt OutEdgeIt;
67 ///Creates convenience typedefs for the undirected graph types and iterators
69 ///This \c \#define creates the same convenience typedefs as defined by
70 ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
71 ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
73 ///\note If \c G it a template parameter, it should be used in this way.
75 /// UGRAPH_TYPEDEFS(typename G)
78 ///\warning There are no typedefs for the graph maps because of the lack of
79 ///template typedefs in C++.
80 #define UGRAPH_TYPEDEFS(Graph) \
81 GRAPH_TYPEDEFS(Graph) \
82 typedef Graph:: UEdge UEdge; \
83 typedef Graph:: UEdgeIt UEdgeIt; \
84 typedef Graph:: IncEdgeIt IncEdgeIt;
85 // typedef Graph::template UEdgeMap<bool> BoolUEdgeMap;
86 // typedef Graph::template UEdgeMap<int> IntUEdgeMap;
87 // typedef Graph::template UEdgeMap<double> DoubleUEdgeMap;
89 ///\brief Creates convenience typedefs for the bipartite undirected graph
90 ///types and iterators
92 ///This \c \#define creates the same convenience typedefs as defined by
93 ///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates
94 ///\c ANodeIt, \c BNodeIt,
96 ///\note If \c G it a template parameter, it should be used in this way.
98 /// BPUGRAPH_TYPEDEFS(typename G)
101 ///\warning There are no typedefs for the graph maps because of the lack of
102 ///template typedefs in C++.
103 #define BPUGRAPH_TYPEDEFS(Graph) \
104 UGRAPH_TYPEDEFS(Graph) \
105 typedef Graph::ANodeIt ANodeIt; \
106 typedef Graph::BNodeIt BNodeIt;
108 /// \brief Function to count the items in the graph.
110 /// This function counts the items (nodes, edges etc) in the graph.
111 /// The complexity of the function is O(n) because
112 /// it iterates on all of the items.
114 template <typename Graph, typename Item>
115 inline int countItems(const Graph& g) {
116 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
118 for (ItemIt it(g); it != INVALID; ++it) {
126 namespace _graph_utils_bits {
128 template <typename Graph, typename Enable = void>
129 struct CountNodesSelector {
130 static int count(const Graph &g) {
131 return countItems<Graph, typename Graph::Node>(g);
135 template <typename Graph>
136 struct CountNodesSelector<
138 enable_if<typename Graph::NodeNumTag, void>::type>
140 static int count(const Graph &g) {
146 /// \brief Function to count the nodes in the graph.
148 /// This function counts the nodes in the graph.
149 /// The complexity of the function is O(n) but for some
150 /// graph structures it is specialized to run in O(1).
152 /// \todo refer how to specialize it
154 template <typename Graph>
155 inline int countNodes(const Graph& g) {
156 return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
159 namespace _graph_utils_bits {
161 template <typename Graph, typename Enable = void>
162 struct CountANodesSelector {
163 static int count(const Graph &g) {
164 return countItems<Graph, typename Graph::ANode>(g);
168 template <typename Graph>
169 struct CountANodesSelector<
171 enable_if<typename Graph::NodeNumTag, void>::type>
173 static int count(const Graph &g) {
179 /// \brief Function to count the anodes in the graph.
181 /// This function counts the anodes in the graph.
182 /// The complexity of the function is O(an) but for some
183 /// graph structures it is specialized to run in O(1).
185 /// \todo refer how to specialize it
187 template <typename Graph>
188 inline int countANodes(const Graph& g) {
189 return _graph_utils_bits::CountANodesSelector<Graph>::count(g);
192 namespace _graph_utils_bits {
194 template <typename Graph, typename Enable = void>
195 struct CountBNodesSelector {
196 static int count(const Graph &g) {
197 return countItems<Graph, typename Graph::BNode>(g);
201 template <typename Graph>
202 struct CountBNodesSelector<
204 enable_if<typename Graph::NodeNumTag, void>::type>
206 static int count(const Graph &g) {
212 /// \brief Function to count the bnodes in the graph.
214 /// This function counts the bnodes in the graph.
215 /// The complexity of the function is O(bn) but for some
216 /// graph structures it is specialized to run in O(1).
218 /// \todo refer how to specialize it
220 template <typename Graph>
221 inline int countBNodes(const Graph& g) {
222 return _graph_utils_bits::CountBNodesSelector<Graph>::count(g);
228 namespace _graph_utils_bits {
230 template <typename Graph, typename Enable = void>
231 struct CountEdgesSelector {
232 static int count(const Graph &g) {
233 return countItems<Graph, typename Graph::Edge>(g);
237 template <typename Graph>
238 struct CountEdgesSelector<
240 typename enable_if<typename Graph::EdgeNumTag, void>::type>
242 static int count(const Graph &g) {
248 /// \brief Function to count the edges in the graph.
250 /// This function counts the edges in the graph.
251 /// The complexity of the function is O(e) but for some
252 /// graph structures it is specialized to run in O(1).
254 template <typename Graph>
255 inline int countEdges(const Graph& g) {
256 return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
259 // Undirected edge counting:
260 namespace _graph_utils_bits {
262 template <typename Graph, typename Enable = void>
263 struct CountUEdgesSelector {
264 static int count(const Graph &g) {
265 return countItems<Graph, typename Graph::UEdge>(g);
269 template <typename Graph>
270 struct CountUEdgesSelector<
272 typename enable_if<typename Graph::EdgeNumTag, void>::type>
274 static int count(const Graph &g) {
280 /// \brief Function to count the undirected edges in the graph.
282 /// This function counts the undirected edges in the graph.
283 /// The complexity of the function is O(e) but for some
284 /// graph structures it is specialized to run in O(1).
286 template <typename Graph>
287 inline int countUEdges(const Graph& g) {
288 return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
293 template <typename Graph, typename DegIt>
294 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
296 for (DegIt it(_g, _n); it != INVALID; ++it) {
302 /// \brief Function to count the number of the out-edges from node \c n.
304 /// This function counts the number of the out-edges from node \c n
306 template <typename Graph>
307 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
308 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
311 /// \brief Function to count the number of the in-edges to node \c n.
313 /// This function counts the number of the in-edges to node \c n
315 template <typename Graph>
316 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
317 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
320 /// \brief Function to count the number of the inc-edges to node \c n.
322 /// This function counts the number of the inc-edges to node \c n
324 template <typename Graph>
325 inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
326 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
329 namespace _graph_utils_bits {
331 template <typename Graph, typename Enable = void>
332 struct FindEdgeSelector {
333 typedef typename Graph::Node Node;
334 typedef typename Graph::Edge Edge;
335 static Edge find(const Graph &g, Node u, Node v, Edge e) {
341 while (e != INVALID && g.target(e) != v) {
348 template <typename Graph>
349 struct FindEdgeSelector<
351 typename enable_if<typename Graph::FindEdgeTag, void>::type>
353 typedef typename Graph::Node Node;
354 typedef typename Graph::Edge Edge;
355 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
356 return g.findEdge(u, v, prev);
361 /// \brief Finds an edge between two nodes of a graph.
363 /// Finds an edge from node \c u to node \c v in graph \c g.
365 /// If \c prev is \ref INVALID (this is the default value), then
366 /// it finds the first edge from \c u to \c v. Otherwise it looks for
367 /// the next edge from \c u to \c v after \c prev.
368 /// \return The found edge or \ref INVALID if there is no such an edge.
370 /// Thus you can iterate through each edge from \c u to \c v as it follows.
372 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
378 template <typename Graph>
379 inline typename Graph::Edge findEdge(const Graph &g,
380 typename Graph::Node u,
381 typename Graph::Node v,
382 typename Graph::Edge prev = INVALID) {
383 return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
386 /// \brief Iterator for iterating on edges connected the same nodes.
388 /// Iterator for iterating on edges connected the same nodes. It is
389 /// higher level interface for the findEdge() function. You can
390 /// use it the following way:
392 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
399 /// \author Balazs Dezso
400 template <typename _Graph>
401 class ConEdgeIt : public _Graph::Edge {
404 typedef _Graph Graph;
405 typedef typename Graph::Edge Parent;
407 typedef typename Graph::Edge Edge;
408 typedef typename Graph::Node Node;
410 /// \brief Constructor.
412 /// Construct a new ConEdgeIt iterating on the edges which
413 /// connects the \c u and \c v node.
414 ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
415 Parent::operator=(findEdge(graph, u, v));
418 /// \brief Constructor.
420 /// Construct a new ConEdgeIt which continues the iterating from
422 ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
424 /// \brief Increment operator.
426 /// It increments the iterator and gives back the next edge.
427 ConEdgeIt& operator++() {
428 Parent::operator=(findEdge(graph, graph.source(*this),
429 graph.target(*this), *this));
436 namespace _graph_utils_bits {
438 template <typename Graph, typename Enable = void>
439 struct FindUEdgeSelector {
440 typedef typename Graph::Node Node;
441 typedef typename Graph::UEdge UEdge;
442 static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
448 b = g.source(e) == u;
451 while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
461 while (e != INVALID && (!b || g.target(e) != v)) {
469 template <typename Graph>
470 struct FindUEdgeSelector<
472 typename enable_if<typename Graph::FindEdgeTag, void>::type>
474 typedef typename Graph::Node Node;
475 typedef typename Graph::UEdge UEdge;
476 static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
477 return g.findUEdge(u, v, prev);
482 /// \brief Finds an uedge between two nodes of a graph.
484 /// Finds an uedge from node \c u to node \c v in graph \c g.
485 /// If the node \c u and node \c v is equal then each loop edge
486 /// will be enumerated.
488 /// If \c prev is \ref INVALID (this is the default value), then
489 /// it finds the first edge from \c u to \c v. Otherwise it looks for
490 /// the next edge from \c u to \c v after \c prev.
491 /// \return The found edge or \ref INVALID if there is no such an edge.
493 /// Thus you can iterate through each edge from \c u to \c v as it follows.
495 /// for(UEdge e = findUEdge(g,u,v); e != INVALID;
496 /// e = findUEdge(g,u,v,e)) {
503 template <typename Graph>
504 inline typename Graph::UEdge findUEdge(const Graph &g,
505 typename Graph::Node u,
506 typename Graph::Node v,
507 typename Graph::UEdge p = INVALID) {
508 return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
511 /// \brief Iterator for iterating on uedges connected the same nodes.
513 /// Iterator for iterating on uedges connected the same nodes. It is
514 /// higher level interface for the findUEdge() function. You can
515 /// use it the following way:
517 /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
524 /// \author Balazs Dezso
525 template <typename _Graph>
526 class ConUEdgeIt : public _Graph::UEdge {
529 typedef _Graph Graph;
530 typedef typename Graph::UEdge Parent;
532 typedef typename Graph::UEdge UEdge;
533 typedef typename Graph::Node Node;
535 /// \brief Constructor.
537 /// Construct a new ConUEdgeIt iterating on the edges which
538 /// connects the \c u and \c v node.
539 ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
540 Parent::operator=(findUEdge(graph, u, v));
543 /// \brief Constructor.
545 /// Construct a new ConUEdgeIt which continues the iterating from
547 ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
549 /// \brief Increment operator.
551 /// It increments the iterator and gives back the next edge.
552 ConUEdgeIt& operator++() {
553 Parent::operator=(findUEdge(graph, graph.source(*this),
554 graph.target(*this), *this));
561 /// \brief Copy a map.
563 /// This function copies the \c source map to the \c target map. It uses the
564 /// given iterator to iterate on the data structure and it uses the \c ref
565 /// mapping to convert the source's keys to the target's keys.
566 template <typename Target, typename Source,
567 typename ItemIt, typename Ref>
568 void copyMap(Target& target, const Source& source,
569 ItemIt it, const Ref& ref) {
570 for (; it != INVALID; ++it) {
571 target[ref[it]] = source[it];
575 /// \brief Copy the source map to the target map.
577 /// Copy the \c source map to the \c target map. It uses the given iterator
578 /// to iterate on the data structure.
579 template <typename Target, typename Source, typename ItemIt>
580 void copyMap(Target& target, const Source& source, ItemIt it) {
581 for (; it != INVALID; ++it) {
582 target[it] = source[it];
586 /// \brief Class to copy a graph.
588 /// Class to copy a graph to another graph (duplicate a graph). The
589 /// simplest way of using it is through the \c copyGraph() function.
590 template <typename Target, typename Source>
593 typedef typename Source::Node Node;
594 typedef typename Source::NodeIt NodeIt;
595 typedef typename Source::Edge Edge;
596 typedef typename Source::EdgeIt EdgeIt;
598 typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
599 typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
601 /// \brief Constructor for the GraphCopy.
603 /// It copies the content of the \c _source graph into the
604 /// \c _target graph. It creates also two references, one beetween
605 /// the two nodeset and one beetween the two edgesets.
606 GraphCopy(Target& _target, const Source& _source)
607 : source(_source), target(_target),
608 nodeRefMap(_source), edgeRefMap(_source) {
609 for (NodeIt it(source); it != INVALID; ++it) {
610 nodeRefMap[it] = target.addNode();
612 for (EdgeIt it(source); it != INVALID; ++it) {
613 edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
614 nodeRefMap[source.target(it)]);
618 /// \brief Copies the node references into the given map.
620 /// Copies the node references into the given map.
621 template <typename NodeRef>
622 const GraphCopy& nodeRef(NodeRef& map) const {
623 for (NodeIt it(source); it != INVALID; ++it) {
624 map.set(it, nodeRefMap[it]);
629 /// \brief Reverse and copies the node references into the given map.
631 /// Reverse and copies the node references into the given map.
632 template <typename NodeRef>
633 const GraphCopy& nodeCrossRef(NodeRef& map) const {
634 for (NodeIt it(source); it != INVALID; ++it) {
635 map.set(nodeRefMap[it], it);
640 /// \brief Copies the edge references into the given map.
642 /// Copies the edge references into the given map.
643 template <typename EdgeRef>
644 const GraphCopy& edgeRef(EdgeRef& map) const {
645 for (EdgeIt it(source); it != INVALID; ++it) {
646 map.set(it, edgeRefMap[it]);
651 /// \brief Reverse and copies the edge references into the given map.
653 /// Reverse and copies the edge references into the given map.
654 template <typename EdgeRef>
655 const GraphCopy& edgeCrossRef(EdgeRef& map) const {
656 for (EdgeIt it(source); it != INVALID; ++it) {
657 map.set(edgeRefMap[it], it);
662 /// \brief Make copy of the given map.
664 /// Makes copy of the given map for the newly created graph.
665 /// The new map's key type is the target graph's node type,
666 /// and the copied map's key type is the source graph's node
668 template <typename TargetMap, typename SourceMap>
669 const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
670 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
674 /// \brief Make copy of the given map.
676 /// Makes copy of the given map for the newly created graph.
677 /// The new map's key type is the target graph's edge type,
678 /// and the copied map's key type is the source graph's edge
680 template <typename TargetMap, typename SourceMap>
681 const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
682 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
686 /// \brief Gives back the stored node references.
688 /// Gives back the stored node references.
689 const NodeRefMap& nodeRef() const {
693 /// \brief Gives back the stored edge references.
695 /// Gives back the stored edge references.
696 const EdgeRefMap& edgeRef() const {
704 const Source& source;
707 NodeRefMap nodeRefMap;
708 EdgeRefMap edgeRefMap;
711 /// \brief Copy a graph to another graph.
713 /// Copy a graph to another graph.
714 /// The usage of the function:
717 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
720 /// After the copy the \c nr map will contain the mapping from the
721 /// source graph's nodes to the target graph's nodes and the \c ecr will
722 /// contain the mapping from the target graph's edges to the source's
724 template <typename Target, typename Source>
725 GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
726 return GraphCopy<Target, Source>(target, source);
729 /// \brief Class to copy an undirected graph.
731 /// Class to copy an undirected graph to another graph (duplicate a graph).
732 /// The simplest way of using it is through the \c copyUGraph() function.
733 template <typename Target, typename Source>
736 typedef typename Source::Node Node;
737 typedef typename Source::NodeIt NodeIt;
738 typedef typename Source::Edge Edge;
739 typedef typename Source::EdgeIt EdgeIt;
740 typedef typename Source::UEdge UEdge;
741 typedef typename Source::UEdgeIt UEdgeIt;
743 typedef typename Source::
744 template NodeMap<typename Target::Node> NodeRefMap;
746 typedef typename Source::
747 template UEdgeMap<typename Target::UEdge> UEdgeRefMap;
752 EdgeRefMap(UGraphCopy& _gc) : gc(_gc) {}
753 typedef typename Source::Edge Key;
754 typedef typename Target::Edge Value;
756 Value operator[](const Key& key) {
757 return gc.target.direct(gc.uEdgeRef[key],
758 gc.target.direction(key));
766 /// \brief Constructor for the UGraphCopy.
768 /// It copies the content of the \c _source graph into the
769 /// \c _target graph. It creates also two references, one beetween
770 /// the two nodeset and one beetween the two edgesets.
771 UGraphCopy(Target& _target, const Source& _source)
772 : source(_source), target(_target),
773 nodeRefMap(_source), edgeRefMap(*this), uEdgeRefMap(_source) {
774 for (NodeIt it(source); it != INVALID; ++it) {
775 nodeRefMap[it] = target.addNode();
777 for (UEdgeIt it(source); it != INVALID; ++it) {
778 uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
779 nodeRefMap[source.target(it)]);
783 /// \brief Copies the node references into the given map.
785 /// Copies the node references into the given map.
786 template <typename NodeRef>
787 const UGraphCopy& nodeRef(NodeRef& map) const {
788 for (NodeIt it(source); it != INVALID; ++it) {
789 map.set(it, nodeRefMap[it]);
794 /// \brief Reverse and copies the node references into the given map.
796 /// Reverse and copies the node references into the given map.
797 template <typename NodeRef>
798 const UGraphCopy& nodeCrossRef(NodeRef& map) const {
799 for (NodeIt it(source); it != INVALID; ++it) {
800 map.set(nodeRefMap[it], it);
805 /// \brief Copies the edge references into the given map.
807 /// Copies the edge references into the given map.
808 template <typename EdgeRef>
809 const UGraphCopy& edgeRef(EdgeRef& map) const {
810 for (EdgeIt it(source); it != INVALID; ++it) {
811 map.set(edgeRefMap[it], it);
816 /// \brief Reverse and copies the undirected edge references into the
819 /// Reverse and copies the undirected edge references into the given map.
820 template <typename EdgeRef>
821 const UGraphCopy& edgeCrossRef(EdgeRef& map) const {
822 for (EdgeIt it(source); it != INVALID; ++it) {
823 map.set(it, edgeRefMap[it]);
828 /// \brief Copies the undirected edge references into the given map.
830 /// Copies the undirected edge references into the given map.
831 template <typename EdgeRef>
832 const UGraphCopy& uEdgeRef(EdgeRef& map) const {
833 for (UEdgeIt it(source); it != INVALID; ++it) {
834 map.set(it, uEdgeRefMap[it]);
839 /// \brief Reverse and copies the undirected edge references into the
842 /// Reverse and copies the undirected edge references into the given map.
843 template <typename EdgeRef>
844 const UGraphCopy& uEdgeCrossRef(EdgeRef& map) const {
845 for (UEdgeIt it(source); it != INVALID; ++it) {
846 map.set(uEdgeRefMap[it], it);
851 /// \brief Make copy of the given map.
853 /// Makes copy of the given map for the newly created graph.
854 /// The new map's key type is the target graph's node type,
855 /// and the copied map's key type is the source graph's node
857 template <typename TargetMap, typename SourceMap>
858 const UGraphCopy& nodeMap(TargetMap& tMap,
859 const SourceMap& sMap) const {
860 copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
864 /// \brief Make copy of the given map.
866 /// Makes copy of the given map for the newly created graph.
867 /// The new map's key type is the target graph's edge type,
868 /// and the copied map's key type is the source graph's edge
870 template <typename TargetMap, typename SourceMap>
871 const UGraphCopy& edgeMap(TargetMap& tMap,
872 const SourceMap& sMap) const {
873 copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
877 /// \brief Make copy of the given map.
879 /// Makes copy of the given map for the newly created graph.
880 /// The new map's key type is the target graph's edge type,
881 /// and the copied map's key type is the source graph's edge
883 template <typename TargetMap, typename SourceMap>
884 const UGraphCopy& uEdgeMap(TargetMap& tMap,
885 const SourceMap& sMap) const {
886 copyMap(tMap, sMap, UEdgeIt(source), uEdgeRefMap);
890 /// \brief Gives back the stored node references.
892 /// Gives back the stored node references.
893 const NodeRefMap& nodeRef() const {
897 /// \brief Gives back the stored edge references.
899 /// Gives back the stored edge references.
900 const EdgeRefMap& edgeRef() const {
904 /// \brief Gives back the stored uedge references.
906 /// Gives back the stored uedge references.
907 const UEdgeRefMap& uEdgeRef() const {
915 const Source& source;
918 NodeRefMap nodeRefMap;
919 EdgeRefMap edgeRefMap;
920 UEdgeRefMap uEdgeRefMap;
923 /// \brief Copy a graph to another graph.
925 /// Copy a graph to another graph.
926 /// The usage of the function:
929 /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
932 /// After the copy the \c nr map will contain the mapping from the
933 /// source graph's nodes to the target graph's nodes and the \c ecr will
934 /// contain the mapping from the target graph's edges to the source's
936 template <typename Target, typename Source>
937 UGraphCopy<Target, Source>
938 copyUGraph(Target& target, const Source& source) {
939 return UGraphCopy<Target, Source>(target, source);
945 /// \addtogroup graph_maps
948 /// Provides an immutable and unique id for each item in the graph.
950 /// The IdMap class provides a unique and immutable id for each item of the
951 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
952 /// different items (nodes) get different ids <li>\b immutable: the id of an
953 /// item (node) does not change (even if you delete other nodes). </ul>
954 /// Through this map you get access (i.e. can read) the inner id values of
955 /// the items stored in the graph. This map can be inverted with its member
956 /// class \c InverseMap.
958 template <typename _Graph, typename _Item>
961 typedef _Graph Graph;
966 /// \brief Constructor.
968 /// Constructor for creating id map.
969 IdMap(const Graph& _graph) : graph(&_graph) {}
971 /// \brief Gives back the \e id of the item.
973 /// Gives back the immutable and unique \e id of the map.
974 int operator[](const Item& item) const { return graph->id(item);}
982 /// \brief The class represents the inverse of its owner (IdMap).
984 /// The class represents the inverse of its owner (IdMap).
989 /// \brief Constructor.
991 /// Constructor for creating an id-to-item map.
992 InverseMap(const Graph& _graph) : graph(&_graph) {}
994 /// \brief Constructor.
996 /// Constructor for creating an id-to-item map.
997 InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
999 /// \brief Gives back the given item from its id.
1001 /// Gives back the given item from its id.
1003 Item operator[](int id) const { return graph->fromId(id, Item());}
1008 /// \brief Gives back the inverse of the map.
1010 /// Gives back the inverse of the IdMap.
1011 InverseMap inverse() const { return InverseMap(*graph);}
1016 /// \brief General invertable graph-map type.
1018 /// This type provides simple invertable graph-maps.
1019 /// The InvertableMap wraps an arbitrary ReadWriteMap
1020 /// and if a key is set to a new value then store it
1021 /// in the inverse map.
1023 /// The values of the map can be accessed
1024 /// with stl compatible forward iterator.
1026 /// \param _Graph The graph type.
1027 /// \param _Item The item type of the graph.
1028 /// \param _Value The value type of the map.
1030 /// \see IterableValueMap
1032 /// \param _Map A ReadWriteMap mapping from the item type to integer.
1034 typename _Graph, typename _Item, typename _Value,
1035 typename _Map = DefaultMap<_Graph, _Item, _Value>
1038 template <typename _Graph, typename _Item, typename _Value>
1040 class InvertableMap : protected _Map {
1043 /// The key type of InvertableMap (Node, Edge, UEdge).
1044 typedef typename _Map::Key Key;
1045 /// The value type of the InvertableMap.
1046 typedef typename _Map::Value Value;
1051 typedef _Graph Graph;
1053 typedef std::map<Value, Key> Container;
1060 /// \brief Constructor.
1062 /// Construct a new InvertableMap for the graph.
1064 InvertableMap(const Graph& graph) : Map(graph) {}
1066 /// \brief Forward iterator for values.
1068 /// This iterator is an stl compatible forward
1069 /// iterator on the values of the map. The values can
1070 /// be accessed in the [beginValue, endValue) range.
1073 : public std::iterator<std::forward_iterator_tag, Value> {
1074 friend class InvertableMap;
1076 ValueIterator(typename Container::const_iterator _it)
1082 ValueIterator& operator++() { ++it; return *this; }
1083 ValueIterator operator++(int) {
1084 ValueIterator tmp(*this);
1089 const Value& operator*() const { return it->first; }
1090 const Value* operator->() const { return &(it->first); }
1092 bool operator==(ValueIterator jt) const { return it == jt.it; }
1093 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1096 typename Container::const_iterator it;
1099 /// \brief Returns an iterator to the first value.
1101 /// Returns an stl compatible iterator to the
1102 /// first value of the map. The values of the
1103 /// map can be accessed in the [beginValue, endValue)
1105 ValueIterator beginValue() const {
1106 return ValueIterator(invMap.begin());
1109 /// \brief Returns an iterator after the last value.
1111 /// Returns an stl compatible iterator after the
1112 /// last value of the map. The values of the
1113 /// map can be accessed in the [beginValue, endValue)
1115 ValueIterator endValue() const {
1116 return ValueIterator(invMap.end());
1119 /// \brief The setter function of the map.
1121 /// Sets the mapped value.
1122 void set(const Key& key, const Value& val) {
1123 Value oldval = Map::operator[](key);
1124 typename Container::iterator it = invMap.find(oldval);
1125 if (it != invMap.end() && it->second == key) {
1128 invMap.insert(make_pair(val, key));
1132 /// \brief The getter function of the map.
1134 /// It gives back the value associated with the key.
1135 typename MapTraits<Map>::ConstReturnValue
1136 operator[](const Key& key) const {
1137 return Map::operator[](key);
1142 /// \brief Erase the key from the map.
1144 /// Erase the key to the map. It is called by the
1145 /// \c AlterationNotifier.
1146 virtual void erase(const Key& key) {
1147 Value val = Map::operator[](key);
1148 typename Container::iterator it = invMap.find(val);
1149 if (it != invMap.end() && it->second == key) {
1155 /// \brief Erase more keys from the map.
1157 /// Erase more keys from the map. It is called by the
1158 /// \c AlterationNotifier.
1159 virtual void erase(const std::vector<Key>& keys) {
1160 for (int i = 0; i < (int)keys.size(); ++i) {
1161 Value val = Map::operator[](keys[i]);
1162 typename Container::iterator it = invMap.find(val);
1163 if (it != invMap.end() && it->second == keys[i]) {
1170 /// \brief Clear the keys from the map and inverse map.
1172 /// Clear the keys from the map and inverse map. It is called by the
1173 /// \c AlterationNotifier.
1174 virtual void clear() {
1181 /// \brief The inverse map type.
1183 /// The inverse of this map. The subscript operator of the map
1184 /// gives back always the item what was last assigned to the value.
1187 /// \brief Constructor of the InverseMap.
1189 /// Constructor of the InverseMap.
1190 InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
1192 /// The value type of the InverseMap.
1193 typedef typename InvertableMap::Key Value;
1194 /// The key type of the InverseMap.
1195 typedef typename InvertableMap::Value Key;
1197 /// \brief Subscript operator.
1199 /// Subscript operator. It gives back always the item
1200 /// what was last assigned to the value.
1201 Value operator[](const Key& key) const {
1202 typename Container::const_iterator it = inverted.invMap.find(key);
1207 const InvertableMap& inverted;
1210 /// \brief It gives back the just readable inverse map.
1212 /// It gives back the just readable inverse map.
1213 InverseMap inverse() const {
1214 return InverseMap(*this);
1221 /// \brief Provides a mutable, continuous and unique descriptor for each
1222 /// item in the graph.
1224 /// The DescriptorMap class provides a unique and continuous (but mutable)
1225 /// descriptor (id) for each item of the same type (e.g. node) in the
1226 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1227 /// different ids <li>\b continuous: the range of the ids is the set of
1228 /// integers between 0 and \c n-1, where \c n is the number of the items of
1229 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1230 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1231 /// with its member class \c InverseMap.
1233 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1234 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
1237 /// \param _Map A ReadWriteMap mapping from the item type to integer.
1239 typename _Graph, typename _Item,
1240 typename _Map = DefaultMap<_Graph, _Item, int>
1243 template <typename _Graph, typename _Item>
1245 class DescriptorMap : protected _Map {
1251 /// The graph class of DescriptorMap.
1252 typedef _Graph Graph;
1254 /// The key type of DescriptorMap (Node, Edge, UEdge).
1255 typedef typename _Map::Key Key;
1256 /// The value type of DescriptorMap.
1257 typedef typename _Map::Value Value;
1259 /// \brief Constructor.
1261 /// Constructor for descriptor map.
1262 DescriptorMap(const Graph& _graph) : Map(_graph) {
1264 const typename Map::Notifier* notifier = Map::getNotifier();
1265 for (notifier->first(it); it != INVALID; notifier->next(it)) {
1266 Map::set(it, invMap.size());
1267 invMap.push_back(it);
1273 /// \brief Add a new key to the map.
1275 /// Add a new key to the map. It is called by the
1276 /// \c AlterationNotifier.
1277 virtual void add(const Item& item) {
1279 Map::set(item, invMap.size());
1280 invMap.push_back(item);
1283 /// \brief Add more new keys to the map.
1285 /// Add more new keys to the map. It is called by the
1286 /// \c AlterationNotifier.
1287 virtual void add(const std::vector<Item>& items) {
1289 for (int i = 0; i < (int)items.size(); ++i) {
1290 Map::set(items[i], invMap.size());
1291 invMap.push_back(items[i]);
1295 /// \brief Erase the key from the map.
1297 /// Erase the key from the map. It is called by the
1298 /// \c AlterationNotifier.
1299 virtual void erase(const Item& item) {
1300 Map::set(invMap.back(), Map::operator[](item));
1301 invMap[Map::operator[](item)] = invMap.back();
1306 /// \brief Erase more keys from the map.
1308 /// Erase more keys from the map. It is called by the
1309 /// \c AlterationNotifier.
1310 virtual void erase(const std::vector<Item>& items) {
1311 for (int i = 0; i < (int)items.size(); ++i) {
1312 Map::set(invMap.back(), Map::operator[](items[i]));
1313 invMap[Map::operator[](items[i])] = invMap.back();
1319 /// \brief Build the unique map.
1321 /// Build the unique map. It is called by the
1322 /// \c AlterationNotifier.
1323 virtual void build() {
1326 const typename Map::Notifier* notifier = Map::getNotifier();
1327 for (notifier->first(it); it != INVALID; notifier->next(it)) {
1328 Map::set(it, invMap.size());
1329 invMap.push_back(it);
1333 /// \brief Clear the keys from the map.
1335 /// Clear the keys from the map. It is called by the
1336 /// \c AlterationNotifier.
1337 virtual void clear() {
1344 /// \brief Returns the maximal value plus one.
1346 /// Returns the maximal value plus one in the map.
1347 unsigned int size() const {
1348 return invMap.size();
1351 /// \brief Swaps the position of the two items in the map.
1353 /// Swaps the position of the two items in the map.
1354 void swap(const Item& p, const Item& q) {
1355 int pi = Map::operator[](p);
1356 int qi = Map::operator[](q);
1363 /// \brief Gives back the \e descriptor of the item.
1365 /// Gives back the mutable and unique \e descriptor of the map.
1366 int operator[](const Item& item) const {
1367 return Map::operator[](item);
1372 typedef std::vector<Item> Container;
1376 /// \brief The inverse map type of DescriptorMap.
1378 /// The inverse map type of DescriptorMap.
1381 /// \brief Constructor of the InverseMap.
1383 /// Constructor of the InverseMap.
1384 InverseMap(const DescriptorMap& _inverted)
1385 : inverted(_inverted) {}
1388 /// The value type of the InverseMap.
1389 typedef typename DescriptorMap::Key Value;
1390 /// The key type of the InverseMap.
1391 typedef typename DescriptorMap::Value Key;
1393 /// \brief Subscript operator.
1395 /// Subscript operator. It gives back the item
1396 /// that the descriptor belongs to currently.
1397 Value operator[](const Key& key) const {
1398 return inverted.invMap[key];
1401 /// \brief Size of the map.
1403 /// Returns the size of the map.
1404 unsigned int size() const {
1405 return inverted.invMap.size();
1409 const DescriptorMap& inverted;
1412 /// \brief Gives back the inverse of the map.
1414 /// Gives back the inverse of the map.
1415 const InverseMap inverse() const {
1416 return InverseMap(*this);
1420 /// \brief Returns the source of the given edge.
1422 /// The SourceMap gives back the source Node of the given edge.
1423 /// \author Balazs Dezso
1424 template <typename Graph>
1428 typedef typename Graph::Node Value;
1429 typedef typename Graph::Edge Key;
1431 /// \brief Constructor
1434 /// \param _graph The graph that the map belongs to.
1435 SourceMap(const Graph& _graph) : graph(_graph) {}
1437 /// \brief The subscript operator.
1439 /// The subscript operator.
1440 /// \param edge The edge
1441 /// \return The source of the edge
1442 Value operator[](const Key& edge) const {
1443 return graph.source(edge);
1450 /// \brief Returns a \ref SourceMap class
1452 /// This function just returns an \ref SourceMap class.
1453 /// \relates SourceMap
1454 template <typename Graph>
1455 inline SourceMap<Graph> sourceMap(const Graph& graph) {
1456 return SourceMap<Graph>(graph);
1459 /// \brief Returns the target of the given edge.
1461 /// The TargetMap gives back the target Node of the given edge.
1462 /// \author Balazs Dezso
1463 template <typename Graph>
1467 typedef typename Graph::Node Value;
1468 typedef typename Graph::Edge Key;
1470 /// \brief Constructor
1473 /// \param _graph The graph that the map belongs to.
1474 TargetMap(const Graph& _graph) : graph(_graph) {}
1476 /// \brief The subscript operator.
1478 /// The subscript operator.
1479 /// \param e The edge
1480 /// \return The target of the edge
1481 Value operator[](const Key& e) const {
1482 return graph.target(e);
1489 /// \brief Returns a \ref TargetMap class
1491 /// This function just returns a \ref TargetMap class.
1492 /// \relates TargetMap
1493 template <typename Graph>
1494 inline TargetMap<Graph> targetMap(const Graph& graph) {
1495 return TargetMap<Graph>(graph);
1498 /// \brief Returns the "forward" directed edge view of an undirected edge.
1500 /// Returns the "forward" directed edge view of an undirected edge.
1501 /// \author Balazs Dezso
1502 template <typename Graph>
1506 typedef typename Graph::Edge Value;
1507 typedef typename Graph::UEdge Key;
1509 /// \brief Constructor
1512 /// \param _graph The graph that the map belongs to.
1513 ForwardMap(const Graph& _graph) : graph(_graph) {}
1515 /// \brief The subscript operator.
1517 /// The subscript operator.
1518 /// \param key An undirected edge
1519 /// \return The "forward" directed edge view of undirected edge
1520 Value operator[](const Key& key) const {
1521 return graph.direct(key, true);
1528 /// \brief Returns a \ref ForwardMap class
1530 /// This function just returns an \ref ForwardMap class.
1531 /// \relates ForwardMap
1532 template <typename Graph>
1533 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
1534 return ForwardMap<Graph>(graph);
1537 /// \brief Returns the "backward" directed edge view of an undirected edge.
1539 /// Returns the "backward" directed edge view of an undirected edge.
1540 /// \author Balazs Dezso
1541 template <typename Graph>
1545 typedef typename Graph::Edge Value;
1546 typedef typename Graph::UEdge Key;
1548 /// \brief Constructor
1551 /// \param _graph The graph that the map belongs to.
1552 BackwardMap(const Graph& _graph) : graph(_graph) {}
1554 /// \brief The subscript operator.
1556 /// The subscript operator.
1557 /// \param key An undirected edge
1558 /// \return The "backward" directed edge view of undirected edge
1559 Value operator[](const Key& key) const {
1560 return graph.direct(key, false);
1567 /// \brief Returns a \ref BackwardMap class
1569 /// This function just returns a \ref BackwardMap class.
1570 /// \relates BackwardMap
1571 template <typename Graph>
1572 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
1573 return BackwardMap<Graph>(graph);
1576 /// \brief Potential difference map
1578 /// If there is an potential map on the nodes then we
1579 /// can get an edge map as we get the substraction of the
1580 /// values of the target and source.
1581 template <typename Graph, typename NodeMap>
1582 class PotentialDifferenceMap {
1584 typedef typename Graph::Edge Key;
1585 typedef typename NodeMap::Value Value;
1587 /// \brief Constructor
1589 /// Contructor of the map
1590 PotentialDifferenceMap(const Graph& _graph, const NodeMap& _potential)
1591 : graph(_graph), potential(_potential) {}
1593 /// \brief Const subscription operator
1595 /// Const subscription operator
1596 Value operator[](const Key& edge) const {
1597 return potential[graph.target(edge)] - potential[graph.source(edge)];
1602 const NodeMap& potential;
1605 /// \brief Just returns a PotentialDifferenceMap
1607 /// Just returns a PotentialDifferenceMap
1608 /// \relates PotentialDifferenceMap
1609 template <typename Graph, typename NodeMap>
1610 PotentialDifferenceMap<Graph, NodeMap>
1611 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
1612 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
1615 /// \brief Map of the node in-degrees.
1617 /// This map returns the in-degree of a node. Once it is constructed,
1618 /// the degrees are stored in a standard NodeMap, so each query is done
1619 /// in constant time. On the other hand, the values are updated automatically
1620 /// whenever the graph changes.
1622 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1623 /// alternative ways to modify the graph. The correct behavior of InDegMap
1624 /// is not guarantied if these additional features are used. For example
1625 /// the functions \ref ListGraph::changeSource() "changeSource()",
1626 /// \ref ListGraph::changeTarget() "changeTarget()" and
1627 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1628 /// of \ref ListGraph will \e not update the degree values correctly.
1632 template <typename _Graph>
1634 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
1635 ::ItemNotifier::ObserverBase {
1639 typedef _Graph Graph;
1641 typedef typename Graph::Node Key;
1643 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
1644 ::ItemNotifier::ObserverBase Parent;
1648 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
1651 typedef DefaultMap<_Graph, Key, int> Parent;
1652 typedef typename Parent::Graph Graph;
1654 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1656 virtual void add(const Key& key) {
1658 Parent::set(key, 0);
1661 virtual void add(const std::vector<Key>& keys) {
1663 for (int i = 0; i < (int)keys.size(); ++i) {
1664 Parent::set(keys[i], 0);
1671 /// \brief Constructor.
1673 /// Constructor for creating in-degree map.
1674 InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1675 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
1677 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1678 deg[it] = countInEdges(graph, it);
1682 /// Gives back the in-degree of a Node.
1683 int operator[](const Key& key) const {
1689 typedef typename Graph::Edge Edge;
1691 virtual void add(const Edge& edge) {
1692 ++deg[graph.target(edge)];
1695 virtual void add(const std::vector<Edge>& edges) {
1696 for (int i = 0; i < (int)edges.size(); ++i) {
1697 ++deg[graph.target(edges[i])];
1701 virtual void erase(const Edge& edge) {
1702 --deg[graph.target(edge)];
1705 virtual void erase(const std::vector<Edge>& edges) {
1706 for (int i = 0; i < (int)edges.size(); ++i) {
1707 --deg[graph.target(edges[i])];
1711 virtual void build() {
1712 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1713 deg[it] = countInEdges(graph, it);
1717 virtual void clear() {
1718 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1724 const _Graph& graph;
1728 /// \brief Map of the node out-degrees.
1730 /// This map returns the out-degree of a node. Once it is constructed,
1731 /// the degrees are stored in a standard NodeMap, so each query is done
1732 /// in constant time. On the other hand, the values are updated automatically
1733 /// whenever the graph changes.
1735 /// \warning Besides addNode() and addEdge(), a graph structure may provide
1736 /// alternative ways to modify the graph. The correct behavior of OutDegMap
1737 /// is not guarantied if these additional features are used. For example
1738 /// the functions \ref ListGraph::changeSource() "changeSource()",
1739 /// \ref ListGraph::changeTarget() "changeTarget()" and
1740 /// \ref ListGraph::reverseEdge() "reverseEdge()"
1741 /// of \ref ListGraph will \e not update the degree values correctly.
1745 template <typename _Graph>
1747 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
1748 ::ItemNotifier::ObserverBase {
1752 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
1753 ::ItemNotifier::ObserverBase Parent;
1755 typedef _Graph Graph;
1757 typedef typename Graph::Node Key;
1761 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
1764 typedef DefaultMap<_Graph, Key, int> Parent;
1765 typedef typename Parent::Graph Graph;
1767 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1769 virtual void add(const Key& key) {
1771 Parent::set(key, 0);
1773 virtual void add(const std::vector<Key>& keys) {
1775 for (int i = 0; i < (int)keys.size(); ++i) {
1776 Parent::set(keys[i], 0);
1783 /// \brief Constructor.
1785 /// Constructor for creating out-degree map.
1786 OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1787 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
1789 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1790 deg[it] = countOutEdges(graph, it);
1794 /// Gives back the out-degree of a Node.
1795 int operator[](const Key& key) const {
1801 typedef typename Graph::Edge Edge;
1803 virtual void add(const Edge& edge) {
1804 ++deg[graph.source(edge)];
1807 virtual void add(const std::vector<Edge>& edges) {
1808 for (int i = 0; i < (int)edges.size(); ++i) {
1809 ++deg[graph.source(edges[i])];
1813 virtual void erase(const Edge& edge) {
1814 --deg[graph.source(edge)];
1817 virtual void erase(const std::vector<Edge>& edges) {
1818 for (int i = 0; i < (int)edges.size(); ++i) {
1819 --deg[graph.source(edges[i])];
1823 virtual void build() {
1824 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1825 deg[it] = countOutEdges(graph, it);
1829 virtual void clear() {
1830 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1836 const _Graph& graph;
1843 } //END OF NAMESPACE LEMON