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 ///\brief LEMON core utilities.
43 ///This header file contains core utilities for LEMON.
44 ///It is automatically included by all graph types, therefore it usually
45 ///do not have to be included directly.
49 /// \brief Dummy type to make it easier to create invalid iterators.
51 /// Dummy type to make it easier to create invalid iterators.
52 /// See \ref INVALID for the usage.
55 bool operator==(Invalid) { return true; }
56 bool operator!=(Invalid) { return false; }
57 bool operator< (Invalid) { return false; }
60 /// \brief Invalid iterators.
62 /// \ref Invalid is a global type that converts to each iterator
63 /// in such a way that the value of the target iterator will be invalid.
64 #ifdef LEMON_ONLY_TEMPLATES
65 const Invalid INVALID = Invalid();
67 extern const Invalid INVALID;
70 /// \addtogroup gutils
73 ///Create convenience typedefs for the digraph types and iterators
75 ///This \c \#define creates convenient type definitions for the following
76 ///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
77 ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
78 ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
80 ///\note If the graph type is a dependent type, ie. the graph type depend
81 ///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
83 #define DIGRAPH_TYPEDEFS(Digraph) \
84 typedef Digraph::Node Node; \
85 typedef Digraph::NodeIt NodeIt; \
86 typedef Digraph::Arc Arc; \
87 typedef Digraph::ArcIt ArcIt; \
88 typedef Digraph::InArcIt InArcIt; \
89 typedef Digraph::OutArcIt OutArcIt; \
90 typedef Digraph::NodeMap<bool> BoolNodeMap; \
91 typedef Digraph::NodeMap<int> IntNodeMap; \
92 typedef Digraph::NodeMap<double> DoubleNodeMap; \
93 typedef Digraph::ArcMap<bool> BoolArcMap; \
94 typedef Digraph::ArcMap<int> IntArcMap; \
95 typedef Digraph::ArcMap<double> DoubleArcMap
97 ///Create convenience typedefs for the digraph types and iterators
99 ///\see DIGRAPH_TYPEDEFS
101 ///\note Use this macro, if the graph type is a dependent type,
102 ///ie. the graph type depend on a template parameter.
103 #define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \
104 typedef typename Digraph::Node Node; \
105 typedef typename Digraph::NodeIt NodeIt; \
106 typedef typename Digraph::Arc Arc; \
107 typedef typename Digraph::ArcIt ArcIt; \
108 typedef typename Digraph::InArcIt InArcIt; \
109 typedef typename Digraph::OutArcIt OutArcIt; \
110 typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \
111 typedef typename Digraph::template NodeMap<int> IntNodeMap; \
112 typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \
113 typedef typename Digraph::template ArcMap<bool> BoolArcMap; \
114 typedef typename Digraph::template ArcMap<int> IntArcMap; \
115 typedef typename Digraph::template ArcMap<double> DoubleArcMap
117 ///Create convenience typedefs for the graph types and iterators
119 ///This \c \#define creates the same convenient type definitions as defined
120 ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
121 ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
124 ///\note If the graph type is a dependent type, ie. the graph type depend
125 ///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
127 #define GRAPH_TYPEDEFS(Graph) \
128 DIGRAPH_TYPEDEFS(Graph); \
129 typedef Graph::Edge Edge; \
130 typedef Graph::EdgeIt EdgeIt; \
131 typedef Graph::IncEdgeIt IncEdgeIt; \
132 typedef Graph::EdgeMap<bool> BoolEdgeMap; \
133 typedef Graph::EdgeMap<int> IntEdgeMap; \
134 typedef Graph::EdgeMap<double> DoubleEdgeMap
136 ///Create convenience typedefs for the graph types and iterators
138 ///\see GRAPH_TYPEDEFS
140 ///\note Use this macro, if the graph type is a dependent type,
141 ///ie. the graph type depend on a template parameter.
142 #define TEMPLATE_GRAPH_TYPEDEFS(Graph) \
143 TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \
144 typedef typename Graph::Edge Edge; \
145 typedef typename Graph::EdgeIt EdgeIt; \
146 typedef typename Graph::IncEdgeIt IncEdgeIt; \
147 typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \
148 typedef typename Graph::template EdgeMap<int> IntEdgeMap; \
149 typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
151 /// \brief Function to count the items in a graph.
153 /// This function counts the items (nodes, arcs etc.) in a graph.
154 /// The complexity of the function is linear because
155 /// it iterates on all of the items.
156 template <typename Graph, typename Item>
157 inline int countItems(const Graph& g) {
158 typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
160 for (ItemIt it(g); it != INVALID; ++it) {
168 namespace _core_bits {
170 template <typename Graph, typename Enable = void>
171 struct CountNodesSelector {
172 static int count(const Graph &g) {
173 return countItems<Graph, typename Graph::Node>(g);
177 template <typename Graph>
178 struct CountNodesSelector<
180 enable_if<typename Graph::NodeNumTag, void>::type>
182 static int count(const Graph &g) {
188 /// \brief Function to count the nodes in the graph.
190 /// This function counts the nodes in the graph.
191 /// The complexity of the function is <em>O</em>(<em>n</em>), but for some
192 /// graph structures it is specialized to run in <em>O</em>(1).
194 /// \note If the graph contains a \c nodeNum() member function and a
195 /// \c NodeNumTag tag then this function calls directly the member
196 /// function to query the cardinality of the node set.
197 template <typename Graph>
198 inline int countNodes(const Graph& g) {
199 return _core_bits::CountNodesSelector<Graph>::count(g);
204 namespace _core_bits {
206 template <typename Graph, typename Enable = void>
207 struct CountArcsSelector {
208 static int count(const Graph &g) {
209 return countItems<Graph, typename Graph::Arc>(g);
213 template <typename Graph>
214 struct CountArcsSelector<
216 typename enable_if<typename Graph::ArcNumTag, void>::type>
218 static int count(const Graph &g) {
224 /// \brief Function to count the arcs in the graph.
226 /// This function counts the arcs in the graph.
227 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
228 /// graph structures it is specialized to run in <em>O</em>(1).
230 /// \note If the graph contains a \c arcNum() member function and a
231 /// \c ArcNumTag tag then this function calls directly the member
232 /// function to query the cardinality of the arc set.
233 template <typename Graph>
234 inline int countArcs(const Graph& g) {
235 return _core_bits::CountArcsSelector<Graph>::count(g);
240 namespace _core_bits {
242 template <typename Graph, typename Enable = void>
243 struct CountEdgesSelector {
244 static int count(const Graph &g) {
245 return countItems<Graph, typename Graph::Edge>(g);
249 template <typename Graph>
250 struct CountEdgesSelector<
252 typename enable_if<typename Graph::EdgeNumTag, void>::type>
254 static int count(const Graph &g) {
260 /// \brief Function to count the edges in the graph.
262 /// This function counts the edges in the graph.
263 /// The complexity of the function is <em>O</em>(<em>m</em>), but for some
264 /// graph structures it is specialized to run in <em>O</em>(1).
266 /// \note If the graph contains a \c edgeNum() member function and a
267 /// \c EdgeNumTag tag then this function calls directly the member
268 /// function to query the cardinality of the edge set.
269 template <typename Graph>
270 inline int countEdges(const Graph& g) {
271 return _core_bits::CountEdgesSelector<Graph>::count(g);
276 template <typename Graph, typename DegIt>
277 inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
279 for (DegIt it(_g, _n); it != INVALID; ++it) {
285 /// \brief Function to count the number of the out-arcs from node \c n.
287 /// This function counts the number of the out-arcs from node \c n
288 /// in the graph \c g.
289 template <typename Graph>
290 inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
291 return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
294 /// \brief Function to count the number of the in-arcs to node \c n.
296 /// This function counts the number of the in-arcs to node \c n
297 /// in the graph \c g.
298 template <typename Graph>
299 inline int countInArcs(const Graph& g, const typename Graph::Node& n) {
300 return countNodeDegree<Graph, typename Graph::InArcIt>(g, n);
303 /// \brief Function to count the number of the inc-edges to node \c n.
305 /// This function counts the number of the inc-edges to node \c n
306 /// in the undirected graph \c g.
307 template <typename Graph>
308 inline int countIncEdges(const Graph& g, const typename Graph::Node& n) {
309 return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n);
312 namespace _core_bits {
314 template <typename Digraph, typename Item, typename RefMap>
317 virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
319 virtual ~MapCopyBase() {}
322 template <typename Digraph, typename Item, typename RefMap,
323 typename FromMap, typename ToMap>
324 class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
327 MapCopy(const FromMap& map, ToMap& tmap)
328 : _map(map), _tmap(tmap) {}
330 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
331 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
332 for (ItemIt it(digraph); it != INVALID; ++it) {
333 _tmap.set(refMap[it], _map[it]);
342 template <typename Digraph, typename Item, typename RefMap, typename It>
343 class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
346 ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
348 virtual void copy(const Digraph&, const RefMap& refMap) {
357 template <typename Digraph, typename Item, typename RefMap, typename Ref>
358 class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
361 RefCopy(Ref& map) : _map(map) {}
363 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
364 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
365 for (ItemIt it(digraph); it != INVALID; ++it) {
366 _map.set(it, refMap[it]);
374 template <typename Digraph, typename Item, typename RefMap,
376 class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
379 CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
381 virtual void copy(const Digraph& digraph, const RefMap& refMap) {
382 typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
383 for (ItemIt it(digraph); it != INVALID; ++it) {
384 _cmap.set(refMap[it], it);
392 template <typename Digraph, typename Enable = void>
393 struct DigraphCopySelector {
394 template <typename From, typename NodeRefMap, typename ArcRefMap>
395 static void copy(const From& from, Digraph &to,
396 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
397 for (typename From::NodeIt it(from); it != INVALID; ++it) {
398 nodeRefMap[it] = to.addNode();
400 for (typename From::ArcIt it(from); it != INVALID; ++it) {
401 arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
402 nodeRefMap[from.target(it)]);
407 template <typename Digraph>
408 struct DigraphCopySelector<
410 typename enable_if<typename Digraph::BuildTag, void>::type>
412 template <typename From, typename NodeRefMap, typename ArcRefMap>
413 static void copy(const From& from, Digraph &to,
414 NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
415 to.build(from, nodeRefMap, arcRefMap);
419 template <typename Graph, typename Enable = void>
420 struct GraphCopySelector {
421 template <typename From, typename NodeRefMap, typename EdgeRefMap>
422 static void copy(const From& from, Graph &to,
423 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
424 for (typename From::NodeIt it(from); it != INVALID; ++it) {
425 nodeRefMap[it] = to.addNode();
427 for (typename From::EdgeIt it(from); it != INVALID; ++it) {
428 edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)],
429 nodeRefMap[from.v(it)]);
434 template <typename Graph>
435 struct GraphCopySelector<
437 typename enable_if<typename Graph::BuildTag, void>::type>
439 template <typename From, typename NodeRefMap, typename EdgeRefMap>
440 static void copy(const From& from, Graph &to,
441 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
442 to.build(from, nodeRefMap, edgeRefMap);
448 /// \brief Class to copy a digraph.
450 /// Class to copy a digraph to another digraph (duplicate a digraph). The
451 /// simplest way of using it is through the \c digraphCopy() function.
453 /// This class not only make a copy of a digraph, but it can create
454 /// references and cross references between the nodes and arcs of
455 /// the two digraphs, and it can copy maps to use with the newly created
458 /// To make a copy from a digraph, first an instance of DigraphCopy
459 /// should be created, then the data belongs to the digraph should
460 /// assigned to copy. In the end, the \c run() member should be
463 /// The next code copies a digraph with several data:
465 /// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
466 /// // Create references for the nodes
467 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
469 /// // Create cross references (inverse) for the arcs
470 /// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
471 /// cg.arcCrossRef(acr);
472 /// // Copy an arc map
473 /// OrigGraph::ArcMap<double> oamap(orig_graph);
474 /// NewGraph::ArcMap<double> namap(new_graph);
475 /// cg.arcMap(oamap, namap);
477 /// OrigGraph::Node on;
478 /// NewGraph::Node nn;
480 /// // Execute copying
483 template <typename From, typename To>
487 typedef typename From::Node Node;
488 typedef typename From::NodeIt NodeIt;
489 typedef typename From::Arc Arc;
490 typedef typename From::ArcIt ArcIt;
492 typedef typename To::Node TNode;
493 typedef typename To::Arc TArc;
495 typedef typename From::template NodeMap<TNode> NodeRefMap;
496 typedef typename From::template ArcMap<TArc> ArcRefMap;
500 /// \brief Constructor of DigraphCopy.
502 /// Constructor of DigraphCopy for copying the content of the
503 /// \c from digraph into the \c to digraph.
504 DigraphCopy(const From& from, To& to)
505 : _from(from), _to(to) {}
507 /// \brief Destructor of DigraphCopy
509 /// Destructor of DigraphCopy.
511 for (int i = 0; i < int(_node_maps.size()); ++i) {
512 delete _node_maps[i];
514 for (int i = 0; i < int(_arc_maps.size()); ++i) {
520 /// \brief Copy the node references into the given map.
522 /// This function copies the node references into the given map.
523 /// The parameter should be a map, whose key type is the Node type of
524 /// the source digraph, while the value type is the Node type of the
525 /// destination digraph.
526 template <typename NodeRef>
527 DigraphCopy& nodeRef(NodeRef& map) {
528 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
529 NodeRefMap, NodeRef>(map));
533 /// \brief Copy the node cross references into the given map.
535 /// This function copies the node cross references (reverse references)
536 /// into the given map. The parameter should be a map, whose key type
537 /// is the Node type of the destination digraph, while the value type is
538 /// the Node type of the source digraph.
539 template <typename NodeCrossRef>
540 DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
541 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
542 NodeRefMap, NodeCrossRef>(map));
546 /// \brief Make a copy of the given node map.
548 /// This function makes a copy of the given node map for the newly
550 /// The key type of the new map \c tmap should be the Node type of the
551 /// destination digraph, and the key type of the original map \c map
552 /// should be the Node type of the source digraph.
553 template <typename FromMap, typename ToMap>
554 DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
555 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
556 NodeRefMap, FromMap, ToMap>(map, tmap));
560 /// \brief Make a copy of the given node.
562 /// This function makes a copy of the given node.
563 DigraphCopy& node(const Node& node, TNode& tnode) {
564 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
565 NodeRefMap, TNode>(node, tnode));
569 /// \brief Copy the arc references into the given map.
571 /// This function copies the arc references into the given map.
572 /// The parameter should be a map, whose key type is the Arc type of
573 /// the source digraph, while the value type is the Arc type of the
574 /// destination digraph.
575 template <typename ArcRef>
576 DigraphCopy& arcRef(ArcRef& map) {
577 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
578 ArcRefMap, ArcRef>(map));
582 /// \brief Copy the arc cross references into the given map.
584 /// This function copies the arc cross references (reverse references)
585 /// into the given map. The parameter should be a map, whose key type
586 /// is the Arc type of the destination digraph, while the value type is
587 /// the Arc type of the source digraph.
588 template <typename ArcCrossRef>
589 DigraphCopy& arcCrossRef(ArcCrossRef& map) {
590 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
591 ArcRefMap, ArcCrossRef>(map));
595 /// \brief Make a copy of the given arc map.
597 /// This function makes a copy of the given arc map for the newly
599 /// The key type of the new map \c tmap should be the Arc type of the
600 /// destination digraph, and the key type of the original map \c map
601 /// should be the Arc type of the source digraph.
602 template <typename FromMap, typename ToMap>
603 DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
604 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
605 ArcRefMap, FromMap, ToMap>(map, tmap));
609 /// \brief Make a copy of the given arc.
611 /// This function makes a copy of the given arc.
612 DigraphCopy& arc(const Arc& arc, TArc& tarc) {
613 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
614 ArcRefMap, TArc>(arc, tarc));
618 /// \brief Execute copying.
620 /// This function executes the copying of the digraph along with the
621 /// copying of the assigned data.
623 NodeRefMap nodeRefMap(_from);
624 ArcRefMap arcRefMap(_from);
625 _core_bits::DigraphCopySelector<To>::
626 copy(_from, _to, nodeRefMap, arcRefMap);
627 for (int i = 0; i < int(_node_maps.size()); ++i) {
628 _node_maps[i]->copy(_from, nodeRefMap);
630 for (int i = 0; i < int(_arc_maps.size()); ++i) {
631 _arc_maps[i]->copy(_from, arcRefMap);
640 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
643 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
648 /// \brief Copy a digraph to another digraph.
650 /// This function copies a digraph to another digraph.
651 /// The complete usage of it is detailed in the DigraphCopy class, but
652 /// a short example shows a basic work:
654 /// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run();
657 /// After the copy the \c nr map will contain the mapping from the
658 /// nodes of the \c from digraph to the nodes of the \c to digraph and
659 /// \c acr will contain the mapping from the arcs of the \c to digraph
660 /// to the arcs of the \c from digraph.
663 template <typename From, typename To>
664 DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
665 return DigraphCopy<From, To>(from, to);
668 /// \brief Class to copy a graph.
670 /// Class to copy a graph to another graph (duplicate a graph). The
671 /// simplest way of using it is through the \c graphCopy() function.
673 /// This class not only make a copy of a graph, but it can create
674 /// references and cross references between the nodes, edges and arcs of
675 /// the two graphs, and it can copy maps for using with the newly created
678 /// To make a copy from a graph, first an instance of GraphCopy
679 /// should be created, then the data belongs to the graph should
680 /// assigned to copy. In the end, the \c run() member should be
683 /// The next code copies a graph with several data:
685 /// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph);
686 /// // Create references for the nodes
687 /// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
689 /// // Create cross references (inverse) for the edges
690 /// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph);
691 /// cg.edgeCrossRef(ecr);
692 /// // Copy an edge map
693 /// OrigGraph::EdgeMap<double> oemap(orig_graph);
694 /// NewGraph::EdgeMap<double> nemap(new_graph);
695 /// cg.edgeMap(oemap, nemap);
697 /// OrigGraph::Node on;
698 /// NewGraph::Node nn;
700 /// // Execute copying
703 template <typename From, typename To>
707 typedef typename From::Node Node;
708 typedef typename From::NodeIt NodeIt;
709 typedef typename From::Arc Arc;
710 typedef typename From::ArcIt ArcIt;
711 typedef typename From::Edge Edge;
712 typedef typename From::EdgeIt EdgeIt;
714 typedef typename To::Node TNode;
715 typedef typename To::Arc TArc;
716 typedef typename To::Edge TEdge;
718 typedef typename From::template NodeMap<TNode> NodeRefMap;
719 typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
722 ArcRefMap(const From& from, const To& to,
723 const EdgeRefMap& edge_ref, const NodeRefMap& node_ref)
724 : _from(from), _to(to),
725 _edge_ref(edge_ref), _node_ref(node_ref) {}
727 typedef typename From::Arc Key;
728 typedef typename To::Arc Value;
730 Value operator[](const Key& key) const {
731 bool forward = _from.u(key) != _from.v(key) ?
732 _node_ref[_from.source(key)] ==
733 _to.source(_to.direct(_edge_ref[key], true)) :
734 _from.direction(key);
735 return _to.direct(_edge_ref[key], forward);
740 const EdgeRefMap& _edge_ref;
741 const NodeRefMap& _node_ref;
746 /// \brief Constructor of GraphCopy.
748 /// Constructor of GraphCopy for copying the content of the
749 /// \c from graph into the \c to graph.
750 GraphCopy(const From& from, To& to)
751 : _from(from), _to(to) {}
753 /// \brief Destructor of GraphCopy
755 /// Destructor of GraphCopy.
757 for (int i = 0; i < int(_node_maps.size()); ++i) {
758 delete _node_maps[i];
760 for (int i = 0; i < int(_arc_maps.size()); ++i) {
763 for (int i = 0; i < int(_edge_maps.size()); ++i) {
764 delete _edge_maps[i];
768 /// \brief Copy the node references into the given map.
770 /// This function copies the node references into the given map.
771 /// The parameter should be a map, whose key type is the Node type of
772 /// the source graph, while the value type is the Node type of the
773 /// destination graph.
774 template <typename NodeRef>
775 GraphCopy& nodeRef(NodeRef& map) {
776 _node_maps.push_back(new _core_bits::RefCopy<From, Node,
777 NodeRefMap, NodeRef>(map));
781 /// \brief Copy the node cross references into the given map.
783 /// This function copies the node cross references (reverse references)
784 /// into the given map. The parameter should be a map, whose key type
785 /// is the Node type of the destination graph, while the value type is
786 /// the Node type of the source graph.
787 template <typename NodeCrossRef>
788 GraphCopy& nodeCrossRef(NodeCrossRef& map) {
789 _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
790 NodeRefMap, NodeCrossRef>(map));
794 /// \brief Make a copy of the given node map.
796 /// This function makes a copy of the given node map for the newly
798 /// The key type of the new map \c tmap should be the Node type of the
799 /// destination graph, and the key type of the original map \c map
800 /// should be the Node type of the source graph.
801 template <typename FromMap, typename ToMap>
802 GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
803 _node_maps.push_back(new _core_bits::MapCopy<From, Node,
804 NodeRefMap, FromMap, ToMap>(map, tmap));
808 /// \brief Make a copy of the given node.
810 /// This function makes a copy of the given node.
811 GraphCopy& node(const Node& node, TNode& tnode) {
812 _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
813 NodeRefMap, TNode>(node, tnode));
817 /// \brief Copy the arc references into the given map.
819 /// This function copies the arc references into the given map.
820 /// The parameter should be a map, whose key type is the Arc type of
821 /// the source graph, while the value type is the Arc type of the
822 /// destination graph.
823 template <typename ArcRef>
824 GraphCopy& arcRef(ArcRef& map) {
825 _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
826 ArcRefMap, ArcRef>(map));
830 /// \brief Copy the arc cross references into the given map.
832 /// This function copies the arc cross references (reverse references)
833 /// into the given map. The parameter should be a map, whose key type
834 /// is the Arc type of the destination graph, while the value type is
835 /// the Arc type of the source graph.
836 template <typename ArcCrossRef>
837 GraphCopy& arcCrossRef(ArcCrossRef& map) {
838 _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
839 ArcRefMap, ArcCrossRef>(map));
843 /// \brief Make a copy of the given arc map.
845 /// This function makes a copy of the given arc map for the newly
847 /// The key type of the new map \c tmap should be the Arc type of the
848 /// destination graph, and the key type of the original map \c map
849 /// should be the Arc type of the source graph.
850 template <typename FromMap, typename ToMap>
851 GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
852 _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
853 ArcRefMap, FromMap, ToMap>(map, tmap));
857 /// \brief Make a copy of the given arc.
859 /// This function makes a copy of the given arc.
860 GraphCopy& arc(const Arc& arc, TArc& tarc) {
861 _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
862 ArcRefMap, TArc>(arc, tarc));
866 /// \brief Copy the edge references into the given map.
868 /// This function copies the edge references into the given map.
869 /// The parameter should be a map, whose key type is the Edge type of
870 /// the source graph, while the value type is the Edge type of the
871 /// destination graph.
872 template <typename EdgeRef>
873 GraphCopy& edgeRef(EdgeRef& map) {
874 _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
875 EdgeRefMap, EdgeRef>(map));
879 /// \brief Copy the edge cross references into the given map.
881 /// This function copies the edge cross references (reverse references)
882 /// into the given map. The parameter should be a map, whose key type
883 /// is the Edge type of the destination graph, while the value type is
884 /// the Edge type of the source graph.
885 template <typename EdgeCrossRef>
886 GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
887 _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
888 Edge, EdgeRefMap, EdgeCrossRef>(map));
892 /// \brief Make a copy of the given edge map.
894 /// This function makes a copy of the given edge map for the newly
896 /// The key type of the new map \c tmap should be the Edge type of the
897 /// destination graph, and the key type of the original map \c map
898 /// should be the Edge type of the source graph.
899 template <typename FromMap, typename ToMap>
900 GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
901 _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
902 EdgeRefMap, FromMap, ToMap>(map, tmap));
906 /// \brief Make a copy of the given edge.
908 /// This function makes a copy of the given edge.
909 GraphCopy& edge(const Edge& edge, TEdge& tedge) {
910 _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
911 EdgeRefMap, TEdge>(edge, tedge));
915 /// \brief Execute copying.
917 /// This function executes the copying of the graph along with the
918 /// copying of the assigned data.
920 NodeRefMap nodeRefMap(_from);
921 EdgeRefMap edgeRefMap(_from);
922 ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap);
923 _core_bits::GraphCopySelector<To>::
924 copy(_from, _to, nodeRefMap, edgeRefMap);
925 for (int i = 0; i < int(_node_maps.size()); ++i) {
926 _node_maps[i]->copy(_from, nodeRefMap);
928 for (int i = 0; i < int(_edge_maps.size()); ++i) {
929 _edge_maps[i]->copy(_from, edgeRefMap);
931 for (int i = 0; i < int(_arc_maps.size()); ++i) {
932 _arc_maps[i]->copy(_from, arcRefMap);
941 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
944 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
947 std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
952 /// \brief Copy a graph to another graph.
954 /// This function copies a graph to another graph.
955 /// The complete usage of it is detailed in the GraphCopy class,
956 /// but a short example shows a basic work:
958 /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
961 /// After the copy the \c nr map will contain the mapping from the
962 /// nodes of the \c from graph to the nodes of the \c to graph and
963 /// \c ecr will contain the mapping from the edges of the \c to graph
964 /// to the edges of the \c from graph.
967 template <typename From, typename To>
969 graphCopy(const From& from, To& to) {
970 return GraphCopy<From, To>(from, to);
973 namespace _core_bits {
975 template <typename Graph, typename Enable = void>
976 struct FindArcSelector {
977 typedef typename Graph::Node Node;
978 typedef typename Graph::Arc Arc;
979 static Arc find(const Graph &g, Node u, Node v, Arc e) {
985 while (e != INVALID && g.target(e) != v) {
992 template <typename Graph>
993 struct FindArcSelector<
995 typename enable_if<typename Graph::FindArcTag, void>::type>
997 typedef typename Graph::Node Node;
998 typedef typename Graph::Arc Arc;
999 static Arc find(const Graph &g, Node u, Node v, Arc prev) {
1000 return g.findArc(u, v, prev);
1005 /// \brief Find an arc between two nodes of a digraph.
1007 /// This function finds an arc from node \c u to node \c v in the
1010 /// If \c prev is \ref INVALID (this is the default value), then
1011 /// it finds the first arc from \c u to \c v. Otherwise it looks for
1012 /// the next arc from \c u to \c v after \c prev.
1013 /// \return The found arc or \ref INVALID if there is no such an arc.
1015 /// Thus you can iterate through each arc from \c u to \c v as it follows.
1017 /// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
1022 /// \note \ref ConArcIt provides iterator interface for the same
1026 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1027 template <typename Graph>
1028 inline typename Graph::Arc
1029 findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1030 typename Graph::Arc prev = INVALID) {
1031 return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev);
1034 /// \brief Iterator for iterating on parallel arcs connecting the same nodes.
1036 /// Iterator for iterating on parallel arcs connecting the same nodes. It is
1037 /// a higher level interface for the \ref findArc() function. You can
1038 /// use it the following way:
1040 /// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
1046 ///\sa ArcLookUp, AllArcLookUp, DynArcLookUp
1047 template <typename GR>
1048 class ConArcIt : public GR::Arc {
1049 typedef typename GR::Arc Parent;
1053 typedef typename GR::Arc Arc;
1054 typedef typename GR::Node Node;
1056 /// \brief Constructor.
1058 /// Construct a new ConArcIt iterating on the arcs that
1059 /// connects nodes \c u and \c v.
1060 ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
1061 Parent::operator=(findArc(_graph, u, v));
1064 /// \brief Constructor.
1066 /// Construct a new ConArcIt that continues the iterating from arc \c a.
1067 ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
1069 /// \brief Increment operator.
1071 /// It increments the iterator and gives back the next arc.
1072 ConArcIt& operator++() {
1073 Parent::operator=(findArc(_graph, _graph.source(*this),
1074 _graph.target(*this), *this));
1081 namespace _core_bits {
1083 template <typename Graph, typename Enable = void>
1084 struct FindEdgeSelector {
1085 typedef typename Graph::Node Node;
1086 typedef typename Graph::Edge Edge;
1087 static Edge find(const Graph &g, Node u, Node v, Edge e) {
1091 g.firstInc(e, b, u);
1096 while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
1101 g.firstInc(e, b, u);
1106 while (e != INVALID && (!b || g.v(e) != v)) {
1114 template <typename Graph>
1115 struct FindEdgeSelector<
1117 typename enable_if<typename Graph::FindEdgeTag, void>::type>
1119 typedef typename Graph::Node Node;
1120 typedef typename Graph::Edge Edge;
1121 static Edge find(const Graph &g, Node u, Node v, Edge prev) {
1122 return g.findEdge(u, v, prev);
1127 /// \brief Find an edge between two nodes of a graph.
1129 /// This function finds an edge from node \c u to node \c v in graph \c g.
1130 /// If node \c u and node \c v is equal then each loop edge
1131 /// will be enumerated once.
1133 /// If \c prev is \ref INVALID (this is the default value), then
1134 /// it finds the first edge from \c u to \c v. Otherwise it looks for
1135 /// the next edge from \c u to \c v after \c prev.
1136 /// \return The found edge or \ref INVALID if there is no such an edge.
1138 /// Thus you can iterate through each edge between \c u and \c v
1141 /// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
1146 /// \note \ref ConEdgeIt provides iterator interface for the same
1150 template <typename Graph>
1151 inline typename Graph::Edge
1152 findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
1153 typename Graph::Edge p = INVALID) {
1154 return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
1157 /// \brief Iterator for iterating on parallel edges connecting the same nodes.
1159 /// Iterator for iterating on parallel edges connecting the same nodes.
1160 /// It is a higher level interface for the findEdge() function. You can
1161 /// use it the following way:
1163 /// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
1169 template <typename GR>
1170 class ConEdgeIt : public GR::Edge {
1171 typedef typename GR::Edge Parent;
1175 typedef typename GR::Edge Edge;
1176 typedef typename GR::Node Node;
1178 /// \brief Constructor.
1180 /// Construct a new ConEdgeIt iterating on the edges that
1181 /// connects nodes \c u and \c v.
1182 ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) {
1183 Parent::operator=(findEdge(_graph, _u, _v));
1186 /// \brief Constructor.
1188 /// Construct a new ConEdgeIt that continues iterating from edge \c e.
1189 ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {}
1191 /// \brief Increment operator.
1193 /// It increments the iterator and gives back the next edge.
1194 ConEdgeIt& operator++() {
1195 Parent::operator=(findEdge(_graph, _u, _v, *this));
1204 ///Dynamic arc look-up between given endpoints.
1206 ///Using this class, you can find an arc in a digraph from a given
1207 ///source to a given target in amortized time <em>O</em>(log<em>d</em>),
1208 ///where <em>d</em> is the out-degree of the source node.
1210 ///It is possible to find \e all parallel arcs between two nodes with
1211 ///the \c operator() member.
1213 ///This is a dynamic data structure. Consider to use \ref ArcLookUp or
1214 ///\ref AllArcLookUp if your digraph is not changed so frequently.
1216 ///This class uses a self-adjusting binary search tree, the Splay tree
1217 ///of Sleator and Tarjan to guarantee the logarithmic amortized
1218 ///time bound for arc look-ups. This class also guarantees the
1219 ///optimal time bound in a constant factor for any distribution of
1222 ///\tparam GR The type of the underlying digraph.
1226 template <typename GR>
1228 : protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase
1230 typedef typename ItemSetTraits<GR, typename GR::Arc>
1231 ::ItemNotifier::ObserverBase Parent;
1233 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1237 /// The Digraph type
1242 class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type {
1243 typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent;
1247 AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
1249 virtual void add(const Node& node) {
1251 Parent::set(node, INVALID);
1254 virtual void add(const std::vector<Node>& nodes) {
1256 for (int i = 0; i < int(nodes.size()); ++i) {
1257 Parent::set(nodes[i], INVALID);
1261 virtual void build() {
1264 typename Parent::Notifier* nf = Parent::notifier();
1265 for (nf->first(it); it != INVALID; nf->next(it)) {
1266 Parent::set(it, INVALID);
1274 ArcLess(const Digraph &_g) : g(_g) {}
1275 bool operator()(Arc a,Arc b) const
1277 return g.target(a)<g.target(b);
1285 typename Digraph::template ArcMap<Arc> _parent;
1286 typename Digraph::template ArcMap<Arc> _left;
1287 typename Digraph::template ArcMap<Arc> _right;
1295 ///It builds up the search database.
1296 DynArcLookUp(const Digraph &g)
1297 : _g(g),_head(g),_parent(g),_left(g),_right(g)
1299 Parent::attach(_g.notifier(typename Digraph::Arc()));
1305 virtual void add(const Arc& arc) {
1309 virtual void add(const std::vector<Arc>& arcs) {
1310 for (int i = 0; i < int(arcs.size()); ++i) {
1315 virtual void erase(const Arc& arc) {
1319 virtual void erase(const std::vector<Arc>& arcs) {
1320 for (int i = 0; i < int(arcs.size()); ++i) {
1325 virtual void build() {
1329 virtual void clear() {
1330 for(NodeIt n(_g);n!=INVALID;++n) {
1335 void insert(Arc arc) {
1336 Node s = _g.source(arc);
1337 Node t = _g.target(arc);
1338 _left[arc] = INVALID;
1339 _right[arc] = INVALID;
1344 _parent[arc] = INVALID;
1348 if (t < _g.target(e)) {
1349 if (_left[e] == INVALID) {
1358 if (_right[e] == INVALID) {
1370 void remove(Arc arc) {
1371 if (_left[arc] == INVALID) {
1372 if (_right[arc] != INVALID) {
1373 _parent[_right[arc]] = _parent[arc];
1375 if (_parent[arc] != INVALID) {
1376 if (_left[_parent[arc]] == arc) {
1377 _left[_parent[arc]] = _right[arc];
1379 _right[_parent[arc]] = _right[arc];
1382 _head[_g.source(arc)] = _right[arc];
1384 } else if (_right[arc] == INVALID) {
1385 _parent[_left[arc]] = _parent[arc];
1386 if (_parent[arc] != INVALID) {
1387 if (_left[_parent[arc]] == arc) {
1388 _left[_parent[arc]] = _left[arc];
1390 _right[_parent[arc]] = _left[arc];
1393 _head[_g.source(arc)] = _left[arc];
1397 if (_right[e] != INVALID) {
1399 while (_right[e] != INVALID) {
1403 _right[_parent[e]] = _left[e];
1404 if (_left[e] != INVALID) {
1405 _parent[_left[e]] = _parent[e];
1408 _left[e] = _left[arc];
1409 _parent[_left[arc]] = e;
1410 _right[e] = _right[arc];
1411 _parent[_right[arc]] = e;
1413 _parent[e] = _parent[arc];
1414 if (_parent[arc] != INVALID) {
1415 if (_left[_parent[arc]] == arc) {
1416 _left[_parent[arc]] = e;
1418 _right[_parent[arc]] = e;
1423 _right[e] = _right[arc];
1424 _parent[_right[arc]] = e;
1425 _parent[e] = _parent[arc];
1427 if (_parent[arc] != INVALID) {
1428 if (_left[_parent[arc]] == arc) {
1429 _left[_parent[arc]] = e;
1431 _right[_parent[arc]] = e;
1434 _head[_g.source(arc)] = e;
1440 Arc refreshRec(std::vector<Arc> &v,int a,int b)
1445 Arc left = refreshRec(v,a,m-1);
1449 _left[me] = INVALID;
1452 Arc right = refreshRec(v,m+1,b);
1454 _parent[right] = me;
1456 _right[me] = INVALID;
1462 for(NodeIt n(_g);n!=INVALID;++n) {
1464 for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a);
1466 std::sort(v.begin(),v.end(),ArcLess(_g));
1467 Arc head = refreshRec(v,0,v.size()-1);
1469 _parent[head] = INVALID;
1471 else _head[n] = INVALID;
1477 _parent[v] = _parent[w];
1479 _left[w] = _right[v];
1481 if (_parent[v] != INVALID) {
1482 if (_right[_parent[v]] == w) {
1483 _right[_parent[v]] = v;
1485 _left[_parent[v]] = v;
1488 if (_left[w] != INVALID){
1489 _parent[_left[w]] = w;
1495 _parent[v] = _parent[w];
1497 _right[w] = _left[v];
1499 if (_parent[v] != INVALID){
1500 if (_left[_parent[v]] == w) {
1501 _left[_parent[v]] = v;
1503 _right[_parent[v]] = v;
1506 if (_right[w] != INVALID){
1507 _parent[_right[w]] = w;
1512 while (_parent[v] != INVALID) {
1513 if (v == _left[_parent[v]]) {
1514 if (_parent[_parent[v]] == INVALID) {
1517 if (_parent[v] == _left[_parent[_parent[v]]]) {
1526 if (_parent[_parent[v]] == INVALID) {
1529 if (_parent[v] == _left[_parent[_parent[v]]]) {
1539 _head[_g.source(v)] = v;
1545 ///Find an arc between two nodes.
1547 ///Find an arc between two nodes.
1548 ///\param s The source node.
1549 ///\param t The target node.
1550 ///\param p The previous arc between \c s and \c t. It it is INVALID or
1551 ///not given, the operator finds the first appropriate arc.
1552 ///\return An arc from \c s to \c t after \c p or
1553 ///\ref INVALID if there is no more.
1555 ///For example, you can count the number of arcs from \c u to \c v in the
1558 ///DynArcLookUp<ListDigraph> ae(g);
1561 ///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++;
1564 ///Finding the arcs take at most <em>O</em>(log<em>d</em>)
1565 ///amortized time, specifically, the time complexity of the lookups
1566 ///is equal to the optimal search tree implementation for the
1567 ///current query distribution in a constant factor.
1569 ///\note This is a dynamic data structure, therefore the data
1570 ///structure is updated after each graph alteration. Thus although
1571 ///this data structure is theoretically faster than \ref ArcLookUp
1572 ///and \ref AllArcLookUp, it often provides worse performance than
1574 Arc operator()(Node s, Node t, Arc p = INVALID) const {
1577 if (a == INVALID) return INVALID;
1580 if (_g.target(a) < t) {
1581 if (_right[a] == INVALID) {
1582 const_cast<DynArcLookUp&>(*this).splay(a);
1588 if (_g.target(a) == t) {
1591 if (_left[a] == INVALID) {
1592 const_cast<DynArcLookUp&>(*this).splay(a);
1601 if (_right[a] != INVALID) {
1603 while (_left[a] != INVALID) {
1606 const_cast<DynArcLookUp&>(*this).splay(a);
1608 while (_parent[a] != INVALID && _right[_parent[a]] == a) {
1611 if (_parent[a] == INVALID) {
1615 const_cast<DynArcLookUp&>(*this).splay(a);
1618 if (_g.target(a) == t) return a;
1619 else return INVALID;
1625 ///Fast arc look-up between given endpoints.
1627 ///Using this class, you can find an arc in a digraph from a given
1628 ///source to a given target in time <em>O</em>(log<em>d</em>),
1629 ///where <em>d</em> is the out-degree of the source node.
1631 ///It is not possible to find \e all parallel arcs between two nodes.
1632 ///Use \ref AllArcLookUp for this purpose.
1634 ///\warning This class is static, so you should call refresh() (or at
1635 ///least refresh(Node)) to refresh this data structure whenever the
1636 ///digraph changes. This is a time consuming (superlinearly proportional
1637 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1639 ///\tparam GR The type of the underlying digraph.
1646 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1650 /// The Digraph type
1655 typename Digraph::template NodeMap<Arc> _head;
1656 typename Digraph::template ArcMap<Arc> _left;
1657 typename Digraph::template ArcMap<Arc> _right;
1662 ArcLess(const Digraph &_g) : g(_g) {}
1663 bool operator()(Arc a,Arc b) const
1665 return g.target(a)<g.target(b);
1675 ///It builds up the search database, which remains valid until the digraph
1677 ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
1680 Arc refreshRec(std::vector<Arc> &v,int a,int b)
1684 _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
1685 _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
1689 ///Refresh the search data structure at a node.
1691 ///Build up the search database of node \c n.
1693 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em>
1694 ///is the number of the outgoing arcs of \c n.
1695 void refresh(Node n)
1698 for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
1700 std::sort(v.begin(),v.end(),ArcLess(_g));
1701 _head[n]=refreshRec(v,0,v.size()-1);
1703 else _head[n]=INVALID;
1705 ///Refresh the full data structure.
1707 ///Build up the full search database. In fact, it simply calls
1708 ///\ref refresh(Node) "refresh(n)" for each node \c n.
1710 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1711 ///the number of the arcs in the digraph and <em>D</em> is the maximum
1712 ///out-degree of the digraph.
1715 for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
1718 ///Find an arc between two nodes.
1720 ///Find an arc between two nodes in time <em>O</em>(log<em>d</em>),
1721 ///where <em>d</em> is the number of outgoing arcs of \c s.
1722 ///\param s The source node.
1723 ///\param t The target node.
1724 ///\return An arc from \c s to \c t if there exists,
1725 ///\ref INVALID otherwise.
1727 ///\warning If you change the digraph, refresh() must be called before using
1728 ///this operator. If you change the outgoing arcs of
1729 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1730 Arc operator()(Node s, Node t) const
1734 e!=INVALID&&_g.target(e)!=t;
1735 e = t < _g.target(e)?_left[e]:_right[e]) ;
1741 ///Fast look-up of all arcs between given endpoints.
1743 ///This class is the same as \ref ArcLookUp, with the addition
1744 ///that it makes it possible to find all parallel arcs between given
1747 ///\warning This class is static, so you should call refresh() (or at
1748 ///least refresh(Node)) to refresh this data structure whenever the
1749 ///digraph changes. This is a time consuming (superlinearly proportional
1750 ///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs).
1752 ///\tparam GR The type of the underlying digraph.
1757 class AllArcLookUp : public ArcLookUp<GR>
1759 using ArcLookUp<GR>::_g;
1760 using ArcLookUp<GR>::_right;
1761 using ArcLookUp<GR>::_left;
1762 using ArcLookUp<GR>::_head;
1764 TEMPLATE_DIGRAPH_TYPEDEFS(GR);
1766 typename GR::template ArcMap<Arc> _next;
1768 Arc refreshNext(Arc head,Arc next=INVALID)
1770 if(head==INVALID) return next;
1772 next=refreshNext(_right[head],next);
1773 _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
1775 return refreshNext(_left[head],head);
1781 for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
1786 /// The Digraph type
1793 ///It builds up the search database, which remains valid until the digraph
1795 AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();}
1797 ///Refresh the data structure at a node.
1799 ///Build up the search database of node \c n.
1801 ///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is
1802 ///the number of the outgoing arcs of \c n.
1803 void refresh(Node n)
1805 ArcLookUp<GR>::refresh(n);
1806 refreshNext(_head[n]);
1809 ///Refresh the full data structure.
1811 ///Build up the full search database. In fact, it simply calls
1812 ///\ref refresh(Node) "refresh(n)" for each node \c n.
1814 ///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is
1815 ///the number of the arcs in the digraph and <em>D</em> is the maximum
1816 ///out-degree of the digraph.
1819 for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
1822 ///Find an arc between two nodes.
1824 ///Find an arc between two nodes.
1825 ///\param s The source node.
1826 ///\param t The target node.
1827 ///\param prev The previous arc between \c s and \c t. It it is INVALID or
1828 ///not given, the operator finds the first appropriate arc.
1829 ///\return An arc from \c s to \c t after \c prev or
1830 ///\ref INVALID if there is no more.
1832 ///For example, you can count the number of arcs from \c u to \c v in the
1835 ///AllArcLookUp<ListDigraph> ae(g);
1838 ///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++;
1841 ///Finding the first arc take <em>O</em>(log<em>d</em>) time,
1842 ///where <em>d</em> is the number of outgoing arcs of \c s. Then the
1843 ///consecutive arcs are found in constant time.
1845 ///\warning If you change the digraph, refresh() must be called before using
1846 ///this operator. If you change the outgoing arcs of
1847 ///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough.
1850 Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
1852 using ArcLookUp<GR>::operator() ;
1853 Arc operator()(Node s, Node t, Arc prev) const
1855 return prev==INVALID?(*this)(s,t):_next[prev];