3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
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.
42 /// \addtogroup gutils
45 ///Creates convenience typedefs for the graph types and iterators
47 ///This \c \#define creates convenience typedefs for the following types
48 ///of \c Graph: \c Node, \c NodeIt, \c Edge, \c EdgeIt, \c InEdgeIt,
50 ///\note If \c G it a template parameter, it should be used in this way.
52 /// GRAPH_TYPEDEFS(typename G)
55 ///\warning There are no typedefs for the graph maps because of the lack of
56 ///template typedefs in C++.
57 #define GRAPH_TYPEDEFS(Graph) \
58 typedef Graph:: Node Node; \
59 typedef Graph:: NodeIt NodeIt; \
60 typedef Graph:: Edge Edge; \
61 typedef Graph:: EdgeIt EdgeIt; \
62 typedef Graph:: InEdgeIt InEdgeIt; \
63 typedef Graph::OutEdgeIt OutEdgeIt;
65 ///Creates convenience typedefs for the undirected graph types and iterators
67 ///This \c \#define creates the same convenience typedefs as defined by
68 ///\ref GRAPH_TYPEDEFS(Graph) and three more, namely it creates
69 ///\c UEdge, \c UEdgeIt, \c IncEdgeIt,
71 ///\note If \c G it a template parameter, it should be used in this way.
73 /// UGRAPH_TYPEDEFS(typename G)
76 ///\warning There are no typedefs for the graph maps because of the lack of
77 ///template typedefs in C++.
78 #define UGRAPH_TYPEDEFS(Graph) \
79 GRAPH_TYPEDEFS(Graph) \
80 typedef Graph:: UEdge UEdge; \
81 typedef Graph:: UEdgeIt UEdgeIt; \
82 typedef Graph:: IncEdgeIt IncEdgeIt;
84 ///\brief Creates convenience typedefs for the bipartite undirected graph
85 ///types and iterators
87 ///This \c \#define creates the same convenience typedefs as defined by
88 ///\ref UGRAPH_TYPEDEFS(Graph) and two more, namely it creates
89 ///\c ANodeIt, \c BNodeIt,
91 ///\note If \c G it a template parameter, it should be used in this way.
93 /// BPUGRAPH_TYPEDEFS(typename G)
96 ///\warning There are no typedefs for the graph maps because of the lack of
97 ///template typedefs in C++.
98 #define BPUGRAPH_TYPEDEFS(Graph) \
99 UGRAPH_TYPEDEFS(Graph) \
100 typedef Graph::ANode ANode; \
101 typedef Graph::BNode BNode; \
102 typedef Graph::ANodeIt ANodeIt; \
103 typedef Graph::BNodeIt BNodeIt;
105 /// \brief Function to count the items in the graph.
107 /// This function counts the items (nodes, edges etc) in the graph.
108 /// The complexity of the function is O(n) because
109 /// it iterates on all of the items.
111 template <typename Graph, typename Item>
112 inline int countItems(const Graph& g) {
113 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
115 for (ItemIt it(g); it != INVALID; ++it) {
123 namespace _graph_utils_bits {
125 template <typename Graph, typename Enable = void>
126 struct CountNodesSelector {
127 static int count(const Graph &g) {
128 return countItems<Graph, typename Graph::Node>(g);
132 template <typename Graph>
133 struct CountNodesSelector<
135 enable_if<typename Graph::NodeNumTag, void>::type>
137 static int count(const Graph &g) {
143 /// \brief Function to count the nodes in the graph.
145 /// This function counts the nodes in the graph.
146 /// The complexity of the function is O(n) but for some
147 /// graph structures it is specialized to run in O(1).
149 /// \todo Refer how to specialize it.
151 template <typename Graph>
152 inline int countNodes(const Graph& g) {
153 return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
156 namespace _graph_utils_bits {
158 template <typename Graph, typename Enable = void>
159 struct CountANodesSelector {
160 static int count(const Graph &g) {
161 return countItems<Graph, typename Graph::ANode>(g);
165 template <typename Graph>
166 struct CountANodesSelector<
168 enable_if<typename Graph::NodeNumTag, void>::type>
170 static int count(const Graph &g) {
176 /// \brief Function to count the anodes in the graph.
178 /// This function counts the anodes in the graph.
179 /// The complexity of the function is O(an) but for some
180 /// graph structures it is specialized to run in O(1).
182 /// \todo Refer how to specialize it.
184 template <typename Graph>
185 inline int countANodes(const Graph& g) {
186 return _graph_utils_bits::CountANodesSelector<Graph>::count(g);
189 namespace _graph_utils_bits {
191 template <typename Graph, typename Enable = void>
192 struct CountBNodesSelector {
193 static int count(const Graph &g) {
194 return countItems<Graph, typename Graph::BNode>(g);
198 template <typename Graph>
199 struct CountBNodesSelector<
201 enable_if<typename Graph::NodeNumTag, void>::type>
203 static int count(const Graph &g) {
209 /// \brief Function to count the bnodes in the graph.
211 /// This function counts the bnodes in the graph.
212 /// The complexity of the function is O(bn) but for some
213 /// graph structures it is specialized to run in O(1).
215 /// \todo Refer how to specialize it.
217 template <typename Graph>
218 inline int countBNodes(const Graph& g) {
219 return _graph_utils_bits::CountBNodesSelector<Graph>::count(g);
225 namespace _graph_utils_bits {
227 template <typename Graph, typename Enable = void>
228 struct CountEdgesSelector {
229 static int count(const Graph &g) {
230 return countItems<Graph, typename Graph::Edge>(g);
234 template <typename Graph>
235 struct CountEdgesSelector<
237 typename enable_if<typename Graph::EdgeNumTag, void>::type>
239 static int count(const Graph &g) {
245 /// \brief Function to count the edges in the graph.
247 /// This function counts the edges in the graph.
248 /// The complexity of the function is O(e) but for some
249 /// graph structures it is specialized to run in O(1).
251 template <typename Graph>
252 inline int countEdges(const Graph& g) {
253 return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
256 // Undirected edge counting:
257 namespace _graph_utils_bits {
259 template <typename Graph, typename Enable = void>
260 struct CountUEdgesSelector {
261 static int count(const Graph &g) {
262 return countItems<Graph, typename Graph::UEdge>(g);
266 template <typename Graph>
267 struct CountUEdgesSelector<
269 typename enable_if<typename Graph::EdgeNumTag, void>::type>
271 static int count(const Graph &g) {
277 /// \brief Function to count the undirected edges in the graph.
279 /// This function counts the undirected edges in the graph.
280 /// The complexity of the function is O(e) but for some
281 /// graph structures it is specialized to run in O(1).
283 template <typename Graph>
284 inline int countUEdges(const Graph& g) {
285 return _graph_utils_bits::CountUEdgesSelector<Graph>::count(g);
290 template <typename Graph, typename DegIt>
291 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
293 for (DegIt it(_g, _n); it != INVALID; ++it) {
299 /// \brief Function to count the number of the out-edges from node \c n.
301 /// This function counts the number of the out-edges from node \c n
303 template <typename Graph>
304 inline int countOutEdges(const Graph& _g, const typename Graph::Node& _n) {
305 return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
308 /// \brief Function to count the number of the in-edges to node \c n.
310 /// This function counts the number of the in-edges to node \c n
312 template <typename Graph>
313 inline int countInEdges(const Graph& _g, const typename Graph::Node& _n) {
314 return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
317 /// \brief Function to count the number of the inc-edges to node \c n.
319 /// This function counts the number of the inc-edges to node \c n
321 template <typename Graph>
322 inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
323 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
326 namespace _graph_utils_bits {
328 template <typename Graph, typename Enable = void>
329 struct FindEdgeSelector {
330 typedef typename Graph::Node Node;
331 typedef typename Graph::Edge Edge;
332 static Edge find(const Graph &g, Node u, Node v, Edge e) {
338 while (e != INVALID && g.target(e) != v) {
345 template <typename Graph>
346 struct FindEdgeSelector<
348 typename enable_if<typename Graph::FindEdgeTag, void>::type>
350 typedef typename Graph::Node Node;
351 typedef typename Graph::Edge Edge;
352 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
353 return g.findEdge(u, v, prev);
358 /// \brief Finds an edge between two nodes of a graph.
360 /// Finds an edge from node \c u to node \c v in graph \c g.
362 /// If \c prev is \ref INVALID (this is the default value), then
363 /// it finds the first edge from \c u to \c v. Otherwise it looks for
364 /// the next edge from \c u to \c v after \c prev.
365 /// \return The found edge or \ref INVALID if there is no such an edge.
367 /// Thus you can iterate through each edge from \c u to \c v as it follows.
369 /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
377 template <typename Graph>
378 inline typename Graph::Edge
379 findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
380 typename Graph::Edge prev = INVALID) {
381 return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, prev);
384 /// \brief Iterator for iterating on edges connected the same nodes.
386 /// Iterator for iterating on edges connected the same nodes. It is
387 /// higher level interface for the findEdge() function. You can
388 /// use it the following way:
390 /// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
399 /// \author Balazs Dezso
400 template <typename _Graph>
401 class ConEdgeIt : public _Graph::Edge {
404 typedef _Graph Graph;
405 typedef typename Graph::Edge Parent;
407 typedef typename Graph::Edge Edge;
408 typedef typename Graph::Node Node;
410 /// \brief Constructor.
412 /// Construct a new ConEdgeIt iterating on the edges which
413 /// connects the \c u and \c v node.
414 ConEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
415 Parent::operator=(findEdge(graph, u, v));
418 /// \brief Constructor.
420 /// Construct a new ConEdgeIt which continues the iterating from
422 ConEdgeIt(const Graph& g, Edge e) : Parent(e), graph(g) {}
424 /// \brief Increment operator.
426 /// It increments the iterator and gives back the next edge.
427 ConEdgeIt& operator++() {
428 Parent::operator=(findEdge(graph, graph.source(*this),
429 graph.target(*this), *this));
436 namespace _graph_utils_bits {
438 template <typename Graph, typename Enable = void>
439 struct FindUEdgeSelector {
440 typedef typename Graph::Node Node;
441 typedef typename Graph::UEdge UEdge;
442 static UEdge find(const Graph &g, Node u, Node v, UEdge e) {
448 b = g.source(e) == u;
451 while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
461 while (e != INVALID && (!b || g.target(e) != v)) {
469 template <typename Graph>
470 struct FindUEdgeSelector<
472 typename enable_if<typename Graph::FindEdgeTag, void>::type>
474 typedef typename Graph::Node Node;
475 typedef typename Graph::UEdge UEdge;
476 static UEdge find(const Graph &g, Node u, Node v, UEdge prev) {
477 return g.findUEdge(u, v, prev);
482 /// \brief Finds an uedge between two nodes of a graph.
484 /// Finds an uedge from node \c u to node \c v in graph \c g.
485 /// If the node \c u and node \c v is equal then each loop edge
486 /// will be enumerated.
488 /// If \c prev is \ref INVALID (this is the default value), then
489 /// it finds the first edge from \c u to \c v. Otherwise it looks for
490 /// the next edge from \c u to \c v after \c prev.
491 /// \return The found edge or \ref INVALID if there is no such an edge.
493 /// Thus you can iterate through each edge from \c u to \c v as it follows.
495 /// for(UEdge e = findUEdge(g,u,v); e != INVALID;
496 /// e = findUEdge(g,u,v,e)) {
503 template <typename Graph>
504 inline typename Graph::UEdge
505 findUEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
506 typename Graph::UEdge p = INVALID) {
507 return _graph_utils_bits::FindUEdgeSelector<Graph>::find(g, u, v, p);
510 /// \brief Iterator for iterating on uedges connected the same nodes.
512 /// Iterator for iterating on uedges connected the same nodes. It is
513 /// higher level interface for the findUEdge() function. You can
514 /// use it the following way:
516 /// for (ConUEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
523 /// \author Balazs Dezso
524 template <typename _Graph>
525 class ConUEdgeIt : public _Graph::UEdge {
528 typedef _Graph Graph;
529 typedef typename Graph::UEdge Parent;
531 typedef typename Graph::UEdge UEdge;
532 typedef typename Graph::Node Node;
534 /// \brief Constructor.
536 /// Construct a new ConUEdgeIt iterating on the edges which
537 /// connects the \c u and \c v node.
538 ConUEdgeIt(const Graph& g, Node u, Node v) : graph(g) {
539 Parent::operator=(findUEdge(graph, u, v));
542 /// \brief Constructor.
544 /// Construct a new ConUEdgeIt which continues the iterating from
546 ConUEdgeIt(const Graph& g, UEdge e) : Parent(e), graph(g) {}
548 /// \brief Increment operator.
550 /// It increments the iterator and gives back the next edge.
551 ConUEdgeIt& operator++() {
552 Parent::operator=(findUEdge(graph, graph.source(*this),
553 graph.target(*this), *this));
560 /// \brief Copy a map.
562 /// This function copies the \c source map to the \c target map. It uses the
563 /// given iterator to iterate on the data structure and it uses the \c ref
564 /// mapping to convert the source's keys to the target's keys.
565 template <typename Target, typename Source,
566 typename ItemIt, typename Ref>
567 void copyMap(Target& target, const Source& source,
568 ItemIt it, const Ref& ref) {
569 for (; it != INVALID; ++it) {
570 target[ref[it]] = source[it];
574 /// \brief Copy the source map to the target map.
576 /// Copy the \c source map to the \c target map. It uses the given iterator
577 /// to iterate on the data structure.
578 template <typename Target, typename Source, typename ItemIt>
579 void copyMap(Target& target, const Source& source, ItemIt it) {
580 for (; it != INVALID; ++it) {
581 target[it] = source[it];
585 namespace _graph_utils_bits {
587 template <typename Graph, typename Item, typename RefMap>
590 virtual void copy(const Graph& source, const RefMap& refMap) = 0;
592 virtual ~MapCopyBase() {}
595 template <typename Graph, typename Item, typename RefMap,
596 typename TargetMap, typename SourceMap>
597 class MapCopy : public MapCopyBase<Graph, Item, RefMap> {
600 MapCopy(TargetMap& tmap, const SourceMap& map)
601 : _tmap(tmap), _map(map) {}
603 virtual void copy(const Graph& graph, const RefMap& refMap) {
604 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
605 for (ItemIt it(graph); it != INVALID; ++it) {
606 _tmap.set(refMap[it], _map[it]);
612 const SourceMap& _map;
615 template <typename Graph, typename Item, typename RefMap, typename It>
616 class ItemCopy : public MapCopyBase<Graph, Item, RefMap> {
619 ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
621 virtual void copy(const Graph&, const RefMap& refMap) {
630 template <typename Graph, typename Item, typename RefMap, typename Ref>
631 class RefCopy : public MapCopyBase<Graph, Item, RefMap> {
634 RefCopy(Ref& map) : _map(map) {}
636 virtual void copy(const Graph& graph, const RefMap& refMap) {
637 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
638 for (ItemIt it(graph); it != INVALID; ++it) {
639 _map.set(it, refMap[it]);
647 template <typename Graph, typename Item, typename RefMap,
649 class CrossRefCopy : public MapCopyBase<Graph, Item, RefMap> {
652 CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
654 virtual void copy(const Graph& graph, const RefMap& refMap) {
655 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
656 for (ItemIt it(graph); it != INVALID; ++it) {
657 _cmap.set(refMap[it], it);
665 template <typename Graph, typename Enable = void>
666 struct GraphCopySelector {
667 template <typename Source, typename NodeRefMap, typename EdgeRefMap>
668 static void copy(Graph &target, const Source& source,
669 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
670 for (typename Source::NodeIt it(source); it != INVALID; ++it) {
671 nodeRefMap[it] = target.addNode();
673 for (typename Source::EdgeIt it(source); it != INVALID; ++it) {
674 edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
675 nodeRefMap[source.target(it)]);
680 template <typename Graph>
681 struct GraphCopySelector<
683 typename enable_if<typename Graph::BuildTag, void>::type>
685 template <typename Source, typename NodeRefMap, typename EdgeRefMap>
686 static void copy(Graph &target, const Source& source,
687 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
688 target.build(source, nodeRefMap, edgeRefMap);
692 template <typename UGraph, typename Enable = void>
693 struct UGraphCopySelector {
694 template <typename Source, typename NodeRefMap, typename UEdgeRefMap>
695 static void copy(UGraph &target, const Source& source,
696 NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
697 for (typename Source::NodeIt it(source); it != INVALID; ++it) {
698 nodeRefMap[it] = target.addNode();
700 for (typename Source::UEdgeIt it(source); it != INVALID; ++it) {
701 uEdgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
702 nodeRefMap[source.target(it)]);
707 template <typename UGraph>
708 struct UGraphCopySelector<
710 typename enable_if<typename UGraph::BuildTag, void>::type>
712 template <typename Source, typename NodeRefMap, typename UEdgeRefMap>
713 static void copy(UGraph &target, const Source& source,
714 NodeRefMap& nodeRefMap, UEdgeRefMap& uEdgeRefMap) {
715 target.build(source, nodeRefMap, uEdgeRefMap);
719 template <typename BpUGraph, typename Enable = void>
720 struct BpUGraphCopySelector {
721 template <typename Source, typename ANodeRefMap,
722 typename BNodeRefMap, typename UEdgeRefMap>
723 static void copy(BpUGraph &target, const Source& source,
724 ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
725 UEdgeRefMap& uEdgeRefMap) {
726 for (typename Source::ANodeIt it(source); it != INVALID; ++it) {
727 aNodeRefMap[it] = target.addANode();
729 for (typename Source::BNodeIt it(source); it != INVALID; ++it) {
730 bNodeRefMap[it] = target.addBNode();
732 for (typename Source::UEdgeIt it(source); it != INVALID; ++it) {
733 uEdgeRefMap[it] = target.addEdge(aNodeRefMap[source.aNode(it)],
734 bNodeRefMap[source.bNode(it)]);
739 template <typename BpUGraph>
740 struct BpUGraphCopySelector<
742 typename enable_if<typename BpUGraph::BuildTag, void>::type>
744 template <typename Source, typename ANodeRefMap,
745 typename BNodeRefMap, typename UEdgeRefMap>
746 static void copy(BpUGraph &target, const Source& source,
747 ANodeRefMap& aNodeRefMap, BNodeRefMap& bNodeRefMap,
748 UEdgeRefMap& uEdgeRefMap) {
749 target.build(source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
756 /// \brief Class to copy a graph.
758 /// Class to copy a graph to another graph (duplicate a graph). The
759 /// simplest way of using it is through the \c copyGraph() function.
760 template <typename Target, typename Source>
764 typedef typename Source::Node Node;
765 typedef typename Source::NodeIt NodeIt;
766 typedef typename Source::Edge Edge;
767 typedef typename Source::EdgeIt EdgeIt;
769 typedef typename Target::Node TNode;
770 typedef typename Target::Edge TEdge;
772 typedef typename Source::template NodeMap<TNode> NodeRefMap;
773 typedef typename Source::template EdgeMap<TEdge> EdgeRefMap;
779 /// \brief Constructor for the GraphCopy.
781 /// It copies the content of the \c _source graph into the
782 /// \c _target graph.
783 GraphCopy(Target& _target, const Source& _source)
784 : source(_source), target(_target) {}
786 /// \brief Destructor of the GraphCopy
788 /// Destructor of the GraphCopy
790 for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
791 delete nodeMapCopies[i];
793 for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
794 delete edgeMapCopies[i];
799 /// \brief Copies the node references into the given map.
801 /// Copies the node references into the given map.
802 template <typename NodeRef>
803 GraphCopy& nodeRef(NodeRef& map) {
804 nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node,
805 NodeRefMap, NodeRef>(map));
809 /// \brief Copies the node cross references into the given map.
811 /// Copies the node cross references (reverse references) into
813 template <typename NodeCrossRef>
814 GraphCopy& nodeCrossRef(NodeCrossRef& map) {
815 nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
816 NodeRefMap, NodeCrossRef>(map));
820 /// \brief Make copy of the given map.
822 /// Makes copy of the given map for the newly created graph.
823 /// The new map's key type is the target graph's node type,
824 /// and the copied map's key type is the source graph's node
826 template <typename TargetMap, typename SourceMap>
827 GraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
828 nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node,
829 NodeRefMap, TargetMap, SourceMap>(tmap, map));
833 /// \brief Make a copy of the given node.
835 /// Make a copy of the given node.
836 GraphCopy& node(TNode& tnode, const Node& snode) {
837 nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node,
838 NodeRefMap, TNode>(tnode, snode));
842 /// \brief Copies the edge references into the given map.
844 /// Copies the edge references into the given map.
845 template <typename EdgeRef>
846 GraphCopy& edgeRef(EdgeRef& map) {
847 edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge,
848 EdgeRefMap, EdgeRef>(map));
852 /// \brief Copies the edge cross references into the given map.
854 /// Copies the edge cross references (reverse references) into
856 template <typename EdgeCrossRef>
857 GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
858 edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
859 EdgeRefMap, EdgeCrossRef>(map));
863 /// \brief Make copy of the given map.
865 /// Makes copy of the given map for the newly created graph.
866 /// The new map's key type is the target graph's edge type,
867 /// and the copied map's key type is the source graph's edge
869 template <typename TargetMap, typename SourceMap>
870 GraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
871 edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge,
872 EdgeRefMap, TargetMap, SourceMap>(tmap, map));
876 /// \brief Make a copy of the given edge.
878 /// Make a copy of the given edge.
879 GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
880 edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge,
881 EdgeRefMap, TEdge>(tedge, sedge));
885 /// \brief Executes the copies.
887 /// Executes the copies.
889 NodeRefMap nodeRefMap(source);
890 EdgeRefMap edgeRefMap(source);
891 _graph_utils_bits::GraphCopySelector<Target>::
892 copy(target, source, nodeRefMap, edgeRefMap);
893 for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
894 nodeMapCopies[i]->copy(source, nodeRefMap);
896 for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
897 edgeMapCopies[i]->copy(source, edgeRefMap);
904 const Source& source;
907 std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* >
910 std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* >
915 /// \brief Copy a graph to another graph.
917 /// Copy a graph to another graph.
918 /// The usage of the function:
921 /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
924 /// After the copy the \c nr map will contain the mapping from the
925 /// source graph's nodes to the target graph's nodes and the \c ecr will
926 /// contain the mapping from the target graph's edges to the source's
930 template <typename Target, typename Source>
931 GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
932 return GraphCopy<Target, Source>(target, source);
935 /// \brief Class to copy an undirected graph.
937 /// Class to copy an undirected graph to another graph (duplicate a graph).
938 /// The simplest way of using it is through the \c copyUGraph() function.
939 template <typename Target, typename Source>
943 typedef typename Source::Node Node;
944 typedef typename Source::NodeIt NodeIt;
945 typedef typename Source::Edge Edge;
946 typedef typename Source::EdgeIt EdgeIt;
947 typedef typename Source::UEdge UEdge;
948 typedef typename Source::UEdgeIt UEdgeIt;
950 typedef typename Target::Node TNode;
951 typedef typename Target::Edge TEdge;
952 typedef typename Target::UEdge TUEdge;
954 typedef typename Source::template NodeMap<TNode> NodeRefMap;
955 typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
958 EdgeRefMap(const Target& _target, const Source& _source,
959 const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref)
960 : target(_target), source(_source),
961 uedge_ref(_uedge_ref), node_ref(_node_ref) {}
963 typedef typename Source::Edge Key;
964 typedef typename Target::Edge Value;
966 Value operator[](const Key& key) const {
968 (source.direction(key) ==
969 (node_ref[source.source(static_cast<const UEdge&>(key))] ==
970 target.source(uedge_ref[static_cast<const UEdge&>(key)])));
971 return target.direct(uedge_ref[key], forward);
974 const Target& target;
975 const Source& source;
976 const UEdgeRefMap& uedge_ref;
977 const NodeRefMap& node_ref;
984 /// \brief Constructor for the GraphCopy.
986 /// It copies the content of the \c _source graph into the
987 /// \c _target graph.
988 UGraphCopy(Target& _target, const Source& _source)
989 : source(_source), target(_target) {}
991 /// \brief Destructor of the GraphCopy
993 /// Destructor of the GraphCopy
995 for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
996 delete nodeMapCopies[i];
998 for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
999 delete edgeMapCopies[i];
1001 for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
1002 delete uEdgeMapCopies[i];
1007 /// \brief Copies the node references into the given map.
1009 /// Copies the node references into the given map.
1010 template <typename NodeRef>
1011 UGraphCopy& nodeRef(NodeRef& map) {
1012 nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node,
1013 NodeRefMap, NodeRef>(map));
1017 /// \brief Copies the node cross references into the given map.
1019 /// Copies the node cross references (reverse references) into
1021 template <typename NodeCrossRef>
1022 UGraphCopy& nodeCrossRef(NodeCrossRef& map) {
1023 nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
1024 NodeRefMap, NodeCrossRef>(map));
1028 /// \brief Make copy of the given map.
1030 /// Makes copy of the given map for the newly created graph.
1031 /// The new map's key type is the target graph's node type,
1032 /// and the copied map's key type is the source graph's node
1034 template <typename TargetMap, typename SourceMap>
1035 UGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
1036 nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node,
1037 NodeRefMap, TargetMap, SourceMap>(tmap, map));
1041 /// \brief Make a copy of the given node.
1043 /// Make a copy of the given node.
1044 UGraphCopy& node(TNode& tnode, const Node& snode) {
1045 nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node,
1046 NodeRefMap, TNode>(tnode, snode));
1050 /// \brief Copies the edge references into the given map.
1052 /// Copies the edge references into the given map.
1053 template <typename EdgeRef>
1054 UGraphCopy& edgeRef(EdgeRef& map) {
1055 edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge,
1056 EdgeRefMap, EdgeRef>(map));
1060 /// \brief Copies the edge cross references into the given map.
1062 /// Copies the edge cross references (reverse references) into
1064 template <typename EdgeCrossRef>
1065 UGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1066 edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
1067 EdgeRefMap, EdgeCrossRef>(map));
1071 /// \brief Make copy of the given map.
1073 /// Makes copy of the given map for the newly created graph.
1074 /// The new map's key type is the target graph's edge type,
1075 /// and the copied map's key type is the source graph's edge
1077 template <typename TargetMap, typename SourceMap>
1078 UGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
1079 edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge,
1080 EdgeRefMap, TargetMap, SourceMap>(tmap, map));
1084 /// \brief Make a copy of the given edge.
1086 /// Make a copy of the given edge.
1087 UGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
1088 edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge,
1089 EdgeRefMap, TEdge>(tedge, sedge));
1093 /// \brief Copies the undirected edge references into the given map.
1095 /// Copies the undirected edge references into the given map.
1096 template <typename UEdgeRef>
1097 UGraphCopy& uEdgeRef(UEdgeRef& map) {
1098 uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge,
1099 UEdgeRefMap, UEdgeRef>(map));
1103 /// \brief Copies the undirected edge cross references into the given map.
1105 /// Copies the undirected edge cross references (reverse
1106 /// references) into the given map.
1107 template <typename UEdgeCrossRef>
1108 UGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
1109 uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
1110 UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
1114 /// \brief Make copy of the given map.
1116 /// Makes copy of the given map for the newly created graph.
1117 /// The new map's key type is the target graph's undirected edge type,
1118 /// and the copied map's key type is the source graph's undirected edge
1120 template <typename TargetMap, typename SourceMap>
1121 UGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
1122 uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge,
1123 UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
1127 /// \brief Make a copy of the given undirected edge.
1129 /// Make a copy of the given undirected edge.
1130 UGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) {
1131 uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge,
1132 UEdgeRefMap, TUEdge>(tuedge, suedge));
1136 /// \brief Executes the copies.
1138 /// Executes the copies.
1140 NodeRefMap nodeRefMap(source);
1141 UEdgeRefMap uEdgeRefMap(source);
1142 EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
1143 _graph_utils_bits::UGraphCopySelector<Target>::
1144 copy(target, source, nodeRefMap, uEdgeRefMap);
1145 for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
1146 nodeMapCopies[i]->copy(source, nodeRefMap);
1148 for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
1149 uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
1151 for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
1152 edgeMapCopies[i]->copy(source, edgeRefMap);
1158 const Source& source;
1161 std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* >
1164 std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* >
1167 std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* >
1172 /// \brief Copy an undirected graph to another graph.
1174 /// Copy an undirected graph to another graph.
1175 /// The usage of the function:
1178 /// copyUGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr).run();
1181 /// After the copy the \c nr map will contain the mapping from the
1182 /// source graph's nodes to the target graph's nodes and the \c ecr will
1183 /// contain the mapping from the target graph's edges to the source's
1187 template <typename Target, typename Source>
1188 UGraphCopy<Target, Source>
1189 copyUGraph(Target& target, const Source& source) {
1190 return UGraphCopy<Target, Source>(target, source);
1193 /// \brief Class to copy a bipartite undirected graph.
1195 /// Class to copy a bipartite undirected graph to another graph
1196 /// (duplicate a graph). The simplest way of using it is through
1197 /// the \c copyBpUGraph() function.
1198 template <typename Target, typename Source>
1199 class BpUGraphCopy {
1202 typedef typename Source::Node Node;
1203 typedef typename Source::ANode ANode;
1204 typedef typename Source::BNode BNode;
1205 typedef typename Source::NodeIt NodeIt;
1206 typedef typename Source::Edge Edge;
1207 typedef typename Source::EdgeIt EdgeIt;
1208 typedef typename Source::UEdge UEdge;
1209 typedef typename Source::UEdgeIt UEdgeIt;
1211 typedef typename Target::Node TNode;
1212 typedef typename Target::Edge TEdge;
1213 typedef typename Target::UEdge TUEdge;
1215 typedef typename Source::template ANodeMap<TNode> ANodeRefMap;
1216 typedef typename Source::template BNodeMap<TNode> BNodeRefMap;
1217 typedef typename Source::template UEdgeMap<TUEdge> UEdgeRefMap;
1220 NodeRefMap(const Source& _source, const ANodeRefMap& _anode_ref,
1221 const BNodeRefMap& _bnode_ref)
1222 : source(_source), anode_ref(_anode_ref), bnode_ref(_bnode_ref) {}
1224 typedef typename Source::Node Key;
1225 typedef typename Target::Node Value;
1227 Value operator[](const Key& key) const {
1228 return source.aNode(key) ? anode_ref[key] : bnode_ref[key];
1231 const Source& source;
1232 const ANodeRefMap& anode_ref;
1233 const BNodeRefMap& bnode_ref;
1237 EdgeRefMap(const Target& _target, const Source& _source,
1238 const UEdgeRefMap& _uedge_ref, const NodeRefMap& _node_ref)
1239 : target(_target), source(_source),
1240 uedge_ref(_uedge_ref), node_ref(_node_ref) {}
1242 typedef typename Source::Edge Key;
1243 typedef typename Target::Edge Value;
1245 Value operator[](const Key& key) const {
1247 (source.direction(key) ==
1248 (node_ref[source.source(static_cast<const UEdge&>(key))] ==
1249 target.source(uedge_ref[static_cast<const UEdge&>(key)])));
1250 return target.direct(uedge_ref[key], forward);
1253 const Target& target;
1254 const Source& source;
1255 const UEdgeRefMap& uedge_ref;
1256 const NodeRefMap& node_ref;
1262 /// \brief Constructor for the GraphCopy.
1264 /// It copies the content of the \c _source graph into the
1265 /// \c _target graph.
1266 BpUGraphCopy(Target& _target, const Source& _source)
1267 : source(_source), target(_target) {}
1269 /// \brief Destructor of the GraphCopy
1271 /// Destructor of the GraphCopy
1273 for (int i = 0; i < int(aNodeMapCopies.size()); ++i) {
1274 delete aNodeMapCopies[i];
1276 for (int i = 0; i < int(bNodeMapCopies.size()); ++i) {
1277 delete bNodeMapCopies[i];
1279 for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
1280 delete nodeMapCopies[i];
1282 for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
1283 delete edgeMapCopies[i];
1285 for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
1286 delete uEdgeMapCopies[i];
1291 /// \brief Copies the A-node references into the given map.
1293 /// Copies the A-node references into the given map.
1294 template <typename ANodeRef>
1295 BpUGraphCopy& aNodeRef(ANodeRef& map) {
1296 aNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, ANode,
1297 ANodeRefMap, ANodeRef>(map));
1301 /// \brief Copies the A-node cross references into the given map.
1303 /// Copies the A-node cross references (reverse references) into
1305 template <typename ANodeCrossRef>
1306 BpUGraphCopy& aNodeCrossRef(ANodeCrossRef& map) {
1307 aNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
1308 ANode, ANodeRefMap, ANodeCrossRef>(map));
1312 /// \brief Make copy of the given A-node map.
1314 /// Makes copy of the given map for the newly created graph.
1315 /// The new map's key type is the target graph's node type,
1316 /// and the copied map's key type is the source graph's node
1318 template <typename TargetMap, typename SourceMap>
1319 BpUGraphCopy& aNodeMap(TargetMap& tmap, const SourceMap& map) {
1320 aNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, ANode,
1321 ANodeRefMap, TargetMap, SourceMap>(tmap, map));
1325 /// \brief Copies the B-node references into the given map.
1327 /// Copies the B-node references into the given map.
1328 template <typename BNodeRef>
1329 BpUGraphCopy& bNodeRef(BNodeRef& map) {
1330 bNodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, BNode,
1331 BNodeRefMap, BNodeRef>(map));
1335 /// \brief Copies the B-node cross references into the given map.
1337 /// Copies the B-node cross references (reverse references) into
1339 template <typename BNodeCrossRef>
1340 BpUGraphCopy& bNodeCrossRef(BNodeCrossRef& map) {
1341 bNodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
1342 BNode, BNodeRefMap, BNodeCrossRef>(map));
1346 /// \brief Make copy of the given B-node map.
1348 /// Makes copy of the given map for the newly created graph.
1349 /// The new map's key type is the target graph's node type,
1350 /// and the copied map's key type is the source graph's node
1352 template <typename TargetMap, typename SourceMap>
1353 BpUGraphCopy& bNodeMap(TargetMap& tmap, const SourceMap& map) {
1354 bNodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, BNode,
1355 BNodeRefMap, TargetMap, SourceMap>(tmap, map));
1358 /// \brief Copies the node references into the given map.
1360 /// Copies the node references into the given map.
1361 template <typename NodeRef>
1362 BpUGraphCopy& nodeRef(NodeRef& map) {
1363 nodeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Node,
1364 NodeRefMap, NodeRef>(map));
1368 /// \brief Copies the node cross references into the given map.
1370 /// Copies the node cross references (reverse references) into
1372 template <typename NodeCrossRef>
1373 BpUGraphCopy& nodeCrossRef(NodeCrossRef& map) {
1374 nodeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Node,
1375 NodeRefMap, NodeCrossRef>(map));
1379 /// \brief Make copy of the given map.
1381 /// Makes copy of the given map for the newly created graph.
1382 /// The new map's key type is the target graph's node type,
1383 /// and the copied map's key type is the source graph's node
1385 template <typename TargetMap, typename SourceMap>
1386 BpUGraphCopy& nodeMap(TargetMap& tmap, const SourceMap& map) {
1387 nodeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Node,
1388 NodeRefMap, TargetMap, SourceMap>(tmap, map));
1392 /// \brief Make a copy of the given node.
1394 /// Make a copy of the given node.
1395 BpUGraphCopy& node(TNode& tnode, const Node& snode) {
1396 nodeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Node,
1397 NodeRefMap, TNode>(tnode, snode));
1401 /// \brief Copies the edge references into the given map.
1403 /// Copies the edge references into the given map.
1404 template <typename EdgeRef>
1405 BpUGraphCopy& edgeRef(EdgeRef& map) {
1406 edgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, Edge,
1407 EdgeRefMap, EdgeRef>(map));
1411 /// \brief Copies the edge cross references into the given map.
1413 /// Copies the edge cross references (reverse references) into
1415 template <typename EdgeCrossRef>
1416 BpUGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1417 edgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source, Edge,
1418 EdgeRefMap, EdgeCrossRef>(map));
1422 /// \brief Make copy of the given map.
1424 /// Makes copy of the given map for the newly created graph.
1425 /// The new map's key type is the target graph's edge type,
1426 /// and the copied map's key type is the source graph's edge
1428 template <typename TargetMap, typename SourceMap>
1429 BpUGraphCopy& edgeMap(TargetMap& tmap, const SourceMap& map) {
1430 edgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, Edge,
1431 EdgeRefMap, TargetMap, SourceMap>(tmap, map));
1435 /// \brief Make a copy of the given edge.
1437 /// Make a copy of the given edge.
1438 BpUGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
1439 edgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, Edge,
1440 EdgeRefMap, TEdge>(tedge, sedge));
1444 /// \brief Copies the undirected edge references into the given map.
1446 /// Copies the undirected edge references into the given map.
1447 template <typename UEdgeRef>
1448 BpUGraphCopy& uEdgeRef(UEdgeRef& map) {
1449 uEdgeMapCopies.push_back(new _graph_utils_bits::RefCopy<Source, UEdge,
1450 UEdgeRefMap, UEdgeRef>(map));
1454 /// \brief Copies the undirected edge cross references into the given map.
1456 /// Copies the undirected edge cross references (reverse
1457 /// references) into the given map.
1458 template <typename UEdgeCrossRef>
1459 BpUGraphCopy& uEdgeCrossRef(UEdgeCrossRef& map) {
1460 uEdgeMapCopies.push_back(new _graph_utils_bits::CrossRefCopy<Source,
1461 UEdge, UEdgeRefMap, UEdgeCrossRef>(map));
1465 /// \brief Make copy of the given map.
1467 /// Makes copy of the given map for the newly created graph.
1468 /// The new map's key type is the target graph's undirected edge type,
1469 /// and the copied map's key type is the source graph's undirected edge
1471 template <typename TargetMap, typename SourceMap>
1472 BpUGraphCopy& uEdgeMap(TargetMap& tmap, const SourceMap& map) {
1473 uEdgeMapCopies.push_back(new _graph_utils_bits::MapCopy<Source, UEdge,
1474 UEdgeRefMap, TargetMap, SourceMap>(tmap, map));
1478 /// \brief Make a copy of the given undirected edge.
1480 /// Make a copy of the given undirected edge.
1481 BpUGraphCopy& uEdge(TUEdge& tuedge, const UEdge& suedge) {
1482 uEdgeMapCopies.push_back(new _graph_utils_bits::ItemCopy<Source, UEdge,
1483 UEdgeRefMap, TUEdge>(tuedge, suedge));
1487 /// \brief Executes the copies.
1489 /// Executes the copies.
1491 ANodeRefMap aNodeRefMap(source);
1492 BNodeRefMap bNodeRefMap(source);
1493 NodeRefMap nodeRefMap(source, aNodeRefMap, bNodeRefMap);
1494 UEdgeRefMap uEdgeRefMap(source);
1495 EdgeRefMap edgeRefMap(target, source, uEdgeRefMap, nodeRefMap);
1496 _graph_utils_bits::BpUGraphCopySelector<Target>::
1497 copy(target, source, aNodeRefMap, bNodeRefMap, uEdgeRefMap);
1498 for (int i = 0; i < int(aNodeMapCopies.size()); ++i) {
1499 aNodeMapCopies[i]->copy(source, aNodeRefMap);
1501 for (int i = 0; i < int(bNodeMapCopies.size()); ++i) {
1502 bNodeMapCopies[i]->copy(source, bNodeRefMap);
1504 for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
1505 nodeMapCopies[i]->copy(source, nodeRefMap);
1507 for (int i = 0; i < int(uEdgeMapCopies.size()); ++i) {
1508 uEdgeMapCopies[i]->copy(source, uEdgeRefMap);
1510 for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
1511 edgeMapCopies[i]->copy(source, edgeRefMap);
1517 const Source& source;
1520 std::vector<_graph_utils_bits::MapCopyBase<Source, ANode, ANodeRefMap>* >
1523 std::vector<_graph_utils_bits::MapCopyBase<Source, BNode, BNodeRefMap>* >
1526 std::vector<_graph_utils_bits::MapCopyBase<Source, Node, NodeRefMap>* >
1529 std::vector<_graph_utils_bits::MapCopyBase<Source, Edge, EdgeRefMap>* >
1532 std::vector<_graph_utils_bits::MapCopyBase<Source, UEdge, UEdgeRefMap>* >
1537 /// \brief Copy a bipartite undirected graph to another graph.
1539 /// Copy a bipartite undirected graph to another graph.
1540 /// The usage of the function:
1543 /// copyBpUGraph(trg, src).aNodeRef(anr).edgeCrossRef(ecr).run();
1546 /// After the copy the \c nr map will contain the mapping from the
1547 /// source graph's nodes to the target graph's nodes and the \c ecr will
1548 /// contain the mapping from the target graph's edges to the source's
1551 /// \see BpUGraphCopy
1552 template <typename Target, typename Source>
1553 BpUGraphCopy<Target, Source>
1554 copyBpUGraph(Target& target, const Source& source) {
1555 return BpUGraphCopy<Target, Source>(target, source);
1561 /// \addtogroup graph_maps
1564 /// Provides an immutable and unique id for each item in the graph.
1566 /// The IdMap class provides a unique and immutable id for each item of the
1567 /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
1568 /// different items (nodes) get different ids <li>\b immutable: the id of an
1569 /// item (node) does not change (even if you delete other nodes). </ul>
1570 /// Through this map you get access (i.e. can read) the inner id values of
1571 /// the items stored in the graph. This map can be inverted with its member
1572 /// class \c InverseMap.
1574 template <typename _Graph, typename _Item>
1577 typedef _Graph Graph;
1582 /// \brief Constructor.
1584 /// Constructor of the map.
1585 explicit IdMap(const Graph& _graph) : graph(&_graph) {}
1587 /// \brief Gives back the \e id of the item.
1589 /// Gives back the immutable and unique \e id of the item.
1590 int operator[](const Item& item) const { return graph->id(item);}
1592 /// \brief Gives back the item by its id.
1594 /// Gives back the item by its id.
1595 Item operator()(int id) { return graph->fromId(id, Item()); }
1602 /// \brief The class represents the inverse of its owner (IdMap).
1604 /// The class represents the inverse of its owner (IdMap).
1609 /// \brief Constructor.
1611 /// Constructor for creating an id-to-item map.
1612 explicit InverseMap(const Graph& _graph) : graph(&_graph) {}
1614 /// \brief Constructor.
1616 /// Constructor for creating an id-to-item map.
1617 explicit InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
1619 /// \brief Gives back the given item from its id.
1621 /// Gives back the given item from its id.
1623 Item operator[](int id) const { return graph->fromId(id, Item());}
1629 /// \brief Gives back the inverse of the map.
1631 /// Gives back the inverse of the IdMap.
1632 InverseMap inverse() const { return InverseMap(*graph);}
1637 /// \brief General invertable graph-map type.
1639 /// This type provides simple invertable graph-maps.
1640 /// The InvertableMap wraps an arbitrary ReadWriteMap
1641 /// and if a key is set to a new value then store it
1642 /// in the inverse map.
1644 /// The values of the map can be accessed
1645 /// with stl compatible forward iterator.
1647 /// \param _Graph The graph type.
1648 /// \param _Item The item type of the graph.
1649 /// \param _Value The value type of the map.
1651 /// \see IterableValueMap
1652 template <typename _Graph, typename _Item, typename _Value>
1653 class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> {
1656 typedef DefaultMap<_Graph, _Item, _Value> Map;
1657 typedef _Graph Graph;
1659 typedef std::map<_Value, _Item> Container;
1664 /// The key type of InvertableMap (Node, Edge, UEdge).
1665 typedef typename Map::Key Key;
1666 /// The value type of the InvertableMap.
1667 typedef typename Map::Value Value;
1671 /// \brief Constructor.
1673 /// Construct a new InvertableMap for the graph.
1675 explicit InvertableMap(const Graph& graph) : Map(graph) {}
1677 /// \brief Forward iterator for values.
1679 /// This iterator is an stl compatible forward
1680 /// iterator on the values of the map. The values can
1681 /// be accessed in the [beginValue, endValue) range.
1684 : public std::iterator<std::forward_iterator_tag, Value> {
1685 friend class InvertableMap;
1687 ValueIterator(typename Container::const_iterator _it)
1693 ValueIterator& operator++() { ++it; return *this; }
1694 ValueIterator operator++(int) {
1695 ValueIterator tmp(*this);
1700 const Value& operator*() const { return it->first; }
1701 const Value* operator->() const { return &(it->first); }
1703 bool operator==(ValueIterator jt) const { return it == jt.it; }
1704 bool operator!=(ValueIterator jt) const { return it != jt.it; }
1707 typename Container::const_iterator it;
1710 /// \brief Returns an iterator to the first value.
1712 /// Returns an stl compatible iterator to the
1713 /// first value of the map. The values of the
1714 /// map can be accessed in the [beginValue, endValue)
1716 ValueIterator beginValue() const {
1717 return ValueIterator(invMap.begin());
1720 /// \brief Returns an iterator after the last value.
1722 /// Returns an stl compatible iterator after the
1723 /// last value of the map. The values of the
1724 /// map can be accessed in the [beginValue, endValue)
1726 ValueIterator endValue() const {
1727 return ValueIterator(invMap.end());
1730 /// \brief The setter function of the map.
1732 /// Sets the mapped value.
1733 void set(const Key& key, const Value& val) {
1734 Value oldval = Map::operator[](key);
1735 typename Container::iterator it = invMap.find(oldval);
1736 if (it != invMap.end() && it->second == key) {
1739 invMap.insert(make_pair(val, key));
1743 /// \brief The getter function of the map.
1745 /// It gives back the value associated with the key.
1746 typename MapTraits<Map>::ConstReturnValue
1747 operator[](const Key& key) const {
1748 return Map::operator[](key);
1751 /// \brief Gives back the item by its value.
1753 /// Gives back the item by its value.
1754 Key operator()(const Value& key) const {
1755 typename Container::const_iterator it = invMap.find(key);
1756 return it != invMap.end() ? it->second : INVALID;
1761 /// \brief Erase the key from the map.
1763 /// Erase the key to the map. It is called by the
1764 /// \c AlterationNotifier.
1765 virtual void erase(const Key& key) {
1766 Value val = Map::operator[](key);
1767 typename Container::iterator it = invMap.find(val);
1768 if (it != invMap.end() && it->second == key) {
1774 /// \brief Erase more keys from the map.
1776 /// Erase more keys from the map. It is called by the
1777 /// \c AlterationNotifier.
1778 virtual void erase(const std::vector<Key>& keys) {
1779 for (int i = 0; i < int(keys.size()); ++i) {
1780 Value val = Map::operator[](keys[i]);
1781 typename Container::iterator it = invMap.find(val);
1782 if (it != invMap.end() && it->second == keys[i]) {
1789 /// \brief Clear the keys from the map and inverse map.
1791 /// Clear the keys from the map and inverse map. It is called by the
1792 /// \c AlterationNotifier.
1793 virtual void clear() {
1800 /// \brief The inverse map type.
1802 /// The inverse of this map. The subscript operator of the map
1803 /// gives back always the item what was last assigned to the value.
1806 /// \brief Constructor of the InverseMap.
1808 /// Constructor of the InverseMap.
1809 explicit InverseMap(const InvertableMap& _inverted)
1810 : inverted(_inverted) {}
1812 /// The value type of the InverseMap.
1813 typedef typename InvertableMap::Key Value;
1814 /// The key type of the InverseMap.
1815 typedef typename InvertableMap::Value Key;
1817 /// \brief Subscript operator.
1819 /// Subscript operator. It gives back always the item
1820 /// what was last assigned to the value.
1821 Value operator[](const Key& key) const {
1822 return inverted(key);
1826 const InvertableMap& inverted;
1829 /// \brief It gives back the just readable inverse map.
1831 /// It gives back the just readable inverse map.
1832 InverseMap inverse() const {
1833 return InverseMap(*this);
1840 /// \brief Provides a mutable, continuous and unique descriptor for each
1841 /// item in the graph.
1843 /// The DescriptorMap class provides a unique and continuous (but mutable)
1844 /// descriptor (id) for each item of the same type (e.g. node) in the
1845 /// graph. This id is <ul><li>\b unique: different items (nodes) get
1846 /// different ids <li>\b continuous: the range of the ids is the set of
1847 /// integers between 0 and \c n-1, where \c n is the number of the items of
1848 /// this type (e.g. nodes) (so the id of a node can change if you delete an
1849 /// other node, i.e. this id is mutable). </ul> This map can be inverted
1850 /// with its member class \c InverseMap.
1852 /// \param _Graph The graph class the \c DescriptorMap belongs to.
1853 /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
1855 template <typename _Graph, typename _Item>
1856 class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
1859 typedef DefaultMap<_Graph, _Item, int> Map;
1862 /// The graph class of DescriptorMap.
1863 typedef _Graph Graph;
1865 /// The key type of DescriptorMap (Node, Edge, UEdge).
1866 typedef typename Map::Key Key;
1867 /// The value type of DescriptorMap.
1868 typedef typename Map::Value Value;
1870 /// \brief Constructor.
1872 /// Constructor for descriptor map.
1873 explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
1875 const typename Map::Notifier* nf = Map::notifier();
1876 for (nf->first(it); it != INVALID; nf->next(it)) {
1877 Map::set(it, invMap.size());
1878 invMap.push_back(it);
1884 /// \brief Add a new key to the map.
1886 /// Add a new key to the map. It is called by the
1887 /// \c AlterationNotifier.
1888 virtual void add(const Item& item) {
1890 Map::set(item, invMap.size());
1891 invMap.push_back(item);
1894 /// \brief Add more new keys to the map.
1896 /// Add more new keys to the map. It is called by the
1897 /// \c AlterationNotifier.
1898 virtual void add(const std::vector<Item>& items) {
1900 for (int i = 0; i < int(items.size()); ++i) {
1901 Map::set(items[i], invMap.size());
1902 invMap.push_back(items[i]);
1906 /// \brief Erase the key from the map.
1908 /// Erase the key from the map. It is called by the
1909 /// \c AlterationNotifier.
1910 virtual void erase(const Item& item) {
1911 Map::set(invMap.back(), Map::operator[](item));
1912 invMap[Map::operator[](item)] = invMap.back();
1917 /// \brief Erase more keys from the map.
1919 /// Erase more keys from the map. It is called by the
1920 /// \c AlterationNotifier.
1921 virtual void erase(const std::vector<Item>& items) {
1922 for (int i = 0; i < int(items.size()); ++i) {
1923 Map::set(invMap.back(), Map::operator[](items[i]));
1924 invMap[Map::operator[](items[i])] = invMap.back();
1930 /// \brief Build the unique map.
1932 /// Build the unique map. It is called by the
1933 /// \c AlterationNotifier.
1934 virtual void build() {
1937 const typename Map::Notifier* nf = Map::notifier();
1938 for (nf->first(it); it != INVALID; nf->next(it)) {
1939 Map::set(it, invMap.size());
1940 invMap.push_back(it);
1944 /// \brief Clear the keys from the map.
1946 /// Clear the keys from the map. It is called by the
1947 /// \c AlterationNotifier.
1948 virtual void clear() {
1955 /// \brief Returns the maximal value plus one.
1957 /// Returns the maximal value plus one in the map.
1958 unsigned int size() const {
1959 return invMap.size();
1962 /// \brief Swaps the position of the two items in the map.
1964 /// Swaps the position of the two items in the map.
1965 void swap(const Item& p, const Item& q) {
1966 int pi = Map::operator[](p);
1967 int qi = Map::operator[](q);
1974 /// \brief Gives back the \e descriptor of the item.
1976 /// Gives back the mutable and unique \e descriptor of the map.
1977 int operator[](const Item& item) const {
1978 return Map::operator[](item);
1981 /// \brief Gives back the item by its descriptor.
1983 /// Gives back th item by its descriptor.
1984 Item operator()(int id) const {
1990 typedef std::vector<Item> Container;
1994 /// \brief The inverse map type of DescriptorMap.
1996 /// The inverse map type of DescriptorMap.
1999 /// \brief Constructor of the InverseMap.
2001 /// Constructor of the InverseMap.
2002 explicit InverseMap(const DescriptorMap& _inverted)
2003 : inverted(_inverted) {}
2006 /// The value type of the InverseMap.
2007 typedef typename DescriptorMap::Key Value;
2008 /// The key type of the InverseMap.
2009 typedef typename DescriptorMap::Value Key;
2011 /// \brief Subscript operator.
2013 /// Subscript operator. It gives back the item
2014 /// that the descriptor belongs to currently.
2015 Value operator[](const Key& key) const {
2016 return inverted(key);
2019 /// \brief Size of the map.
2021 /// Returns the size of the map.
2022 unsigned int size() const {
2023 return inverted.size();
2027 const DescriptorMap& inverted;
2030 /// \brief Gives back the inverse of the map.
2032 /// Gives back the inverse of the map.
2033 const InverseMap inverse() const {
2034 return InverseMap(*this);
2038 /// \brief Returns the source of the given edge.
2040 /// 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.
2082 /// \author Balazs Dezso
2083 template <typename Graph>
2087 typedef typename Graph::Node Value;
2088 typedef typename Graph::Edge Key;
2090 /// \brief Constructor
2093 /// \param _graph The graph that the map belongs to.
2094 explicit TargetMap(const Graph& _graph) : graph(_graph) {}
2096 /// \brief The subscript operator.
2098 /// The subscript operator.
2099 /// \param e The edge
2100 /// \return The target of the edge
2101 Value operator[](const Key& e) const {
2102 return graph.target(e);
2109 /// \brief Returns a \ref TargetMap class.
2111 /// This function just returns a \ref TargetMap class.
2112 /// \relates TargetMap
2113 template <typename Graph>
2114 inline TargetMap<Graph> targetMap(const Graph& graph) {
2115 return TargetMap<Graph>(graph);
2118 /// \brief Returns the "forward" directed edge view of an undirected edge.
2120 /// Returns the "forward" directed edge view of an undirected edge.
2121 /// \see BackwardMap
2122 /// \author Balazs Dezso
2123 template <typename Graph>
2127 typedef typename Graph::Edge Value;
2128 typedef typename Graph::UEdge Key;
2130 /// \brief Constructor
2133 /// \param _graph The graph that the map belongs to.
2134 explicit ForwardMap(const Graph& _graph) : graph(_graph) {}
2136 /// \brief The subscript operator.
2138 /// The subscript operator.
2139 /// \param key An undirected edge
2140 /// \return The "forward" directed edge view of undirected edge
2141 Value operator[](const Key& key) const {
2142 return graph.direct(key, true);
2149 /// \brief Returns a \ref ForwardMap class.
2151 /// This function just returns an \ref ForwardMap class.
2152 /// \relates ForwardMap
2153 template <typename Graph>
2154 inline ForwardMap<Graph> forwardMap(const Graph& graph) {
2155 return ForwardMap<Graph>(graph);
2158 /// \brief Returns the "backward" directed edge view of an undirected edge.
2160 /// Returns the "backward" directed edge view of an undirected edge.
2162 /// \author Balazs Dezso
2163 template <typename Graph>
2167 typedef typename Graph::Edge Value;
2168 typedef typename Graph::UEdge Key;
2170 /// \brief Constructor
2173 /// \param _graph The graph that the map belongs to.
2174 explicit BackwardMap(const Graph& _graph) : graph(_graph) {}
2176 /// \brief The subscript operator.
2178 /// The subscript operator.
2179 /// \param key An undirected edge
2180 /// \return The "backward" directed edge view of undirected edge
2181 Value operator[](const Key& key) const {
2182 return graph.direct(key, false);
2189 /// \brief Returns a \ref BackwardMap class
2191 /// This function just returns a \ref BackwardMap class.
2192 /// \relates BackwardMap
2193 template <typename Graph>
2194 inline BackwardMap<Graph> backwardMap(const Graph& graph) {
2195 return BackwardMap<Graph>(graph);
2198 /// \brief Potential difference map
2200 /// If there is an potential map on the nodes then we
2201 /// can get an edge map as we get the substraction of the
2202 /// values of the target and source.
2203 template <typename Graph, typename NodeMap>
2204 class PotentialDifferenceMap {
2206 typedef typename Graph::Edge Key;
2207 typedef typename NodeMap::Value Value;
2209 /// \brief Constructor
2211 /// Contructor of the map
2212 explicit PotentialDifferenceMap(const Graph& _graph,
2213 const NodeMap& _potential)
2214 : graph(_graph), potential(_potential) {}
2216 /// \brief Const subscription operator
2218 /// Const subscription operator
2219 Value operator[](const Key& edge) const {
2220 return potential[graph.target(edge)] - potential[graph.source(edge)];
2225 const NodeMap& potential;
2228 /// \brief Returns a PotentialDifferenceMap.
2230 /// This function just returns a PotentialDifferenceMap.
2231 /// \relates PotentialDifferenceMap
2232 template <typename Graph, typename NodeMap>
2233 PotentialDifferenceMap<Graph, NodeMap>
2234 potentialDifferenceMap(const Graph& graph, const NodeMap& potential) {
2235 return PotentialDifferenceMap<Graph, NodeMap>(graph, potential);
2238 /// \brief Map of the node in-degrees.
2240 /// This map returns the in-degree of a node. Once it is constructed,
2241 /// the degrees are stored in a standard NodeMap, so each query is done
2242 /// in constant time. On the other hand, the values are updated automatically
2243 /// whenever the graph changes.
2245 /// \warning Besides addNode() and addEdge(), a graph structure may provide
2246 /// alternative ways to modify the graph. The correct behavior of InDegMap
2247 /// is not guarantied if these additional features are used. For example
2248 /// the functions \ref ListGraph::changeSource() "changeSource()",
2249 /// \ref ListGraph::changeTarget() "changeTarget()" and
2250 /// \ref ListGraph::reverseEdge() "reverseEdge()"
2251 /// of \ref ListGraph will \e not update the degree values correctly.
2255 template <typename _Graph>
2257 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
2258 ::ItemNotifier::ObserverBase {
2262 typedef _Graph Graph;
2264 typedef typename Graph::Node Key;
2266 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
2267 ::ItemNotifier::ObserverBase Parent;
2271 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
2274 typedef DefaultMap<_Graph, Key, int> Parent;
2275 typedef typename Parent::Graph Graph;
2277 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
2279 virtual void add(const Key& key) {
2281 Parent::set(key, 0);
2284 virtual void add(const std::vector<Key>& keys) {
2286 for (int i = 0; i < int(keys.size()); ++i) {
2287 Parent::set(keys[i], 0);
2294 /// \brief Constructor.
2296 /// Constructor for creating in-degree map.
2297 explicit InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
2298 Parent::attach(graph.notifier(typename _Graph::Edge()));
2300 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2301 deg[it] = countInEdges(graph, it);
2305 /// Gives back the in-degree of a Node.
2306 int operator[](const Key& key) const {
2312 typedef typename Graph::Edge Edge;
2314 virtual void add(const Edge& edge) {
2315 ++deg[graph.target(edge)];
2318 virtual void add(const std::vector<Edge>& edges) {
2319 for (int i = 0; i < int(edges.size()); ++i) {
2320 ++deg[graph.target(edges[i])];
2324 virtual void erase(const Edge& edge) {
2325 --deg[graph.target(edge)];
2328 virtual void erase(const std::vector<Edge>& edges) {
2329 for (int i = 0; i < int(edges.size()); ++i) {
2330 --deg[graph.target(edges[i])];
2334 virtual void build() {
2335 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2336 deg[it] = countInEdges(graph, it);
2340 virtual void clear() {
2341 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2347 const _Graph& graph;
2351 /// \brief Map of the node out-degrees.
2353 /// This map returns the out-degree of a node. Once it is constructed,
2354 /// the degrees are stored in a standard NodeMap, so each query is done
2355 /// in constant time. On the other hand, the values are updated automatically
2356 /// whenever the graph changes.
2358 /// \warning Besides addNode() and addEdge(), a graph structure may provide
2359 /// alternative ways to modify the graph. The correct behavior of OutDegMap
2360 /// is not guarantied if these additional features are used. For example
2361 /// the functions \ref ListGraph::changeSource() "changeSource()",
2362 /// \ref ListGraph::changeTarget() "changeTarget()" and
2363 /// \ref ListGraph::reverseEdge() "reverseEdge()"
2364 /// of \ref ListGraph will \e not update the degree values correctly.
2368 template <typename _Graph>
2370 : protected ItemSetTraits<_Graph, typename _Graph::Edge>
2371 ::ItemNotifier::ObserverBase {
2375 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge>
2376 ::ItemNotifier::ObserverBase Parent;
2378 typedef _Graph Graph;
2380 typedef typename Graph::Node Key;
2384 class AutoNodeMap : public DefaultMap<_Graph, Key, int> {
2387 typedef DefaultMap<_Graph, Key, int> Parent;
2388 typedef typename Parent::Graph Graph;
2390 AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
2392 virtual void add(const Key& key) {
2394 Parent::set(key, 0);
2396 virtual void add(const std::vector<Key>& keys) {
2398 for (int i = 0; i < int(keys.size()); ++i) {
2399 Parent::set(keys[i], 0);
2406 /// \brief Constructor.
2408 /// Constructor for creating out-degree map.
2409 explicit OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
2410 Parent::attach(graph.notifier(typename _Graph::Edge()));
2412 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2413 deg[it] = countOutEdges(graph, it);
2417 /// Gives back the out-degree of a Node.
2418 int operator[](const Key& key) const {
2424 typedef typename Graph::Edge Edge;
2426 virtual void add(const Edge& edge) {
2427 ++deg[graph.source(edge)];
2430 virtual void add(const std::vector<Edge>& edges) {
2431 for (int i = 0; i < int(edges.size()); ++i) {
2432 ++deg[graph.source(edges[i])];
2436 virtual void erase(const Edge& edge) {
2437 --deg[graph.source(edge)];
2440 virtual void erase(const std::vector<Edge>& edges) {
2441 for (int i = 0; i < int(edges.size()); ++i) {
2442 --deg[graph.source(edges[i])];
2446 virtual void build() {
2447 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2448 deg[it] = countOutEdges(graph, it);
2452 virtual void clear() {
2453 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
2459 const _Graph& graph;
2464 ///Fast edge look up between given endpoints.
2467 ///Using this class, you can find an edge in a graph from a given
2468 ///source to a given target in time <em>O(log d)</em>,
2469 ///where <em>d</em> is the out-degree of the source node.
2471 ///It is not possible to find \e all parallel edges between two nodes.
2472 ///Use \ref AllEdgeLookUp for this purpose.
2474 ///\warning This class is static, so you should refresh() (or at least
2475 ///refresh(Node)) this data structure
2476 ///whenever the graph changes. This is a time consuming (superlinearly
2477 ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
2479 ///\param G The type of the underlying graph.
2481 ///\sa AllEdgeLookUp
2486 GRAPH_TYPEDEFS(typename G)
2491 typename Graph::template NodeMap<Edge> _head;
2492 typename Graph::template EdgeMap<Edge> _left;
2493 typename Graph::template EdgeMap<Edge> _right;
2498 EdgeLess(const Graph &_g) : g(_g) {}
2499 bool operator()(Edge a,Edge b) const
2501 return g.target(a)<g.target(b);
2511 ///It builds up the search database, which remains valid until the graph
2513 EdgeLookUp(const Graph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
2516 Edge refresh_rec(std::vector<Edge> &v,int a,int b)
2520 _left[me] = a<m?refresh_rec(v,a,m-1):INVALID;
2521 _right[me] = m<b?refresh_rec(v,m+1,b):INVALID;
2525 ///Refresh the data structure at a node.
2527 ///Build up the search database of node \c n.
2529 ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2530 ///the number of the outgoing edges of \c n.
2531 void refresh(Node n)
2533 std::vector<Edge> v;
2534 for(OutEdgeIt e(_g,n);e!=INVALID;++e) v.push_back(e);
2536 std::sort(v.begin(),v.end(),EdgeLess(_g));
2537 _head[n]=refresh_rec(v,0,v.size()-1);
2539 else _head[n]=INVALID;
2541 ///Refresh the full data structure.
2543 ///Build up the full search database. In fact, it simply calls
2544 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2546 ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2547 ///the number of the edges of \c n and <em>D</em> is the maximum
2548 ///out-degree of the graph.
2552 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
2555 ///Find an edge between two nodes.
2557 ///Find an edge between two nodes in time <em>O(</em>log<em>d)</em>, where
2558 /// <em>d</em> is the number of outgoing edges of \c s.
2559 ///\param s The source node
2560 ///\param t The target node
2561 ///\return An edge from \c s to \c t if there exists,
2562 ///\ref INVALID otherwise.
2564 ///\warning If you change the graph, refresh() must be called before using
2565 ///this operator. If you change the outgoing edges of
2566 ///a single node \c n, then
2567 ///\ref refresh(Node) "refresh(n)" is enough.
2569 Edge operator()(Node s, Node t) const
2573 e!=INVALID&&_g.target(e)!=t;
2574 e = t < _g.target(e)?_left[e]:_right[e]) ;
2580 ///Fast look up of all edges between given endpoints.
2583 ///This class is the same as \ref EdgeLookUp, with the addition
2584 ///that it makes it possible to find all edges between given endpoints.
2586 ///\warning This class is static, so you should refresh() (or at least
2587 ///refresh(Node)) this data structure
2588 ///whenever the graph changes. This is a time consuming (superlinearly
2589 ///proportional (<em>O(m</em>log<em>m)</em>) to the number of edges).
2591 ///\param G The type of the underlying graph.
2595 class AllEdgeLookUp : public EdgeLookUp<G>
2597 using EdgeLookUp<G>::_g;
2598 using EdgeLookUp<G>::_right;
2599 using EdgeLookUp<G>::_left;
2600 using EdgeLookUp<G>::_head;
2602 GRAPH_TYPEDEFS(typename G)
2605 typename Graph::template EdgeMap<Edge> _next;
2607 Edge refreshNext(Edge head,Edge next=INVALID)
2609 if(head==INVALID) return next;
2611 next=refreshNext(_right[head],next);
2612 // _next[head]=next;
2613 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
2615 return refreshNext(_left[head],head);
2621 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
2629 ///It builds up the search database, which remains valid until the graph
2631 AllEdgeLookUp(const Graph &g) : EdgeLookUp<G>(g), _next(g) {refreshNext();}
2633 ///Refresh the data structure at a node.
2635 ///Build up the search database of node \c n.
2637 ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2638 ///the number of the outgoing edges of \c n.
2640 void refresh(Node n)
2642 EdgeLookUp<G>::refresh(n);
2643 refreshNext(_head[n]);
2646 ///Refresh the full data structure.
2648 ///Build up the full search database. In fact, it simply calls
2649 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2651 ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
2652 ///the number of the edges of \c n and <em>D</em> is the maximum
2653 ///out-degree of the graph.
2657 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
2660 ///Find an edge between two nodes.
2662 ///Find an edge between two nodes.
2663 ///\param s The source node
2664 ///\param t The target node
2665 ///\param prev The previous edge between \c s and \c t. It it is INVALID or
2666 ///not given, the operator finds the first appropriate edge.
2667 ///\return An edge from \c s to \c t after \c prev or
2668 ///\ref INVALID if there is no more.
2670 ///For example, you can count the number of edges from \c u to \c v in the
2673 ///AllEdgeLookUp<ListGraph> ae(g);
2676 ///for(Edge e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
2679 ///Finding the first edge take <em>O(</em>log<em>d)</em> time, where
2680 /// <em>d</em> is the number of outgoing edges of \c s. Then, the
2681 ///consecutive edges are found in constant time.
2683 ///\warning If you change the graph, refresh() must be called before using
2684 ///this operator. If you change the outgoing edges of
2685 ///a single node \c n, then
2686 ///\ref refresh(Node) "refresh(n)" is enough.
2689 Edge operator()(Node s, Node t, Edge prev=INVALID) const {}
2691 using EdgeLookUp<G>::operator() ;
2692 Edge operator()(Node s, Node t, Edge prev) const
2694 return prev==INVALID?(*this)(s,t):_next[prev];
2702 } //END OF NAMESPACE LEMON