1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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 /// \brief Function to count the items in a graph.
158 /// This function counts the items (nodes, arcs etc.) in a graph.
159 /// The complexity of the function is linear because
160 /// it iterates on all of the items.
161 template <typename Graph, typename Item>
162 inline int countItems(const Graph& g) {
163 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
165 for (ItemIt it(g); it != INVALID; ++it) {
173 namespace _core_bits {
175 template <typename Graph, typename Enable = void>
176 struct CountNodesSelector {
177 static int count(const Graph &g) {
178 return countItems<Graph, typename Graph::Node>(g);
182 template <typename Graph>
183 struct CountNodesSelector<
185 enable_if<typename Graph::NodeNumTag, void>::type>
187 static int count(const Graph &g) {
193 /// \brief Function to count the nodes in the graph.
195 /// This function counts the nodes in the graph.
196 /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
197 /// graph structures it is specialized to run in <em>O</em>(1).
199 /// \note If the graph contains a \c nodeNum() member function and a
200 /// \c NodeNumTag tag then this function calls directly the member
201 /// function to query the cardinality of the node set.
202 template <typename Graph>
203 inline int countNodes(const Graph& g) {
204 return _core_bits::CountNodesSelector<Graph>::count(g);
209 namespace _core_bits {
211 template <typename Graph, typename Enable = void>
212 struct CountArcsSelector {
213 static int count(const Graph &g) {
214 return countItems<Graph, typename Graph::Arc>(g);
218 template <typename Graph>
219 struct CountArcsSelector<
221 typename enable_if<typename Graph::ArcNumTag, void>::type>
223 static int count(const Graph &g) {
229 /// \brief Function to count the arcs in the graph.
231 /// This function counts the arcs in the graph.
232 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
233 /// graph structures it is specialized to run in <em>O</em>(1).
235 /// \note If the graph contains a \c arcNum() member function and a
236 /// \c ArcNumTag tag then this function calls directly the member
237 /// function to query the cardinality of the arc set.
238 template <typename Graph>
239 inline int countArcs(const Graph& g) {
240 return _core_bits::CountArcsSelector<Graph>::count(g);
245 namespace _core_bits {
247 template <typename Graph, typename Enable = void>
248 struct CountEdgesSelector {
249 static int count(const Graph &g) {
250 return countItems<Graph, typename Graph::Edge>(g);
254 template <typename Graph>
255 struct CountEdgesSelector<
257 typename enable_if<typename Graph::EdgeNumTag, void>::type>
259 static int count(const Graph &g) {
265 /// \brief Function to count the edges in the graph.
267 /// This function counts the edges in the graph.
268 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
269 /// graph structures it is specialized to run in <em>O</em>(1).
271 /// \note If the graph contains a \c edgeNum() member function and a
272 /// \c EdgeNumTag tag then this function calls directly the member
273 /// function to query the cardinality of the edge set.
274 template <typename Graph>
275 inline int countEdges(const Graph& g) {
276 return _core_bits::CountEdgesSelector<Graph>::count(g);
281 template <typename Graph, typename DegIt>
282 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
284 for (DegIt it(_g, _n); it != INVALID; ++it) {
290 /// \brief Function to count the number of the out-arcs from node \c n.
292 /// This function counts the number of the out-arcs from node \c n
293 /// in the graph \c g.
294 template <typename Graph>
295 inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
296 return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
299 /// \brief Function to count the number of the in-arcs to node \c n.
301 /// This function counts the number of the in-arcs to node \c n
302 /// in the graph \c g.
303 template <typename Graph>
304 inline int countInArcs(const Graph& g, const typename Graph::Node& n) {
305 return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
308 /// \brief Function to count the number of the inc-edges to node \c n.
310 /// This function counts the number of the inc-edges to node \c n
311 /// in the undirected graph \c g.
312 template <typename Graph>
313 inline int countIncEdges(const Graph& g, const typename Graph::Node& n) {
314 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
317 namespace _core_bits {
319 template <typename Digraph, typename Item, typename RefMap>
322 virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
324 virtual ~MapCopyBase() {}
327 template <typename Digraph, typename Item, typename RefMap,
328 typename FromMap, typename ToMap>
329 class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
332 MapCopy(const FromMap& map, ToMap& tmap)
333 : _map(map), _tmap(tmap) {}
335 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
336 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
337 for (ItemIt it(digraph); it != INVALID; ++it) {
338 _tmap.set(refMap[it], _map[it]);
347 template <typename Digraph, typename Item, typename RefMap, typename It>
348 class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
351 ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
353 virtual void copy(const Digraph&, const RefMap& refMap) {
362 template <typename Digraph, typename Item, typename RefMap, typename Ref>
363 class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
366 RefCopy(Ref& map) : _map(map) {}
368 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
369 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
370 for (ItemIt it(digraph); it != INVALID; ++it) {
371 _map.set(it, refMap[it]);
379 template <typename Digraph, typename Item, typename RefMap,
381 class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
384 CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
386 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
387 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
388 for (ItemIt it(digraph); it != INVALID; ++it) {
389 _cmap.set(refMap[it], it);
397 template <typename Digraph, typename Enable = void>
398 struct DigraphCopySelector {
399 template <typename From, typename NodeRefMap, typename ArcRefMap>
400 static void copy(const From& from, Digraph &to,
401 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
403 for (typename From::NodeIt it(from); it != INVALID; ++it) {
404 nodeRefMap[it] = to.addNode();
406 for (typename From::ArcIt it(from); it != INVALID; ++it) {
407 arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
408 nodeRefMap[from.target(it)]);
413 template <typename Digraph>
414 struct DigraphCopySelector<
416 typename enable_if<typename Digraph::BuildTag, void>::type>
418 template <typename From, typename NodeRefMap, typename ArcRefMap>
419 static void copy(const From& from, Digraph &to,
420 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
421 to.build(from, nodeRefMap, arcRefMap);
425 template <typename Graph, typename Enable = void>
426 struct GraphCopySelector {
427 template <typename From, typename NodeRefMap, typename EdgeRefMap>
428 static void copy(const From& from, Graph &to,
429 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
431 for (typename From::NodeIt it(from); it != INVALID; ++it) {
432 nodeRefMap[it] = to.addNode();
434 for (typename From::EdgeIt it(from); it != INVALID; ++it) {
435 edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
436 nodeRefMap[from.v(it)]);
441 template <typename Graph>
442 struct GraphCopySelector<
444 typename enable_if<typename Graph::BuildTag, void>::type>
446 template <typename From, typename NodeRefMap, typename EdgeRefMap>
447 static void copy(const From& from, Graph &to,
448 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
449 to.build(from, nodeRefMap, edgeRefMap);
455 /// \brief Class to copy a digraph.
457 /// Class to copy a digraph to another digraph (duplicate a digraph). The
458 /// simplest way of using it is through the \c digraphCopy() function.
460 /// This class not only make a copy of a digraph, but it can create
461 /// references and cross references between the nodes and arcs of
462 /// the two digraphs, and it can copy maps to use with the newly created
465 /// To make a copy from a digraph, first an instance of DigraphCopy
466 /// should be created, then the data belongs to the digraph should
467 /// assigned to copy. In the end, the \c run() member should be
470 /// The next code copies a digraph with several data:
472 /// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
473 /// // Create references for the nodes
474 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
476 /// // Create cross references (inverse) for the arcs
477 /// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
478 /// cg.arcCrossRef(acr);
479 /// // Copy an arc map
480 /// OrigGraph::ArcMap<double> oamap(orig_graph);
481 /// NewGraph::ArcMap<double> namap(new_graph);
482 /// cg.arcMap(oamap, namap);
484 /// OrigGraph::Node on;
485 /// NewGraph::Node nn;
487 /// // Execute copying
490 template <typename From, typename To>
494 typedef typename From::Node Node;
495 typedef typename From::NodeIt NodeIt;
496 typedef typename From::Arc Arc;
497 typedef typename From::ArcIt ArcIt;
499 typedef typename To::Node TNode;
500 typedef typename To::Arc TArc;
502 typedef typename From::template NodeMap<TNode> NodeRefMap;
503 typedef typename From::template ArcMap<TArc> ArcRefMap;
507 /// \brief Constructor of DigraphCopy.
509 /// Constructor of DigraphCopy for copying the content of the
510 /// \c from digraph into the \c to digraph.
511 DigraphCopy(const From& from, To& to)
512 : _from(from), _to(to) {}
514 /// \brief Destructor of DigraphCopy
516 /// Destructor of DigraphCopy.
518 for (int i = 0; i < int(_node_maps.size()); ++i) {
519 delete _node_maps[i];
521 for (int i = 0; i < int(_arc_maps.size()); ++i) {
527 /// \brief Copy the node references into the given map.
529 /// This function copies the node references into the given map.
530 /// The parameter should be a map, whose key type is the Node type of
531 /// the source digraph, while the value type is the Node type of the
532 /// destination digraph.
533 template <typename NodeRef>
534 DigraphCopy& nodeRef(NodeRef& map) {
535 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
536 NodeRefMap, NodeRef>(map));
540 /// \brief Copy the node cross references into the given map.
542 /// This function copies the node cross references (reverse references)
543 /// into the given map. The parameter should be a map, whose key type
544 /// is the Node type of the destination digraph, while the value type is
545 /// the Node type of the source digraph.
546 template <typename NodeCrossRef>
547 DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
548 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
549 NodeRefMap, NodeCrossRef>(map));
553 /// \brief Make a copy of the given node map.
555 /// This function makes a copy of the given node map for the newly
557 /// The key type of the new map \c tmap should be the Node type of the
558 /// destination digraph, and the key type of the original map \c map
559 /// should be the Node type of the source digraph.
560 template <typename FromMap, typename ToMap>
561 DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
562 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
563 NodeRefMap, FromMap, ToMap>(map, tmap));
567 /// \brief Make a copy of the given node.
569 /// This function makes a copy of the given node.
570 DigraphCopy& node(const Node& node, TNode& tnode) {
571 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
572 NodeRefMap, TNode>(node, tnode));
576 /// \brief Copy the arc references into the given map.
578 /// This function copies the arc references into the given map.
579 /// The parameter should be a map, whose key type is the Arc type of
580 /// the source digraph, while the value type is the Arc type of the
581 /// destination digraph.
582 template <typename ArcRef>
583 DigraphCopy& arcRef(ArcRef& map) {
584 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
585 ArcRefMap, ArcRef>(map));
589 /// \brief Copy the arc cross references into the given map.
591 /// This function copies the arc cross references (reverse references)
592 /// into the given map. The parameter should be a map, whose key type
593 /// is the Arc type of the destination digraph, while the value type is
594 /// the Arc type of the source digraph.
595 template <typename ArcCrossRef>
596 DigraphCopy& arcCrossRef(ArcCrossRef& map) {
597 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
598 ArcRefMap, ArcCrossRef>(map));
602 /// \brief Make a copy of the given arc map.
604 /// This function makes a copy of the given arc map for the newly
606 /// The key type of the new map \c tmap should be the Arc type of the
607 /// destination digraph, and the key type of the original map \c map
608 /// should be the Arc type of the source digraph.
609 template <typename FromMap, typename ToMap>
610 DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
611 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
612 ArcRefMap, FromMap, ToMap>(map, tmap));
616 /// \brief Make a copy of the given arc.
618 /// This function makes a copy of the given arc.
619 DigraphCopy& arc(const Arc& arc, TArc& tarc) {
620 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
621 ArcRefMap, TArc>(arc, tarc));
625 /// \brief Execute copying.
627 /// This function executes the copying of the digraph along with the
628 /// copying of the assigned data.
630 NodeRefMap nodeRefMap(_from);
631 ArcRefMap arcRefMap(_from);
632 _core_bits::DigraphCopySelector<To>::
633 copy(_from, _to, nodeRefMap, arcRefMap);
634 for (int i = 0; i < int(_node_maps.size()); ++i) {
635 _node_maps[i]->copy(_from, nodeRefMap);
637 for (int i = 0; i < int(_arc_maps.size()); ++i) {
638 _arc_maps[i]->copy(_from, arcRefMap);
647 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
650 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
655 /// \brief Copy a digraph to another digraph.
657 /// This function copies a digraph to another digraph.
658 /// The complete usage of it is detailed in the DigraphCopy class, but
659 /// a short example shows a basic work:
661 /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
664 /// After the copy the \c nr map will contain the mapping from the
665 /// nodes of the \c from digraph to the nodes of the \c to digraph and
666 /// \c acr will contain the mapping from the arcs of the \c to digraph
667 /// to the arcs of the \c from digraph.
670 template <typename From, typename To>
671 DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
672 return DigraphCopy<From, To>(from, to);
675 /// \brief Class to copy a graph.
677 /// Class to copy a graph to another graph (duplicate a graph). The
678 /// simplest way of using it is through the \c graphCopy() function.
680 /// This class not only make a copy of a graph, but it can create
681 /// references and cross references between the nodes, edges and arcs of
682 /// the two graphs, and it can copy maps for using with the newly created
685 /// To make a copy from a graph, first an instance of GraphCopy
686 /// should be created, then the data belongs to the graph should
687 /// assigned to copy. In the end, the \c run() member should be
690 /// The next code copies a graph with several data:
692 /// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
693 /// // Create references for the nodes
694 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
696 /// // Create cross references (inverse) for the edges
697 /// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
698 /// cg.edgeCrossRef(ecr);
699 /// // Copy an edge map
700 /// OrigGraph::EdgeMap<double> oemap(orig_graph);
701 /// NewGraph::EdgeMap<double> nemap(new_graph);
702 /// cg.edgeMap(oemap, nemap);
704 /// OrigGraph::Node on;
705 /// NewGraph::Node nn;
707 /// // Execute copying
710 template <typename From, typename To>
714 typedef typename From::Node Node;
715 typedef typename From::NodeIt NodeIt;
716 typedef typename From::Arc Arc;
717 typedef typename From::ArcIt ArcIt;
718 typedef typename From::Edge Edge;
719 typedef typename From::EdgeIt EdgeIt;
721 typedef typename To::Node TNode;
722 typedef typename To::Arc TArc;
723 typedef typename To::Edge TEdge;
725 typedef typename From::template NodeMap<TNode> NodeRefMap;
726 typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
729 ArcRefMap(const From& from, const To& to,
730 const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
731 : _from(from), _to(to),
732 _edge_ref(edge_ref), _node_ref(node_ref) {}
734 typedef typename From::Arc Key;
735 typedef typename To::Arc Value;
737 Value operator[](const Key& key) const {
738 bool forward = _from.u(key) != _from.v(key) ?
739 _node_ref[_from.source(key)] ==
740 _to.source(_to.direct(_edge_ref[key], true)) :
741 _from.direction(key);
742 return _to.direct(_edge_ref[key], forward);
747 const EdgeRefMap& _edge_ref;
748 const NodeRefMap& _node_ref;
753 /// \brief Constructor of GraphCopy.
755 /// Constructor of GraphCopy for copying the content of the
756 /// \c from graph into the \c to graph.
757 GraphCopy(const From& from, To& to)
758 : _from(from), _to(to) {}
760 /// \brief Destructor of GraphCopy
762 /// Destructor of GraphCopy.
764 for (int i = 0; i < int(_node_maps.size()); ++i) {
765 delete _node_maps[i];
767 for (int i = 0; i < int(_arc_maps.size()); ++i) {
770 for (int i = 0; i < int(_edge_maps.size()); ++i) {
771 delete _edge_maps[i];
775 /// \brief Copy the node references into the given map.
777 /// This function copies the node references into the given map.
778 /// The parameter should be a map, whose key type is the Node type of
779 /// the source graph, while the value type is the Node type of the
780 /// destination graph.
781 template <typename NodeRef>
782 GraphCopy& nodeRef(NodeRef& map) {
783 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
784 NodeRefMap, NodeRef>(map));
788 /// \brief Copy the node cross references into the given map.
790 /// This function copies the node cross references (reverse references)
791 /// into the given map. The parameter should be a map, whose key type
792 /// is the Node type of the destination graph, while the value type is
793 /// the Node type of the source graph.
794 template <typename NodeCrossRef>
795 GraphCopy& nodeCrossRef(NodeCrossRef& map) {
796 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
797 NodeRefMap, NodeCrossRef>(map));
801 /// \brief Make a copy of the given node map.
803 /// This function makes a copy of the given node map for the newly
805 /// The key type of the new map \c tmap should be the Node type of the
806 /// destination graph, and the key type of the original map \c map
807 /// should be the Node type of the source graph.
808 template <typename FromMap, typename ToMap>
809 GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
810 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
811 NodeRefMap, FromMap, ToMap>(map, tmap));
815 /// \brief Make a copy of the given node.
817 /// This function makes a copy of the given node.
818 GraphCopy& node(const Node& node, TNode& tnode) {
819 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
820 NodeRefMap, TNode>(node, tnode));
824 /// \brief Copy the arc references into the given map.
826 /// This function copies the arc references into the given map.
827 /// The parameter should be a map, whose key type is the Arc type of
828 /// the source graph, while the value type is the Arc type of the
829 /// destination graph.
830 template <typename ArcRef>
831 GraphCopy& arcRef(ArcRef& map) {
832 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
833 ArcRefMap, ArcRef>(map));
837 /// \brief Copy the arc cross references into the given map.
839 /// This function copies the arc cross references (reverse references)
840 /// into the given map. The parameter should be a map, whose key type
841 /// is the Arc type of the destination graph, while the value type is
842 /// the Arc type of the source graph.
843 template <typename ArcCrossRef>
844 GraphCopy& arcCrossRef(ArcCrossRef& map) {
845 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
846 ArcRefMap, ArcCrossRef>(map));
850 /// \brief Make a copy of the given arc map.
852 /// This function makes a copy of the given arc map for the newly
854 /// The key type of the new map \c tmap should be the Arc type of the
855 /// destination graph, and the key type of the original map \c map
856 /// should be the Arc type of the source graph.
857 template <typename FromMap, typename ToMap>
858 GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
859 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
860 ArcRefMap, FromMap, ToMap>(map, tmap));
864 /// \brief Make a copy of the given arc.
866 /// This function makes a copy of the given arc.
867 GraphCopy& arc(const Arc& arc, TArc& tarc) {
868 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
869 ArcRefMap, TArc>(arc, tarc));
873 /// \brief Copy the edge references into the given map.
875 /// This function copies the edge references into the given map.
876 /// The parameter should be a map, whose key type is the Edge type of
877 /// the source graph, while the value type is the Edge type of the
878 /// destination graph.
879 template <typename EdgeRef>
880 GraphCopy& edgeRef(EdgeRef& map) {
881 _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
882 EdgeRefMap, EdgeRef>(map));
886 /// \brief Copy the edge cross references into the given map.
888 /// This function copies the edge cross references (reverse references)
889 /// into the given map. The parameter should be a map, whose key type
890 /// is the Edge type of the destination graph, while the value type is
891 /// the Edge type of the source graph.
892 template <typename EdgeCrossRef>
893 GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
894 _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
895 Edge, EdgeRefMap, EdgeCrossRef>(map));
899 /// \brief Make a copy of the given edge map.
901 /// This function makes a copy of the given edge map for the newly
903 /// The key type of the new map \c tmap should be the Edge type of the
904 /// destination graph, and the key type of the original map \c map
905 /// should be the Edge type of the source graph.
906 template <typename FromMap, typename ToMap>
907 GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
908 _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
909 EdgeRefMap, FromMap, ToMap>(map, tmap));
913 /// \brief Make a copy of the given edge.
915 /// This function makes a copy of the given edge.
916 GraphCopy& edge(const Edge& edge, TEdge& tedge) {
917 _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
918 EdgeRefMap, TEdge>(edge, tedge));
922 /// \brief Execute copying.
924 /// This function executes the copying of the graph along with the
925 /// copying of the assigned data.
927 NodeRefMap nodeRefMap(_from);
928 EdgeRefMap edgeRefMap(_from);
929 ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
930 _core_bits::GraphCopySelector<To>::
931 copy(_from, _to, nodeRefMap, edgeRefMap);
932 for (int i = 0; i < int(_node_maps.size()); ++i) {
933 _node_maps[i]->copy(_from, nodeRefMap);
935 for (int i = 0; i < int(_edge_maps.size()); ++i) {
936 _edge_maps[i]->copy(_from, edgeRefMap);
938 for (int i = 0; i < int(_arc_maps.size()); ++i) {
939 _arc_maps[i]->copy(_from, arcRefMap);
948 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
951 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
954 std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
959 /// \brief Copy a graph to another graph.
961 /// This function copies a graph to another graph.
962 /// The complete usage of it is detailed in the GraphCopy class,
963 /// but a short example shows a basic work:
965 /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
968 /// After the copy the \c nr map will contain the mapping from the
969 /// nodes of the \c from graph to the nodes of the \c to graph and
970 /// \c ecr will contain the mapping from the edges of the \c to graph
971 /// to the edges of the \c from graph.
974 template <typename From, typename To>
976 graphCopy(const From& from, To& to) {
977 return GraphCopy<From, To>(from, to);
980 namespace _core_bits {
982 template <typename Graph, typename Enable = void>
983 struct FindArcSelector {
984 typedef typename Graph::Node Node;
985 typedef typename Graph::Arc Arc;
986 static Arc find(const Graph &g, Node u, Node v, Arc e) {
992 while (e != INVALID && g.target(e) != v) {
999 template <typename Graph>
1000 struct FindArcSelector<
1002 typename enable_if<typename Graph::FindArcTag, void>::type>
1004 typedef typename Graph::Node Node;
1005 typedef typename Graph::Arc Arc;
1006 static Arc find(const Graph &g, Node u, Node v, Arc prev) {
1007 return g.findArc(u, v, prev);
1012 /// \brief Find an arc between two nodes of a digraph.
1014 /// This function finds an arc from node \c u to node \c v in the
1017 /// If \c prev is \ref INVALID (this is the default value), then
1018 /// it finds the first arc from \c u to \c v. Otherwise it looks for
1019 /// the next arc from \c u to \c v after \c prev.
1020 /// \return The found arc or \ref INVALID if there is no such an arc.
1022 /// Thus you can iterate through each arc from \c u to \c v as it follows.
1024 /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
1029 /// \note \ref ConArcIt provides iterator interface for the same
1033 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1034 template <typename Graph>
1035 inline typename Graph::Arc
1036 findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1037 typename Graph::Arc prev = INVALID) {
1038 return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
1041 /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
1043 /// Iterator for iterating on parallel arcs connecting the same nodes. It is
1044 /// a higher level interface for the \ref findArc() function. You can
1045 /// use it the following way:
1047 /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
1053 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1054 template <typename GR>
1055 class ConArcIt : public GR::Arc {
1056 typedef typename GR::Arc Parent;
1060 typedef typename GR::Arc Arc;
1061 typedef typename GR::Node Node;
1063 /// \brief Constructor.
1065 /// Construct a new ConArcIt iterating on the arcs that
1066 /// connects nodes \c u and \c v.
1067 ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
1068 Parent::operator=(findArc(_graph, u, v));
1071 /// \brief Constructor.
1073 /// Construct a new ConArcIt that continues the iterating from arc \c a.
1074 ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
1076 /// \brief Increment operator.
1078 /// It increments the iterator and gives back the next arc.
1079 ConArcIt& operator++() {
1080 Parent::operator=(findArc(_graph, _graph.source(*this),
1081 _graph.target(*this), *this));
1088 namespace _core_bits {
1090 template <typename Graph, typename Enable = void>
1091 struct FindEdgeSelector {
1092 typedef typename Graph::Node Node;
1093 typedef typename Graph::Edge Edge;
1094 static Edge find(const Graph &g, Node u, Node v, Edge e) {
1098 g.firstInc(e, b, u);
1103 while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
1108 g.firstInc(e, b, u);
1113 while (e != INVALID && (!b || g.v(e) != v)) {
1121 template <typename Graph>
1122 struct FindEdgeSelector<
1124 typename enable_if<typename Graph::FindEdgeTag, void>::type>
1126 typedef typename Graph::Node Node;
1127 typedef typename Graph::Edge Edge;
1128 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
1129 return g.findEdge(u, v, prev);
1134 /// \brief Find an edge between two nodes of a graph.
1136 /// This function finds an edge from node \c u to node \c v in graph \c g.
1137 /// If node \c u and node \c v is equal then each loop edge
1138 /// will be enumerated once.
1140 /// If \c prev is \ref INVALID (this is the default value), then
1141 /// it finds the first edge from \c u to \c v. Otherwise it looks for
1142 /// the next edge from \c u to \c v after \c prev.
1143 /// \return The found edge or \ref INVALID if there is no such an edge.
1145 /// Thus you can iterate through each edge between \c u and \c v
1148 /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
1153 /// \note \ref ConEdgeIt provides iterator interface for the same
1157 template <typename Graph>
1158 inline typename Graph::Edge
1159 findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1160 typename Graph::Edge p = INVALID) {
1161 return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
1164 /// \brief Iterator for iterating on parallel edges connecting the same nodes.
1166 /// Iterator for iterating on parallel edges connecting the same nodes.
1167 /// It is a higher level interface for the findEdge() function. You can
1168 /// use it the following way:
1170 /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
1176 template <typename GR>
1177 class ConEdgeIt : public GR::Edge {
1178 typedef typename GR::Edge Parent;
1182 typedef typename GR::Edge Edge;
1183 typedef typename GR::Node Node;
1185 /// \brief Constructor.
1187 /// Construct a new ConEdgeIt iterating on the edges that
1188 /// connects nodes \c u and \c v.
1189 ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1190 Parent::operator=(findEdge(_graph, _u, _v));
1193 /// \brief Constructor.
1195 /// Construct a new ConEdgeIt that continues iterating from edge \c e.
1196 ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
1198 /// \brief Increment operator.
1200 /// It increments the iterator and gives back the next edge.
1201 ConEdgeIt& operator++() {
1202 Parent::operator=(findEdge(_graph, _u, _v, *this));
1211 ///Dynamic arc look-up between given endpoints.
1213 ///Using this class, you can find an arc in a digraph from a given
1214 ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
1215 ///where <em>d</em> is the out-degree of the source node.
1217 ///It is possible to find \e all parallel arcs between two nodes with
1218 ///the \c operator() member.
1220 ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
1221 ///\ref AllArcLookUp if your digraph is not changed so frequently.
1223 ///This class uses a self-adjusting binary search tree, the Splay tree
1224 ///of Sleator and Tarjan to guarantee the logarithmic amortized
1225 ///time bound for arc look-ups. This class also guarantees the
1226 ///optimal time bound in a constant factor for any distribution of
1229 ///\tparam GR The type of the underlying digraph.
1233 template <typename GR>
1235 : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
1237 typedef typename ItemSetTraits<GR, typename GR::Arc>
1238 ::ItemNotifier::ObserverBase Parent;
1240 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1244 /// The Digraph type
1249 class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type {
1250 typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
1254 AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
1256 virtual void add(const Node& node) {
1258 Parent::set(node, INVALID);
1261 virtual void add(const std::vector<Node>& nodes) {
1263 for (int i = 0; i < int(nodes.size()); ++i) {
1264 Parent::set(nodes[i], INVALID);
1268 virtual void build() {
1271 typename Parent::Notifier* nf = Parent::notifier();
1272 for (nf->first(it); it != INVALID; nf->next(it)) {
1273 Parent::set(it, INVALID);
1281 ArcLess(const Digraph &_g) : g(_g) {}
1282 bool operator()(Arc a,Arc b) const
1284 return g.target(a)<g.target(b);
1292 typename Digraph::template ArcMap<Arc> _parent;
1293 typename Digraph::template ArcMap<Arc> _left;
1294 typename Digraph::template ArcMap<Arc> _right;
1302 ///It builds up the search database.
1303 DynArcLookUp(const Digraph &g)
1304 : _g(g),_head(g),_parent(g),_left(g),_right(g)
1306 Parent::attach(_g.notifier(typename Digraph::Arc()));
1312 virtual void add(const Arc& arc) {
1316 virtual void add(const std::vector<Arc>& arcs) {
1317 for (int i = 0; i < int(arcs.size()); ++i) {
1322 virtual void erase(const Arc& arc) {
1326 virtual void erase(const std::vector<Arc>& arcs) {
1327 for (int i = 0; i < int(arcs.size()); ++i) {
1332 virtual void build() {
1336 virtual void clear() {
1337 for(NodeIt n(_g);n!=INVALID;++n) {
1342 void insert(Arc arc) {
1343 Node s = _g.source(arc);
1344 Node t = _g.target(arc);
1345 _left[arc] = INVALID;
1346 _right[arc] = INVALID;
1351 _parent[arc] = INVALID;
1355 if (t < _g.target(e)) {
1356 if (_left[e] == INVALID) {
1365 if (_right[e] == INVALID) {
1377 void remove(Arc arc) {
1378 if (_left[arc] == INVALID) {
1379 if (_right[arc] != INVALID) {
1380 _parent[_right[arc]] = _parent[arc];
1382 if (_parent[arc] != INVALID) {
1383 if (_left[_parent[arc]] == arc) {
1384 _left[_parent[arc]] = _right[arc];
1386 _right[_parent[arc]] = _right[arc];
1389 _head[_g.source(arc)] = _right[arc];
1391 } else if (_right[arc] == INVALID) {
1392 _parent[_left[arc]] = _parent[arc];
1393 if (_parent[arc] != INVALID) {
1394 if (_left[_parent[arc]] == arc) {
1395 _left[_parent[arc]] = _left[arc];
1397 _right[_parent[arc]] = _left[arc];
1400 _head[_g.source(arc)] = _left[arc];
1404 if (_right[e] != INVALID) {
1406 while (_right[e] != INVALID) {
1410 _right[_parent[e]] = _left[e];
1411 if (_left[e] != INVALID) {
1412 _parent[_left[e]] = _parent[e];
1415 _left[e] = _left[arc];
1416 _parent[_left[arc]] = e;
1417 _right[e] = _right[arc];
1418 _parent[_right[arc]] = e;
1420 _parent[e] = _parent[arc];
1421 if (_parent[arc] != INVALID) {
1422 if (_left[_parent[arc]] == arc) {
1423 _left[_parent[arc]] = e;
1425 _right[_parent[arc]] = e;
1430 _right[e] = _right[arc];
1431 _parent[_right[arc]] = e;
1432 _parent[e] = _parent[arc];
1434 if (_parent[arc] != INVALID) {
1435 if (_left[_parent[arc]] == arc) {
1436 _left[_parent[arc]] = e;
1438 _right[_parent[arc]] = e;
1441 _head[_g.source(arc)] = e;
1447 Arc refreshRec(std::vector<Arc> &v,int a,int b)
1452 Arc left = refreshRec(v,a,m-1);
1456 _left[me] = INVALID;
1459 Arc right = refreshRec(v,m+1,b);
1461 _parent[right] = me;
1463 _right[me] = INVALID;
1469 for(NodeIt n(_g);n!=INVALID;++n) {
1471 for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
1473 std::sort(v.begin(),v.end(),ArcLess(_g));
1474 Arc head = refreshRec(v,0,v.size()-1);
1476 _parent[head] = INVALID;
1478 else _head[n] = INVALID;
1484 _parent[v] = _parent[w];
1486 _left[w] = _right[v];
1488 if (_parent[v] != INVALID) {
1489 if (_right[_parent[v]] == w) {
1490 _right[_parent[v]] = v;
1492 _left[_parent[v]] = v;
1495 if (_left[w] != INVALID){
1496 _parent[_left[w]] = w;
1502 _parent[v] = _parent[w];
1504 _right[w] = _left[v];
1506 if (_parent[v] != INVALID){
1507 if (_left[_parent[v]] == w) {
1508 _left[_parent[v]] = v;
1510 _right[_parent[v]] = v;
1513 if (_right[w] != INVALID){
1514 _parent[_right[w]] = w;
1519 while (_parent[v] != INVALID) {
1520 if (v == _left[_parent[v]]) {
1521 if (_parent[_parent[v]] == INVALID) {
1524 if (_parent[v] == _left[_parent[_parent[v]]]) {
1533 if (_parent[_parent[v]] == INVALID) {
1536 if (_parent[v] == _left[_parent[_parent[v]]]) {
1546 _head[_g.source(v)] = v;
1552 ///Find an arc between two nodes.
1554 ///Find an arc between two nodes.
1555 ///\param s The source node.
1556 ///\param t The target node.
1557 ///\param p The previous arc between \c s and \c t. It it is INVALID or
1558 ///not given, the operator finds the first appropriate arc.
1559 ///\return An arc from \c s to \c t after \c p or
1560 ///\ref INVALID if there is no more.
1562 ///For example, you can count the number of arcs from \c u to \c v in the
1565 ///DynArcLookUp<ListDigraph> ae(g);
1568 ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
1571 ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
1572 ///amortized time, specifically, the time complexity of the lookups
1573 ///is equal to the optimal search tree implementation for the
1574 ///current query distribution in a constant factor.
1576 ///\note This is a dynamic data structure, therefore the data
1577 ///structure is updated after each graph alteration. Thus although
1578 ///this data structure is theoretically faster than \ref ArcLookUp
1579 ///and \ref AllArcLookUp, it often provides worse performance than
1581 Arc operator()(Node s, Node t, Arc p = INVALID) const {
1584 if (a == INVALID) return INVALID;
1587 if (_g.target(a) < t) {
1588 if (_right[a] == INVALID) {
1589 const_cast<DynArcLookUp&>(*this).splay(a);
1595 if (_g.target(a) == t) {
1598 if (_left[a] == INVALID) {
1599 const_cast<DynArcLookUp&>(*this).splay(a);
1608 if (_right[a] != INVALID) {
1610 while (_left[a] != INVALID) {
1613 const_cast<DynArcLookUp&>(*this).splay(a);
1615 while (_parent[a] != INVALID && _right[_parent[a]] == a) {
1618 if (_parent[a] == INVALID) {
1622 const_cast<DynArcLookUp&>(*this).splay(a);
1625 if (_g.target(a) == t) return a;
1626 else return INVALID;
1632 ///Fast arc look-up between given endpoints.
1634 ///Using this class, you can find an arc in a digraph from a given
1635 ///source to a given target in time <em>O</em>(log<em>d</em>),
1636 ///where <em>d</em> is the out-degree of the source node.
1638 ///It is not possible to find \e all parallel arcs between two nodes.
1639 ///Use \ref AllArcLookUp for this purpose.
1641 ///\warning This class is static, so you should call refresh() (or at
1642 ///least refresh(Node)) to refresh this data structure whenever the
1643 ///digraph changes. This is a time consuming (superlinearly proportional
1644 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1646 ///\tparam GR The type of the underlying digraph.
1653 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1657 /// The Digraph type
1662 typename Digraph::template NodeMap<Arc> _head;
1663 typename Digraph::template ArcMap<Arc> _left;
1664 typename Digraph::template ArcMap<Arc> _right;
1669 ArcLess(const Digraph &_g) : g(_g) {}
1670 bool operator()(Arc a,Arc b) const
1672 return g.target(a)<g.target(b);
1682 ///It builds up the search database, which remains valid until the digraph
1684 ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1687 Arc refreshRec(std::vector<Arc> &v,int a,int b)
1691 _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
1692 _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
1696 ///Refresh the search data structure at a node.
1698 ///Build up the search database of node \c n.
1700 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
1701 ///is the number of the outgoing arcs of \c n.
1702 void refresh(Node n)
1705 for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1707 std::sort(v.begin(),v.end(),ArcLess(_g));
1708 _head[n]=refreshRec(v,0,v.size()-1);
1710 else _head[n]=INVALID;
1712 ///Refresh the full data structure.
1714 ///Build up the full search database. In fact, it simply calls
1715 ///\ref refresh(Node) "refresh(n)" for each node \c n.
1717 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1718 ///the number of the arcs in the digraph and <em>D</em> is the maximum
1719 ///out-degree of the digraph.
1722 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1725 ///Find an arc between two nodes.
1727 ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
1728 ///where <em>d</em> is the number of outgoing arcs of \c s.
1729 ///\param s The source node.
1730 ///\param t The target node.
1731 ///\return An arc from \c s to \c t if there exists,
1732 ///\ref INVALID otherwise.
1734 ///\warning If you change the digraph, refresh() must be called before using
1735 ///this operator. If you change the outgoing arcs of
1736 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1737 Arc operator()(Node s, Node t) const
1741 e!=INVALID&&_g.target(e)!=t;
1742 e = t < _g.target(e)?_left[e]:_right[e]) ;
1748 ///Fast look-up of all arcs between given endpoints.
1750 ///This class is the same as \ref ArcLookUp, with the addition
1751 ///that it makes it possible to find all parallel arcs between given
1754 ///\warning This class is static, so you should call refresh() (or at
1755 ///least refresh(Node)) to refresh this data structure whenever the
1756 ///digraph changes. This is a time consuming (superlinearly proportional
1757 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1759 ///\tparam GR The type of the underlying digraph.
1764 class AllArcLookUp : public ArcLookUp<GR>
1766 using ArcLookUp<GR>::_g;
1767 using ArcLookUp<GR>::_right;
1768 using ArcLookUp<GR>::_left;
1769 using ArcLookUp<GR>::_head;
1771 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1773 typename GR::template ArcMap<Arc> _next;
1775 Arc refreshNext(Arc head,Arc next=INVALID)
1777 if(head==INVALID) return next;
1779 next=refreshNext(_right[head],next);
1780 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1782 return refreshNext(_left[head],head);
1788 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
1793 /// The Digraph type
1800 ///It builds up the search database, which remains valid until the digraph
1802 AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
1804 ///Refresh the data structure at a node.
1806 ///Build up the search database of node \c n.
1808 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
1809 ///the number of the outgoing arcs of \c n.
1810 void refresh(Node n)
1812 ArcLookUp<GR>::refresh(n);
1813 refreshNext(_head[n]);
1816 ///Refresh the full data structure.
1818 ///Build up the full search database. In fact, it simply calls
1819 ///\ref refresh(Node) "refresh(n)" for each node \c n.
1821 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1822 ///the number of the arcs in the digraph and <em>D</em> is the maximum
1823 ///out-degree of the digraph.
1826 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
1829 ///Find an arc between two nodes.
1831 ///Find an arc between two nodes.
1832 ///\param s The source node.
1833 ///\param t The target node.
1834 ///\param prev The previous arc between \c s and \c t. It it is INVALID or
1835 ///not given, the operator finds the first appropriate arc.
1836 ///\return An arc from \c s to \c t after \c prev or
1837 ///\ref INVALID if there is no more.
1839 ///For example, you can count the number of arcs from \c u to \c v in the
1842 ///AllArcLookUp<ListDigraph> ae(g);
1845 ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
1848 ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
1849 ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
1850 ///consecutive arcs are found in constant time.
1852 ///\warning If you change the digraph, refresh() must be called before using
1853 ///this operator. If you change the outgoing arcs of
1854 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1856 Arc operator()(Node s, Node t, Arc prev=INVALID) const
1863 e!=INVALID&&_g.target(e)!=t;
1864 e = t < _g.target(e)?_left[e]:_right[e]) ;
1874 else return _next[prev];