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 #define GCC_VERSION (__GNUC__ * 10000 \
42 + __GNUC_MINOR__ * 100 \
43 + __GNUC_PATCHLEVEL__)
46 #if GCC_VERSION >= 40800
47 // Needed by the [DI]GRAPH_TYPEDEFS marcos for gcc 4.8
48 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
52 ///\brief LEMON core utilities.
54 ///This header file contains core utilities for LEMON.
55 ///It is automatically included by all graph types, therefore it usually
56 ///do not have to be included directly.
60 /// \brief Dummy type to make it easier to create invalid iterators.
62 /// Dummy type to make it easier to create invalid iterators.
63 /// See \ref INVALID for the usage.
66 bool operator==(Invalid) { return true; }
67 bool operator!=(Invalid) { return false; }
68 bool operator< (Invalid) { return false; }
71 /// \brief Invalid iterators.
73 /// \ref Invalid is a global type that converts to each iterator
74 /// in such a way that the value of the target iterator will be invalid.
75 #ifdef LEMON_ONLY_TEMPLATES
76 const Invalid INVALID = Invalid();
78 extern const Invalid INVALID;
81 /// \addtogroup gutils
84 ///Create convenience typedefs for the digraph types and iterators
86 ///This \c \#define creates convenient type definitions for the following
87 ///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
88 ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
89 ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
91 ///\note If the graph type is a dependent type, ie. the graph type depend
92 ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
94 #define DIGRAPH_TYPEDEFS(Digraph) \
95 typedef Digraph::Node Node; \
96 typedef Digraph::NodeIt NodeIt; \
97 typedef Digraph::Arc Arc; \
98 typedef Digraph::ArcIt ArcIt; \
99 typedef Digraph::InArcIt InArcIt; \
100 typedef Digraph::OutArcIt OutArcIt; \
101 typedef Digraph::NodeMap<bool> BoolNodeMap; \
102 typedef Digraph::NodeMap<int> IntNodeMap; \
103 typedef Digraph::NodeMap<double> DoubleNodeMap; \
104 typedef Digraph::ArcMap<bool> BoolArcMap; \
105 typedef Digraph::ArcMap<int> IntArcMap; \
106 typedef Digraph::ArcMap<double> DoubleArcMap
108 ///Create convenience typedefs for the digraph types and iterators
110 ///\see DIGRAPH_TYPEDEFS
112 ///\note Use this macro, if the graph type is a dependent type,
113 ///ie. the graph type depend on a template parameter.
114 #define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \
115 typedef typename Digraph::Node Node; \
116 typedef typename Digraph::NodeIt NodeIt; \
117 typedef typename Digraph::Arc Arc; \
118 typedef typename Digraph::ArcIt ArcIt; \
119 typedef typename Digraph::InArcIt InArcIt; \
120 typedef typename Digraph::OutArcIt OutArcIt; \
121 typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \
122 typedef typename Digraph::template NodeMap<int> IntNodeMap; \
123 typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \
124 typedef typename Digraph::template ArcMap<bool> BoolArcMap; \
125 typedef typename Digraph::template ArcMap<int> IntArcMap; \
126 typedef typename Digraph::template ArcMap<double> DoubleArcMap
128 ///Create convenience typedefs for the graph types and iterators
130 ///This \c \#define creates the same convenient type definitions as defined
131 ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
132 ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
135 ///\note If the graph type is a dependent type, ie. the graph type depend
136 ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
138 #define GRAPH_TYPEDEFS(Graph) \
139 DIGRAPH_TYPEDEFS(Graph); \
140 typedef Graph::Edge Edge; \
141 typedef Graph::EdgeIt EdgeIt; \
142 typedef Graph::IncEdgeIt IncEdgeIt; \
143 typedef Graph::EdgeMap<bool> BoolEdgeMap; \
144 typedef Graph::EdgeMap<int> IntEdgeMap; \
145 typedef Graph::EdgeMap<double> DoubleEdgeMap
147 ///Create convenience typedefs for the graph types and iterators
149 ///\see GRAPH_TYPEDEFS
151 ///\note Use this macro, if the graph type is a dependent type,
152 ///ie. the graph type depend on a template parameter.
153 #define TEMPLATE_GRAPH_TYPEDEFS(Graph) \
154 TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \
155 typedef typename Graph::Edge Edge; \
156 typedef typename Graph::EdgeIt EdgeIt; \
157 typedef typename Graph::IncEdgeIt IncEdgeIt; \
158 typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \
159 typedef typename Graph::template EdgeMap<int> IntEdgeMap; \
160 typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
162 /// \brief Function to count the items in a graph.
164 /// This function counts the items (nodes, arcs etc.) in a graph.
165 /// The complexity of the function is linear because
166 /// it iterates on all of the items.
167 template <typename Graph, typename Item>
168 inline int countItems(const Graph& g) {
169 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
171 for (ItemIt it(g); it != INVALID; ++it) {
179 namespace _core_bits {
181 template <typename Graph, typename Enable = void>
182 struct CountNodesSelector {
183 static int count(const Graph &g) {
184 return countItems<Graph, typename Graph::Node>(g);
188 template <typename Graph>
189 struct CountNodesSelector<
191 enable_if<typename Graph::NodeNumTag, void>::type>
193 static int count(const Graph &g) {
199 /// \brief Function to count the nodes in the graph.
201 /// This function counts the nodes in the graph.
202 /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
203 /// graph structures it is specialized to run in <em>O</em>(1).
205 /// \note If the graph contains a \c nodeNum() member function and a
206 /// \c NodeNumTag tag then this function calls directly the member
207 /// function to query the cardinality of the node set.
208 template <typename Graph>
209 inline int countNodes(const Graph& g) {
210 return _core_bits::CountNodesSelector<Graph>::count(g);
215 namespace _core_bits {
217 template <typename Graph, typename Enable = void>
218 struct CountArcsSelector {
219 static int count(const Graph &g) {
220 return countItems<Graph, typename Graph::Arc>(g);
224 template <typename Graph>
225 struct CountArcsSelector<
227 typename enable_if<typename Graph::ArcNumTag, void>::type>
229 static int count(const Graph &g) {
235 /// \brief Function to count the arcs in the graph.
237 /// This function counts the arcs in the graph.
238 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
239 /// graph structures it is specialized to run in <em>O</em>(1).
241 /// \note If the graph contains a \c arcNum() member function and a
242 /// \c ArcNumTag tag then this function calls directly the member
243 /// function to query the cardinality of the arc set.
244 template <typename Graph>
245 inline int countArcs(const Graph& g) {
246 return _core_bits::CountArcsSelector<Graph>::count(g);
251 namespace _core_bits {
253 template <typename Graph, typename Enable = void>
254 struct CountEdgesSelector {
255 static int count(const Graph &g) {
256 return countItems<Graph, typename Graph::Edge>(g);
260 template <typename Graph>
261 struct CountEdgesSelector<
263 typename enable_if<typename Graph::EdgeNumTag, void>::type>
265 static int count(const Graph &g) {
271 /// \brief Function to count the edges in the graph.
273 /// This function counts the edges in the graph.
274 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
275 /// graph structures it is specialized to run in <em>O</em>(1).
277 /// \note If the graph contains a \c edgeNum() member function and a
278 /// \c EdgeNumTag tag then this function calls directly the member
279 /// function to query the cardinality of the edge set.
280 template <typename Graph>
281 inline int countEdges(const Graph& g) {
282 return _core_bits::CountEdgesSelector<Graph>::count(g);
287 template <typename Graph, typename DegIt>
288 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
290 for (DegIt it(_g, _n); it != INVALID; ++it) {
296 /// \brief Function to count the number of the out-arcs from node \c n.
298 /// This function counts the number of the out-arcs from node \c n
299 /// in the graph \c g.
300 template <typename Graph>
301 inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
302 return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
305 /// \brief Function to count the number of the in-arcs to node \c n.
307 /// This function counts the number of the in-arcs to node \c n
308 /// in the graph \c g.
309 template <typename Graph>
310 inline int countInArcs(const Graph& g, const typename Graph::Node& n) {
311 return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
314 /// \brief Function to count the number of the inc-edges to node \c n.
316 /// This function counts the number of the inc-edges to node \c n
317 /// in the undirected graph \c g.
318 template <typename Graph>
319 inline int countIncEdges(const Graph& g, const typename Graph::Node& n) {
320 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
323 namespace _core_bits {
325 template <typename Digraph, typename Item, typename RefMap>
328 virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
330 virtual ~MapCopyBase() {}
333 template <typename Digraph, typename Item, typename RefMap,
334 typename FromMap, typename ToMap>
335 class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
338 MapCopy(const FromMap& map, ToMap& tmap)
339 : _map(map), _tmap(tmap) {}
341 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
342 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
343 for (ItemIt it(digraph); it != INVALID; ++it) {
344 _tmap.set(refMap[it], _map[it]);
353 template <typename Digraph, typename Item, typename RefMap, typename It>
354 class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
357 ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
359 virtual void copy(const Digraph&, const RefMap& refMap) {
368 template <typename Digraph, typename Item, typename RefMap, typename Ref>
369 class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
372 RefCopy(Ref& map) : _map(map) {}
374 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
375 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
376 for (ItemIt it(digraph); it != INVALID; ++it) {
377 _map.set(it, refMap[it]);
385 template <typename Digraph, typename Item, typename RefMap,
387 class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
390 CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
392 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
393 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
394 for (ItemIt it(digraph); it != INVALID; ++it) {
395 _cmap.set(refMap[it], it);
403 template <typename Digraph, typename Enable = void>
404 struct DigraphCopySelector {
405 template <typename From, typename NodeRefMap, typename ArcRefMap>
406 static void copy(const From& from, Digraph &to,
407 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
409 for (typename From::NodeIt it(from); it != INVALID; ++it) {
410 nodeRefMap[it] = to.addNode();
412 for (typename From::ArcIt it(from); it != INVALID; ++it) {
413 arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
414 nodeRefMap[from.target(it)]);
419 template <typename Digraph>
420 struct DigraphCopySelector<
422 typename enable_if<typename Digraph::BuildTag, void>::type>
424 template <typename From, typename NodeRefMap, typename ArcRefMap>
425 static void copy(const From& from, Digraph &to,
426 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
427 to.build(from, nodeRefMap, arcRefMap);
431 template <typename Graph, typename Enable = void>
432 struct GraphCopySelector {
433 template <typename From, typename NodeRefMap, typename EdgeRefMap>
434 static void copy(const From& from, Graph &to,
435 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
437 for (typename From::NodeIt it(from); it != INVALID; ++it) {
438 nodeRefMap[it] = to.addNode();
440 for (typename From::EdgeIt it(from); it != INVALID; ++it) {
441 edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
442 nodeRefMap[from.v(it)]);
447 template <typename Graph>
448 struct GraphCopySelector<
450 typename enable_if<typename Graph::BuildTag, void>::type>
452 template <typename From, typename NodeRefMap, typename EdgeRefMap>
453 static void copy(const From& from, Graph &to,
454 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
455 to.build(from, nodeRefMap, edgeRefMap);
461 /// \brief Class to copy a digraph.
463 /// Class to copy a digraph to another digraph (duplicate a digraph). The
464 /// simplest way of using it is through the \c digraphCopy() function.
466 /// This class not only make a copy of a digraph, but it can create
467 /// references and cross references between the nodes and arcs of
468 /// the two digraphs, and it can copy maps to use with the newly created
471 /// To make a copy from a digraph, first an instance of DigraphCopy
472 /// should be created, then the data belongs to the digraph should
473 /// assigned to copy. In the end, the \c run() member should be
476 /// The next code copies a digraph with several data:
478 /// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
479 /// // Create references for the nodes
480 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
482 /// // Create cross references (inverse) for the arcs
483 /// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
484 /// cg.arcCrossRef(acr);
485 /// // Copy an arc map
486 /// OrigGraph::ArcMap<double> oamap(orig_graph);
487 /// NewGraph::ArcMap<double> namap(new_graph);
488 /// cg.arcMap(oamap, namap);
490 /// OrigGraph::Node on;
491 /// NewGraph::Node nn;
493 /// // Execute copying
496 template <typename From, typename To>
500 typedef typename From::Node Node;
501 typedef typename From::NodeIt NodeIt;
502 typedef typename From::Arc Arc;
503 typedef typename From::ArcIt ArcIt;
505 typedef typename To::Node TNode;
506 typedef typename To::Arc TArc;
508 typedef typename From::template NodeMap<TNode> NodeRefMap;
509 typedef typename From::template ArcMap<TArc> ArcRefMap;
513 /// \brief Constructor of DigraphCopy.
515 /// Constructor of DigraphCopy for copying the content of the
516 /// \c from digraph into the \c to digraph.
517 DigraphCopy(const From& from, To& to)
518 : _from(from), _to(to) {}
520 /// \brief Destructor of DigraphCopy
522 /// Destructor of DigraphCopy.
524 for (int i = 0; i < int(_node_maps.size()); ++i) {
525 delete _node_maps[i];
527 for (int i = 0; i < int(_arc_maps.size()); ++i) {
533 /// \brief Copy the node references into the given map.
535 /// This function copies the node references into the given map.
536 /// The parameter should be a map, whose key type is the Node type of
537 /// the source digraph, while the value type is the Node type of the
538 /// destination digraph.
539 template <typename NodeRef>
540 DigraphCopy& nodeRef(NodeRef& map) {
541 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
542 NodeRefMap, NodeRef>(map));
546 /// \brief Copy the node cross references into the given map.
548 /// This function copies the node cross references (reverse references)
549 /// into the given map. The parameter should be a map, whose key type
550 /// is the Node type of the destination digraph, while the value type is
551 /// the Node type of the source digraph.
552 template <typename NodeCrossRef>
553 DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
554 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
555 NodeRefMap, NodeCrossRef>(map));
559 /// \brief Make a copy of the given node map.
561 /// This function makes a copy of the given node map for the newly
563 /// The key type of the new map \c tmap should be the Node type of the
564 /// destination digraph, and the key type of the original map \c map
565 /// should be the Node type of the source digraph.
566 template <typename FromMap, typename ToMap>
567 DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
568 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
569 NodeRefMap, FromMap, ToMap>(map, tmap));
573 /// \brief Make a copy of the given node.
575 /// This function makes a copy of the given node.
576 DigraphCopy& node(const Node& node, TNode& tnode) {
577 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
578 NodeRefMap, TNode>(node, tnode));
582 /// \brief Copy the arc references into the given map.
584 /// This function copies the arc references into the given map.
585 /// The parameter should be a map, whose key type is the Arc type of
586 /// the source digraph, while the value type is the Arc type of the
587 /// destination digraph.
588 template <typename ArcRef>
589 DigraphCopy& arcRef(ArcRef& map) {
590 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
591 ArcRefMap, ArcRef>(map));
595 /// \brief Copy the arc cross references into the given map.
597 /// This function copies the arc cross references (reverse references)
598 /// into the given map. The parameter should be a map, whose key type
599 /// is the Arc type of the destination digraph, while the value type is
600 /// the Arc type of the source digraph.
601 template <typename ArcCrossRef>
602 DigraphCopy& arcCrossRef(ArcCrossRef& map) {
603 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
604 ArcRefMap, ArcCrossRef>(map));
608 /// \brief Make a copy of the given arc map.
610 /// This function makes a copy of the given arc map for the newly
612 /// The key type of the new map \c tmap should be the Arc type of the
613 /// destination digraph, and the key type of the original map \c map
614 /// should be the Arc type of the source digraph.
615 template <typename FromMap, typename ToMap>
616 DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
617 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
618 ArcRefMap, FromMap, ToMap>(map, tmap));
622 /// \brief Make a copy of the given arc.
624 /// This function makes a copy of the given arc.
625 DigraphCopy& arc(const Arc& arc, TArc& tarc) {
626 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
627 ArcRefMap, TArc>(arc, tarc));
631 /// \brief Execute copying.
633 /// This function executes the copying of the digraph along with the
634 /// copying of the assigned data.
636 NodeRefMap nodeRefMap(_from);
637 ArcRefMap arcRefMap(_from);
638 _core_bits::DigraphCopySelector<To>::
639 copy(_from, _to, nodeRefMap, arcRefMap);
640 for (int i = 0; i < int(_node_maps.size()); ++i) {
641 _node_maps[i]->copy(_from, nodeRefMap);
643 for (int i = 0; i < int(_arc_maps.size()); ++i) {
644 _arc_maps[i]->copy(_from, arcRefMap);
653 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
656 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
661 /// \brief Copy a digraph to another digraph.
663 /// This function copies a digraph to another digraph.
664 /// The complete usage of it is detailed in the DigraphCopy class, but
665 /// a short example shows a basic work:
667 /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
670 /// After the copy the \c nr map will contain the mapping from the
671 /// nodes of the \c from digraph to the nodes of the \c to digraph and
672 /// \c acr will contain the mapping from the arcs of the \c to digraph
673 /// to the arcs of the \c from digraph.
676 template <typename From, typename To>
677 DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
678 return DigraphCopy<From, To>(from, to);
681 /// \brief Class to copy a graph.
683 /// Class to copy a graph to another graph (duplicate a graph). The
684 /// simplest way of using it is through the \c graphCopy() function.
686 /// This class not only make a copy of a graph, but it can create
687 /// references and cross references between the nodes, edges and arcs of
688 /// the two graphs, and it can copy maps for using with the newly created
691 /// To make a copy from a graph, first an instance of GraphCopy
692 /// should be created, then the data belongs to the graph should
693 /// assigned to copy. In the end, the \c run() member should be
696 /// The next code copies a graph with several data:
698 /// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
699 /// // Create references for the nodes
700 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
702 /// // Create cross references (inverse) for the edges
703 /// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
704 /// cg.edgeCrossRef(ecr);
705 /// // Copy an edge map
706 /// OrigGraph::EdgeMap<double> oemap(orig_graph);
707 /// NewGraph::EdgeMap<double> nemap(new_graph);
708 /// cg.edgeMap(oemap, nemap);
710 /// OrigGraph::Node on;
711 /// NewGraph::Node nn;
713 /// // Execute copying
716 template <typename From, typename To>
720 typedef typename From::Node Node;
721 typedef typename From::NodeIt NodeIt;
722 typedef typename From::Arc Arc;
723 typedef typename From::ArcIt ArcIt;
724 typedef typename From::Edge Edge;
725 typedef typename From::EdgeIt EdgeIt;
727 typedef typename To::Node TNode;
728 typedef typename To::Arc TArc;
729 typedef typename To::Edge TEdge;
731 typedef typename From::template NodeMap<TNode> NodeRefMap;
732 typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
735 ArcRefMap(const From& from, const To& to,
736 const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
737 : _from(from), _to(to),
738 _edge_ref(edge_ref), _node_ref(node_ref) {}
740 typedef typename From::Arc Key;
741 typedef typename To::Arc Value;
743 Value operator[](const Key& key) const {
744 bool forward = _from.u(key) != _from.v(key) ?
745 _node_ref[_from.source(key)] ==
746 _to.source(_to.direct(_edge_ref[key], true)) :
747 _from.direction(key);
748 return _to.direct(_edge_ref[key], forward);
753 const EdgeRefMap& _edge_ref;
754 const NodeRefMap& _node_ref;
759 /// \brief Constructor of GraphCopy.
761 /// Constructor of GraphCopy for copying the content of the
762 /// \c from graph into the \c to graph.
763 GraphCopy(const From& from, To& to)
764 : _from(from), _to(to) {}
766 /// \brief Destructor of GraphCopy
768 /// Destructor of GraphCopy.
770 for (int i = 0; i < int(_node_maps.size()); ++i) {
771 delete _node_maps[i];
773 for (int i = 0; i < int(_arc_maps.size()); ++i) {
776 for (int i = 0; i < int(_edge_maps.size()); ++i) {
777 delete _edge_maps[i];
781 /// \brief Copy the node references into the given map.
783 /// This function copies the node references into the given map.
784 /// The parameter should be a map, whose key type is the Node type of
785 /// the source graph, while the value type is the Node type of the
786 /// destination graph.
787 template <typename NodeRef>
788 GraphCopy& nodeRef(NodeRef& map) {
789 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
790 NodeRefMap, NodeRef>(map));
794 /// \brief Copy the node cross references into the given map.
796 /// This function copies the node cross references (reverse references)
797 /// into the given map. The parameter should be a map, whose key type
798 /// is the Node type of the destination graph, while the value type is
799 /// the Node type of the source graph.
800 template <typename NodeCrossRef>
801 GraphCopy& nodeCrossRef(NodeCrossRef& map) {
802 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
803 NodeRefMap, NodeCrossRef>(map));
807 /// \brief Make a copy of the given node map.
809 /// This function makes a copy of the given node map for the newly
811 /// The key type of the new map \c tmap should be the Node type of the
812 /// destination graph, and the key type of the original map \c map
813 /// should be the Node type of the source graph.
814 template <typename FromMap, typename ToMap>
815 GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
816 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
817 NodeRefMap, FromMap, ToMap>(map, tmap));
821 /// \brief Make a copy of the given node.
823 /// This function makes a copy of the given node.
824 GraphCopy& node(const Node& node, TNode& tnode) {
825 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
826 NodeRefMap, TNode>(node, tnode));
830 /// \brief Copy the arc references into the given map.
832 /// This function copies the arc references into the given map.
833 /// The parameter should be a map, whose key type is the Arc type of
834 /// the source graph, while the value type is the Arc type of the
835 /// destination graph.
836 template <typename ArcRef>
837 GraphCopy& arcRef(ArcRef& map) {
838 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
839 ArcRefMap, ArcRef>(map));
843 /// \brief Copy the arc cross references into the given map.
845 /// This function copies the arc cross references (reverse references)
846 /// into the given map. The parameter should be a map, whose key type
847 /// is the Arc type of the destination graph, while the value type is
848 /// the Arc type of the source graph.
849 template <typename ArcCrossRef>
850 GraphCopy& arcCrossRef(ArcCrossRef& map) {
851 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
852 ArcRefMap, ArcCrossRef>(map));
856 /// \brief Make a copy of the given arc map.
858 /// This function makes a copy of the given arc map for the newly
860 /// The key type of the new map \c tmap should be the Arc type of the
861 /// destination graph, and the key type of the original map \c map
862 /// should be the Arc type of the source graph.
863 template <typename FromMap, typename ToMap>
864 GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
865 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
866 ArcRefMap, FromMap, ToMap>(map, tmap));
870 /// \brief Make a copy of the given arc.
872 /// This function makes a copy of the given arc.
873 GraphCopy& arc(const Arc& arc, TArc& tarc) {
874 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
875 ArcRefMap, TArc>(arc, tarc));
879 /// \brief Copy the edge references into the given map.
881 /// This function copies the edge references into the given map.
882 /// The parameter should be a map, whose key type is the Edge type of
883 /// the source graph, while the value type is the Edge type of the
884 /// destination graph.
885 template <typename EdgeRef>
886 GraphCopy& edgeRef(EdgeRef& map) {
887 _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
888 EdgeRefMap, EdgeRef>(map));
892 /// \brief Copy the edge cross references into the given map.
894 /// This function copies the edge cross references (reverse references)
895 /// into the given map. The parameter should be a map, whose key type
896 /// is the Edge type of the destination graph, while the value type is
897 /// the Edge type of the source graph.
898 template <typename EdgeCrossRef>
899 GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
900 _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
901 Edge, EdgeRefMap, EdgeCrossRef>(map));
905 /// \brief Make a copy of the given edge map.
907 /// This function makes a copy of the given edge map for the newly
909 /// The key type of the new map \c tmap should be the Edge type of the
910 /// destination graph, and the key type of the original map \c map
911 /// should be the Edge type of the source graph.
912 template <typename FromMap, typename ToMap>
913 GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
914 _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
915 EdgeRefMap, FromMap, ToMap>(map, tmap));
919 /// \brief Make a copy of the given edge.
921 /// This function makes a copy of the given edge.
922 GraphCopy& edge(const Edge& edge, TEdge& tedge) {
923 _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
924 EdgeRefMap, TEdge>(edge, tedge));
928 /// \brief Execute copying.
930 /// This function executes the copying of the graph along with the
931 /// copying of the assigned data.
933 NodeRefMap nodeRefMap(_from);
934 EdgeRefMap edgeRefMap(_from);
935 ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
936 _core_bits::GraphCopySelector<To>::
937 copy(_from, _to, nodeRefMap, edgeRefMap);
938 for (int i = 0; i < int(_node_maps.size()); ++i) {
939 _node_maps[i]->copy(_from, nodeRefMap);
941 for (int i = 0; i < int(_edge_maps.size()); ++i) {
942 _edge_maps[i]->copy(_from, edgeRefMap);
944 for (int i = 0; i < int(_arc_maps.size()); ++i) {
945 _arc_maps[i]->copy(_from, arcRefMap);
954 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
957 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
960 std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
965 /// \brief Copy a graph to another graph.
967 /// This function copies a graph to another graph.
968 /// The complete usage of it is detailed in the GraphCopy class,
969 /// but a short example shows a basic work:
971 /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
974 /// After the copy the \c nr map will contain the mapping from the
975 /// nodes of the \c from graph to the nodes of the \c to graph and
976 /// \c ecr will contain the mapping from the edges of the \c to graph
977 /// to the edges of the \c from graph.
980 template <typename From, typename To>
982 graphCopy(const From& from, To& to) {
983 return GraphCopy<From, To>(from, to);
986 namespace _core_bits {
988 template <typename Graph, typename Enable = void>
989 struct FindArcSelector {
990 typedef typename Graph::Node Node;
991 typedef typename Graph::Arc Arc;
992 static Arc find(const Graph &g, Node u, Node v, Arc e) {
998 while (e != INVALID && g.target(e) != v) {
1005 template <typename Graph>
1006 struct FindArcSelector<
1008 typename enable_if<typename Graph::FindArcTag, void>::type>
1010 typedef typename Graph::Node Node;
1011 typedef typename Graph::Arc Arc;
1012 static Arc find(const Graph &g, Node u, Node v, Arc prev) {
1013 return g.findArc(u, v, prev);
1018 /// \brief Find an arc between two nodes of a digraph.
1020 /// This function finds an arc from node \c u to node \c v in the
1023 /// If \c prev is \ref INVALID (this is the default value), then
1024 /// it finds the first arc from \c u to \c v. Otherwise it looks for
1025 /// the next arc from \c u to \c v after \c prev.
1026 /// \return The found arc or \ref INVALID if there is no such an arc.
1028 /// Thus you can iterate through each arc from \c u to \c v as it follows.
1030 /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
1035 /// \note \ref ConArcIt provides iterator interface for the same
1039 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1040 template <typename Graph>
1041 inline typename Graph::Arc
1042 findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1043 typename Graph::Arc prev = INVALID) {
1044 return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
1047 /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
1049 /// Iterator for iterating on parallel arcs connecting the same nodes. It is
1050 /// a higher level interface for the \ref findArc() function. You can
1051 /// use it the following way:
1053 /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
1059 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1060 template <typename GR>
1061 class ConArcIt : public GR::Arc {
1062 typedef typename GR::Arc Parent;
1066 typedef typename GR::Arc Arc;
1067 typedef typename GR::Node Node;
1069 /// \brief Constructor.
1071 /// Construct a new ConArcIt iterating on the arcs that
1072 /// connects nodes \c u and \c v.
1073 ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
1074 Parent::operator=(findArc(_graph, u, v));
1077 /// \brief Constructor.
1079 /// Construct a new ConArcIt that continues the iterating from arc \c a.
1080 ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
1082 /// \brief Increment operator.
1084 /// It increments the iterator and gives back the next arc.
1085 ConArcIt& operator++() {
1086 Parent::operator=(findArc(_graph, _graph.source(*this),
1087 _graph.target(*this), *this));
1094 namespace _core_bits {
1096 template <typename Graph, typename Enable = void>
1097 struct FindEdgeSelector {
1098 typedef typename Graph::Node Node;
1099 typedef typename Graph::Edge Edge;
1100 static Edge find(const Graph &g, Node u, Node v, Edge e) {
1104 g.firstInc(e, b, u);
1109 while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
1114 g.firstInc(e, b, u);
1119 while (e != INVALID && (!b || g.v(e) != v)) {
1127 template <typename Graph>
1128 struct FindEdgeSelector<
1130 typename enable_if<typename Graph::FindEdgeTag, void>::type>
1132 typedef typename Graph::Node Node;
1133 typedef typename Graph::Edge Edge;
1134 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
1135 return g.findEdge(u, v, prev);
1140 /// \brief Find an edge between two nodes of a graph.
1142 /// This function finds an edge from node \c u to node \c v in graph \c g.
1143 /// If node \c u and node \c v is equal then each loop edge
1144 /// will be enumerated once.
1146 /// If \c prev is \ref INVALID (this is the default value), then
1147 /// it finds the first edge from \c u to \c v. Otherwise it looks for
1148 /// the next edge from \c u to \c v after \c prev.
1149 /// \return The found edge or \ref INVALID if there is no such an edge.
1151 /// Thus you can iterate through each edge between \c u and \c v
1154 /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
1159 /// \note \ref ConEdgeIt provides iterator interface for the same
1163 template <typename Graph>
1164 inline typename Graph::Edge
1165 findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1166 typename Graph::Edge p = INVALID) {
1167 return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
1170 /// \brief Iterator for iterating on parallel edges connecting the same nodes.
1172 /// Iterator for iterating on parallel edges connecting the same nodes.
1173 /// It is a higher level interface for the findEdge() function. You can
1174 /// use it the following way:
1176 /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
1182 template <typename GR>
1183 class ConEdgeIt : public GR::Edge {
1184 typedef typename GR::Edge Parent;
1188 typedef typename GR::Edge Edge;
1189 typedef typename GR::Node Node;
1191 /// \brief Constructor.
1193 /// Construct a new ConEdgeIt iterating on the edges that
1194 /// connects nodes \c u and \c v.
1195 ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1196 Parent::operator=(findEdge(_graph, _u, _v));
1199 /// \brief Constructor.
1201 /// Construct a new ConEdgeIt that continues iterating from edge \c e.
1202 ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
1204 /// \brief Increment operator.
1206 /// It increments the iterator and gives back the next edge.
1207 ConEdgeIt& operator++() {
1208 Parent::operator=(findEdge(_graph, _u, _v, *this));
1217 ///Dynamic arc look-up between given endpoints.
1219 ///Using this class, you can find an arc in a digraph from a given
1220 ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
1221 ///where <em>d</em> is the out-degree of the source node.
1223 ///It is possible to find \e all parallel arcs between two nodes with
1224 ///the \c operator() member.
1226 ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
1227 ///\ref AllArcLookUp if your digraph is not changed so frequently.
1229 ///This class uses a self-adjusting binary search tree, the Splay tree
1230 ///of Sleator and Tarjan to guarantee the logarithmic amortized
1231 ///time bound for arc look-ups. This class also guarantees the
1232 ///optimal time bound in a constant factor for any distribution of
1235 ///\tparam GR The type of the underlying digraph.
1239 template <typename GR>
1241 : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
1243 typedef typename ItemSetTraits<GR, typename GR::Arc>
1244 ::ItemNotifier::ObserverBase Parent;
1246 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1250 /// The Digraph type
1255 class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type
1257 typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
1261 AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
1263 virtual void add(const Node& node) {
1265 Parent::set(node, INVALID);
1268 virtual void add(const std::vector<Node>& nodes) {
1270 for (int i = 0; i < int(nodes.size()); ++i) {
1271 Parent::set(nodes[i], INVALID);
1275 virtual void build() {
1278 typename Parent::Notifier* nf = Parent::notifier();
1279 for (nf->first(it); it != INVALID; nf->next(it)) {
1280 Parent::set(it, INVALID);
1288 ArcLess(const Digraph &_g) : g(_g) {}
1289 bool operator()(Arc a,Arc b) const
1291 return g.target(a)<g.target(b);
1299 typename Digraph::template ArcMap<Arc> _parent;
1300 typename Digraph::template ArcMap<Arc> _left;
1301 typename Digraph::template ArcMap<Arc> _right;
1309 ///It builds up the search database.
1310 DynArcLookUp(const Digraph &g)
1311 : _g(g),_head(g),_parent(g),_left(g),_right(g)
1313 Parent::attach(_g.notifier(typename Digraph::Arc()));
1319 virtual void add(const Arc& arc) {
1323 virtual void add(const std::vector<Arc>& arcs) {
1324 for (int i = 0; i < int(arcs.size()); ++i) {
1329 virtual void erase(const Arc& arc) {
1333 virtual void erase(const std::vector<Arc>& arcs) {
1334 for (int i = 0; i < int(arcs.size()); ++i) {
1339 virtual void build() {
1343 virtual void clear() {
1344 for(NodeIt n(_g);n!=INVALID;++n) {
1349 void insert(Arc arc) {
1350 Node s = _g.source(arc);
1351 Node t = _g.target(arc);
1352 _left[arc] = INVALID;
1353 _right[arc] = INVALID;
1358 _parent[arc] = INVALID;
1362 if (t < _g.target(e)) {
1363 if (_left[e] == INVALID) {
1372 if (_right[e] == INVALID) {
1384 void remove(Arc arc) {
1385 if (_left[arc] == INVALID) {
1386 if (_right[arc] != INVALID) {
1387 _parent[_right[arc]] = _parent[arc];
1389 if (_parent[arc] != INVALID) {
1390 if (_left[_parent[arc]] == arc) {
1391 _left[_parent[arc]] = _right[arc];
1393 _right[_parent[arc]] = _right[arc];
1396 _head[_g.source(arc)] = _right[arc];
1398 } else if (_right[arc] == INVALID) {
1399 _parent[_left[arc]] = _parent[arc];
1400 if (_parent[arc] != INVALID) {
1401 if (_left[_parent[arc]] == arc) {
1402 _left[_parent[arc]] = _left[arc];
1404 _right[_parent[arc]] = _left[arc];
1407 _head[_g.source(arc)] = _left[arc];
1411 if (_right[e] != INVALID) {
1413 while (_right[e] != INVALID) {
1417 _right[_parent[e]] = _left[e];
1418 if (_left[e] != INVALID) {
1419 _parent[_left[e]] = _parent[e];
1422 _left[e] = _left[arc];
1423 _parent[_left[arc]] = e;
1424 _right[e] = _right[arc];
1425 _parent[_right[arc]] = e;
1427 _parent[e] = _parent[arc];
1428 if (_parent[arc] != INVALID) {
1429 if (_left[_parent[arc]] == arc) {
1430 _left[_parent[arc]] = e;
1432 _right[_parent[arc]] = e;
1437 _right[e] = _right[arc];
1438 _parent[_right[arc]] = e;
1439 _parent[e] = _parent[arc];
1441 if (_parent[arc] != INVALID) {
1442 if (_left[_parent[arc]] == arc) {
1443 _left[_parent[arc]] = e;
1445 _right[_parent[arc]] = e;
1448 _head[_g.source(arc)] = e;
1454 Arc refreshRec(std::vector<Arc> &v,int a,int b)
1459 Arc left = refreshRec(v,a,m-1);
1463 _left[me] = INVALID;
1466 Arc right = refreshRec(v,m+1,b);
1468 _parent[right] = me;
1470 _right[me] = INVALID;
1476 for(NodeIt n(_g);n!=INVALID;++n) {
1478 for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
1480 std::sort(v.begin(),v.end(),ArcLess(_g));
1481 Arc head = refreshRec(v,0,v.size()-1);
1483 _parent[head] = INVALID;
1485 else _head[n] = INVALID;
1491 _parent[v] = _parent[w];
1493 _left[w] = _right[v];
1495 if (_parent[v] != INVALID) {
1496 if (_right[_parent[v]] == w) {
1497 _right[_parent[v]] = v;
1499 _left[_parent[v]] = v;
1502 if (_left[w] != INVALID){
1503 _parent[_left[w]] = w;
1509 _parent[v] = _parent[w];
1511 _right[w] = _left[v];
1513 if (_parent[v] != INVALID){
1514 if (_left[_parent[v]] == w) {
1515 _left[_parent[v]] = v;
1517 _right[_parent[v]] = v;
1520 if (_right[w] != INVALID){
1521 _parent[_right[w]] = w;
1526 while (_parent[v] != INVALID) {
1527 if (v == _left[_parent[v]]) {
1528 if (_parent[_parent[v]] == INVALID) {
1531 if (_parent[v] == _left[_parent[_parent[v]]]) {
1540 if (_parent[_parent[v]] == INVALID) {
1543 if (_parent[v] == _left[_parent[_parent[v]]]) {
1553 _head[_g.source(v)] = v;
1559 ///Find an arc between two nodes.
1561 ///Find an arc between two nodes.
1562 ///\param s The source node.
1563 ///\param t The target node.
1564 ///\param p The previous arc between \c s and \c t. It it is INVALID or
1565 ///not given, the operator finds the first appropriate arc.
1566 ///\return An arc from \c s to \c t after \c p or
1567 ///\ref INVALID if there is no more.
1569 ///For example, you can count the number of arcs from \c u to \c v in the
1572 ///DynArcLookUp<ListDigraph> ae(g);
1575 ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
1578 ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
1579 ///amortized time, specifically, the time complexity of the lookups
1580 ///is equal to the optimal search tree implementation for the
1581 ///current query distribution in a constant factor.
1583 ///\note This is a dynamic data structure, therefore the data
1584 ///structure is updated after each graph alteration. Thus although
1585 ///this data structure is theoretically faster than \ref ArcLookUp
1586 ///and \ref AllArcLookUp, it often provides worse performance than
1588 Arc operator()(Node s, Node t, Arc p = INVALID) const {
1591 if (a == INVALID) return INVALID;
1594 if (_g.target(a) < t) {
1595 if (_right[a] == INVALID) {
1596 const_cast<DynArcLookUp&>(*this).splay(a);
1602 if (_g.target(a) == t) {
1605 if (_left[a] == INVALID) {
1606 const_cast<DynArcLookUp&>(*this).splay(a);
1615 if (_right[a] != INVALID) {
1617 while (_left[a] != INVALID) {
1620 const_cast<DynArcLookUp&>(*this).splay(a);
1622 while (_parent[a] != INVALID && _right[_parent[a]] == a) {
1625 if (_parent[a] == INVALID) {
1629 const_cast<DynArcLookUp&>(*this).splay(a);
1632 if (_g.target(a) == t) return a;
1633 else return INVALID;
1639 ///Fast arc look-up between given endpoints.
1641 ///Using this class, you can find an arc in a digraph from a given
1642 ///source to a given target in time <em>O</em>(log<em>d</em>),
1643 ///where <em>d</em> is the out-degree of the source node.
1645 ///It is not possible to find \e all parallel arcs between two nodes.
1646 ///Use \ref AllArcLookUp for this purpose.
1648 ///\warning This class is static, so you should call refresh() (or at
1649 ///least refresh(Node)) to refresh this data structure whenever the
1650 ///digraph changes. This is a time consuming (superlinearly proportional
1651 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1653 ///\tparam GR The type of the underlying digraph.
1660 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1664 /// The Digraph type
1669 typename Digraph::template NodeMap<Arc> _head;
1670 typename Digraph::template ArcMap<Arc> _left;
1671 typename Digraph::template ArcMap<Arc> _right;
1676 ArcLess(const Digraph &_g) : g(_g) {}
1677 bool operator()(Arc a,Arc b) const
1679 return g.target(a)<g.target(b);
1689 ///It builds up the search database, which remains valid until the digraph
1691 ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1694 Arc refreshRec(std::vector<Arc> &v,int a,int b)
1698 _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
1699 _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
1703 ///Refresh the search data structure at a node.
1705 ///Build up the search database of node \c n.
1707 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
1708 ///is the number of the outgoing arcs of \c n.
1709 void refresh(Node n)
1712 for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1714 std::sort(v.begin(),v.end(),ArcLess(_g));
1715 _head[n]=refreshRec(v,0,v.size()-1);
1717 else _head[n]=INVALID;
1719 ///Refresh the full data structure.
1721 ///Build up the full search database. In fact, it simply calls
1722 ///\ref refresh(Node) "refresh(n)" for each node \c n.
1724 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1725 ///the number of the arcs in the digraph and <em>D</em> is the maximum
1726 ///out-degree of the digraph.
1729 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1732 ///Find an arc between two nodes.
1734 ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
1735 ///where <em>d</em> is the number of outgoing arcs of \c s.
1736 ///\param s The source node.
1737 ///\param t The target node.
1738 ///\return An arc from \c s to \c t if there exists,
1739 ///\ref INVALID otherwise.
1741 ///\warning If you change the digraph, refresh() must be called before using
1742 ///this operator. If you change the outgoing arcs of
1743 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1744 Arc operator()(Node s, Node t) const
1748 e!=INVALID&&_g.target(e)!=t;
1749 e = t < _g.target(e)?_left[e]:_right[e]) ;
1755 ///Fast look-up of all arcs between given endpoints.
1757 ///This class is the same as \ref ArcLookUp, with the addition
1758 ///that it makes it possible to find all parallel arcs between given
1761 ///\warning This class is static, so you should call refresh() (or at
1762 ///least refresh(Node)) to refresh this data structure whenever the
1763 ///digraph changes. This is a time consuming (superlinearly proportional
1764 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1766 ///\tparam GR The type of the underlying digraph.
1771 class AllArcLookUp : public ArcLookUp<GR>
1773 using ArcLookUp<GR>::_g;
1774 using ArcLookUp<GR>::_right;
1775 using ArcLookUp<GR>::_left;
1776 using ArcLookUp<GR>::_head;
1778 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1780 typename GR::template ArcMap<Arc> _next;
1782 Arc refreshNext(Arc head,Arc next=INVALID)
1784 if(head==INVALID) return next;
1786 next=refreshNext(_right[head],next);
1787 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1789 return refreshNext(_left[head],head);
1795 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
1800 /// The Digraph type
1807 ///It builds up the search database, which remains valid until the digraph
1809 AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
1811 ///Refresh the data structure at a node.
1813 ///Build up the search database of node \c n.
1815 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
1816 ///the number of the outgoing arcs of \c n.
1817 void refresh(Node n)
1819 ArcLookUp<GR>::refresh(n);
1820 refreshNext(_head[n]);
1823 ///Refresh the full data structure.
1825 ///Build up the full search database. In fact, it simply calls
1826 ///\ref refresh(Node) "refresh(n)" for each node \c n.
1828 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1829 ///the number of the arcs in the digraph and <em>D</em> is the maximum
1830 ///out-degree of the digraph.
1833 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
1836 ///Find an arc between two nodes.
1838 ///Find an arc between two nodes.
1839 ///\param s The source node.
1840 ///\param t The target node.
1841 ///\param prev The previous arc between \c s and \c t. It it is INVALID or
1842 ///not given, the operator finds the first appropriate arc.
1843 ///\return An arc from \c s to \c t after \c prev or
1844 ///\ref INVALID if there is no more.
1846 ///For example, you can count the number of arcs from \c u to \c v in the
1849 ///AllArcLookUp<ListDigraph> ae(g);
1852 ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
1855 ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
1856 ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
1857 ///consecutive arcs are found in constant time.
1859 ///\warning If you change the digraph, refresh() must be called before using
1860 ///this operator. If you change the outgoing arcs of
1861 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1863 Arc operator()(Node s, Node t, Arc prev=INVALID) const
1870 e!=INVALID&&_g.target(e)!=t;
1871 e = t < _g.target(e)?_left[e]:_right[e]) ;
1881 else return _next[prev];