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
28 #include <lemon/bits/invalid.h>
29 #include <lemon/bits/utility.h>
30 #include <lemon/maps.h>
31 #include <lemon/bits/traits.h>
33 #include <lemon/bits/alteration_notifier.h>
34 #include <lemon/bits/default_map.h>
38 ///\brief Graph utilities.
45 /// \addtogroup gutils
48 ///Creates convenience typedefs for the graph types and iterators
50 ///This \c \#define creates convenience typedefs for the following types
51 ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
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;
68 ///Creates convenience typedefs for the undirected graph types and iterators
70 ///This \c \#define creates the same convenience typedefs as defined by
71 ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
72 ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
74 ///\note If \c G it a template parameter, it should be used in this way.
76 /// UGRAPH_TYPEDEFS(typename G)
79 ///\warning There are no typedefs for the graph maps because of the lack of
80 ///template typedefs in C++.
81 #define UGRAPH_TYPEDEFS(Graph) \
82 GRAPH_TYPEDEFS(Graph) \
83 typedef Graph:: UEdge UEdge; \
84 typedef Graph:: UEdgeIt UEdgeIt; \
85 typedef Graph:: IncEdgeIt IncEdgeIt;
87 ///\brief Creates convenience typedefs for the bipartite undirected graph
88 ///types and iterators
90 ///This \c \#define creates the same convenience typedefs as defined by
91 ///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates
92 ///\c ANodeIt, \c BNodeIt,
94 ///\note If \c G it a template parameter, it should be used in this way.
96 /// BPUGRAPH_TYPEDEFS(typename G)
99 ///\warning There are no typedefs for the graph maps because of the lack of
100 ///template typedefs in C++.
101 #define BPUGRAPH_TYPEDEFS(Graph) \
102 UGRAPH_TYPEDEFS(Graph) \
103 typedef Graph::ANode ANode; \
104 typedef Graph::BNode BNode; \
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)) {
380 template <typename Graph>
381 inline typename Graph::Edge
382 findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
383 typename Graph::Edge prev = INVALID) {
384 return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
387 /// \brief Iterator for iterating on edges connected the same nodes.
389 /// Iterator for iterating on edges connected the same nodes. It is
390 /// higher level interface for the findEdge() function. You can
391 /// use it the following way:
393 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
402 /// \author Balazs Dezso
403 template <typename _Graph>
404 class ConEdgeIt : public _Graph::Edge {
407 typedef _Graph Graph;
408 typedef typename Graph::Edge Parent;
410 typedef typename Graph::Edge Edge;
411 typedef typename Graph::Node Node;
413 /// \brief Constructor.
415 /// Construct a new ConEdgeIt iterating on the edges which
416 /// connects the \c u and \c v node.
417 ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
418 Parent::operator=(findEdge(graph, u, v));
421 /// \brief Constructor.
423 /// Construct a new ConEdgeIt which continues the iterating from
425 ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
427 /// \brief Increment operator.
429 /// It increments the iterator and gives back the next edge.
430 ConEdgeIt& operator++() {
431 Parent::operator=(findEdge(graph, graph.source(*this),
432 graph.target(*this), *this));
439 namespace _graph_utils_bits {
441 template <typename Graph, typename Enable = void>
442 struct FindUEdgeSelector {
443 typedef typename Graph::Node Node;
444 typedef typename Graph::UEdge UEdge;
445 static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
451 b = g.source(e) == u;
454 while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
464 while (e != INVALID && (!b || g.target(e) != v)) {
472 template <typename Graph>
473 struct FindUEdgeSelector<
475 typename enable_if<typename Graph::FindEdgeTag, void>::type>
477 typedef typename Graph::Node Node;
478 typedef typename Graph::UEdge UEdge;
479 static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
480 return g.findUEdge(u, v, prev);
485 /// \brief Finds an uedge between two nodes of a graph.
487 /// Finds an uedge from node \c u to node \c v in graph \c g.
488 /// If the node \c u and node \c v is equal then each loop edge
489 /// will be enumerated.
491 /// If \c prev is \ref INVALID (this is the default value), then
492 /// it finds the first edge from \c u to \c v. Otherwise it looks for
493 /// the next edge from \c u to \c v after \c prev.
494 /// \return The found edge or \ref INVALID if there is no such an edge.
496 /// Thus you can iterate through each edge from \c u to \c v as it follows.
498 /// for(UEdge e = findUEdge(g,u,v); e != INVALID;
499 /// e = findUEdge(g,u,v,e)) {
506 template <typename Graph>
507 inline typename Graph::UEdge
508 findUEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
509 typename Graph::UEdge p = INVALID) {
510 return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
513 /// \brief Iterator for iterating on uedges connected the same nodes.
515 /// Iterator for iterating on uedges connected the same nodes. It is
516 /// higher level interface for the findUEdge() function. You can
517 /// use it the following way:
519 /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
526 /// \author Balazs Dezso
527 template <typename _Graph>
528 class ConUEdgeIt : public _Graph::UEdge {
531 typedef _Graph Graph;
532 typedef typename Graph::UEdge Parent;
534 typedef typename Graph::UEdge UEdge;
535 typedef typename Graph::Node Node;
537 /// \brief Constructor.
539 /// Construct a new ConUEdgeIt iterating on the edges which
540 /// connects the \c u and \c v node.
541 ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
542 Parent::operator=(findUEdge(graph, u, v));
545 /// \brief Constructor.
547 /// Construct a new ConUEdgeIt which continues the iterating from
549 ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
551 /// \brief Increment operator.
553 /// It increments the iterator and gives back the next edge.
554 ConUEdgeIt& operator++() {
555 Parent::operator=(findUEdge(graph, graph.source(*this),
556 graph.target(*this), *this));
563 /// \brief Copy a map.
565 /// This function copies the \c source map to the \c target map. It uses the
566 /// given iterator to iterate on the data structure and it uses the \c ref
567 /// mapping to convert the source's keys to the target's keys.
568 template <typename Target, typename Source,
569 typename ItemIt, typename Ref>
570 void copyMap(Target& target, const Source& source,
571 ItemIt it, const Ref& ref) {
572 for (; it != INVALID; ++it) {
573 target[ref[it]] = source[it];
577 /// \brief Copy the source map to the target map.
579 /// Copy the \c source map to the \c target map. It uses the given iterator
580 /// to iterate on the data structure.
581 template <typename Target, typename Source, typename ItemIt>
582 void copyMap(Target& target, const Source& source, ItemIt it) {
583 for (; it != INVALID; ++it) {
584 target[it] = source[it];
588 namespace _graph_utils_bits {
590 template <typename Graph, typename Item, typename RefMap>
593 virtual void copy(const Graph& source, const RefMap& refMap) = 0;
595 virtual ~MapCopyBase() {}
598 template <typename Graph, typename Item, typename RefMap,
599 typename TargetMap, typename SourceMap>
600 class MapCopy : public MapCopyBase<Graph, Item, RefMap> {
603 MapCopy(TargetMap& tmap, const SourceMap& map)
604 : _tmap(tmap), _map(map) {}
606 virtual void copy(const Graph& graph, const RefMap& refMap) {
607 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
608 for (ItemIt it(graph); it != INVALID; ++it) {
609 _tmap.set(refMap[it], _map[it]);
615 const SourceMap& _map;
618 template <typename Graph, typename Item, typename RefMap, typename It>
619 class ItemCopy : public MapCopyBase<Graph, Item, RefMap> {
622 ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
624 virtual void copy(const Graph&, const RefMap& refMap) {
633 template <typename Graph, typename Item, typename RefMap, typename Ref>
634 class RefCopy : public MapCopyBase<Graph, Item, RefMap> {
637 RefCopy(Ref& map) : _map(map) {}
639 virtual void copy(const Graph& graph, const RefMap& refMap) {
640 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
641 for (ItemIt it(graph); it != INVALID; ++it) {
642 _map.set(it, refMap[it]);
650 template <typename Graph, typename Item, typename RefMap,
652 class CrossRefCopy : public MapCopyBase<Graph, Item, RefMap> {
655 CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
657 virtual void copy(const Graph& graph, const RefMap& refMap) {
658 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
659 for (ItemIt it(graph); it != INVALID; ++it) {
660 _cmap.set(refMap[it], it);
668 template <typename Graph, typename Enable = void>
669 struct GraphCopySelector {
670 template <typename Source, typename NodeRefMap, typename EdgeRefMap>
671 static void copy(Graph &target, const Source& source,
672 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
673 for (typename Source::NodeIt it(source); it != INVALID; ++it) {
674 nodeRefMap[it] = target.addNode();
676 for (typename Source::EdgeIt it(source); it != INVALID; ++it) {
677 edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
678 nodeRefMap[source.target(it)]);
683 template <typename Graph>
684 struct GraphCopySelector<
686 typename enable_if<typename Graph::BuildTag, void>::type>
688 template <typename Source, typename NodeRefMap, typename EdgeRefMap>
689 static void copy(Graph &target, const Source& source,
690 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
691 target.build(source, nodeRefMap, edgeRefMap);
695 template <typename UGraph, typename Enable = void>
696 struct UGraphCopySelector {
697 template <typename Source, typename NodeRefMap, typename UEdgeRefMap>
698 static void copy(UGraph &target, const Source& source,
699 NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
700 for (typename Source::NodeIt it(source); it != INVALID; ++it) {
701 nodeRefMap[it] = target.addNode();
703 for (typename Source::UEdgeIt it(source); it != INVALID; ++it) {
704 uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
705 nodeRefMap[source.target(it)]);
710 template <typename UGraph>
711 struct UGraphCopySelector<
713 typename enable_if<typename UGraph::BuildTag, void>::type>
715 template <typename Source, typename NodeRefMap, typename UEdgeRefMap>
716 static void copy(UGraph &target, const Source& source,
717 NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
718 target.build(source, nodeRefMap, uEdgeRefMap);
722 template <typename BpUGraph, typename Enable = void>
723 struct BpUGraphCopySelector {
724 template <typename Source, typename ANodeRefMap,
725 typename BNodeRefMap, typename UEdgeRefMap>
726 static void copy(BpUGraph &target, const Source& source,
727 ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
728 UEdgeRefMap& uEdgeRefMap) {
729 for (typename Source::ANodeIt it(source); it != INVALID; ++it) {
730 aNodeRefMap[it] = target.addANode();
732 for (typename Source::BNodeIt it(source); it != INVALID; ++it) {
733 bNodeRefMap[it] = target.addBNode();
735 for (typename Source::UEdgeIt it(source); it != INVALID; ++it) {
736 uEdgeRefMap[it] = target.addEdge(aNodeRefMap[source.aNode(it)],
737 bNodeRefMap[source.bNode(it)]);
742 template <typename BpUGraph>
743 struct BpUGraphCopySelector<
745 typename enable_if<typename BpUGraph::BuildTag, void>::type>
747 template <typename Source, typename ANodeRefMap,
748 typename BNodeRefMap, typename UEdgeRefMap>
749 static void copy(BpUGraph &target, const Source& source,
750 ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
751 UEdgeRefMap& uEdgeRefMap) {
752 target.build(source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
759 /// \brief Class to copy a graph.
761 /// Class to copy a graph to another graph (duplicate a graph). The
762 /// simplest way of using it is through the \c copyGraph() function.
763 template <typename Target, typename Source>
767 typedef typename Source::Node Node;
768 typedef typename Source::NodeIt NodeIt;
769 typedef typename Source::Edge Edge;
770 typedef typename Source::EdgeIt EdgeIt;
772 typedef typename Target::Node TNode;
773 typedef typename Target::Edge TEdge;
775 typedef typename Source::template NodeMap<TNode> NodeRefMap;
776 typedef typename Source::template EdgeMap<TEdge> EdgeRefMap;
782 /// \brief Constructor for the GraphCopy.
784 /// It copies the content of the \c _source graph into the
785 /// \c _target graph.
786 GraphCopy(Target& _target, const Source& _source)
787 : source(_source), target(_target) {}
789 /// \brief Destructor of the GraphCopy
791 /// Destructor of the GraphCopy
793 for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
794 delete nodeMapCopies[i];
796 for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
797 delete edgeMapCopies[i];
802 /// \brief Copies the node references into the given map.
804 /// Copies the node references into the given map.
805 template <typename NodeRef>
806 GraphCopy& nodeRef(NodeRef& map) {
807 nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node,
808 NodeRefMap, NodeRef>(map));
812 /// \brief Copies the node cross references into the given map.
814 /// Copies the node cross references (reverse references) into
816 template <typename NodeCrossRef>
817 GraphCopy& nodeCrossRef(NodeCrossRef& map) {
818 nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
819 NodeRefMap, NodeCrossRef>(map));
823 /// \brief Make copy of the given map.
825 /// Makes copy of the given map for the newly created graph.
826 /// The new map's key type is the target graph's node type,
827 /// and the copied map's key type is the source graph's node
829 template <typename TargetMap, typename SourceMap>
830 GraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
831 nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node,
832 NodeRefMap, TargetMap, SourceMap>(tmap, map));
836 /// \brief Make a copy of the given node.
838 /// Make a copy of the given node.
839 GraphCopy& node(TNode& tnode, const Node& node) {
840 nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node,
841 NodeRefMap, TNode>(tnode, node));
845 /// \brief Copies the edge references into the given map.
847 /// Copies the edge references into the given map.
848 template <typename EdgeRef>
849 GraphCopy& edgeRef(EdgeRef& map) {
850 edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge,
851 EdgeRefMap, EdgeRef>(map));
855 /// \brief Copies the edge cross references into the given map.
857 /// Copies the edge cross references (reverse references) into
859 template <typename EdgeCrossRef>
860 GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
861 edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
862 EdgeRefMap, EdgeCrossRef>(map));
866 /// \brief Make copy of the given map.
868 /// Makes copy of the given map for the newly created graph.
869 /// The new map's key type is the target graph's edge type,
870 /// and the copied map's key type is the source graph's edge
872 template <typename TargetMap, typename SourceMap>
873 GraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
874 edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge,
875 EdgeRefMap, TargetMap, SourceMap>(tmap, map));
879 /// \brief Make a copy of the given edge.
881 /// Make a copy of the given edge.
882 GraphCopy& edge(TEdge& tedge, const Edge& edge) {
883 edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge,
884 EdgeRefMap, TEdge>(tedge, edge));
888 /// \brief Executes the copies.
890 /// Executes the copies.
892 NodeRefMap nodeRefMap(source);
893 EdgeRefMap edgeRefMap(source);
894 _graph_utils_bits::GraphCopySelector<Target>::
895 copy(target, source, nodeRefMap, edgeRefMap);
896 for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
897 nodeMapCopies[i]->copy(source, nodeRefMap);
899 for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
900 edgeMapCopies[i]->copy(source, edgeRefMap);
907 const Source& source;
910 std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* >
913 std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* >
918 /// \brief Copy a graph to another graph.
920 /// Copy a graph to another graph.
921 /// The usage of the function:
924 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
927 /// After the copy the \c nr map will contain the mapping from the
928 /// source graph's nodes to the target graph's nodes and the \c ecr will
929 /// contain the mapping from the target graph's edges to the source's
933 template <typename Target, typename Source>
934 GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
935 return GraphCopy<Target, Source>(target, source);
938 /// \brief Class to copy an undirected graph.
940 /// Class to copy an undirected graph to another graph (duplicate a graph).
941 /// The simplest way of using it is through the \c copyUGraph() function.
942 template <typename Target, typename Source>
946 typedef typename Source::Node Node;
947 typedef typename Source::NodeIt NodeIt;
948 typedef typename Source::Edge Edge;
949 typedef typename Source::EdgeIt EdgeIt;
950 typedef typename Source::UEdge UEdge;
951 typedef typename Source::UEdgeIt UEdgeIt;
953 typedef typename Target::Node TNode;
954 typedef typename Target::Edge TEdge;
955 typedef typename Target::UEdge TUEdge;
957 typedef typename Source::template NodeMap<TNode> NodeRefMap;
958 typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
961 EdgeRefMap(const Target& _target, const Source& _source,
962 const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref)
963 : target(_target), source(_source),
964 uedge_ref(_uedge_ref), node_ref(_node_ref) {}
966 typedef typename Source::Edge Key;
967 typedef typename Target::Edge Value;
969 Value operator[](const Key& key) const {
970 bool forward = (source.direction(key) ==
971 (node_ref[source.source((UEdge)key)] ==
972 target.source(uedge_ref[(UEdge)key])));
973 return target.direct(uedge_ref[key], forward);
976 const Target& target;
977 const Source& source;
978 const UEdgeRefMap& uedge_ref;
979 const NodeRefMap& node_ref;
986 /// \brief Constructor for the GraphCopy.
988 /// It copies the content of the \c _source graph into the
989 /// \c _target graph.
990 UGraphCopy(Target& _target, const Source& _source)
991 : source(_source), target(_target) {}
993 /// \brief Destructor of the GraphCopy
995 /// Destructor of the GraphCopy
997 for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
998 delete nodeMapCopies[i];
1000 for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
1001 delete edgeMapCopies[i];
1003 for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
1004 delete uEdgeMapCopies[i];
1009 /// \brief Copies the node references into the given map.
1011 /// Copies the node references into the given map.
1012 template <typename NodeRef>
1013 UGraphCopy& nodeRef(NodeRef& map) {
1014 nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node,
1015 NodeRefMap, NodeRef>(map));
1019 /// \brief Copies the node cross references into the given map.
1021 /// Copies the node cross references (reverse references) into
1023 template <typename NodeCrossRef>
1024 UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
1025 nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
1026 NodeRefMap, NodeCrossRef>(map));
1030 /// \brief Make copy of the given map.
1032 /// Makes copy of the given map for the newly created graph.
1033 /// The new map's key type is the target graph's node type,
1034 /// and the copied map's key type is the source graph's node
1036 template <typename TargetMap, typename SourceMap>
1037 UGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
1038 nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node,
1039 NodeRefMap, TargetMap, SourceMap>(tmap, map));
1043 /// \brief Make a copy of the given node.
1045 /// Make a copy of the given node.
1046 UGraphCopy& node(TNode& tnode, const Node& node) {
1047 nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node,
1048 NodeRefMap, TNode>(tnode, node));
1052 /// \brief Copies the edge references into the given map.
1054 /// Copies the edge references into the given map.
1055 template <typename EdgeRef>
1056 UGraphCopy& edgeRef(EdgeRef& map) {
1057 edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge,
1058 EdgeRefMap, EdgeRef>(map));
1062 /// \brief Copies the edge cross references into the given map.
1064 /// Copies the edge cross references (reverse references) into
1066 template <typename EdgeCrossRef>
1067 UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1068 edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
1069 EdgeRefMap, EdgeCrossRef>(map));
1073 /// \brief Make copy of the given map.
1075 /// Makes copy of the given map for the newly created graph.
1076 /// The new map's key type is the target graph's edge type,
1077 /// and the copied map's key type is the source graph's edge
1079 template <typename TargetMap, typename SourceMap>
1080 UGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
1081 edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge,
1082 EdgeRefMap, TargetMap, SourceMap>(tmap, map));
1086 /// \brief Make a copy of the given edge.
1088 /// Make a copy of the given edge.
1089 UGraphCopy& edge(TEdge& tedge, const Edge& edge) {
1090 edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge,
1091 EdgeRefMap, TEdge>(tedge, edge));
1095 /// \brief Copies the undirected edge references into the given map.
1097 /// Copies the undirected edge references into the given map.
1098 template <typename UEdgeRef>
1099 UGraphCopy& uEdgeRef(UEdgeRef& map) {
1100 uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge,
1101 UEdgeRefMap, UEdgeRef>(map));
1105 /// \brief Copies the undirected edge cross references into the given map.
1107 /// Copies the undirected edge cross references (reverse
1108 /// references) into the given map.
1109 template <typename UEdgeCrossRef>
1110 UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
1111 uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
1112 UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
1116 /// \brief Make copy of the given map.
1118 /// Makes copy of the given map for the newly created graph.
1119 /// The new map's key type is the target graph's undirected edge type,
1120 /// and the copied map's key type is the source graph's undirected edge
1122 template <typename TargetMap, typename SourceMap>
1123 UGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
1124 uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge,
1125 UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
1129 /// \brief Make a copy of the given undirected edge.
1131 /// Make a copy of the given undirected edge.
1132 UGraphCopy& uEdge(TUEdge& tuedge, const UEdge& uedge) {
1133 uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge,
1134 UEdgeRefMap, TUEdge>(tuedge, uedge));
1138 /// \brief Executes the copies.
1140 /// Executes the copies.
1142 NodeRefMap nodeRefMap(source);
1143 UEdgeRefMap uEdgeRefMap(source);
1144 EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
1145 _graph_utils_bits::UGraphCopySelector<Target>::
1146 copy(target, source, nodeRefMap, uEdgeRefMap);
1147 for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
1148 nodeMapCopies[i]->copy(source, nodeRefMap);
1150 for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
1151 uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
1153 for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
1154 edgeMapCopies[i]->copy(source, edgeRefMap);
1160 const Source& source;
1163 std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* >
1166 std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* >
1169 std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* >
1174 /// \brief Copy an undirected graph to another graph.
1176 /// Copy an undirected graph to another graph.
1177 /// The usage of the function:
1180 /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
1183 /// After the copy the \c nr map will contain the mapping from the
1184 /// source graph's nodes to the target graph's nodes and the \c ecr will
1185 /// contain the mapping from the target graph's edges to the source's
1189 template <typename Target, typename Source>
1190 UGraphCopy<Target, Source>
1191 copyUGraph(Target& target, const Source& source) {
1192 return UGraphCopy<Target, Source>(target, source);
1195 /// \brief Class to copy a bipartite undirected graph.
1197 /// Class to copy a bipartite undirected graph to another graph
1198 /// (duplicate a graph). The simplest way of using it is through
1199 /// the \c copyBpUGraph() function.
1200 template <typename Target, typename Source>
1201 class BpUGraphCopy {
1204 typedef typename Source::Node Node;
1205 typedef typename Source::ANode ANode;
1206 typedef typename Source::BNode BNode;
1207 typedef typename Source::NodeIt NodeIt;
1208 typedef typename Source::Edge Edge;
1209 typedef typename Source::EdgeIt EdgeIt;
1210 typedef typename Source::UEdge UEdge;
1211 typedef typename Source::UEdgeIt UEdgeIt;
1213 typedef typename Target::Node TNode;
1214 typedef typename Target::Edge TEdge;
1215 typedef typename Target::UEdge TUEdge;
1217 typedef typename Source::template ANodeMap<TNode> ANodeRefMap;
1218 typedef typename Source::template BNodeMap<TNode> BNodeRefMap;
1219 typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
1222 NodeRefMap(const Source& _source, const ANodeRefMap& _anode_ref,
1223 const BNodeRefMap& _bnode_ref)
1224 : source(_source), anode_ref(_anode_ref), bnode_ref(_bnode_ref) {}
1226 typedef typename Source::Node Key;
1227 typedef typename Target::Node Value;
1229 Value operator[](const Key& key) const {
1230 return source.aNode(key) ? anode_ref[key] : bnode_ref[key];
1233 const Source& source;
1234 const ANodeRefMap& anode_ref;
1235 const BNodeRefMap& bnode_ref;
1239 EdgeRefMap(const Target& _target, const Source& _source,
1240 const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref)
1241 : target(_target), source(_source),
1242 uedge_ref(_uedge_ref), node_ref(_node_ref) {}
1244 typedef typename Source::Edge Key;
1245 typedef typename Target::Edge Value;
1247 Value operator[](const Key& key) const {
1248 bool forward = (source.direction(key) ==
1249 (node_ref[source.source((UEdge)key)] ==
1250 target.source(uedge_ref[(UEdge)key])));
1251 return target.direct(uedge_ref[key], forward);
1254 const Target& target;
1255 const Source& source;
1256 const UEdgeRefMap& uedge_ref;
1257 const NodeRefMap& node_ref;
1263 /// \brief Constructor for the GraphCopy.
1265 /// It copies the content of the \c _source graph into the
1266 /// \c _target graph.
1267 BpUGraphCopy(Target& _target, const Source& _source)
1268 : source(_source), target(_target) {}
1270 /// \brief Destructor of the GraphCopy
1272 /// Destructor of the GraphCopy
1274 for (int i = 0; i < (int)aNodeMapCopies.size(); ++i) {
1275 delete aNodeMapCopies[i];
1277 for (int i = 0; i < (int)bNodeMapCopies.size(); ++i) {
1278 delete bNodeMapCopies[i];
1280 for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
1281 delete nodeMapCopies[i];
1283 for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
1284 delete edgeMapCopies[i];
1286 for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
1287 delete uEdgeMapCopies[i];
1292 /// \brief Copies the A-node references into the given map.
1294 /// Copies the A-node references into the given map.
1295 template <typename ANodeRef>
1296 BpUGraphCopy& aNodeRef(ANodeRef& map) {
1297 aNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, ANode,
1298 ANodeRefMap, ANodeRef>(map));
1302 /// \brief Copies the A-node cross references into the given map.
1304 /// Copies the A-node cross references (reverse references) into
1306 template <typename ANodeCrossRef>
1307 BpUGraphCopy& aNodeCrossRef(ANodeCrossRef& map) {
1308 aNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
1309 ANode, ANodeRefMap, ANodeCrossRef>(map));
1313 /// \brief Make copy of the given A-node map.
1315 /// Makes copy of the given map for the newly created graph.
1316 /// The new map's key type is the target graph's node type,
1317 /// and the copied map's key type is the source graph's node
1319 template <typename TargetMap, typename SourceMap>
1320 BpUGraphCopy& aNodeMap(TargetMap& tmap, const SourceMap& map) {
1321 aNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, ANode,
1322 ANodeRefMap, TargetMap, SourceMap>(tmap, map));
1326 /// \brief Copies the B-node references into the given map.
1328 /// Copies the B-node references into the given map.
1329 template <typename BNodeRef>
1330 BpUGraphCopy& bNodeRef(BNodeRef& map) {
1331 bNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, BNode,
1332 BNodeRefMap, BNodeRef>(map));
1336 /// \brief Copies the B-node cross references into the given map.
1338 /// Copies the B-node cross references (reverse references) into
1340 template <typename BNodeCrossRef>
1341 BpUGraphCopy& bNodeCrossRef(BNodeCrossRef& map) {
1342 bNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
1343 BNode, BNodeRefMap, BNodeCrossRef>(map));
1347 /// \brief Make copy of the given B-node map.
1349 /// Makes copy of the given map for the newly created graph.
1350 /// The new map's key type is the target graph's node type,
1351 /// and the copied map's key type is the source graph's node
1353 template <typename TargetMap, typename SourceMap>
1354 BpUGraphCopy& bNodeMap(TargetMap& tmap, const SourceMap& map) {
1355 bNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, BNode,
1356 BNodeRefMap, TargetMap, SourceMap>(tmap, map));
1359 /// \brief Copies the node references into the given map.
1361 /// Copies the node references into the given map.
1362 template <typename NodeRef>
1363 BpUGraphCopy& nodeRef(NodeRef& map) {
1364 nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node,
1365 NodeRefMap, NodeRef>(map));
1369 /// \brief Copies the node cross references into the given map.
1371 /// Copies the node cross references (reverse references) into
1373 template <typename NodeCrossRef>
1374 BpUGraphCopy& nodeCrossRef(NodeCrossRef& map) {
1375 nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
1376 NodeRefMap, NodeCrossRef>(map));
1380 /// \brief Make copy of the given map.
1382 /// Makes copy of the given map for the newly created graph.
1383 /// The new map's key type is the target graph's node type,
1384 /// and the copied map's key type is the source graph's node
1386 template <typename TargetMap, typename SourceMap>
1387 BpUGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
1388 nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node,
1389 NodeRefMap, TargetMap, SourceMap>(tmap, map));
1393 /// \brief Make a copy of the given node.
1395 /// Make a copy of the given node.
1396 BpUGraphCopy& node(TNode& tnode, const Node& node) {
1397 nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node,
1398 NodeRefMap, TNode>(tnode, node));
1402 /// \brief Copies the edge references into the given map.
1404 /// Copies the edge references into the given map.
1405 template <typename EdgeRef>
1406 BpUGraphCopy& edgeRef(EdgeRef& map) {
1407 edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge,
1408 EdgeRefMap, EdgeRef>(map));
1412 /// \brief Copies the edge cross references into the given map.
1414 /// Copies the edge cross references (reverse references) into
1416 template <typename EdgeCrossRef>
1417 BpUGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1418 edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
1419 EdgeRefMap, EdgeCrossRef>(map));
1423 /// \brief Make copy of the given map.
1425 /// Makes copy of the given map for the newly created graph.
1426 /// The new map's key type is the target graph's edge type,
1427 /// and the copied map's key type is the source graph's edge
1429 template <typename TargetMap, typename SourceMap>
1430 BpUGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
1431 edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge,
1432 EdgeRefMap, TargetMap, SourceMap>(tmap, map));
1436 /// \brief Make a copy of the given edge.
1438 /// Make a copy of the given edge.
1439 BpUGraphCopy& edge(TEdge& tedge, const Edge& edge) {
1440 edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge,
1441 EdgeRefMap, TEdge>(tedge, edge));
1445 /// \brief Copies the undirected edge references into the given map.
1447 /// Copies the undirected edge references into the given map.
1448 template <typename UEdgeRef>
1449 BpUGraphCopy& uEdgeRef(UEdgeRef& map) {
1450 uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge,
1451 UEdgeRefMap, UEdgeRef>(map));
1455 /// \brief Copies the undirected edge cross references into the given map.
1457 /// Copies the undirected edge cross references (reverse
1458 /// references) into the given map.
1459 template <typename UEdgeCrossRef>
1460 BpUGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
1461 uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
1462 UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
1466 /// \brief Make copy of the given map.
1468 /// Makes copy of the given map for the newly created graph.
1469 /// The new map's key type is the target graph's undirected edge type,
1470 /// and the copied map's key type is the source graph's undirected edge
1472 template <typename TargetMap, typename SourceMap>
1473 BpUGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
1474 uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge,
1475 UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
1479 /// \brief Make a copy of the given undirected edge.
1481 /// Make a copy of the given undirected edge.
1482 BpUGraphCopy& uEdge(TUEdge& tuedge, const UEdge& uedge) {
1483 uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge,
1484 UEdgeRefMap, TUEdge>(tuedge, uedge));
1488 /// \brief Executes the copies.
1490 /// Executes the copies.
1492 ANodeRefMap aNodeRefMap(source);
1493 BNodeRefMap bNodeRefMap(source);
1494 NodeRefMap nodeRefMap(source, aNodeRefMap, bNodeRefMap);
1495 UEdgeRefMap uEdgeRefMap(source);
1496 EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
1497 _graph_utils_bits::BpUGraphCopySelector<Target>::
1498 copy(target, source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
1499 for (int i = 0; i < (int)aNodeMapCopies.size(); ++i) {
1500 aNodeMapCopies[i]->copy(source, aNodeRefMap);
1502 for (int i = 0; i < (int)bNodeMapCopies.size(); ++i) {
1503 bNodeMapCopies[i]->copy(source, bNodeRefMap);
1505 for (int i = 0; i < (int)nodeMapCopies.size(); ++i) {
1506 nodeMapCopies[i]->copy(source, nodeRefMap);
1508 for (int i = 0; i < (int)uEdgeMapCopies.size(); ++i) {
1509 uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
1511 for (int i = 0; i < (int)edgeMapCopies.size(); ++i) {
1512 edgeMapCopies[i]->copy(source, edgeRefMap);
1518 const Source& source;
1521 std::vector<_graph_utils_bits::MapCopyBase<Source, ANode, ANodeRefMap>* >
1524 std::vector<_graph_utils_bits::MapCopyBase<Source, BNode, BNodeRefMap>* >
1527 std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* >
1530 std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* >
1533 std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* >
1538 /// \brief Copy a bipartite undirected graph to another graph.
1540 /// Copy a bipartite undirected graph to another graph.
1541 /// The usage of the function:
1544 /// copyBpUGraph(trg, src).aNodeRef(anr).edgeCrossRef(ecr).run();
1547 /// After the copy the \c nr map will contain the mapping from the
1548 /// source graph's nodes to the target graph's nodes and the \c ecr will
1549 /// contain the mapping from the target graph's edges to the source's
1552 /// \see BpUGraphCopy
1553 template <typename Target, typename Source>
1554 BpUGraphCopy<Target, Source>
1555 copyBpUGraph(Target& target, const Source& source) {
1556 return BpUGraphCopy<Target, Source>(target, source);
1562 /// \addtogroup graph_maps
1565 /// Provides an immutable and unique id for each item in the graph.
1567 /// The IdMap class provides a unique and immutable id for each item of the
1568 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
1569 /// different items (nodes) get different ids <li>\b immutable: the id of an
1570 /// item (node) does not change (even if you delete other nodes). </ul>
1571 /// Through this map you get access (i.e. can read) the inner id values of
1572 /// the items stored in the graph. This map can be inverted with its member
1573 /// class \c InverseMap.
1575 template <typename _Graph, typename _Item>
1578 typedef _Graph Graph;
1583 /// \brief Constructor.
1585 /// Constructor of the map.
1586 explicit IdMap(const Graph& _graph) : graph(&_graph) {}
1588 /// \brief Gives back the \e id of the item.
1590 /// Gives back the immutable and unique \e id of the item.
1591 int operator[](const Item& item) const { return graph->id(item);}
1593 /// \brief Gives back the item by its id.
1595 /// Gives back the item by its id.
1596 Item operator()(int id) { return graph->fromId(id, Item()); }
1603 /// \brief The class represents the inverse of its owner (IdMap).
1605 /// The class represents the inverse of its owner (IdMap).
1610 /// \brief Constructor.
1612 /// Constructor for creating an id-to-item map.
1613 explicit InverseMap(const Graph& _graph) : graph(&_graph) {}
1615 /// \brief Constructor.
1617 /// Constructor for creating an id-to-item map.
1618 explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
1620 /// \brief Gives back the given item from its id.
1622 /// Gives back the given item from its id.
1624 Item operator[](int id) const { return graph->fromId(id, Item());}
1630 /// \brief Gives back the inverse of the map.
1632 /// Gives back the inverse of the IdMap.
1633 InverseMap inverse() const { return InverseMap(*graph);}
1638 /// \brief General invertable graph-map type.
1640 /// This type provides simple invertable graph-maps.
1641 /// The InvertableMap wraps an arbitrary ReadWriteMap
1642 /// and if a key is set to a new value then store it
1643 /// in the inverse map.
1645 /// The values of the map can be accessed
1646 /// with stl compatible forward iterator.
1648 /// \param _Graph The graph type.
1649 /// \param _Item The item type of the graph.
1650 /// \param _Value The value type of the map.
1652 /// \see IterableValueMap
1653 template <typename _Graph, typename _Item, typename _Value>
1654 class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> {
1657 typedef DefaultMap<_Graph, _Item, _Value> Map;
1658 typedef _Graph Graph;
1660 typedef std::map<_Value, _Item> Container;
1665 /// The key type of InvertableMap (Node, Edge, UEdge).
1666 typedef typename Map::Key Key;
1667 /// The value type of the InvertableMap.
1668 typedef typename Map::Value Value;
1672 /// \brief Constructor.
1674 /// Construct a new InvertableMap for the graph.
1676 explicit InvertableMap(const Graph& graph) : Map(graph) {}
1678 /// \brief Forward iterator for values.
1680 /// This iterator is an stl compatible forward
1681 /// iterator on the values of the map. The values can
1682 /// be accessed in the [beginValue, endValue) range.
1685 : public std::iterator<std::forward_iterator_tag, Value> {
1686 friend class InvertableMap;
1688 ValueIterator(typename Container::const_iterator _it)
1694 ValueIterator& operator++() { ++it; return *this; }
1695 ValueIterator operator++(int) {
1696 ValueIterator tmp(*this);
1701 const Value& operator*() const { return it->first; }
1702 const Value* operator->() const { return &(it->first); }
1704 bool operator==(ValueIterator jt) const { return it == jt.it; }
1705 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1708 typename Container::const_iterator it;
1711 /// \brief Returns an iterator to the first value.
1713 /// Returns an stl compatible iterator to the
1714 /// first value of the map. The values of the
1715 /// map can be accessed in the [beginValue, endValue)
1717 ValueIterator beginValue() const {
1718 return ValueIterator(invMap.begin());
1721 /// \brief Returns an iterator after the last value.
1723 /// Returns an stl compatible iterator after the
1724 /// last value of the map. The values of the
1725 /// map can be accessed in the [beginValue, endValue)
1727 ValueIterator endValue() const {
1728 return ValueIterator(invMap.end());
1731 /// \brief The setter function of the map.
1733 /// Sets the mapped value.
1734 void set(const Key& key, const Value& val) {
1735 Value oldval = Map::operator[](key);
1736 typename Container::iterator it = invMap.find(oldval);
1737 if (it != invMap.end() && it->second == key) {
1740 invMap.insert(make_pair(val, key));
1744 /// \brief The getter function of the map.
1746 /// It gives back the value associated with the key.
1747 typename MapTraits<Map>::ConstReturnValue
1748 operator[](const Key& key) const {
1749 return Map::operator[](key);
1752 /// \brief Gives back the item by its value.
1754 /// Gives back the item by its value.
1755 Key operator()(const Value& key) const {
1756 typename Container::const_iterator it = invMap.find(key);
1757 return it != invMap.end() ? it->second : INVALID;
1762 /// \brief Erase the key from the map.
1764 /// Erase the key to the map. It is called by the
1765 /// \c AlterationNotifier.
1766 virtual void erase(const Key& key) {
1767 Value val = Map::operator[](key);
1768 typename Container::iterator it = invMap.find(val);
1769 if (it != invMap.end() && it->second == key) {
1775 /// \brief Erase more keys from the map.
1777 /// Erase more keys from the map. It is called by the
1778 /// \c AlterationNotifier.
1779 virtual void erase(const std::vector<Key>& keys) {
1780 for (int i = 0; i < (int)keys.size(); ++i) {
1781 Value val = Map::operator[](keys[i]);
1782 typename Container::iterator it = invMap.find(val);
1783 if (it != invMap.end() && it->second == keys[i]) {
1790 /// \brief Clear the keys from the map and inverse map.
1792 /// Clear the keys from the map and inverse map. It is called by the
1793 /// \c AlterationNotifier.
1794 virtual void clear() {
1801 /// \brief The inverse map type.
1803 /// The inverse of this map. The subscript operator of the map
1804 /// gives back always the item what was last assigned to the value.
1807 /// \brief Constructor of the InverseMap.
1809 /// Constructor of the InverseMap.
1810 explicit InverseMap(const InvertableMap& _inverted)
1811 : inverted(_inverted) {}
1813 /// The value type of the InverseMap.
1814 typedef typename InvertableMap::Key Value;
1815 /// The key type of the InverseMap.
1816 typedef typename InvertableMap::Value Key;
1818 /// \brief Subscript operator.
1820 /// Subscript operator. It gives back always the item
1821 /// what was last assigned to the value.
1822 Value operator[](const Key& key) const {
1823 return inverted(key);
1827 const InvertableMap& inverted;
1830 /// \brief It gives back the just readable inverse map.
1832 /// It gives back the just readable inverse map.
1833 InverseMap inverse() const {
1834 return InverseMap(*this);
1841 /// \brief Provides a mutable, continuous and unique descriptor for each
1842 /// item in the graph.
1844 /// The DescriptorMap class provides a unique and continuous (but mutable)
1845 /// descriptor (id) for each item of the same type (e.g. node) in the
1846 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1847 /// different ids <li>\b continuous: the range of the ids is the set of
1848 /// integers between 0 and \c n-1, where \c n is the number of the items of
1849 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1850 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1851 /// with its member class \c InverseMap.
1853 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1854 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
1856 template <typename _Graph, typename _Item>
1857 class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
1860 typedef DefaultMap<_Graph, _Item, int> Map;
1863 /// The graph class of DescriptorMap.
1864 typedef _Graph Graph;
1866 /// The key type of DescriptorMap (Node, Edge, UEdge).
1867 typedef typename Map::Key Key;
1868 /// The value type of DescriptorMap.
1869 typedef typename Map::Value Value;
1871 /// \brief Constructor.
1873 /// Constructor for descriptor map.
1874 explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
1876 const typename Map::Notifier* notifier = Map::getNotifier();
1877 for (notifier->first(it); it != INVALID; notifier->next(it)) {
1878 Map::set(it, invMap.size());
1879 invMap.push_back(it);
1885 /// \brief Add a new key to the map.
1887 /// Add a new key to the map. It is called by the
1888 /// \c AlterationNotifier.
1889 virtual void add(const Item& item) {
1891 Map::set(item, invMap.size());
1892 invMap.push_back(item);
1895 /// \brief Add more new keys to the map.
1897 /// Add more new keys to the map. It is called by the
1898 /// \c AlterationNotifier.
1899 virtual void add(const std::vector<Item>& items) {
1901 for (int i = 0; i < (int)items.size(); ++i) {
1902 Map::set(items[i], invMap.size());
1903 invMap.push_back(items[i]);
1907 /// \brief Erase the key from the map.
1909 /// Erase the key from the map. It is called by the
1910 /// \c AlterationNotifier.
1911 virtual void erase(const Item& item) {
1912 Map::set(invMap.back(), Map::operator[](item));
1913 invMap[Map::operator[](item)] = invMap.back();
1918 /// \brief Erase more keys from the map.
1920 /// Erase more keys from the map. It is called by the
1921 /// \c AlterationNotifier.
1922 virtual void erase(const std::vector<Item>& items) {
1923 for (int i = 0; i < (int)items.size(); ++i) {
1924 Map::set(invMap.back(), Map::operator[](items[i]));
1925 invMap[Map::operator[](items[i])] = invMap.back();
1931 /// \brief Build the unique map.
1933 /// Build the unique map. It is called by the
1934 /// \c AlterationNotifier.
1935 virtual void build() {
1938 const typename Map::Notifier* notifier = Map::getNotifier();
1939 for (notifier->first(it); it != INVALID; notifier->next(it)) {
1940 Map::set(it, invMap.size());
1941 invMap.push_back(it);
1945 /// \brief Clear the keys from the map.
1947 /// Clear the keys from the map. It is called by the
1948 /// \c AlterationNotifier.
1949 virtual void clear() {
1956 /// \brief Returns the maximal value plus one.
1958 /// Returns the maximal value plus one in the map.
1959 unsigned int size() const {
1960 return invMap.size();
1963 /// \brief Swaps the position of the two items in the map.
1965 /// Swaps the position of the two items in the map.
1966 void swap(const Item& p, const Item& q) {
1967 int pi = Map::operator[](p);
1968 int qi = Map::operator[](q);
1975 /// \brief Gives back the \e descriptor of the item.
1977 /// Gives back the mutable and unique \e descriptor of the map.
1978 int operator[](const Item& item) const {
1979 return Map::operator[](item);
1982 /// \brief Gives back the item by its descriptor.
1984 /// Gives back th item by its descriptor.
1985 Item operator()(int id) const {
1991 typedef std::vector<Item> Container;
1995 /// \brief The inverse map type of DescriptorMap.
1997 /// The inverse map type of DescriptorMap.
2000 /// \brief Constructor of the InverseMap.
2002 /// Constructor of the InverseMap.
2003 explicit InverseMap(const DescriptorMap& _inverted)
2004 : inverted(_inverted) {}
2007 /// The value type of the InverseMap.
2008 typedef typename DescriptorMap::Key Value;
2009 /// The key type of the InverseMap.
2010 typedef typename DescriptorMap::Value Key;
2012 /// \brief Subscript operator.
2014 /// Subscript operator. It gives back the item
2015 /// that the descriptor belongs to currently.
2016 Value operator[](const Key& key) const {
2017 return inverted(key);
2020 /// \brief Size of the map.
2022 /// Returns the size of the map.
2023 unsigned int size() const {
2024 return inverted.size();
2028 const DescriptorMap& inverted;
2031 /// \brief Gives back the inverse of the map.
2033 /// Gives back the inverse of the map.
2034 const InverseMap inverse() const {
2035 return InverseMap(*this);
2039 /// \brief Returns the source of the given edge.
2041 /// The SourceMap gives back the source Node of the given edge.
2042 /// \author Balazs Dezso
2043 template <typename Graph>
2047 typedef typename Graph::Node Value;
2048 typedef typename Graph::Edge Key;
2050 /// \brief Constructor
2053 /// \param _graph The graph that the map belongs to.
2054 explicit SourceMap(const Graph& _graph) : graph(_graph) {}
2056 /// \brief The subscript operator.
2058 /// The subscript operator.
2059 /// \param edge The edge
2060 /// \return The source of the edge
2061 Value operator[](const Key& edge) const {
2062 return graph.source(edge);
2069 /// \brief Returns a \ref SourceMap class
2071 /// This function just returns an \ref SourceMap class.
2072 /// \relates SourceMap
2073 template <typename Graph>
2074 inline SourceMap<Graph> sourceMap(const Graph& graph) {
2075 return SourceMap<Graph>(graph);
2078 /// \brief Returns the target of the given edge.
2080 /// The TargetMap gives back the target Node of the given edge.
2081 /// \author Balazs Dezso
2082 template <typename Graph>
2086 typedef typename Graph::Node Value;
2087 typedef typename Graph::Edge Key;
2089 /// \brief Constructor
2092 /// \param _graph The graph that the map belongs to.
2093 explicit TargetMap(const Graph& _graph) : graph(_graph) {}
2095 /// \brief The subscript operator.
2097 /// The subscript operator.
2098 /// \param e The edge
2099 /// \return The target of the edge
2100 Value operator[](const Key& e) const {
2101 return graph.target(e);
2108 /// \brief Returns a \ref TargetMap class
2110 /// This function just returns a \ref TargetMap class.
2111 /// \relates TargetMap
2112 template <typename Graph>
2113 inline TargetMap<Graph> targetMap(const Graph& graph) {
2114 return TargetMap<Graph>(graph);
2117 /// \brief Returns the "forward" directed edge view of an undirected edge.
2119 /// Returns the "forward" directed edge view of an undirected edge.
2120 /// \author Balazs Dezso
2121 template <typename Graph>
2125 typedef typename Graph::Edge Value;
2126 typedef typename Graph::UEdge Key;
2128 /// \brief Constructor
2131 /// \param _graph The graph that the map belongs to.
2132 explicit ForwardMap(const Graph& _graph) : graph(_graph) {}
2134 /// \brief The subscript operator.
2136 /// The subscript operator.
2137 /// \param key An undirected edge
2138 /// \return The "forward" directed edge view of undirected edge
2139 Value operator[](const Key& key) const {
2140 return graph.direct(key, true);
2147 /// \brief Returns a \ref ForwardMap class
2149 /// This function just returns an \ref ForwardMap class.
2150 /// \relates ForwardMap
2151 template <typename Graph>
2152 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
2153 return ForwardMap<Graph>(graph);
2156 /// \brief Returns the "backward" directed edge view of an undirected edge.
2158 /// Returns the "backward" directed edge view of an undirected edge.
2159 /// \author Balazs Dezso
2160 template <typename Graph>
2164 typedef typename Graph::Edge Value;
2165 typedef typename Graph::UEdge Key;
2167 /// \brief Constructor
2170 /// \param _graph The graph that the map belongs to.
2171 explicit BackwardMap(const Graph& _graph) : graph(_graph) {}
2173 /// \brief The subscript operator.
2175 /// The subscript operator.
2176 /// \param key An undirected edge
2177 /// \return The "backward" directed edge view of undirected edge
2178 Value operator[](const Key& key) const {
2179 return graph.direct(key, false);
2186 /// \brief Returns a \ref BackwardMap class
2188 /// This function just returns a \ref BackwardMap class.
2189 /// \relates BackwardMap
2190 template <typename Graph>
2191 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
2192 return BackwardMap<Graph>(graph);
2195 /// \brief Potential difference map
2197 /// If there is an potential map on the nodes then we
2198 /// can get an edge map as we get the substraction of the
2199 /// values of the target and source.
2200 template <typename Graph, typename NodeMap>
2201 class PotentialDifferenceMap {
2203 typedef typename Graph::Edge Key;
2204 typedef typename NodeMap::Value Value;
2206 /// \brief Constructor
2208 /// Contructor of the map
2209 explicit PotentialDifferenceMap(const Graph& _graph,
2210 const NodeMap& _potential)
2211 : graph(_graph), potential(_potential) {}
2213 /// \brief Const subscription operator
2215 /// Const subscription operator
2216 Value operator[](const Key& edge) const {
2217 return potential[graph.target(edge)] - potential[graph.source(edge)];
2222 const NodeMap& potential;
2225 /// \brief Just returns a PotentialDifferenceMap
2227 /// Just returns a PotentialDifferenceMap
2228 /// \relates PotentialDifferenceMap
2229 template <typename Graph, typename NodeMap>
2230 PotentialDifferenceMap<Graph, NodeMap>
2231 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
2232 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
2235 /// \brief Map of the node in-degrees.
2237 /// This map returns the in-degree of a node. Once it is constructed,
2238 /// the degrees are stored in a standard NodeMap, so each query is done
2239 /// in constant time. On the other hand, the values are updated automatically
2240 /// whenever the graph changes.
2242 /// \warning Besides addNode() and addEdge(), a graph structure may provide
2243 /// alternative ways to modify the graph. The correct behavior of InDegMap
2244 /// is not guarantied if these additional features are used. For example
2245 /// the functions \ref ListGraph::changeSource() "changeSource()",
2246 /// \ref ListGraph::changeTarget() "changeTarget()" and
2247 /// \ref ListGraph::reverseEdge() "reverseEdge()"
2248 /// of \ref ListGraph will \e not update the degree values correctly.
2252 template <typename _Graph>
2254 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
2255 ::ItemNotifier::ObserverBase {
2259 typedef _Graph Graph;
2261 typedef typename Graph::Node Key;
2263 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
2264 ::ItemNotifier::ObserverBase Parent;
2268 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
2271 typedef DefaultMap<_Graph, Key, int> Parent;
2272 typedef typename Parent::Graph Graph;
2274 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
2276 virtual void add(const Key& key) {
2278 Parent::set(key, 0);
2281 virtual void add(const std::vector<Key>& keys) {
2283 for (int i = 0; i < (int)keys.size(); ++i) {
2284 Parent::set(keys[i], 0);
2291 /// \brief Constructor.
2293 /// Constructor for creating in-degree map.
2294 explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
2295 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
2297 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2298 deg[it] = countInEdges(graph, it);
2302 /// Gives back the in-degree of a Node.
2303 int operator[](const Key& key) const {
2309 typedef typename Graph::Edge Edge;
2311 virtual void add(const Edge& edge) {
2312 ++deg[graph.target(edge)];
2315 virtual void add(const std::vector<Edge>& edges) {
2316 for (int i = 0; i < (int)edges.size(); ++i) {
2317 ++deg[graph.target(edges[i])];
2321 virtual void erase(const Edge& edge) {
2322 --deg[graph.target(edge)];
2325 virtual void erase(const std::vector<Edge>& edges) {
2326 for (int i = 0; i < (int)edges.size(); ++i) {
2327 --deg[graph.target(edges[i])];
2331 virtual void build() {
2332 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2333 deg[it] = countInEdges(graph, it);
2337 virtual void clear() {
2338 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2344 const _Graph& graph;
2348 /// \brief Map of the node out-degrees.
2350 /// This map returns the out-degree of a node. Once it is constructed,
2351 /// the degrees are stored in a standard NodeMap, so each query is done
2352 /// in constant time. On the other hand, the values are updated automatically
2353 /// whenever the graph changes.
2355 /// \warning Besides addNode() and addEdge(), a graph structure may provide
2356 /// alternative ways to modify the graph. The correct behavior of OutDegMap
2357 /// is not guarantied if these additional features are used. For example
2358 /// the functions \ref ListGraph::changeSource() "changeSource()",
2359 /// \ref ListGraph::changeTarget() "changeTarget()" and
2360 /// \ref ListGraph::reverseEdge() "reverseEdge()"
2361 /// of \ref ListGraph will \e not update the degree values correctly.
2365 template <typename _Graph>
2367 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
2368 ::ItemNotifier::ObserverBase {
2372 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
2373 ::ItemNotifier::ObserverBase Parent;
2375 typedef _Graph Graph;
2377 typedef typename Graph::Node Key;
2381 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
2384 typedef DefaultMap<_Graph, Key, int> Parent;
2385 typedef typename Parent::Graph Graph;
2387 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
2389 virtual void add(const Key& key) {
2391 Parent::set(key, 0);
2393 virtual void add(const std::vector<Key>& keys) {
2395 for (int i = 0; i < (int)keys.size(); ++i) {
2396 Parent::set(keys[i], 0);
2403 /// \brief Constructor.
2405 /// Constructor for creating out-degree map.
2406 explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
2407 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
2409 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2410 deg[it] = countOutEdges(graph, it);
2414 /// Gives back the out-degree of a Node.
2415 int operator[](const Key& key) const {
2421 typedef typename Graph::Edge Edge;
2423 virtual void add(const Edge& edge) {
2424 ++deg[graph.source(edge)];
2427 virtual void add(const std::vector<Edge>& edges) {
2428 for (int i = 0; i < (int)edges.size(); ++i) {
2429 ++deg[graph.source(edges[i])];
2433 virtual void erase(const Edge& edge) {
2434 --deg[graph.source(edge)];
2437 virtual void erase(const std::vector<Edge>& edges) {
2438 for (int i = 0; i < (int)edges.size(); ++i) {
2439 --deg[graph.source(edges[i])];
2443 virtual void build() {
2444 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2445 deg[it] = countOutEdges(graph, it);
2449 virtual void clear() {
2450 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2456 const _Graph& graph;
2461 ///Fast edge look up between given endpoints.
2464 ///Using this class, you can find an edge in a graph from a given
2465 ///source to a given target in time <em>O(log d)</em>,
2466 ///where <em>d</em> is the out-degree of the source node.
2468 ///It is not possible to find \e all parallel edges between two nodes.
2469 ///Use \ref AllEdgeLookUp for this purpose.
2471 ///\warning This class is static, so you should refresh() (or at least
2472 ///refresh(Node)) this data structure
2473 ///whenever the graph changes. This is a time consuming (superlinearly
2474 ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
2476 ///\param G The type of the underlying graph.
2478 ///\sa AllEdgeLookUp
2483 GRAPH_TYPEDEFS(typename G)
2488 typename Graph::template NodeMap<Edge> _head;
2489 typename Graph::template EdgeMap<Edge> _left;
2490 typename Graph::template EdgeMap<Edge> _right;
2495 EdgeLess(const Graph &_g) : g(_g) {}
2496 bool operator()(Edge a,Edge b) const
2498 return g.target(a)<g.target(b);
2508 ///It builds up the search database, which remains valid until the graph
2510 EdgeLookUp(const Graph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
2513 Edge refresh_rec(std::vector<Edge> &v,int a,int b)
2517 _left[me] = a<m?refresh_rec(v,a,m-1):INVALID;
2518 _right[me] = m<b?refresh_rec(v,m+1,b):INVALID;
2522 ///Refresh the data structure at a node.
2524 ///Build up the search database of node \c n.
2526 ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2527 ///the number of the outgoing edges of \c n.
2528 void refresh(Node n)
2530 std::vector<Edge> v;
2531 for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
2533 std::sort(v.begin(),v.end(),EdgeLess(_g));
2534 _head[n]=refresh_rec(v,0,v.size()-1);
2536 else _head[n]=INVALID;
2538 ///Refresh the full data structure.
2540 ///Build up the full search database. In fact, it simply calls
2541 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2543 ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2544 ///the number of the edges of \c n and <em>D</em> is the maximum
2545 ///out-degree of the graph.
2549 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
2552 ///Find an edge between two nodes.
2554 ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
2555 /// <em>d</em> is the number of outgoing edges of \c s.
2556 ///\param s The source node
2557 ///\param t The target node
2558 ///\return An edge from \c s to \c t if there exists,
2559 ///\ref INVALID otherwise.
2561 ///\warning If you change the graph, refresh() must be called before using
2562 ///this operator. If you change the outgoing edges of
2563 ///a single node \c n, then
2564 ///\ref refresh(Node) "refresh(n)" is enough.
2566 Edge operator()(Node s, Node t) const
2570 e!=INVALID&&_g.target(e)!=t;
2571 e = t < _g.target(e)?_left[e]:_right[e]) ;
2577 ///Fast look up of all edges between given endpoints.
2580 ///This class is the same as \ref EdgeLookUp, with the addition
2581 ///that it makes it possible to find all edges between given endpoints.
2583 ///\warning This class is static, so you should refresh() (or at least
2584 ///refresh(Node)) this data structure
2585 ///whenever the graph changes. This is a time consuming (superlinearly
2586 ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
2588 ///\param G The type of the underlying graph.
2592 class AllEdgeLookUp : public EdgeLookUp<G>
2594 using EdgeLookUp<G>::_g;
2595 using EdgeLookUp<G>::_right;
2596 using EdgeLookUp<G>::_left;
2597 using EdgeLookUp<G>::_head;
2599 GRAPH_TYPEDEFS(typename G)
2602 typename Graph::template EdgeMap<Edge> _next;
2604 Edge refreshNext(Edge head,Edge next=INVALID)
2606 if(head==INVALID) return next;
2608 next=refreshNext(_right[head],next);
2609 // _next[head]=next;
2610 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
2612 return refreshNext(_left[head],head);
2618 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
2626 ///It builds up the search database, which remains valid until the graph
2628 AllEdgeLookUp(const Graph &g) : EdgeLookUp<G>(g), _next(g) {refreshNext();}
2630 ///Refresh the data structure at a node.
2632 ///Build up the search database of node \c n.
2634 ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2635 ///the number of the outgoing edges of \c n.
2637 void refresh(Node n)
2639 EdgeLookUp<G>::refresh(n);
2640 refreshNext(_head[n]);
2643 ///Refresh the full data structure.
2645 ///Build up the full search database. In fact, it simply calls
2646 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2648 ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2649 ///the number of the edges of \c n and <em>D</em> is the maximum
2650 ///out-degree of the graph.
2654 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
2657 ///Find an edge between two nodes.
2659 ///Find an edge between two nodes.
2660 ///\param s The source node
2661 ///\param t The target node
2662 ///\param prev The previous edge between \c s and \c t. It it is INVALID or
2663 ///not given, the operator finds the first appropriate edge.
2664 ///\return An edge from \c s to \c t after \c prev or
2665 ///\ref INVALID if there is no more.
2667 ///For example, you can count the number of edges from \c u to \c v in the
2670 ///AllEdgeLookUp<ListGraph> ae(g);
2673 ///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
2676 ///Finding the first edge take <em>O(</em>log<em>d)</em> time, where
2677 /// <em>d</em> is the number of outgoing edges of \c s. Then, the
2678 ///consecutive edges are found in constant time.
2680 ///\warning If you change the graph, refresh() must be called before using
2681 ///this operator. If you change the outgoing edges of
2682 ///a single node \c n, then
2683 ///\ref refresh(Node) "refresh(n)" is enough.
2686 Edge operator()(Node s, Node t, Edge prev=INVALID) const {}
2688 using EdgeLookUp<G>::operator() ;
2689 Edge operator()(Node s, Node t, Edge prev) const
2691 return prev==INVALID?(*this)(s,t):_next[prev];
2699 } //END OF NAMESPACE LEMON