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::CloneableTag, 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.clone(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::CloneableTag, 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.clone(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::CloneableTag, 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.clone(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 for creating id 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 map.
1591 int operator[](const Item& item) const { return graph->id(item);}
1599 /// \brief The class represents the inverse of its owner (IdMap).
1601 /// The class represents the inverse of its owner (IdMap).
1606 /// \brief Constructor.
1608 /// Constructor for creating an id-to-item map.
1609 explicit InverseMap(const Graph& _graph) : graph(&_graph) {}
1611 /// \brief Constructor.
1613 /// Constructor for creating an id-to-item map.
1614 explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
1616 /// \brief Gives back the given item from its id.
1618 /// Gives back the given item from its id.
1620 Item operator[](int id) const { return graph->fromId(id, Item());}
1625 /// \brief Gives back the inverse of the map.
1627 /// Gives back the inverse of the IdMap.
1628 InverseMap inverse() const { return InverseMap(*graph);}
1633 /// \brief General invertable graph-map type.
1635 /// This type provides simple invertable graph-maps.
1636 /// The InvertableMap wraps an arbitrary ReadWriteMap
1637 /// and if a key is set to a new value then store it
1638 /// in the inverse map.
1640 /// The values of the map can be accessed
1641 /// with stl compatible forward iterator.
1643 /// \param _Graph The graph type.
1644 /// \param _Item The item type of the graph.
1645 /// \param _Value The value type of the map.
1647 /// \see IterableValueMap
1648 template <typename _Graph, typename _Item, typename _Value>
1649 class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> {
1652 typedef DefaultMap<_Graph, _Item, _Value> Map;
1653 typedef _Graph Graph;
1655 typedef std::map<_Value, _Item> Container;
1660 /// The key type of InvertableMap (Node, Edge, UEdge).
1661 typedef typename Map::Key Key;
1662 /// The value type of the InvertableMap.
1663 typedef typename Map::Value Value;
1667 /// \brief Constructor.
1669 /// Construct a new InvertableMap for the graph.
1671 explicit InvertableMap(const Graph& graph) : Map(graph) {}
1673 /// \brief Forward iterator for values.
1675 /// This iterator is an stl compatible forward
1676 /// iterator on the values of the map. The values can
1677 /// be accessed in the [beginValue, endValue) range.
1680 : public std::iterator<std::forward_iterator_tag, Value> {
1681 friend class InvertableMap;
1683 ValueIterator(typename Container::const_iterator _it)
1689 ValueIterator& operator++() { ++it; return *this; }
1690 ValueIterator operator++(int) {
1691 ValueIterator tmp(*this);
1696 const Value& operator*() const { return it->first; }
1697 const Value* operator->() const { return &(it->first); }
1699 bool operator==(ValueIterator jt) const { return it == jt.it; }
1700 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1703 typename Container::const_iterator it;
1706 /// \brief Returns an iterator to the first value.
1708 /// Returns an stl compatible iterator to the
1709 /// first value of the map. The values of the
1710 /// map can be accessed in the [beginValue, endValue)
1712 ValueIterator beginValue() const {
1713 return ValueIterator(invMap.begin());
1716 /// \brief Returns an iterator after the last value.
1718 /// Returns an stl compatible iterator after the
1719 /// last value of the map. The values of the
1720 /// map can be accessed in the [beginValue, endValue)
1722 ValueIterator endValue() const {
1723 return ValueIterator(invMap.end());
1726 /// \brief The setter function of the map.
1728 /// Sets the mapped value.
1729 void set(const Key& key, const Value& val) {
1730 Value oldval = Map::operator[](key);
1731 typename Container::iterator it = invMap.find(oldval);
1732 if (it != invMap.end() && it->second == key) {
1735 invMap.insert(make_pair(val, key));
1739 /// \brief The getter function of the map.
1741 /// It gives back the value associated with the key.
1742 typename MapTraits<Map>::ConstReturnValue
1743 operator[](const Key& key) const {
1744 return Map::operator[](key);
1749 /// \brief Erase the key from the map.
1751 /// Erase the key to the map. It is called by the
1752 /// \c AlterationNotifier.
1753 virtual void erase(const Key& key) {
1754 Value val = Map::operator[](key);
1755 typename Container::iterator it = invMap.find(val);
1756 if (it != invMap.end() && it->second == key) {
1762 /// \brief Erase more keys from the map.
1764 /// Erase more keys from the map. It is called by the
1765 /// \c AlterationNotifier.
1766 virtual void erase(const std::vector<Key>& keys) {
1767 for (int i = 0; i < (int)keys.size(); ++i) {
1768 Value val = Map::operator[](keys[i]);
1769 typename Container::iterator it = invMap.find(val);
1770 if (it != invMap.end() && it->second == keys[i]) {
1777 /// \brief Clear the keys from the map and inverse map.
1779 /// Clear the keys from the map and inverse map. It is called by the
1780 /// \c AlterationNotifier.
1781 virtual void clear() {
1788 /// \brief The inverse map type.
1790 /// The inverse of this map. The subscript operator of the map
1791 /// gives back always the item what was last assigned to the value.
1794 /// \brief Constructor of the InverseMap.
1796 /// Constructor of the InverseMap.
1797 explicit InverseMap(const InvertableMap& _inverted)
1798 : inverted(_inverted) {}
1800 /// The value type of the InverseMap.
1801 typedef typename InvertableMap::Key Value;
1802 /// The key type of the InverseMap.
1803 typedef typename InvertableMap::Value Key;
1805 /// \brief Subscript operator.
1807 /// Subscript operator. It gives back always the item
1808 /// what was last assigned to the value.
1809 Value operator[](const Key& key) const {
1810 typename Container::const_iterator it = inverted.invMap.find(key);
1815 const InvertableMap& inverted;
1818 /// \brief It gives back the just readable inverse map.
1820 /// It gives back the just readable inverse map.
1821 InverseMap inverse() const {
1822 return InverseMap(*this);
1829 /// \brief Provides a mutable, continuous and unique descriptor for each
1830 /// item in the graph.
1832 /// The DescriptorMap class provides a unique and continuous (but mutable)
1833 /// descriptor (id) for each item of the same type (e.g. node) in the
1834 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1835 /// different ids <li>\b continuous: the range of the ids is the set of
1836 /// integers between 0 and \c n-1, where \c n is the number of the items of
1837 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1838 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1839 /// with its member class \c InverseMap.
1841 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1842 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
1844 template <typename _Graph, typename _Item>
1845 class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
1848 typedef DefaultMap<_Graph, _Item, int> Map;
1851 /// The graph class of DescriptorMap.
1852 typedef _Graph Graph;
1854 /// The key type of DescriptorMap (Node, Edge, UEdge).
1855 typedef typename Map::Key Key;
1856 /// The value type of DescriptorMap.
1857 typedef typename Map::Value Value;
1859 /// \brief Constructor.
1861 /// Constructor for descriptor map.
1862 explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
1864 const typename Map::Notifier* notifier = Map::getNotifier();
1865 for (notifier->first(it); it != INVALID; notifier->next(it)) {
1866 Map::set(it, invMap.size());
1867 invMap.push_back(it);
1873 /// \brief Add a new key to the map.
1875 /// Add a new key to the map. It is called by the
1876 /// \c AlterationNotifier.
1877 virtual void add(const Item& item) {
1879 Map::set(item, invMap.size());
1880 invMap.push_back(item);
1883 /// \brief Add more new keys to the map.
1885 /// Add more new keys to the map. It is called by the
1886 /// \c AlterationNotifier.
1887 virtual void add(const std::vector<Item>& items) {
1889 for (int i = 0; i < (int)items.size(); ++i) {
1890 Map::set(items[i], invMap.size());
1891 invMap.push_back(items[i]);
1895 /// \brief Erase the key from the map.
1897 /// Erase the key from the map. It is called by the
1898 /// \c AlterationNotifier.
1899 virtual void erase(const Item& item) {
1900 Map::set(invMap.back(), Map::operator[](item));
1901 invMap[Map::operator[](item)] = invMap.back();
1906 /// \brief Erase more keys from the map.
1908 /// Erase more keys from the map. It is called by the
1909 /// \c AlterationNotifier.
1910 virtual void erase(const std::vector<Item>& items) {
1911 for (int i = 0; i < (int)items.size(); ++i) {
1912 Map::set(invMap.back(), Map::operator[](items[i]));
1913 invMap[Map::operator[](items[i])] = invMap.back();
1919 /// \brief Build the unique map.
1921 /// Build the unique map. It is called by the
1922 /// \c AlterationNotifier.
1923 virtual void build() {
1926 const typename Map::Notifier* notifier = Map::getNotifier();
1927 for (notifier->first(it); it != INVALID; notifier->next(it)) {
1928 Map::set(it, invMap.size());
1929 invMap.push_back(it);
1933 /// \brief Clear the keys from the map.
1935 /// Clear the keys from the map. It is called by the
1936 /// \c AlterationNotifier.
1937 virtual void clear() {
1944 /// \brief Returns the maximal value plus one.
1946 /// Returns the maximal value plus one in the map.
1947 unsigned int size() const {
1948 return invMap.size();
1951 /// \brief Swaps the position of the two items in the map.
1953 /// Swaps the position of the two items in the map.
1954 void swap(const Item& p, const Item& q) {
1955 int pi = Map::operator[](p);
1956 int qi = Map::operator[](q);
1963 /// \brief Gives back the \e descriptor of the item.
1965 /// Gives back the mutable and unique \e descriptor of the map.
1966 int operator[](const Item& item) const {
1967 return Map::operator[](item);
1972 typedef std::vector<Item> Container;
1976 /// \brief The inverse map type of DescriptorMap.
1978 /// The inverse map type of DescriptorMap.
1981 /// \brief Constructor of the InverseMap.
1983 /// Constructor of the InverseMap.
1984 explicit InverseMap(const DescriptorMap& _inverted)
1985 : inverted(_inverted) {}
1988 /// The value type of the InverseMap.
1989 typedef typename DescriptorMap::Key Value;
1990 /// The key type of the InverseMap.
1991 typedef typename DescriptorMap::Value Key;
1993 /// \brief Subscript operator.
1995 /// Subscript operator. It gives back the item
1996 /// that the descriptor belongs to currently.
1997 Value operator[](const Key& key) const {
1998 return inverted.invMap[key];
2001 /// \brief Size of the map.
2003 /// Returns the size of the map.
2004 unsigned int size() const {
2005 return inverted.invMap.size();
2009 const DescriptorMap& inverted;
2012 /// \brief Gives back the inverse of the map.
2014 /// Gives back the inverse of the map.
2015 const InverseMap inverse() const {
2016 return InverseMap(*this);
2020 /// \brief Returns the source of the given edge.
2022 /// The SourceMap gives back the source Node of the given edge.
2023 /// \author Balazs Dezso
2024 template <typename Graph>
2028 typedef typename Graph::Node Value;
2029 typedef typename Graph::Edge Key;
2031 /// \brief Constructor
2034 /// \param _graph The graph that the map belongs to.
2035 explicit SourceMap(const Graph& _graph) : graph(_graph) {}
2037 /// \brief The subscript operator.
2039 /// The subscript operator.
2040 /// \param edge The edge
2041 /// \return The source of the edge
2042 Value operator[](const Key& edge) const {
2043 return graph.source(edge);
2050 /// \brief Returns a \ref SourceMap class
2052 /// This function just returns an \ref SourceMap class.
2053 /// \relates SourceMap
2054 template <typename Graph>
2055 inline SourceMap<Graph> sourceMap(const Graph& graph) {
2056 return SourceMap<Graph>(graph);
2059 /// \brief Returns the target of the given edge.
2061 /// The TargetMap gives back the target Node of the given edge.
2062 /// \author Balazs Dezso
2063 template <typename Graph>
2067 typedef typename Graph::Node Value;
2068 typedef typename Graph::Edge Key;
2070 /// \brief Constructor
2073 /// \param _graph The graph that the map belongs to.
2074 explicit TargetMap(const Graph& _graph) : graph(_graph) {}
2076 /// \brief The subscript operator.
2078 /// The subscript operator.
2079 /// \param e The edge
2080 /// \return The target of the edge
2081 Value operator[](const Key& e) const {
2082 return graph.target(e);
2089 /// \brief Returns a \ref TargetMap class
2091 /// This function just returns a \ref TargetMap class.
2092 /// \relates TargetMap
2093 template <typename Graph>
2094 inline TargetMap<Graph> targetMap(const Graph& graph) {
2095 return TargetMap<Graph>(graph);
2098 /// \brief Returns the "forward" directed edge view of an undirected edge.
2100 /// Returns the "forward" directed edge view of an undirected edge.
2101 /// \author Balazs Dezso
2102 template <typename Graph>
2106 typedef typename Graph::Edge Value;
2107 typedef typename Graph::UEdge Key;
2109 /// \brief Constructor
2112 /// \param _graph The graph that the map belongs to.
2113 explicit ForwardMap(const Graph& _graph) : graph(_graph) {}
2115 /// \brief The subscript operator.
2117 /// The subscript operator.
2118 /// \param key An undirected edge
2119 /// \return The "forward" directed edge view of undirected edge
2120 Value operator[](const Key& key) const {
2121 return graph.direct(key, true);
2128 /// \brief Returns a \ref ForwardMap class
2130 /// This function just returns an \ref ForwardMap class.
2131 /// \relates ForwardMap
2132 template <typename Graph>
2133 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
2134 return ForwardMap<Graph>(graph);
2137 /// \brief Returns the "backward" directed edge view of an undirected edge.
2139 /// Returns the "backward" directed edge view of an undirected edge.
2140 /// \author Balazs Dezso
2141 template <typename Graph>
2145 typedef typename Graph::Edge Value;
2146 typedef typename Graph::UEdge Key;
2148 /// \brief Constructor
2151 /// \param _graph The graph that the map belongs to.
2152 explicit BackwardMap(const Graph& _graph) : graph(_graph) {}
2154 /// \brief The subscript operator.
2156 /// The subscript operator.
2157 /// \param key An undirected edge
2158 /// \return The "backward" directed edge view of undirected edge
2159 Value operator[](const Key& key) const {
2160 return graph.direct(key, false);
2167 /// \brief Returns a \ref BackwardMap class
2169 /// This function just returns a \ref BackwardMap class.
2170 /// \relates BackwardMap
2171 template <typename Graph>
2172 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
2173 return BackwardMap<Graph>(graph);
2176 /// \brief Potential difference map
2178 /// If there is an potential map on the nodes then we
2179 /// can get an edge map as we get the substraction of the
2180 /// values of the target and source.
2181 template <typename Graph, typename NodeMap>
2182 class PotentialDifferenceMap {
2184 typedef typename Graph::Edge Key;
2185 typedef typename NodeMap::Value Value;
2187 /// \brief Constructor
2189 /// Contructor of the map
2190 explicit PotentialDifferenceMap(const Graph& _graph,
2191 const NodeMap& _potential)
2192 : graph(_graph), potential(_potential) {}
2194 /// \brief Const subscription operator
2196 /// Const subscription operator
2197 Value operator[](const Key& edge) const {
2198 return potential[graph.target(edge)] - potential[graph.source(edge)];
2203 const NodeMap& potential;
2206 /// \brief Just returns a PotentialDifferenceMap
2208 /// Just returns a PotentialDifferenceMap
2209 /// \relates PotentialDifferenceMap
2210 template <typename Graph, typename NodeMap>
2211 PotentialDifferenceMap<Graph, NodeMap>
2212 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
2213 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
2216 /// \brief Map of the node in-degrees.
2218 /// This map returns the in-degree of a node. Once it is constructed,
2219 /// the degrees are stored in a standard NodeMap, so each query is done
2220 /// in constant time. On the other hand, the values are updated automatically
2221 /// whenever the graph changes.
2223 /// \warning Besides addNode() and addEdge(), a graph structure may provide
2224 /// alternative ways to modify the graph. The correct behavior of InDegMap
2225 /// is not guarantied if these additional features are used. For example
2226 /// the functions \ref ListGraph::changeSource() "changeSource()",
2227 /// \ref ListGraph::changeTarget() "changeTarget()" and
2228 /// \ref ListGraph::reverseEdge() "reverseEdge()"
2229 /// of \ref ListGraph will \e not update the degree values correctly.
2233 template <typename _Graph>
2235 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
2236 ::ItemNotifier::ObserverBase {
2240 typedef _Graph Graph;
2242 typedef typename Graph::Node Key;
2244 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
2245 ::ItemNotifier::ObserverBase Parent;
2249 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
2252 typedef DefaultMap<_Graph, Key, int> Parent;
2253 typedef typename Parent::Graph Graph;
2255 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
2257 virtual void add(const Key& key) {
2259 Parent::set(key, 0);
2262 virtual void add(const std::vector<Key>& keys) {
2264 for (int i = 0; i < (int)keys.size(); ++i) {
2265 Parent::set(keys[i], 0);
2272 /// \brief Constructor.
2274 /// Constructor for creating in-degree map.
2275 explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
2276 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
2278 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2279 deg[it] = countInEdges(graph, it);
2283 /// Gives back the in-degree of a Node.
2284 int operator[](const Key& key) const {
2290 typedef typename Graph::Edge Edge;
2292 virtual void add(const Edge& edge) {
2293 ++deg[graph.target(edge)];
2296 virtual void add(const std::vector<Edge>& edges) {
2297 for (int i = 0; i < (int)edges.size(); ++i) {
2298 ++deg[graph.target(edges[i])];
2302 virtual void erase(const Edge& edge) {
2303 --deg[graph.target(edge)];
2306 virtual void erase(const std::vector<Edge>& edges) {
2307 for (int i = 0; i < (int)edges.size(); ++i) {
2308 --deg[graph.target(edges[i])];
2312 virtual void build() {
2313 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2314 deg[it] = countInEdges(graph, it);
2318 virtual void clear() {
2319 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2325 const _Graph& graph;
2329 /// \brief Map of the node out-degrees.
2331 /// This map returns the out-degree of a node. Once it is constructed,
2332 /// the degrees are stored in a standard NodeMap, so each query is done
2333 /// in constant time. On the other hand, the values are updated automatically
2334 /// whenever the graph changes.
2336 /// \warning Besides addNode() and addEdge(), a graph structure may provide
2337 /// alternative ways to modify the graph. The correct behavior of OutDegMap
2338 /// is not guarantied if these additional features are used. For example
2339 /// the functions \ref ListGraph::changeSource() "changeSource()",
2340 /// \ref ListGraph::changeTarget() "changeTarget()" and
2341 /// \ref ListGraph::reverseEdge() "reverseEdge()"
2342 /// of \ref ListGraph will \e not update the degree values correctly.
2346 template <typename _Graph>
2348 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
2349 ::ItemNotifier::ObserverBase {
2353 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
2354 ::ItemNotifier::ObserverBase Parent;
2356 typedef _Graph Graph;
2358 typedef typename Graph::Node Key;
2362 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
2365 typedef DefaultMap<_Graph, Key, int> Parent;
2366 typedef typename Parent::Graph Graph;
2368 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
2370 virtual void add(const Key& key) {
2372 Parent::set(key, 0);
2374 virtual void add(const std::vector<Key>& keys) {
2376 for (int i = 0; i < (int)keys.size(); ++i) {
2377 Parent::set(keys[i], 0);
2384 /// \brief Constructor.
2386 /// Constructor for creating out-degree map.
2387 explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
2388 Parent::attach(graph.getNotifier(typename _Graph::Edge()));
2390 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2391 deg[it] = countOutEdges(graph, it);
2395 /// Gives back the out-degree of a Node.
2396 int operator[](const Key& key) const {
2402 typedef typename Graph::Edge Edge;
2404 virtual void add(const Edge& edge) {
2405 ++deg[graph.source(edge)];
2408 virtual void add(const std::vector<Edge>& edges) {
2409 for (int i = 0; i < (int)edges.size(); ++i) {
2410 ++deg[graph.source(edges[i])];
2414 virtual void erase(const Edge& edge) {
2415 --deg[graph.source(edge)];
2418 virtual void erase(const std::vector<Edge>& edges) {
2419 for (int i = 0; i < (int)edges.size(); ++i) {
2420 --deg[graph.source(edges[i])];
2424 virtual void build() {
2425 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2426 deg[it] = countOutEdges(graph, it);
2430 virtual void clear() {
2431 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2437 const _Graph& graph;
2442 ///Fast edge look up between given endpoints.
2445 ///Using this class, you can find an edge in a graph from a given
2446 ///source to a given target in time <em>O(log d)</em>,
2447 ///where <em>d</em> is the out-degree of the source node.
2449 ///It is not possible to find \e all parallel edges between two nodes.
2450 ///Use \ref AllEdgeLookUp for this purpose.
2452 ///\warning This class is static, so you should refresh() (or at least
2453 ///refresh(Node)) this data structure
2454 ///whenever the graph changes. This is a time consuming (superlinearly
2455 ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
2457 ///\param G The type of the underlying graph.
2459 ///\sa AllEdgeLookUp
2464 GRAPH_TYPEDEFS(typename G)
2469 typename Graph::template NodeMap<Edge> _head;
2470 typename Graph::template EdgeMap<Edge> _left;
2471 typename Graph::template EdgeMap<Edge> _right;
2476 EdgeLess(const Graph &_g) : g(_g) {}
2477 bool operator()(Edge a,Edge b) const
2479 return g.target(a)<g.target(b);
2489 ///It builds up the search database, which remains valid until the graph
2491 EdgeLookUp(const Graph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
2494 Edge refresh_rec(std::vector<Edge> &v,int a,int b)
2498 _left[me] = a<m?refresh_rec(v,a,m-1):INVALID;
2499 _right[me] = m<b?refresh_rec(v,m+1,b):INVALID;
2503 ///Refresh the data structure at a node.
2505 ///Build up the search database of node \c n.
2507 ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2508 ///the number of the outgoing edges of \c n.
2509 void refresh(Node n)
2511 std::vector<Edge> v;
2512 for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
2514 std::sort(v.begin(),v.end(),EdgeLess(_g));
2515 _head[n]=refresh_rec(v,0,v.size()-1);
2517 else _head[n]=INVALID;
2519 ///Refresh the full data structure.
2521 ///Build up the full search database. In fact, it simply calls
2522 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2524 ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2525 ///the number of the edges of \c n and <em>D</em> is the maximum
2526 ///out-degree of the graph.
2530 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
2533 ///Find an edge between two nodes.
2535 ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
2536 /// <em>d</em> is the number of outgoing edges of \c s.
2537 ///\param s The source node
2538 ///\param t The target node
2539 ///\return An edge from \c s to \c t if there exists,
2540 ///\ref INVALID otherwise.
2542 ///\warning If you change the graph, refresh() must be called before using
2543 ///this operator. If you change the outgoing edges of
2544 ///a single node \c n, then
2545 ///\ref refresh(Node) "refresh(n)" is enough.
2547 Edge operator()(Node s, Node t) const
2551 e!=INVALID&&_g.target(e)!=t;
2552 e = t < _g.target(e)?_left[e]:_right[e]) ;
2558 ///Fast look up of all edges between given endpoints.
2561 ///This class is the same as \ref EdgeLookUp, with the addition
2562 ///that it makes it possible to find all edges between given endpoints.
2564 ///\warning This class is static, so you should refresh() (or at least
2565 ///refresh(Node)) this data structure
2566 ///whenever the graph changes. This is a time consuming (superlinearly
2567 ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
2569 ///\param G The type of the underlying graph.
2573 class AllEdgeLookUp : public EdgeLookUp<G>
2575 using EdgeLookUp<G>::_g;
2576 using EdgeLookUp<G>::_right;
2577 using EdgeLookUp<G>::_left;
2578 using EdgeLookUp<G>::_head;
2580 GRAPH_TYPEDEFS(typename G)
2583 typename Graph::template EdgeMap<Edge> _next;
2585 Edge refreshNext(Edge head,Edge next=INVALID)
2587 if(head==INVALID) return next;
2589 next=refreshNext(_right[head],next);
2590 // _next[head]=next;
2591 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
2593 return refreshNext(_left[head],head);
2599 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
2607 ///It builds up the search database, which remains valid until the graph
2609 AllEdgeLookUp(const Graph &g) : EdgeLookUp<G>(g), _next(g) {refreshNext();}
2611 ///Refresh the data structure at a node.
2613 ///Build up the search database of node \c n.
2615 ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2616 ///the number of the outgoing edges of \c n.
2618 void refresh(Node n)
2620 EdgeLookUp<G>::refresh(n);
2621 refreshNext(_head[n]);
2624 ///Refresh the full data structure.
2626 ///Build up the full search database. In fact, it simply calls
2627 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2629 ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2630 ///the number of the edges of \c n and <em>D</em> is the maximum
2631 ///out-degree of the graph.
2635 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
2638 ///Find an edge between two nodes.
2640 ///Find an edge between two nodes.
2641 ///\param s The source node
2642 ///\param t The target node
2643 ///\param prev The previous edge between \c s and \c t. It it is INVALID or
2644 ///not given, the operator finds the first appropriate edge.
2645 ///\return An edge from \c s to \c t after \prev or
2646 ///\ref INVALID if there is no more.
2648 ///For example, you can count the number of edges from \c u to \c v in the
2651 ///AllEdgeLookUp<ListGraph> ae(g);
2654 ///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
2657 ///Finding the first edge take <em>O(</em>log<em>d)</em> time, where
2658 /// <em>d</em> is the number of outgoing edges of \c s. Then, the
2659 ///consecutive edges are found in constant time.
2661 ///\warning If you change the graph, refresh() must be called before using
2662 ///this operator. If you change the outgoing edges of
2663 ///a single node \c n, then
2664 ///\ref refresh(Node) "refresh(n)" is enough.
2667 Edge operator()(Node s, Node t, Edge prev=INVALID) const {}
2669 using EdgeLookUp<G>::operator() ;
2670 Edge operator()(Node s, Node t, Edge prev) const
2672 return prev==INVALID?(*this)(s,t):_next[prev];
2680 } //END OF NAMESPACE LEMON