1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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
25 #include <lemon/config.h>
26 #include <lemon/bits/enable_if.h>
27 #include <lemon/bits/traits.h>
28 #include <lemon/assert.h>
30 // Disable the following warnings when compiling with MSVC:
31 // C4250: 'class1' : inherits 'class2::member' via dominance
32 // C4355: 'this' : used in base member initializer list
33 // C4503: 'function' : decorated name length exceeded, name was truncated
34 // C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
35 // C4996: 'function': was declared deprecated
37 #pragma warning( disable : 4250 4355 4503 4800 4996 )
41 // Needed by the [DI]GRAPH_TYPEDEFS marcos for gcc 4.8
42 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
46 ///\brief LEMON core utilities.
48 ///This header file contains core utilities for LEMON.
49 ///It is automatically included by all graph types, therefore it usually
50 ///do not have to be included directly.
54 /// \brief Dummy type to make it easier to create invalid iterators.
56 /// Dummy type to make it easier to create invalid iterators.
57 /// See \ref INVALID for the usage.
60 bool operator==(Invalid) { return true; }
61 bool operator!=(Invalid) { return false; }
62 bool operator< (Invalid) { return false; }
65 /// \brief Invalid iterators.
67 /// \ref Invalid is a global type that converts to each iterator
68 /// in such a way that the value of the target iterator will be invalid.
69 #ifdef LEMON_ONLY_TEMPLATES
70 const Invalid INVALID = Invalid();
72 extern const Invalid INVALID;
75 /// \addtogroup gutils
78 ///Create convenience typedefs for the digraph types and iterators
80 ///This \c \#define creates convenient type definitions for the following
81 ///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
82 ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
83 ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
85 ///\note If the graph type is a dependent type, ie. the graph type depend
86 ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
88 #define DIGRAPH_TYPEDEFS(Digraph) \
89 typedef Digraph::Node Node; \
90 typedef Digraph::NodeIt NodeIt; \
91 typedef Digraph::Arc Arc; \
92 typedef Digraph::ArcIt ArcIt; \
93 typedef Digraph::InArcIt InArcIt; \
94 typedef Digraph::OutArcIt OutArcIt; \
95 typedef Digraph::NodeMap<bool> BoolNodeMap; \
96 typedef Digraph::NodeMap<int> IntNodeMap; \
97 typedef Digraph::NodeMap<double> DoubleNodeMap; \
98 typedef Digraph::ArcMap<bool> BoolArcMap; \
99 typedef Digraph::ArcMap<int> IntArcMap; \
100 typedef Digraph::ArcMap<double> DoubleArcMap
102 ///Create convenience typedefs for the digraph types and iterators
104 ///\see DIGRAPH_TYPEDEFS
106 ///\note Use this macro, if the graph type is a dependent type,
107 ///ie. the graph type depend on a template parameter.
108 #define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \
109 typedef typename Digraph::Node Node; \
110 typedef typename Digraph::NodeIt NodeIt; \
111 typedef typename Digraph::Arc Arc; \
112 typedef typename Digraph::ArcIt ArcIt; \
113 typedef typename Digraph::InArcIt InArcIt; \
114 typedef typename Digraph::OutArcIt OutArcIt; \
115 typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \
116 typedef typename Digraph::template NodeMap<int> IntNodeMap; \
117 typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \
118 typedef typename Digraph::template ArcMap<bool> BoolArcMap; \
119 typedef typename Digraph::template ArcMap<int> IntArcMap; \
120 typedef typename Digraph::template ArcMap<double> DoubleArcMap
122 ///Create convenience typedefs for the graph types and iterators
124 ///This \c \#define creates the same convenient type definitions as defined
125 ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
126 ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
129 ///\note If the graph type is a dependent type, ie. the graph type depend
130 ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
132 #define GRAPH_TYPEDEFS(Graph) \
133 DIGRAPH_TYPEDEFS(Graph); \
134 typedef Graph::Edge Edge; \
135 typedef Graph::EdgeIt EdgeIt; \
136 typedef Graph::IncEdgeIt IncEdgeIt; \
137 typedef Graph::EdgeMap<bool> BoolEdgeMap; \
138 typedef Graph::EdgeMap<int> IntEdgeMap; \
139 typedef Graph::EdgeMap<double> DoubleEdgeMap
141 ///Create convenience typedefs for the graph types and iterators
143 ///\see GRAPH_TYPEDEFS
145 ///\note Use this macro, if the graph type is a dependent type,
146 ///ie. the graph type depend on a template parameter.
147 #define TEMPLATE_GRAPH_TYPEDEFS(Graph) \
148 TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \
149 typedef typename Graph::Edge Edge; \
150 typedef typename Graph::EdgeIt EdgeIt; \
151 typedef typename Graph::IncEdgeIt IncEdgeIt; \
152 typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \
153 typedef typename Graph::template EdgeMap<int> IntEdgeMap; \
154 typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
156 ///Create convenience typedefs for the bipartite graph types and iterators
158 ///This \c \#define creates the same convenient type definitions as
159 ///defined by \ref GRAPH_TYPEDEFS(BpGraph) and ten more, namely it
160 ///creates \c RedNode, \c RedNodeIt, \c BoolRedNodeMap,
161 ///\c IntRedNodeMap, \c DoubleRedNodeMap, \c BlueNode, \c BlueNodeIt,
162 ///\c BoolBlueNodeMap, \c IntBlueNodeMap, \c DoubleBlueNodeMap.
164 ///\note If the graph type is a dependent type, ie. the graph type depend
165 ///on a template parameter, then use \c TEMPLATE_BPGRAPH_TYPEDEFS()
167 #define BPGRAPH_TYPEDEFS(BpGraph) \
168 GRAPH_TYPEDEFS(BpGraph); \
169 typedef BpGraph::RedNode RedNode; \
170 typedef BpGraph::RedNodeIt RedNodeIt; \
171 typedef BpGraph::RedNodeMap<bool> BoolRedNodeMap; \
172 typedef BpGraph::RedNodeMap<int> IntRedNodeMap; \
173 typedef BpGraph::RedNodeMap<double> DoubleRedNodeMap; \
174 typedef BpGraph::BlueNode BlueNode; \
175 typedef BpGraph::BlueNodeIt BlueNodeIt; \
176 typedef BpGraph::BlueNodeMap<bool> BoolBlueNodeMap; \
177 typedef BpGraph::BlueNodeMap<int> IntBlueNodeMap; \
178 typedef BpGraph::BlueNodeMap<double> DoubleBlueNodeMap
180 ///Create convenience typedefs for the bipartite graph types and iterators
182 ///\see BPGRAPH_TYPEDEFS
184 ///\note Use this macro, if the graph type is a dependent type,
185 ///ie. the graph type depend on a template parameter.
186 #define TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph) \
187 TEMPLATE_GRAPH_TYPEDEFS(BpGraph); \
188 typedef typename BpGraph::RedNode RedNode; \
189 typedef typename BpGraph::RedNodeIt RedNodeIt; \
190 typedef typename BpGraph::template RedNodeMap<bool> BoolRedNodeMap; \
191 typedef typename BpGraph::template RedNodeMap<int> IntRedNodeMap; \
192 typedef typename BpGraph::template RedNodeMap<double> DoubleRedNodeMap; \
193 typedef typename BpGraph::BlueNode BlueNode; \
194 typedef typename BpGraph::BlueNodeIt BlueNodeIt; \
195 typedef typename BpGraph::template BlueNodeMap<bool> BoolBlueNodeMap; \
196 typedef typename BpGraph::template BlueNodeMap<int> IntBlueNodeMap; \
197 typedef typename BpGraph::template BlueNodeMap<double> DoubleBlueNodeMap
199 /// \brief Function to count the items in a graph.
201 /// This function counts the items (nodes, arcs etc.) in a graph.
202 /// The complexity of the function is linear because
203 /// it iterates on all of the items.
204 template <typename Graph, typename Item>
205 inline int countItems(const Graph& g) {
206 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
208 for (ItemIt it(g); it != INVALID; ++it) {
216 namespace _core_bits {
218 template <typename Graph, typename Enable = void>
219 struct CountNodesSelector {
220 static int count(const Graph &g) {
221 return countItems<Graph, typename Graph::Node>(g);
225 template <typename Graph>
226 struct CountNodesSelector<
228 enable_if<typename Graph::NodeNumTag, void>::type>
230 static int count(const Graph &g) {
236 /// \brief Function to count the nodes in the graph.
238 /// This function counts the nodes in the graph.
239 /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
240 /// graph structures it is specialized to run in <em>O</em>(1).
242 /// \note If the graph contains a \c nodeNum() member function and a
243 /// \c NodeNumTag tag then this function calls directly the member
244 /// function to query the cardinality of the node set.
245 template <typename Graph>
246 inline int countNodes(const Graph& g) {
247 return _core_bits::CountNodesSelector<Graph>::count(g);
250 namespace _graph_utils_bits {
252 template <typename Graph, typename Enable = void>
253 struct CountRedNodesSelector {
254 static int count(const Graph &g) {
255 return countItems<Graph, typename Graph::RedNode>(g);
259 template <typename Graph>
260 struct CountRedNodesSelector<
262 enable_if<typename Graph::NodeNumTag, void>::type>
264 static int count(const Graph &g) {
270 /// \brief Function to count the red nodes in the graph.
272 /// This function counts the red nodes in the graph.
273 /// The complexity of the function is O(n) but for some
274 /// graph structures it is specialized to run in O(1).
276 /// If the graph contains a \e redNum() member function and a
277 /// \e NodeNumTag tag then this function calls directly the member
278 /// function to query the cardinality of the node set.
279 template <typename Graph>
280 inline int countRedNodes(const Graph& g) {
281 return _graph_utils_bits::CountRedNodesSelector<Graph>::count(g);
284 namespace _graph_utils_bits {
286 template <typename Graph, typename Enable = void>
287 struct CountBlueNodesSelector {
288 static int count(const Graph &g) {
289 return countItems<Graph, typename Graph::BlueNode>(g);
293 template <typename Graph>
294 struct CountBlueNodesSelector<
296 enable_if<typename Graph::NodeNumTag, void>::type>
298 static int count(const Graph &g) {
304 /// \brief Function to count the blue nodes in the graph.
306 /// This function counts the blue nodes in the graph.
307 /// The complexity of the function is O(n) but for some
308 /// graph structures it is specialized to run in O(1).
310 /// If the graph contains a \e blueNum() member function and a
311 /// \e NodeNumTag tag then this function calls directly the member
312 /// function to query the cardinality of the node set.
313 template <typename Graph>
314 inline int countBlueNodes(const Graph& g) {
315 return _graph_utils_bits::CountBlueNodesSelector<Graph>::count(g);
320 namespace _core_bits {
322 template <typename Graph, typename Enable = void>
323 struct CountArcsSelector {
324 static int count(const Graph &g) {
325 return countItems<Graph, typename Graph::Arc>(g);
329 template <typename Graph>
330 struct CountArcsSelector<
332 typename enable_if<typename Graph::ArcNumTag, void>::type>
334 static int count(const Graph &g) {
340 /// \brief Function to count the arcs in the graph.
342 /// This function counts the arcs in the graph.
343 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
344 /// graph structures it is specialized to run in <em>O</em>(1).
346 /// \note If the graph contains a \c arcNum() member function and a
347 /// \c ArcNumTag tag then this function calls directly the member
348 /// function to query the cardinality of the arc set.
349 template <typename Graph>
350 inline int countArcs(const Graph& g) {
351 return _core_bits::CountArcsSelector<Graph>::count(g);
356 namespace _core_bits {
358 template <typename Graph, typename Enable = void>
359 struct CountEdgesSelector {
360 static int count(const Graph &g) {
361 return countItems<Graph, typename Graph::Edge>(g);
365 template <typename Graph>
366 struct CountEdgesSelector<
368 typename enable_if<typename Graph::EdgeNumTag, void>::type>
370 static int count(const Graph &g) {
376 /// \brief Function to count the edges in the graph.
378 /// This function counts the edges in the graph.
379 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
380 /// graph structures it is specialized to run in <em>O</em>(1).
382 /// \note If the graph contains a \c edgeNum() member function and a
383 /// \c EdgeNumTag tag then this function calls directly the member
384 /// function to query the cardinality of the edge set.
385 template <typename Graph>
386 inline int countEdges(const Graph& g) {
387 return _core_bits::CountEdgesSelector<Graph>::count(g);
392 template <typename Graph, typename DegIt>
393 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
395 for (DegIt it(_g, _n); it != INVALID; ++it) {
401 /// \brief Function to count the number of the out-arcs from node \c n.
403 /// This function counts the number of the out-arcs from node \c n
404 /// in the graph \c g.
405 template <typename Graph>
406 inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
407 return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
410 /// \brief Function to count the number of the in-arcs to node \c n.
412 /// This function counts the number of the in-arcs to node \c n
413 /// in the graph \c g.
414 template <typename Graph>
415 inline int countInArcs(const Graph& g, const typename Graph::Node& n) {
416 return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
419 /// \brief Function to count the number of the inc-edges to node \c n.
421 /// This function counts the number of the inc-edges to node \c n
422 /// in the undirected graph \c g.
423 template <typename Graph>
424 inline int countIncEdges(const Graph& g, const typename Graph::Node& n) {
425 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
428 namespace _core_bits {
430 template <typename Digraph, typename Item, typename RefMap>
433 virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
435 virtual ~MapCopyBase() {}
438 template <typename Digraph, typename Item, typename RefMap,
439 typename FromMap, typename ToMap>
440 class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
443 MapCopy(const FromMap& map, ToMap& tmap)
444 : _map(map), _tmap(tmap) {}
446 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
447 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
448 for (ItemIt it(digraph); it != INVALID; ++it) {
449 _tmap.set(refMap[it], _map[it]);
458 template <typename Digraph, typename Item, typename RefMap, typename It>
459 class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
462 ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
464 virtual void copy(const Digraph&, const RefMap& refMap) {
473 template <typename Digraph, typename Item, typename RefMap, typename Ref>
474 class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
477 RefCopy(Ref& map) : _map(map) {}
479 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
480 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
481 for (ItemIt it(digraph); it != INVALID; ++it) {
482 _map.set(it, refMap[it]);
490 template <typename Digraph, typename Item, typename RefMap,
492 class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
495 CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
497 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
498 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
499 for (ItemIt it(digraph); it != INVALID; ++it) {
500 _cmap.set(refMap[it], it);
508 template <typename Digraph, typename Enable = void>
509 struct DigraphCopySelector {
510 template <typename From, typename NodeRefMap, typename ArcRefMap>
511 static void copy(const From& from, Digraph &to,
512 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
514 for (typename From::NodeIt it(from); it != INVALID; ++it) {
515 nodeRefMap[it] = to.addNode();
517 for (typename From::ArcIt it(from); it != INVALID; ++it) {
518 arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
519 nodeRefMap[from.target(it)]);
524 template <typename Digraph>
525 struct DigraphCopySelector<
527 typename enable_if<typename Digraph::BuildTag, void>::type>
529 template <typename From, typename NodeRefMap, typename ArcRefMap>
530 static void copy(const From& from, Digraph &to,
531 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
532 to.build(from, nodeRefMap, arcRefMap);
536 template <typename Graph, typename Enable = void>
537 struct GraphCopySelector {
538 template <typename From, typename NodeRefMap, typename EdgeRefMap>
539 static void copy(const From& from, Graph &to,
540 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
542 for (typename From::NodeIt it(from); it != INVALID; ++it) {
543 nodeRefMap[it] = to.addNode();
545 for (typename From::EdgeIt it(from); it != INVALID; ++it) {
546 edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
547 nodeRefMap[from.v(it)]);
552 template <typename Graph>
553 struct GraphCopySelector<
555 typename enable_if<typename Graph::BuildTag, void>::type>
557 template <typename From, typename NodeRefMap, typename EdgeRefMap>
558 static void copy(const From& from, Graph &to,
559 NodeRefMap& nodeRefMap,
560 EdgeRefMap& edgeRefMap) {
561 to.build(from, nodeRefMap, edgeRefMap);
565 template <typename BpGraph, typename Enable = void>
566 struct BpGraphCopySelector {
567 template <typename From, typename RedNodeRefMap,
568 typename BlueNodeRefMap, typename EdgeRefMap>
569 static void copy(const From& from, BpGraph &to,
570 RedNodeRefMap& redNodeRefMap,
571 BlueNodeRefMap& blueNodeRefMap,
572 EdgeRefMap& edgeRefMap) {
574 for (typename From::RedNodeIt it(from); it != INVALID; ++it) {
575 redNodeRefMap[it] = to.addRedNode();
577 for (typename From::BlueNodeIt it(from); it != INVALID; ++it) {
578 blueNodeRefMap[it] = to.addBlueNode();
580 for (typename From::EdgeIt it(from); it != INVALID; ++it) {
581 edgeRefMap[it] = to.addEdge(redNodeRefMap[from.redNode(it)],
582 blueNodeRefMap[from.blueNode(it)]);
587 template <typename BpGraph>
588 struct BpGraphCopySelector<
590 typename enable_if<typename BpGraph::BuildTag, void>::type>
592 template <typename From, typename RedNodeRefMap,
593 typename BlueNodeRefMap, typename EdgeRefMap>
594 static void copy(const From& from, BpGraph &to,
595 RedNodeRefMap& redNodeRefMap,
596 BlueNodeRefMap& blueNodeRefMap,
597 EdgeRefMap& edgeRefMap) {
598 to.build(from, redNodeRefMap, blueNodeRefMap, edgeRefMap);
604 /// \brief Check whether a graph is undirected.
606 /// This function returns \c true if the given graph is undirected.
608 template <typename GR>
609 bool undirected(const GR& g) { return false; }
611 template <typename GR>
612 typename enable_if<UndirectedTagIndicator<GR>, bool>::type
613 undirected(const GR&) {
616 template <typename GR>
617 typename disable_if<UndirectedTagIndicator<GR>, bool>::type
618 undirected(const GR&) {
623 /// \brief Class to copy a digraph.
625 /// Class to copy a digraph to another digraph (duplicate a digraph). The
626 /// simplest way of using it is through the \c digraphCopy() function.
628 /// This class not only make a copy of a digraph, but it can create
629 /// references and cross references between the nodes and arcs of
630 /// the two digraphs, and it can copy maps to use with the newly created
633 /// To make a copy from a digraph, first an instance of DigraphCopy
634 /// should be created, then the data belongs to the digraph should
635 /// assigned to copy. In the end, the \c run() member should be
638 /// The next code copies a digraph with several data:
640 /// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
641 /// // Create references for the nodes
642 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
644 /// // Create cross references (inverse) for the arcs
645 /// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
646 /// cg.arcCrossRef(acr);
647 /// // Copy an arc map
648 /// OrigGraph::ArcMap<double> oamap(orig_graph);
649 /// NewGraph::ArcMap<double> namap(new_graph);
650 /// cg.arcMap(oamap, namap);
652 /// OrigGraph::Node on;
653 /// NewGraph::Node nn;
655 /// // Execute copying
658 template <typename From, typename To>
662 typedef typename From::Node Node;
663 typedef typename From::NodeIt NodeIt;
664 typedef typename From::Arc Arc;
665 typedef typename From::ArcIt ArcIt;
667 typedef typename To::Node TNode;
668 typedef typename To::Arc TArc;
670 typedef typename From::template NodeMap<TNode> NodeRefMap;
671 typedef typename From::template ArcMap<TArc> ArcRefMap;
675 /// \brief Constructor of DigraphCopy.
677 /// Constructor of DigraphCopy for copying the content of the
678 /// \c from digraph into the \c to digraph.
679 DigraphCopy(const From& from, To& to)
680 : _from(from), _to(to) {}
682 /// \brief Destructor of DigraphCopy
684 /// Destructor of DigraphCopy.
686 for (int i = 0; i < int(_node_maps.size()); ++i) {
687 delete _node_maps[i];
689 for (int i = 0; i < int(_arc_maps.size()); ++i) {
695 /// \brief Copy the node references into the given map.
697 /// This function copies the node references into the given map.
698 /// The parameter should be a map, whose key type is the Node type of
699 /// the source digraph, while the value type is the Node type of the
700 /// destination digraph.
701 template <typename NodeRef>
702 DigraphCopy& nodeRef(NodeRef& map) {
703 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
704 NodeRefMap, NodeRef>(map));
708 /// \brief Copy the node cross references into the given map.
710 /// This function copies the node cross references (reverse references)
711 /// into the given map. The parameter should be a map, whose key type
712 /// is the Node type of the destination digraph, while the value type is
713 /// the Node type of the source digraph.
714 template <typename NodeCrossRef>
715 DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
716 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
717 NodeRefMap, NodeCrossRef>(map));
721 /// \brief Make a copy of the given node map.
723 /// This function makes a copy of the given node map for the newly
725 /// The key type of the new map \c tmap should be the Node type of the
726 /// destination digraph, and the key type of the original map \c map
727 /// should be the Node type of the source digraph.
728 template <typename FromMap, typename ToMap>
729 DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
730 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
731 NodeRefMap, FromMap, ToMap>(map, tmap));
735 /// \brief Make a copy of the given node.
737 /// This function makes a copy of the given node.
738 DigraphCopy& node(const Node& node, TNode& tnode) {
739 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
740 NodeRefMap, TNode>(node, tnode));
744 /// \brief Copy the arc references into the given map.
746 /// This function copies the arc references into the given map.
747 /// The parameter should be a map, whose key type is the Arc type of
748 /// the source digraph, while the value type is the Arc type of the
749 /// destination digraph.
750 template <typename ArcRef>
751 DigraphCopy& arcRef(ArcRef& map) {
752 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
753 ArcRefMap, ArcRef>(map));
757 /// \brief Copy the arc cross references into the given map.
759 /// This function copies the arc cross references (reverse references)
760 /// into the given map. The parameter should be a map, whose key type
761 /// is the Arc type of the destination digraph, while the value type is
762 /// the Arc type of the source digraph.
763 template <typename ArcCrossRef>
764 DigraphCopy& arcCrossRef(ArcCrossRef& map) {
765 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
766 ArcRefMap, ArcCrossRef>(map));
770 /// \brief Make a copy of the given arc map.
772 /// This function makes a copy of the given arc map for the newly
774 /// The key type of the new map \c tmap should be the Arc type of the
775 /// destination digraph, and the key type of the original map \c map
776 /// should be the Arc type of the source digraph.
777 template <typename FromMap, typename ToMap>
778 DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
779 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
780 ArcRefMap, FromMap, ToMap>(map, tmap));
784 /// \brief Make a copy of the given arc.
786 /// This function makes a copy of the given arc.
787 DigraphCopy& arc(const Arc& arc, TArc& tarc) {
788 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
789 ArcRefMap, TArc>(arc, tarc));
793 /// \brief Execute copying.
795 /// This function executes the copying of the digraph along with the
796 /// copying of the assigned data.
798 NodeRefMap nodeRefMap(_from);
799 ArcRefMap arcRefMap(_from);
800 _core_bits::DigraphCopySelector<To>::
801 copy(_from, _to, nodeRefMap, arcRefMap);
802 for (int i = 0; i < int(_node_maps.size()); ++i) {
803 _node_maps[i]->copy(_from, nodeRefMap);
805 for (int i = 0; i < int(_arc_maps.size()); ++i) {
806 _arc_maps[i]->copy(_from, arcRefMap);
815 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
818 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
823 /// \brief Copy a digraph to another digraph.
825 /// This function copies a digraph to another digraph.
826 /// The complete usage of it is detailed in the DigraphCopy class, but
827 /// a short example shows a basic work:
829 /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
832 /// After the copy the \c nr map will contain the mapping from the
833 /// nodes of the \c from digraph to the nodes of the \c to digraph and
834 /// \c acr will contain the mapping from the arcs of the \c to digraph
835 /// to the arcs of the \c from digraph.
838 template <typename From, typename To>
839 DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
840 return DigraphCopy<From, To>(from, to);
843 /// \brief Class to copy a graph.
845 /// Class to copy a graph to another graph (duplicate a graph). The
846 /// simplest way of using it is through the \c graphCopy() function.
848 /// This class not only make a copy of a graph, but it can create
849 /// references and cross references between the nodes, edges and arcs of
850 /// the two graphs, and it can copy maps for using with the newly created
853 /// To make a copy from a graph, first an instance of GraphCopy
854 /// should be created, then the data belongs to the graph should
855 /// assigned to copy. In the end, the \c run() member should be
858 /// The next code copies a graph with several data:
860 /// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
861 /// // Create references for the nodes
862 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
864 /// // Create cross references (inverse) for the edges
865 /// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
866 /// cg.edgeCrossRef(ecr);
867 /// // Copy an edge map
868 /// OrigGraph::EdgeMap<double> oemap(orig_graph);
869 /// NewGraph::EdgeMap<double> nemap(new_graph);
870 /// cg.edgeMap(oemap, nemap);
872 /// OrigGraph::Node on;
873 /// NewGraph::Node nn;
875 /// // Execute copying
878 template <typename From, typename To>
882 typedef typename From::Node Node;
883 typedef typename From::NodeIt NodeIt;
884 typedef typename From::Arc Arc;
885 typedef typename From::ArcIt ArcIt;
886 typedef typename From::Edge Edge;
887 typedef typename From::EdgeIt EdgeIt;
889 typedef typename To::Node TNode;
890 typedef typename To::Arc TArc;
891 typedef typename To::Edge TEdge;
893 typedef typename From::template NodeMap<TNode> NodeRefMap;
894 typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
897 ArcRefMap(const From& from, const To& to,
898 const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
899 : _from(from), _to(to),
900 _edge_ref(edge_ref), _node_ref(node_ref) {}
902 typedef typename From::Arc Key;
903 typedef typename To::Arc Value;
905 Value operator[](const Key& key) const {
906 bool forward = _from.u(key) != _from.v(key) ?
907 _node_ref[_from.source(key)] ==
908 _to.source(_to.direct(_edge_ref[key], true)) :
909 _from.direction(key);
910 return _to.direct(_edge_ref[key], forward);
915 const EdgeRefMap& _edge_ref;
916 const NodeRefMap& _node_ref;
921 /// \brief Constructor of GraphCopy.
923 /// Constructor of GraphCopy for copying the content of the
924 /// \c from graph into the \c to graph.
925 GraphCopy(const From& from, To& to)
926 : _from(from), _to(to) {}
928 /// \brief Destructor of GraphCopy
930 /// Destructor of GraphCopy.
932 for (int i = 0; i < int(_node_maps.size()); ++i) {
933 delete _node_maps[i];
935 for (int i = 0; i < int(_arc_maps.size()); ++i) {
938 for (int i = 0; i < int(_edge_maps.size()); ++i) {
939 delete _edge_maps[i];
943 /// \brief Copy the node references into the given map.
945 /// This function copies the node references into the given map.
946 /// The parameter should be a map, whose key type is the Node type of
947 /// the source graph, while the value type is the Node type of the
948 /// destination graph.
949 template <typename NodeRef>
950 GraphCopy& nodeRef(NodeRef& map) {
951 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
952 NodeRefMap, NodeRef>(map));
956 /// \brief Copy the node cross references into the given map.
958 /// This function copies the node cross references (reverse references)
959 /// into the given map. The parameter should be a map, whose key type
960 /// is the Node type of the destination graph, while the value type is
961 /// the Node type of the source graph.
962 template <typename NodeCrossRef>
963 GraphCopy& nodeCrossRef(NodeCrossRef& map) {
964 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
965 NodeRefMap, NodeCrossRef>(map));
969 /// \brief Make a copy of the given node map.
971 /// This function makes a copy of the given node map for the newly
973 /// The key type of the new map \c tmap should be the Node type of the
974 /// destination graph, and the key type of the original map \c map
975 /// should be the Node type of the source graph.
976 template <typename FromMap, typename ToMap>
977 GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
978 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
979 NodeRefMap, FromMap, ToMap>(map, tmap));
983 /// \brief Make a copy of the given node.
985 /// This function makes a copy of the given node.
986 GraphCopy& node(const Node& node, TNode& tnode) {
987 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
988 NodeRefMap, TNode>(node, tnode));
992 /// \brief Copy the arc references into the given map.
994 /// This function copies the arc references into the given map.
995 /// The parameter should be a map, whose key type is the Arc type of
996 /// the source graph, while the value type is the Arc type of the
997 /// destination graph.
998 template <typename ArcRef>
999 GraphCopy& arcRef(ArcRef& map) {
1000 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
1001 ArcRefMap, ArcRef>(map));
1005 /// \brief Copy the arc cross references into the given map.
1007 /// This function copies the arc cross references (reverse references)
1008 /// into the given map. The parameter should be a map, whose key type
1009 /// is the Arc type of the destination graph, while the value type is
1010 /// the Arc type of the source graph.
1011 template <typename ArcCrossRef>
1012 GraphCopy& arcCrossRef(ArcCrossRef& map) {
1013 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
1014 ArcRefMap, ArcCrossRef>(map));
1018 /// \brief Make a copy of the given arc map.
1020 /// This function makes a copy of the given arc map for the newly
1022 /// The key type of the new map \c tmap should be the Arc type of the
1023 /// destination graph, and the key type of the original map \c map
1024 /// should be the Arc type of the source graph.
1025 template <typename FromMap, typename ToMap>
1026 GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
1027 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
1028 ArcRefMap, FromMap, ToMap>(map, tmap));
1032 /// \brief Make a copy of the given arc.
1034 /// This function makes a copy of the given arc.
1035 GraphCopy& arc(const Arc& arc, TArc& tarc) {
1036 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
1037 ArcRefMap, TArc>(arc, tarc));
1041 /// \brief Copy the edge references into the given map.
1043 /// This function copies the edge references into the given map.
1044 /// The parameter should be a map, whose key type is the Edge type of
1045 /// the source graph, while the value type is the Edge type of the
1046 /// destination graph.
1047 template <typename EdgeRef>
1048 GraphCopy& edgeRef(EdgeRef& map) {
1049 _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
1050 EdgeRefMap, EdgeRef>(map));
1054 /// \brief Copy the edge cross references into the given map.
1056 /// This function copies the edge cross references (reverse references)
1057 /// into the given map. The parameter should be a map, whose key type
1058 /// is the Edge type of the destination graph, while the value type is
1059 /// the Edge type of the source graph.
1060 template <typename EdgeCrossRef>
1061 GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1062 _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
1063 Edge, EdgeRefMap, EdgeCrossRef>(map));
1067 /// \brief Make a copy of the given edge map.
1069 /// This function makes a copy of the given edge map for the newly
1071 /// The key type of the new map \c tmap should be the Edge type of the
1072 /// destination graph, and the key type of the original map \c map
1073 /// should be the Edge type of the source graph.
1074 template <typename FromMap, typename ToMap>
1075 GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
1076 _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
1077 EdgeRefMap, FromMap, ToMap>(map, tmap));
1081 /// \brief Make a copy of the given edge.
1083 /// This function makes a copy of the given edge.
1084 GraphCopy& edge(const Edge& edge, TEdge& tedge) {
1085 _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
1086 EdgeRefMap, TEdge>(edge, tedge));
1090 /// \brief Execute copying.
1092 /// This function executes the copying of the graph along with the
1093 /// copying of the assigned data.
1095 NodeRefMap nodeRefMap(_from);
1096 EdgeRefMap edgeRefMap(_from);
1097 ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
1098 _core_bits::GraphCopySelector<To>::
1099 copy(_from, _to, nodeRefMap, edgeRefMap);
1100 for (int i = 0; i < int(_node_maps.size()); ++i) {
1101 _node_maps[i]->copy(_from, nodeRefMap);
1103 for (int i = 0; i < int(_edge_maps.size()); ++i) {
1104 _edge_maps[i]->copy(_from, edgeRefMap);
1106 for (int i = 0; i < int(_arc_maps.size()); ++i) {
1107 _arc_maps[i]->copy(_from, arcRefMap);
1116 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
1119 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
1122 std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
1127 /// \brief Copy a graph to another graph.
1129 /// This function copies a graph to another graph.
1130 /// The complete usage of it is detailed in the GraphCopy class,
1131 /// but a short example shows a basic work:
1133 /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
1136 /// After the copy the \c nr map will contain the mapping from the
1137 /// nodes of the \c from graph to the nodes of the \c to graph and
1138 /// \c ecr will contain the mapping from the edges of the \c to graph
1139 /// to the edges of the \c from graph.
1142 template <typename From, typename To>
1144 graphCopy(const From& from, To& to) {
1145 return GraphCopy<From, To>(from, to);
1148 /// \brief Class to copy a bipartite graph.
1150 /// Class to copy a bipartite graph to another graph (duplicate a
1151 /// graph). The simplest way of using it is through the
1152 /// \c bpGraphCopy() function.
1154 /// This class not only make a copy of a bipartite graph, but it can
1155 /// create references and cross references between the nodes, edges
1156 /// and arcs of the two graphs, and it can copy maps for using with
1157 /// the newly created graph.
1159 /// To make a copy from a graph, first an instance of BpGraphCopy
1160 /// should be created, then the data belongs to the graph should
1161 /// assigned to copy. In the end, the \c run() member should be
1164 /// The next code copies a graph with several data:
1166 /// BpGraphCopy<OrigBpGraph, NewBpGraph> cg(orig_graph, new_graph);
1167 /// // Create references for the nodes
1168 /// OrigBpGraph::NodeMap<NewBpGraph::Node> nr(orig_graph);
1170 /// // Create cross references (inverse) for the edges
1171 /// NewBpGraph::EdgeMap<OrigBpGraph::Edge> ecr(new_graph);
1172 /// cg.edgeCrossRef(ecr);
1173 /// // Copy a red node map
1174 /// OrigBpGraph::RedNodeMap<double> ormap(orig_graph);
1175 /// NewBpGraph::RedNodeMap<double> nrmap(new_graph);
1176 /// cg.redNodeMap(ormap, nrmap);
1178 /// OrigBpGraph::Node on;
1179 /// NewBpGraph::Node nn;
1180 /// cg.node(on, nn);
1181 /// // Execute copying
1184 template <typename From, typename To>
1188 typedef typename From::Node Node;
1189 typedef typename From::RedNode RedNode;
1190 typedef typename From::BlueNode BlueNode;
1191 typedef typename From::NodeIt NodeIt;
1192 typedef typename From::Arc Arc;
1193 typedef typename From::ArcIt ArcIt;
1194 typedef typename From::Edge Edge;
1195 typedef typename From::EdgeIt EdgeIt;
1197 typedef typename To::Node TNode;
1198 typedef typename To::RedNode TRedNode;
1199 typedef typename To::BlueNode TBlueNode;
1200 typedef typename To::Arc TArc;
1201 typedef typename To::Edge TEdge;
1203 typedef typename From::template RedNodeMap<TRedNode> RedNodeRefMap;
1204 typedef typename From::template BlueNodeMap<TBlueNode> BlueNodeRefMap;
1205 typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
1208 NodeRefMap(const From& from, const RedNodeRefMap& red_node_ref,
1209 const BlueNodeRefMap& blue_node_ref)
1210 : _from(from), _red_node_ref(red_node_ref),
1211 _blue_node_ref(blue_node_ref) {}
1213 typedef typename From::Node Key;
1214 typedef typename To::Node Value;
1216 Value operator[](const Key& key) const {
1217 if (_from.red(key)) {
1218 return _red_node_ref[_from.asRedNodeUnsafe(key)];
1220 return _blue_node_ref[_from.asBlueNodeUnsafe(key)];
1225 const RedNodeRefMap& _red_node_ref;
1226 const BlueNodeRefMap& _blue_node_ref;
1230 ArcRefMap(const From& from, const To& to, const EdgeRefMap& edge_ref)
1231 : _from(from), _to(to), _edge_ref(edge_ref) {}
1233 typedef typename From::Arc Key;
1234 typedef typename To::Arc Value;
1236 Value operator[](const Key& key) const {
1237 return _to.direct(_edge_ref[key], _from.direction(key));
1242 const EdgeRefMap& _edge_ref;
1247 /// \brief Constructor of BpGraphCopy.
1249 /// Constructor of BpGraphCopy for copying the content of the
1250 /// \c from graph into the \c to graph.
1251 BpGraphCopy(const From& from, To& to)
1252 : _from(from), _to(to) {}
1254 /// \brief Destructor of BpGraphCopy
1256 /// Destructor of BpGraphCopy.
1258 for (int i = 0; i < int(_node_maps.size()); ++i) {
1259 delete _node_maps[i];
1261 for (int i = 0; i < int(_red_maps.size()); ++i) {
1262 delete _red_maps[i];
1264 for (int i = 0; i < int(_blue_maps.size()); ++i) {
1265 delete _blue_maps[i];
1267 for (int i = 0; i < int(_arc_maps.size()); ++i) {
1268 delete _arc_maps[i];
1270 for (int i = 0; i < int(_edge_maps.size()); ++i) {
1271 delete _edge_maps[i];
1275 /// \brief Copy the node references into the given map.
1277 /// This function copies the node references into the given map.
1278 /// The parameter should be a map, whose key type is the Node type of
1279 /// the source graph, while the value type is the Node type of the
1280 /// destination graph.
1281 template <typename NodeRef>
1282 BpGraphCopy& nodeRef(NodeRef& map) {
1283 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
1284 NodeRefMap, NodeRef>(map));
1288 /// \brief Copy the node cross references into the given map.
1290 /// This function copies the node cross references (reverse references)
1291 /// into the given map. The parameter should be a map, whose key type
1292 /// is the Node type of the destination graph, while the value type is
1293 /// the Node type of the source graph.
1294 template <typename NodeCrossRef>
1295 BpGraphCopy& nodeCrossRef(NodeCrossRef& map) {
1296 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
1297 NodeRefMap, NodeCrossRef>(map));
1301 /// \brief Make a copy of the given node map.
1303 /// This function makes a copy of the given node map for the newly
1305 /// The key type of the new map \c tmap should be the Node type of the
1306 /// destination graph, and the key type of the original map \c map
1307 /// should be the Node type of the source graph.
1308 template <typename FromMap, typename ToMap>
1309 BpGraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
1310 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
1311 NodeRefMap, FromMap, ToMap>(map, tmap));
1315 /// \brief Make a copy of the given node.
1317 /// This function makes a copy of the given node.
1318 BpGraphCopy& node(const Node& node, TNode& tnode) {
1319 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
1320 NodeRefMap, TNode>(node, tnode));
1324 /// \brief Copy the red node references into the given map.
1326 /// This function copies the red node references into the given
1327 /// map. The parameter should be a map, whose key type is the
1328 /// Node type of the source graph with the red item set, while the
1329 /// value type is the Node type of the destination graph.
1330 template <typename RedRef>
1331 BpGraphCopy& redRef(RedRef& map) {
1332 _red_maps.push_back(new _core_bits::RefCopy<From, RedNode,
1333 RedNodeRefMap, RedRef>(map));
1337 /// \brief Copy the red node cross references into the given map.
1339 /// This function copies the red node cross references (reverse
1340 /// references) into the given map. The parameter should be a map,
1341 /// whose key type is the Node type of the destination graph with
1342 /// the red item set, while the value type is the Node type of the
1344 template <typename RedCrossRef>
1345 BpGraphCopy& redCrossRef(RedCrossRef& map) {
1346 _red_maps.push_back(new _core_bits::CrossRefCopy<From, RedNode,
1347 RedNodeRefMap, RedCrossRef>(map));
1351 /// \brief Make a copy of the given red node map.
1353 /// This function makes a copy of the given red node map for the newly
1355 /// The key type of the new map \c tmap should be the Node type of
1356 /// the destination graph with the red items, and the key type of
1357 /// the original map \c map should be the Node type of the source
1359 template <typename FromMap, typename ToMap>
1360 BpGraphCopy& redNodeMap(const FromMap& map, ToMap& tmap) {
1361 _red_maps.push_back(new _core_bits::MapCopy<From, RedNode,
1362 RedNodeRefMap, FromMap, ToMap>(map, tmap));
1366 /// \brief Make a copy of the given red node.
1368 /// This function makes a copy of the given red node.
1369 BpGraphCopy& redNode(const RedNode& node, TRedNode& tnode) {
1370 _red_maps.push_back(new _core_bits::ItemCopy<From, RedNode,
1371 RedNodeRefMap, TRedNode>(node, tnode));
1375 /// \brief Copy the blue node references into the given map.
1377 /// This function copies the blue node references into the given
1378 /// map. The parameter should be a map, whose key type is the
1379 /// Node type of the source graph with the blue item set, while the
1380 /// value type is the Node type of the destination graph.
1381 template <typename BlueRef>
1382 BpGraphCopy& blueRef(BlueRef& map) {
1383 _blue_maps.push_back(new _core_bits::RefCopy<From, BlueNode,
1384 BlueNodeRefMap, BlueRef>(map));
1388 /// \brief Copy the blue node cross references into the given map.
1390 /// This function copies the blue node cross references (reverse
1391 /// references) into the given map. The parameter should be a map,
1392 /// whose key type is the Node type of the destination graph with
1393 /// the blue item set, while the value type is the Node type of the
1395 template <typename BlueCrossRef>
1396 BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
1397 _blue_maps.push_back(new _core_bits::CrossRefCopy<From, BlueNode,
1398 BlueNodeRefMap, BlueCrossRef>(map));
1402 /// \brief Make a copy of the given blue node map.
1404 /// This function makes a copy of the given blue node map for the newly
1406 /// The key type of the new map \c tmap should be the Node type of
1407 /// the destination graph with the blue items, and the key type of
1408 /// the original map \c map should be the Node type of the source
1410 template <typename FromMap, typename ToMap>
1411 BpGraphCopy& blueNodeMap(const FromMap& map, ToMap& tmap) {
1412 _blue_maps.push_back(new _core_bits::MapCopy<From, BlueNode,
1413 BlueNodeRefMap, FromMap, ToMap>(map, tmap));
1417 /// \brief Make a copy of the given blue node.
1419 /// This function makes a copy of the given blue node.
1420 BpGraphCopy& blueNode(const BlueNode& node, TBlueNode& tnode) {
1421 _blue_maps.push_back(new _core_bits::ItemCopy<From, BlueNode,
1422 BlueNodeRefMap, TBlueNode>(node, tnode));
1426 /// \brief Copy the arc references into the given map.
1428 /// This function copies the arc references into the given map.
1429 /// The parameter should be a map, whose key type is the Arc type of
1430 /// the source graph, while the value type is the Arc type of the
1431 /// destination graph.
1432 template <typename ArcRef>
1433 BpGraphCopy& arcRef(ArcRef& map) {
1434 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
1435 ArcRefMap, ArcRef>(map));
1439 /// \brief Copy the arc cross references into the given map.
1441 /// This function copies the arc cross references (reverse references)
1442 /// into the given map. The parameter should be a map, whose key type
1443 /// is the Arc type of the destination graph, while the value type is
1444 /// the Arc type of the source graph.
1445 template <typename ArcCrossRef>
1446 BpGraphCopy& arcCrossRef(ArcCrossRef& map) {
1447 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
1448 ArcRefMap, ArcCrossRef>(map));
1452 /// \brief Make a copy of the given arc map.
1454 /// This function makes a copy of the given arc map for the newly
1456 /// The key type of the new map \c tmap should be the Arc type of the
1457 /// destination graph, and the key type of the original map \c map
1458 /// should be the Arc type of the source graph.
1459 template <typename FromMap, typename ToMap>
1460 BpGraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
1461 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
1462 ArcRefMap, FromMap, ToMap>(map, tmap));
1466 /// \brief Make a copy of the given arc.
1468 /// This function makes a copy of the given arc.
1469 BpGraphCopy& arc(const Arc& arc, TArc& tarc) {
1470 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
1471 ArcRefMap, TArc>(arc, tarc));
1475 /// \brief Copy the edge references into the given map.
1477 /// This function copies the edge references into the given map.
1478 /// The parameter should be a map, whose key type is the Edge type of
1479 /// the source graph, while the value type is the Edge type of the
1480 /// destination graph.
1481 template <typename EdgeRef>
1482 BpGraphCopy& edgeRef(EdgeRef& map) {
1483 _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
1484 EdgeRefMap, EdgeRef>(map));
1488 /// \brief Copy the edge cross references into the given map.
1490 /// This function copies the edge cross references (reverse references)
1491 /// into the given map. The parameter should be a map, whose key type
1492 /// is the Edge type of the destination graph, while the value type is
1493 /// the Edge type of the source graph.
1494 template <typename EdgeCrossRef>
1495 BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
1496 _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
1497 Edge, EdgeRefMap, EdgeCrossRef>(map));
1501 /// \brief Make a copy of the given edge map.
1503 /// This function makes a copy of the given edge map for the newly
1505 /// The key type of the new map \c tmap should be the Edge type of the
1506 /// destination graph, and the key type of the original map \c map
1507 /// should be the Edge type of the source graph.
1508 template <typename FromMap, typename ToMap>
1509 BpGraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
1510 _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
1511 EdgeRefMap, FromMap, ToMap>(map, tmap));
1515 /// \brief Make a copy of the given edge.
1517 /// This function makes a copy of the given edge.
1518 BpGraphCopy& edge(const Edge& edge, TEdge& tedge) {
1519 _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
1520 EdgeRefMap, TEdge>(edge, tedge));
1524 /// \brief Execute copying.
1526 /// This function executes the copying of the graph along with the
1527 /// copying of the assigned data.
1529 RedNodeRefMap redNodeRefMap(_from);
1530 BlueNodeRefMap blueNodeRefMap(_from);
1531 NodeRefMap nodeRefMap(_from, redNodeRefMap, blueNodeRefMap);
1532 EdgeRefMap edgeRefMap(_from);
1533 ArcRefMap arcRefMap(_from, _to, edgeRefMap);
1534 _core_bits::BpGraphCopySelector<To>::
1535 copy(_from, _to, redNodeRefMap, blueNodeRefMap, edgeRefMap);
1536 for (int i = 0; i < int(_node_maps.size()); ++i) {
1537 _node_maps[i]->copy(_from, nodeRefMap);
1539 for (int i = 0; i < int(_red_maps.size()); ++i) {
1540 _red_maps[i]->copy(_from, redNodeRefMap);
1542 for (int i = 0; i < int(_blue_maps.size()); ++i) {
1543 _blue_maps[i]->copy(_from, blueNodeRefMap);
1545 for (int i = 0; i < int(_edge_maps.size()); ++i) {
1546 _edge_maps[i]->copy(_from, edgeRefMap);
1548 for (int i = 0; i < int(_arc_maps.size()); ++i) {
1549 _arc_maps[i]->copy(_from, arcRefMap);
1558 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
1561 std::vector<_core_bits::MapCopyBase<From, RedNode, RedNodeRefMap>* >
1564 std::vector<_core_bits::MapCopyBase<From, BlueNode, BlueNodeRefMap>* >
1567 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
1570 std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
1575 /// \brief Copy a graph to another graph.
1577 /// This function copies a graph to another graph.
1578 /// The complete usage of it is detailed in the BpGraphCopy class,
1579 /// but a short example shows a basic work:
1581 /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
1584 /// After the copy the \c nr map will contain the mapping from the
1585 /// nodes of the \c from graph to the nodes of the \c to graph and
1586 /// \c ecr will contain the mapping from the edges of the \c to graph
1587 /// to the edges of the \c from graph.
1589 /// \see BpGraphCopy
1590 template <typename From, typename To>
1591 BpGraphCopy<From, To>
1592 bpGraphCopy(const From& from, To& to) {
1593 return BpGraphCopy<From, To>(from, to);
1596 namespace _core_bits {
1598 template <typename Graph, typename Enable = void>
1599 struct FindArcSelector {
1600 typedef typename Graph::Node Node;
1601 typedef typename Graph::Arc Arc;
1602 static Arc find(const Graph &g, Node u, Node v, Arc e) {
1608 while (e != INVALID && g.target(e) != v) {
1615 template <typename Graph>
1616 struct FindArcSelector<
1618 typename enable_if<typename Graph::FindArcTag, void>::type>
1620 typedef typename Graph::Node Node;
1621 typedef typename Graph::Arc Arc;
1622 static Arc find(const Graph &g, Node u, Node v, Arc prev) {
1623 return g.findArc(u, v, prev);
1628 /// \brief Find an arc between two nodes of a digraph.
1630 /// This function finds an arc from node \c u to node \c v in the
1633 /// If \c prev is \ref INVALID (this is the default value), then
1634 /// it finds the first arc from \c u to \c v. Otherwise it looks for
1635 /// the next arc from \c u to \c v after \c prev.
1636 /// \return The found arc or \ref INVALID if there is no such an arc.
1638 /// Thus you can iterate through each arc from \c u to \c v as it follows.
1640 /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
1645 /// \note \ref ConArcIt provides iterator interface for the same
1649 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1650 template <typename Graph>
1651 inline typename Graph::Arc
1652 findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1653 typename Graph::Arc prev = INVALID) {
1654 return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
1657 /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
1659 /// Iterator for iterating on parallel arcs connecting the same nodes. It is
1660 /// a higher level interface for the \ref findArc() function. You can
1661 /// use it the following way:
1663 /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
1669 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1670 template <typename GR>
1671 class ConArcIt : public GR::Arc {
1672 typedef typename GR::Arc Parent;
1676 typedef typename GR::Arc Arc;
1677 typedef typename GR::Node Node;
1679 /// \brief Constructor.
1681 /// Construct a new ConArcIt iterating on the arcs that
1682 /// connects nodes \c u and \c v.
1683 ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
1684 Parent::operator=(findArc(_graph, u, v));
1687 /// \brief Constructor.
1689 /// Construct a new ConArcIt that continues the iterating from arc \c a.
1690 ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
1692 /// \brief Increment operator.
1694 /// It increments the iterator and gives back the next arc.
1695 ConArcIt& operator++() {
1696 Parent::operator=(findArc(_graph, _graph.source(*this),
1697 _graph.target(*this), *this));
1704 namespace _core_bits {
1706 template <typename Graph, typename Enable = void>
1707 struct FindEdgeSelector {
1708 typedef typename Graph::Node Node;
1709 typedef typename Graph::Edge Edge;
1710 static Edge find(const Graph &g, Node u, Node v, Edge e) {
1714 g.firstInc(e, b, u);
1719 while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
1724 g.firstInc(e, b, u);
1729 while (e != INVALID && (!b || g.v(e) != v)) {
1737 template <typename Graph>
1738 struct FindEdgeSelector<
1740 typename enable_if<typename Graph::FindEdgeTag, void>::type>
1742 typedef typename Graph::Node Node;
1743 typedef typename Graph::Edge Edge;
1744 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
1745 return g.findEdge(u, v, prev);
1750 /// \brief Find an edge between two nodes of a graph.
1752 /// This function finds an edge from node \c u to node \c v in graph \c g.
1753 /// If node \c u and node \c v is equal then each loop edge
1754 /// will be enumerated once.
1756 /// If \c prev is \ref INVALID (this is the default value), then
1757 /// it finds the first edge from \c u to \c v. Otherwise it looks for
1758 /// the next edge from \c u to \c v after \c prev.
1759 /// \return The found edge or \ref INVALID if there is no such an edge.
1761 /// Thus you can iterate through each edge between \c u and \c v
1764 /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
1769 /// \note \ref ConEdgeIt provides iterator interface for the same
1773 template <typename Graph>
1774 inline typename Graph::Edge
1775 findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1776 typename Graph::Edge p = INVALID) {
1777 return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
1780 /// \brief Iterator for iterating on parallel edges connecting the same nodes.
1782 /// Iterator for iterating on parallel edges connecting the same nodes.
1783 /// It is a higher level interface for the findEdge() function. You can
1784 /// use it the following way:
1786 /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
1792 template <typename GR>
1793 class ConEdgeIt : public GR::Edge {
1794 typedef typename GR::Edge Parent;
1798 typedef typename GR::Edge Edge;
1799 typedef typename GR::Node Node;
1801 /// \brief Constructor.
1803 /// Construct a new ConEdgeIt iterating on the edges that
1804 /// connects nodes \c u and \c v.
1805 ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1806 Parent::operator=(findEdge(_graph, _u, _v));
1809 /// \brief Constructor.
1811 /// Construct a new ConEdgeIt that continues iterating from edge \c e.
1812 ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
1814 /// \brief Increment operator.
1816 /// It increments the iterator and gives back the next edge.
1817 ConEdgeIt& operator++() {
1818 Parent::operator=(findEdge(_graph, _u, _v, *this));
1827 ///Dynamic arc look-up between given endpoints.
1829 ///Using this class, you can find an arc in a digraph from a given
1830 ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
1831 ///where <em>d</em> is the out-degree of the source node.
1833 ///It is possible to find \e all parallel arcs between two nodes with
1834 ///the \c operator() member.
1836 ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
1837 ///\ref AllArcLookUp if your digraph is not changed so frequently.
1839 ///This class uses a self-adjusting binary search tree, the Splay tree
1840 ///of Sleator and Tarjan to guarantee the logarithmic amortized
1841 ///time bound for arc look-ups. This class also guarantees the
1842 ///optimal time bound in a constant factor for any distribution of
1845 ///\tparam GR The type of the underlying digraph.
1849 template <typename GR>
1851 : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
1853 typedef typename ItemSetTraits<GR, typename GR::Arc>
1854 ::ItemNotifier::ObserverBase Parent;
1856 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1860 /// The Digraph type
1865 class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type
1867 typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
1871 AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
1873 virtual void add(const Node& node) {
1875 Parent::set(node, INVALID);
1878 virtual void add(const std::vector<Node>& nodes) {
1880 for (int i = 0; i < int(nodes.size()); ++i) {
1881 Parent::set(nodes[i], INVALID);
1885 virtual void build() {
1888 typename Parent::Notifier* nf = Parent::notifier();
1889 for (nf->first(it); it != INVALID; nf->next(it)) {
1890 Parent::set(it, INVALID);
1898 ArcLess(const Digraph &_g) : g(_g) {}
1899 bool operator()(Arc a,Arc b) const
1901 return g.target(a)<g.target(b);
1909 typename Digraph::template ArcMap<Arc> _parent;
1910 typename Digraph::template ArcMap<Arc> _left;
1911 typename Digraph::template ArcMap<Arc> _right;
1919 ///It builds up the search database.
1920 DynArcLookUp(const Digraph &g)
1921 : _g(g),_head(g),_parent(g),_left(g),_right(g)
1923 Parent::attach(_g.notifier(typename Digraph::Arc()));
1929 virtual void add(const Arc& arc) {
1933 virtual void add(const std::vector<Arc>& arcs) {
1934 for (int i = 0; i < int(arcs.size()); ++i) {
1939 virtual void erase(const Arc& arc) {
1943 virtual void erase(const std::vector<Arc>& arcs) {
1944 for (int i = 0; i < int(arcs.size()); ++i) {
1949 virtual void build() {
1953 virtual void clear() {
1954 for(NodeIt n(_g);n!=INVALID;++n) {
1959 void insert(Arc arc) {
1960 Node s = _g.source(arc);
1961 Node t = _g.target(arc);
1962 _left[arc] = INVALID;
1963 _right[arc] = INVALID;
1968 _parent[arc] = INVALID;
1972 if (t < _g.target(e)) {
1973 if (_left[e] == INVALID) {
1982 if (_right[e] == INVALID) {
1994 void remove(Arc arc) {
1995 if (_left[arc] == INVALID) {
1996 if (_right[arc] != INVALID) {
1997 _parent[_right[arc]] = _parent[arc];
1999 if (_parent[arc] != INVALID) {
2000 if (_left[_parent[arc]] == arc) {
2001 _left[_parent[arc]] = _right[arc];
2003 _right[_parent[arc]] = _right[arc];
2006 _head[_g.source(arc)] = _right[arc];
2008 } else if (_right[arc] == INVALID) {
2009 _parent[_left[arc]] = _parent[arc];
2010 if (_parent[arc] != INVALID) {
2011 if (_left[_parent[arc]] == arc) {
2012 _left[_parent[arc]] = _left[arc];
2014 _right[_parent[arc]] = _left[arc];
2017 _head[_g.source(arc)] = _left[arc];
2021 if (_right[e] != INVALID) {
2023 while (_right[e] != INVALID) {
2027 _right[_parent[e]] = _left[e];
2028 if (_left[e] != INVALID) {
2029 _parent[_left[e]] = _parent[e];
2032 _left[e] = _left[arc];
2033 _parent[_left[arc]] = e;
2034 _right[e] = _right[arc];
2035 _parent[_right[arc]] = e;
2037 _parent[e] = _parent[arc];
2038 if (_parent[arc] != INVALID) {
2039 if (_left[_parent[arc]] == arc) {
2040 _left[_parent[arc]] = e;
2042 _right[_parent[arc]] = e;
2047 _right[e] = _right[arc];
2048 _parent[_right[arc]] = e;
2049 _parent[e] = _parent[arc];
2051 if (_parent[arc] != INVALID) {
2052 if (_left[_parent[arc]] == arc) {
2053 _left[_parent[arc]] = e;
2055 _right[_parent[arc]] = e;
2058 _head[_g.source(arc)] = e;
2064 Arc refreshRec(std::vector<Arc> &v,int a,int b)
2069 Arc left = refreshRec(v,a,m-1);
2073 _left[me] = INVALID;
2076 Arc right = refreshRec(v,m+1,b);
2078 _parent[right] = me;
2080 _right[me] = INVALID;
2086 for(NodeIt n(_g);n!=INVALID;++n) {
2088 for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
2090 std::sort(v.begin(),v.end(),ArcLess(_g));
2091 Arc head = refreshRec(v,0,v.size()-1);
2093 _parent[head] = INVALID;
2095 else _head[n] = INVALID;
2101 _parent[v] = _parent[w];
2103 _left[w] = _right[v];
2105 if (_parent[v] != INVALID) {
2106 if (_right[_parent[v]] == w) {
2107 _right[_parent[v]] = v;
2109 _left[_parent[v]] = v;
2112 if (_left[w] != INVALID){
2113 _parent[_left[w]] = w;
2119 _parent[v] = _parent[w];
2121 _right[w] = _left[v];
2123 if (_parent[v] != INVALID){
2124 if (_left[_parent[v]] == w) {
2125 _left[_parent[v]] = v;
2127 _right[_parent[v]] = v;
2130 if (_right[w] != INVALID){
2131 _parent[_right[w]] = w;
2136 while (_parent[v] != INVALID) {
2137 if (v == _left[_parent[v]]) {
2138 if (_parent[_parent[v]] == INVALID) {
2141 if (_parent[v] == _left[_parent[_parent[v]]]) {
2150 if (_parent[_parent[v]] == INVALID) {
2153 if (_parent[v] == _left[_parent[_parent[v]]]) {
2163 _head[_g.source(v)] = v;
2169 ///Find an arc between two nodes.
2171 ///Find an arc between two nodes.
2172 ///\param s The source node.
2173 ///\param t The target node.
2174 ///\param p The previous arc between \c s and \c t. It it is INVALID or
2175 ///not given, the operator finds the first appropriate arc.
2176 ///\return An arc from \c s to \c t after \c p or
2177 ///\ref INVALID if there is no more.
2179 ///For example, you can count the number of arcs from \c u to \c v in the
2182 ///DynArcLookUp<ListDigraph> ae(g);
2185 ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
2188 ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
2189 ///amortized time, specifically, the time complexity of the lookups
2190 ///is equal to the optimal search tree implementation for the
2191 ///current query distribution in a constant factor.
2193 ///\note This is a dynamic data structure, therefore the data
2194 ///structure is updated after each graph alteration. Thus although
2195 ///this data structure is theoretically faster than \ref ArcLookUp
2196 ///and \ref AllArcLookUp, it often provides worse performance than
2198 Arc operator()(Node s, Node t, Arc p = INVALID) const {
2201 if (a == INVALID) return INVALID;
2204 if (_g.target(a) < t) {
2205 if (_right[a] == INVALID) {
2206 const_cast<DynArcLookUp&>(*this).splay(a);
2212 if (_g.target(a) == t) {
2215 if (_left[a] == INVALID) {
2216 const_cast<DynArcLookUp&>(*this).splay(a);
2225 if (_right[a] != INVALID) {
2227 while (_left[a] != INVALID) {
2230 const_cast<DynArcLookUp&>(*this).splay(a);
2232 while (_parent[a] != INVALID && _right[_parent[a]] == a) {
2235 if (_parent[a] == INVALID) {
2239 const_cast<DynArcLookUp&>(*this).splay(a);
2242 if (_g.target(a) == t) return a;
2243 else return INVALID;
2249 ///Fast arc look-up between given endpoints.
2251 ///Using this class, you can find an arc in a digraph from a given
2252 ///source to a given target in time <em>O</em>(log<em>d</em>),
2253 ///where <em>d</em> is the out-degree of the source node.
2255 ///It is not possible to find \e all parallel arcs between two nodes.
2256 ///Use \ref AllArcLookUp for this purpose.
2258 ///\warning This class is static, so you should call refresh() (or at
2259 ///least refresh(Node)) to refresh this data structure whenever the
2260 ///digraph changes. This is a time consuming (superlinearly proportional
2261 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
2263 ///\tparam GR The type of the underlying digraph.
2270 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
2274 /// The Digraph type
2279 typename Digraph::template NodeMap<Arc> _head;
2280 typename Digraph::template ArcMap<Arc> _left;
2281 typename Digraph::template ArcMap<Arc> _right;
2286 ArcLess(const Digraph &_g) : g(_g) {}
2287 bool operator()(Arc a,Arc b) const
2289 return g.target(a)<g.target(b);
2299 ///It builds up the search database, which remains valid until the digraph
2301 ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
2304 Arc refreshRec(std::vector<Arc> &v,int a,int b)
2308 _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
2309 _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
2313 ///Refresh the search data structure at a node.
2315 ///Build up the search database of node \c n.
2317 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
2318 ///is the number of the outgoing arcs of \c n.
2319 void refresh(Node n)
2322 for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
2324 std::sort(v.begin(),v.end(),ArcLess(_g));
2325 _head[n]=refreshRec(v,0,v.size()-1);
2327 else _head[n]=INVALID;
2329 ///Refresh the full data structure.
2331 ///Build up the full search database. In fact, it simply calls
2332 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2334 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
2335 ///the number of the arcs in the digraph and <em>D</em> is the maximum
2336 ///out-degree of the digraph.
2339 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
2342 ///Find an arc between two nodes.
2344 ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
2345 ///where <em>d</em> is the number of outgoing arcs of \c s.
2346 ///\param s The source node.
2347 ///\param t The target node.
2348 ///\return An arc from \c s to \c t if there exists,
2349 ///\ref INVALID otherwise.
2351 ///\warning If you change the digraph, refresh() must be called before using
2352 ///this operator. If you change the outgoing arcs of
2353 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
2354 Arc operator()(Node s, Node t) const
2358 e!=INVALID&&_g.target(e)!=t;
2359 e = t < _g.target(e)?_left[e]:_right[e]) ;
2365 ///Fast look-up of all arcs between given endpoints.
2367 ///This class is the same as \ref ArcLookUp, with the addition
2368 ///that it makes it possible to find all parallel arcs between given
2371 ///\warning This class is static, so you should call refresh() (or at
2372 ///least refresh(Node)) to refresh this data structure whenever the
2373 ///digraph changes. This is a time consuming (superlinearly proportional
2374 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
2376 ///\tparam GR The type of the underlying digraph.
2381 class AllArcLookUp : public ArcLookUp<GR>
2383 using ArcLookUp<GR>::_g;
2384 using ArcLookUp<GR>::_right;
2385 using ArcLookUp<GR>::_left;
2386 using ArcLookUp<GR>::_head;
2388 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
2390 typename GR::template ArcMap<Arc> _next;
2392 Arc refreshNext(Arc head,Arc next=INVALID)
2394 if(head==INVALID) return next;
2396 next=refreshNext(_right[head],next);
2397 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
2399 return refreshNext(_left[head],head);
2405 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
2410 /// The Digraph type
2417 ///It builds up the search database, which remains valid until the digraph
2419 AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
2421 ///Refresh the data structure at a node.
2423 ///Build up the search database of node \c n.
2425 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
2426 ///the number of the outgoing arcs of \c n.
2427 void refresh(Node n)
2429 ArcLookUp<GR>::refresh(n);
2430 refreshNext(_head[n]);
2433 ///Refresh the full data structure.
2435 ///Build up the full search database. In fact, it simply calls
2436 ///\ref refresh(Node) "refresh(n)" for each node \c n.
2438 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
2439 ///the number of the arcs in the digraph and <em>D</em> is the maximum
2440 ///out-degree of the digraph.
2443 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
2446 ///Find an arc between two nodes.
2448 ///Find an arc between two nodes.
2449 ///\param s The source node.
2450 ///\param t The target node.
2451 ///\param prev The previous arc between \c s and \c t. It it is INVALID or
2452 ///not given, the operator finds the first appropriate arc.
2453 ///\return An arc from \c s to \c t after \c prev or
2454 ///\ref INVALID if there is no more.
2456 ///For example, you can count the number of arcs from \c u to \c v in the
2459 ///AllArcLookUp<ListDigraph> ae(g);
2462 ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
2465 ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
2466 ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
2467 ///consecutive arcs are found in constant time.
2469 ///\warning If you change the digraph, refresh() must be called before using
2470 ///this operator. If you change the outgoing arcs of
2471 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
2473 Arc operator()(Node s, Node t, Arc prev=INVALID) const
2480 e!=INVALID&&_g.target(e)!=t;
2481 e = t < _g.target(e)?_left[e]:_right[e]) ;
2491 else return _next[prev];