Bfs/Dfs/Dijkstra and their deps ported from svn trung -r 3441.
1.1 --- a/lemon/Makefile.am Thu Feb 07 21:28:39 2008 +0000
1.2 +++ b/lemon/Makefile.am Thu Feb 07 21:37:07 2008 +0000
1.3 @@ -15,9 +15,13 @@
1.4 lemon_libemon_la_LDFLAGS = $(GLPK_LIBS) $(CPLEX_LIBS) $(SOPLEX_LIBS)
1.5
1.6 lemon_HEADERS += \
1.7 - lemon/concept_check.h \
1.8 + lemon/bfs.h \
1.9 + lemon/bin_heap.h \
1.10 + lemon/dfs.h \
1.11 + lemon/dijkstra.h \
1.12 lemon/dim2.h \
1.13 lemon/error.h \
1.14 + lemon/graph_utils.h \
1.15 lemon/list_graph.h \
1.16 lemon/maps.h \
1.17 lemon/path.h \
1.18 @@ -32,6 +36,7 @@
1.19 lemon/bits/graph_extender.h \
1.20 lemon/bits/invalid.h \
1.21 lemon/bits/map_extender.h \
1.22 + lemon/bits/path_dump.h \
1.23 lemon/bits/traits.h \
1.24 lemon/bits/utility.h \
1.25 lemon/bits/vector_map.h
1.26 @@ -40,6 +45,7 @@
1.27 lemon/concept_check.h \
1.28 lemon/concepts/digraph.h \
1.29 lemon/concepts/graph.h \
1.30 + lemon/concepts/heap.h \
1.31 lemon/concepts/maps.h \
1.32 lemon/concepts/path.h \
1.33 lemon/concepts/graph_components.h
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/lemon/bfs.h Thu Feb 07 21:37:07 2008 +0000
2.3 @@ -0,0 +1,1597 @@
2.4 +/* -*- C++ -*-
2.5 + *
2.6 + * This file is a part of LEMON, a generic C++ optimization library
2.7 + *
2.8 + * Copyright (C) 2003-2008
2.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
2.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
2.11 + *
2.12 + * Permission to use, modify and distribute this software is granted
2.13 + * provided that this copyright notice appears in all copies. For
2.14 + * precise terms see the accompanying LICENSE file.
2.15 + *
2.16 + * This software is provided "AS IS" with no warranty of any kind,
2.17 + * express or implied, and with no claim as to its suitability for any
2.18 + * purpose.
2.19 + *
2.20 + */
2.21 +
2.22 +#ifndef LEMON_BFS_H
2.23 +#define LEMON_BFS_H
2.24 +
2.25 +///\ingroup search
2.26 +///\file
2.27 +///\brief Bfs algorithm.
2.28 +
2.29 +#include <lemon/list_graph.h>
2.30 +#include <lemon/graph_utils.h>
2.31 +#include <lemon/bits/path_dump.h>
2.32 +#include <lemon/bits/invalid.h>
2.33 +#include <lemon/error.h>
2.34 +#include <lemon/maps.h>
2.35 +
2.36 +namespace lemon {
2.37 +
2.38 +
2.39 +
2.40 + ///Default traits class of Bfs class.
2.41 +
2.42 + ///Default traits class of Bfs class.
2.43 + ///\param GR Digraph type.
2.44 + template<class GR>
2.45 + struct BfsDefaultTraits
2.46 + {
2.47 + ///The digraph type the algorithm runs on.
2.48 + typedef GR Digraph;
2.49 + ///\brief The type of the map that stores the last
2.50 + ///arcs of the shortest paths.
2.51 + ///
2.52 + ///The type of the map that stores the last
2.53 + ///arcs of the shortest paths.
2.54 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
2.55 + ///
2.56 + typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap;
2.57 + ///Instantiates a PredMap.
2.58 +
2.59 + ///This function instantiates a \ref PredMap.
2.60 + ///\param G is the digraph, to which we would like to define the PredMap.
2.61 + ///\todo The digraph alone may be insufficient to initialize
2.62 + static PredMap *createPredMap(const GR &G)
2.63 + {
2.64 + return new PredMap(G);
2.65 + }
2.66 + ///The type of the map that indicates which nodes are processed.
2.67 +
2.68 + ///The type of the map that indicates which nodes are processed.
2.69 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
2.70 + ///\todo named parameter to set this type, function to read and write.
2.71 + typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
2.72 + ///Instantiates a ProcessedMap.
2.73 +
2.74 + ///This function instantiates a \ref ProcessedMap.
2.75 + ///\param g is the digraph, to which
2.76 + ///we would like to define the \ref ProcessedMap
2.77 +#ifdef DOXYGEN
2.78 + static ProcessedMap *createProcessedMap(const GR &g)
2.79 +#else
2.80 + static ProcessedMap *createProcessedMap(const GR &)
2.81 +#endif
2.82 + {
2.83 + return new ProcessedMap();
2.84 + }
2.85 + ///The type of the map that indicates which nodes are reached.
2.86 +
2.87 + ///The type of the map that indicates which nodes are reached.
2.88 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
2.89 + ///\todo named parameter to set this type, function to read and write.
2.90 + typedef typename Digraph::template NodeMap<bool> ReachedMap;
2.91 + ///Instantiates a ReachedMap.
2.92 +
2.93 + ///This function instantiates a \ref ReachedMap.
2.94 + ///\param G is the digraph, to which
2.95 + ///we would like to define the \ref ReachedMap.
2.96 + static ReachedMap *createReachedMap(const GR &G)
2.97 + {
2.98 + return new ReachedMap(G);
2.99 + }
2.100 + ///The type of the map that stores the dists of the nodes.
2.101 +
2.102 + ///The type of the map that stores the dists of the nodes.
2.103 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
2.104 + ///
2.105 + typedef typename Digraph::template NodeMap<int> DistMap;
2.106 + ///Instantiates a DistMap.
2.107 +
2.108 + ///This function instantiates a \ref DistMap.
2.109 + ///\param G is the digraph, to which we would like to define the \ref DistMap
2.110 + static DistMap *createDistMap(const GR &G)
2.111 + {
2.112 + return new DistMap(G);
2.113 + }
2.114 + };
2.115 +
2.116 + ///%BFS algorithm class.
2.117 +
2.118 + ///\ingroup search
2.119 + ///This class provides an efficient implementation of the %BFS algorithm.
2.120 + ///
2.121 + ///\param GR The digraph type the algorithm runs on. The default value is
2.122 + ///\ref ListDigraph. The value of GR is not used directly by Bfs, it
2.123 + ///is only passed to \ref BfsDefaultTraits.
2.124 + ///\param TR Traits class to set various data types used by the algorithm.
2.125 + ///The default traits class is
2.126 + ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
2.127 + ///See \ref BfsDefaultTraits for the documentation of
2.128 + ///a Bfs traits class.
2.129 + ///
2.130 + ///\author Alpar Juttner
2.131 +
2.132 +#ifdef DOXYGEN
2.133 + template <typename GR,
2.134 + typename TR>
2.135 +#else
2.136 + template <typename GR=ListDigraph,
2.137 + typename TR=BfsDefaultTraits<GR> >
2.138 +#endif
2.139 + class Bfs {
2.140 + public:
2.141 + /**
2.142 + * \brief \ref Exception for uninitialized parameters.
2.143 + *
2.144 + * This error represents problems in the initialization
2.145 + * of the parameters of the algorithms.
2.146 + */
2.147 + class UninitializedParameter : public lemon::UninitializedParameter {
2.148 + public:
2.149 + virtual const char* what() const throw() {
2.150 + return "lemon::Bfs::UninitializedParameter";
2.151 + }
2.152 + };
2.153 +
2.154 + typedef TR Traits;
2.155 + ///The type of the underlying digraph.
2.156 + typedef typename TR::Digraph Digraph;
2.157 +
2.158 + ///\brief The type of the map that stores the last
2.159 + ///arcs of the shortest paths.
2.160 + typedef typename TR::PredMap PredMap;
2.161 + ///The type of the map indicating which nodes are reached.
2.162 + typedef typename TR::ReachedMap ReachedMap;
2.163 + ///The type of the map indicating which nodes are processed.
2.164 + typedef typename TR::ProcessedMap ProcessedMap;
2.165 + ///The type of the map that stores the dists of the nodes.
2.166 + typedef typename TR::DistMap DistMap;
2.167 + private:
2.168 +
2.169 + typedef typename Digraph::Node Node;
2.170 + typedef typename Digraph::NodeIt NodeIt;
2.171 + typedef typename Digraph::Arc Arc;
2.172 + typedef typename Digraph::OutArcIt OutArcIt;
2.173 +
2.174 + /// Pointer to the underlying digraph.
2.175 + const Digraph *G;
2.176 + ///Pointer to the map of predecessors arcs.
2.177 + PredMap *_pred;
2.178 + ///Indicates if \ref _pred is locally allocated (\c true) or not.
2.179 + bool local_pred;
2.180 + ///Pointer to the map of distances.
2.181 + DistMap *_dist;
2.182 + ///Indicates if \ref _dist is locally allocated (\c true) or not.
2.183 + bool local_dist;
2.184 + ///Pointer to the map of reached status of the nodes.
2.185 + ReachedMap *_reached;
2.186 + ///Indicates if \ref _reached is locally allocated (\c true) or not.
2.187 + bool local_reached;
2.188 + ///Pointer to the map of processed status of the nodes.
2.189 + ProcessedMap *_processed;
2.190 + ///Indicates if \ref _processed is locally allocated (\c true) or not.
2.191 + bool local_processed;
2.192 +
2.193 + std::vector<typename Digraph::Node> _queue;
2.194 + int _queue_head,_queue_tail,_queue_next_dist;
2.195 + int _curr_dist;
2.196 +
2.197 + ///Creates the maps if necessary.
2.198 +
2.199 + ///\todo Better memory allocation (instead of new).
2.200 + void create_maps()
2.201 + {
2.202 + if(!_pred) {
2.203 + local_pred = true;
2.204 + _pred = Traits::createPredMap(*G);
2.205 + }
2.206 + if(!_dist) {
2.207 + local_dist = true;
2.208 + _dist = Traits::createDistMap(*G);
2.209 + }
2.210 + if(!_reached) {
2.211 + local_reached = true;
2.212 + _reached = Traits::createReachedMap(*G);
2.213 + }
2.214 + if(!_processed) {
2.215 + local_processed = true;
2.216 + _processed = Traits::createProcessedMap(*G);
2.217 + }
2.218 + }
2.219 +
2.220 + protected:
2.221 +
2.222 + Bfs() {}
2.223 +
2.224 + public:
2.225 +
2.226 + typedef Bfs Create;
2.227 +
2.228 + ///\name Named template parameters
2.229 +
2.230 + ///@{
2.231 +
2.232 + template <class T>
2.233 + struct DefPredMapTraits : public Traits {
2.234 + typedef T PredMap;
2.235 + static PredMap *createPredMap(const Digraph &)
2.236 + {
2.237 + throw UninitializedParameter();
2.238 + }
2.239 + };
2.240 + ///\brief \ref named-templ-param "Named parameter" for setting
2.241 + ///PredMap type
2.242 + ///
2.243 + ///\ref named-templ-param "Named parameter" for setting PredMap type
2.244 + ///
2.245 + template <class T>
2.246 + struct DefPredMap : public Bfs< Digraph, DefPredMapTraits<T> > {
2.247 + typedef Bfs< Digraph, DefPredMapTraits<T> > Create;
2.248 + };
2.249 +
2.250 + template <class T>
2.251 + struct DefDistMapTraits : public Traits {
2.252 + typedef T DistMap;
2.253 + static DistMap *createDistMap(const Digraph &)
2.254 + {
2.255 + throw UninitializedParameter();
2.256 + }
2.257 + };
2.258 + ///\brief \ref named-templ-param "Named parameter" for setting
2.259 + ///DistMap type
2.260 + ///
2.261 + ///\ref named-templ-param "Named parameter" for setting DistMap type
2.262 + ///
2.263 + template <class T>
2.264 + struct DefDistMap : public Bfs< Digraph, DefDistMapTraits<T> > {
2.265 + typedef Bfs< Digraph, DefDistMapTraits<T> > Create;
2.266 + };
2.267 +
2.268 + template <class T>
2.269 + struct DefReachedMapTraits : public Traits {
2.270 + typedef T ReachedMap;
2.271 + static ReachedMap *createReachedMap(const Digraph &)
2.272 + {
2.273 + throw UninitializedParameter();
2.274 + }
2.275 + };
2.276 + ///\brief \ref named-templ-param "Named parameter" for setting
2.277 + ///ReachedMap type
2.278 + ///
2.279 + ///\ref named-templ-param "Named parameter" for setting ReachedMap type
2.280 + ///
2.281 + template <class T>
2.282 + struct DefReachedMap : public Bfs< Digraph, DefReachedMapTraits<T> > {
2.283 + typedef Bfs< Digraph, DefReachedMapTraits<T> > Create;
2.284 + };
2.285 +
2.286 + template <class T>
2.287 + struct DefProcessedMapTraits : public Traits {
2.288 + typedef T ProcessedMap;
2.289 + static ProcessedMap *createProcessedMap(const Digraph &)
2.290 + {
2.291 + throw UninitializedParameter();
2.292 + }
2.293 + };
2.294 + ///\brief \ref named-templ-param "Named parameter" for setting
2.295 + ///ProcessedMap type
2.296 + ///
2.297 + ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
2.298 + ///
2.299 + template <class T>
2.300 + struct DefProcessedMap : public Bfs< Digraph, DefProcessedMapTraits<T> > {
2.301 + typedef Bfs< Digraph, DefProcessedMapTraits<T> > Create;
2.302 + };
2.303 +
2.304 + struct DefDigraphProcessedMapTraits : public Traits {
2.305 + typedef typename Digraph::template NodeMap<bool> ProcessedMap;
2.306 + static ProcessedMap *createProcessedMap(const Digraph &G)
2.307 + {
2.308 + return new ProcessedMap(G);
2.309 + }
2.310 + };
2.311 + ///\brief \ref named-templ-param "Named parameter"
2.312 + ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
2.313 + ///
2.314 + ///\ref named-templ-param "Named parameter"
2.315 + ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
2.316 + ///If you don't set it explicitly, it will be automatically allocated.
2.317 + template <class T>
2.318 + struct DefProcessedMapToBeDefaultMap :
2.319 + public Bfs< Digraph, DefDigraphProcessedMapTraits> {
2.320 + typedef Bfs< Digraph, DefDigraphProcessedMapTraits> Create;
2.321 + };
2.322 +
2.323 + ///@}
2.324 +
2.325 + public:
2.326 +
2.327 + ///Constructor.
2.328 +
2.329 + ///\param _G the digraph the algorithm will run on.
2.330 + ///
2.331 + Bfs(const Digraph& _G) :
2.332 + G(&_G),
2.333 + _pred(NULL), local_pred(false),
2.334 + _dist(NULL), local_dist(false),
2.335 + _reached(NULL), local_reached(false),
2.336 + _processed(NULL), local_processed(false)
2.337 + { }
2.338 +
2.339 + ///Destructor.
2.340 + ~Bfs()
2.341 + {
2.342 + if(local_pred) delete _pred;
2.343 + if(local_dist) delete _dist;
2.344 + if(local_reached) delete _reached;
2.345 + if(local_processed) delete _processed;
2.346 + }
2.347 +
2.348 + ///Sets the map storing the predecessor arcs.
2.349 +
2.350 + ///Sets the map storing the predecessor arcs.
2.351 + ///If you don't use this function before calling \ref run(),
2.352 + ///it will allocate one. The destructor deallocates this
2.353 + ///automatically allocated map, of course.
2.354 + ///\return <tt> (*this) </tt>
2.355 + Bfs &predMap(PredMap &m)
2.356 + {
2.357 + if(local_pred) {
2.358 + delete _pred;
2.359 + local_pred=false;
2.360 + }
2.361 + _pred = &m;
2.362 + return *this;
2.363 + }
2.364 +
2.365 + ///Sets the map indicating the reached nodes.
2.366 +
2.367 + ///Sets the map indicating the reached nodes.
2.368 + ///If you don't use this function before calling \ref run(),
2.369 + ///it will allocate one. The destructor deallocates this
2.370 + ///automatically allocated map, of course.
2.371 + ///\return <tt> (*this) </tt>
2.372 + Bfs &reachedMap(ReachedMap &m)
2.373 + {
2.374 + if(local_reached) {
2.375 + delete _reached;
2.376 + local_reached=false;
2.377 + }
2.378 + _reached = &m;
2.379 + return *this;
2.380 + }
2.381 +
2.382 + ///Sets the map indicating the processed nodes.
2.383 +
2.384 + ///Sets the map indicating the processed nodes.
2.385 + ///If you don't use this function before calling \ref run(),
2.386 + ///it will allocate one. The destructor deallocates this
2.387 + ///automatically allocated map, of course.
2.388 + ///\return <tt> (*this) </tt>
2.389 + Bfs &processedMap(ProcessedMap &m)
2.390 + {
2.391 + if(local_processed) {
2.392 + delete _processed;
2.393 + local_processed=false;
2.394 + }
2.395 + _processed = &m;
2.396 + return *this;
2.397 + }
2.398 +
2.399 + ///Sets the map storing the distances calculated by the algorithm.
2.400 +
2.401 + ///Sets the map storing the distances calculated by the algorithm.
2.402 + ///If you don't use this function before calling \ref run(),
2.403 + ///it will allocate one. The destructor deallocates this
2.404 + ///automatically allocated map, of course.
2.405 + ///\return <tt> (*this) </tt>
2.406 + Bfs &distMap(DistMap &m)
2.407 + {
2.408 + if(local_dist) {
2.409 + delete _dist;
2.410 + local_dist=false;
2.411 + }
2.412 + _dist = &m;
2.413 + return *this;
2.414 + }
2.415 +
2.416 + public:
2.417 + ///\name Execution control
2.418 + ///The simplest way to execute the algorithm is to use
2.419 + ///one of the member functions called \c run(...).
2.420 + ///\n
2.421 + ///If you need more control on the execution,
2.422 + ///first you must call \ref init(), then you can add several source nodes
2.423 + ///with \ref addSource().
2.424 + ///Finally \ref start() will perform the actual path
2.425 + ///computation.
2.426 +
2.427 + ///@{
2.428 +
2.429 + ///\brief Initializes the internal data structures.
2.430 + ///
2.431 + ///Initializes the internal data structures.
2.432 + ///
2.433 + void init()
2.434 + {
2.435 + create_maps();
2.436 + _queue.resize(countNodes(*G));
2.437 + _queue_head=_queue_tail=0;
2.438 + _curr_dist=1;
2.439 + for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
2.440 + _pred->set(u,INVALID);
2.441 + _reached->set(u,false);
2.442 + _processed->set(u,false);
2.443 + }
2.444 + }
2.445 +
2.446 + ///Adds a new source node.
2.447 +
2.448 + ///Adds a new source node to the set of nodes to be processed.
2.449 + ///
2.450 + void addSource(Node s)
2.451 + {
2.452 + if(!(*_reached)[s])
2.453 + {
2.454 + _reached->set(s,true);
2.455 + _pred->set(s,INVALID);
2.456 + _dist->set(s,0);
2.457 + _queue[_queue_head++]=s;
2.458 + _queue_next_dist=_queue_head;
2.459 + }
2.460 + }
2.461 +
2.462 + ///Processes the next node.
2.463 +
2.464 + ///Processes the next node.
2.465 + ///
2.466 + ///\return The processed node.
2.467 + ///
2.468 + ///\warning The queue must not be empty!
2.469 + Node processNextNode()
2.470 + {
2.471 + if(_queue_tail==_queue_next_dist) {
2.472 + _curr_dist++;
2.473 + _queue_next_dist=_queue_head;
2.474 + }
2.475 + Node n=_queue[_queue_tail++];
2.476 + _processed->set(n,true);
2.477 + Node m;
2.478 + for(OutArcIt e(*G,n);e!=INVALID;++e)
2.479 + if(!(*_reached)[m=G->target(e)]) {
2.480 + _queue[_queue_head++]=m;
2.481 + _reached->set(m,true);
2.482 + _pred->set(m,e);
2.483 + _dist->set(m,_curr_dist);
2.484 + }
2.485 + return n;
2.486 + }
2.487 +
2.488 + ///Processes the next node.
2.489 +
2.490 + ///Processes the next node. And checks that the given target node
2.491 + ///is reached. If the target node is reachable from the processed
2.492 + ///node then the reached parameter will be set true. The reached
2.493 + ///parameter should be initially false.
2.494 + ///
2.495 + ///\param target The target node.
2.496 + ///\retval reach Indicates that the target node is reached.
2.497 + ///\return The processed node.
2.498 + ///
2.499 + ///\warning The queue must not be empty!
2.500 + Node processNextNode(Node target, bool& reach)
2.501 + {
2.502 + if(_queue_tail==_queue_next_dist) {
2.503 + _curr_dist++;
2.504 + _queue_next_dist=_queue_head;
2.505 + }
2.506 + Node n=_queue[_queue_tail++];
2.507 + _processed->set(n,true);
2.508 + Node m;
2.509 + for(OutArcIt e(*G,n);e!=INVALID;++e)
2.510 + if(!(*_reached)[m=G->target(e)]) {
2.511 + _queue[_queue_head++]=m;
2.512 + _reached->set(m,true);
2.513 + _pred->set(m,e);
2.514 + _dist->set(m,_curr_dist);
2.515 + reach = reach || (target == m);
2.516 + }
2.517 + return n;
2.518 + }
2.519 +
2.520 + ///Processes the next node.
2.521 +
2.522 + ///Processes the next node. And checks that at least one of
2.523 + ///reached node has true value in the \c nm node map. If one node
2.524 + ///with true value is reachable from the processed node then the
2.525 + ///rnode parameter will be set to the first of such nodes.
2.526 + ///
2.527 + ///\param nm The node map of possible targets.
2.528 + ///\retval rnode The reached target node.
2.529 + ///\return The processed node.
2.530 + ///
2.531 + ///\warning The queue must not be empty!
2.532 + template<class NM>
2.533 + Node processNextNode(const NM& nm, Node& rnode)
2.534 + {
2.535 + if(_queue_tail==_queue_next_dist) {
2.536 + _curr_dist++;
2.537 + _queue_next_dist=_queue_head;
2.538 + }
2.539 + Node n=_queue[_queue_tail++];
2.540 + _processed->set(n,true);
2.541 + Node m;
2.542 + for(OutArcIt e(*G,n);e!=INVALID;++e)
2.543 + if(!(*_reached)[m=G->target(e)]) {
2.544 + _queue[_queue_head++]=m;
2.545 + _reached->set(m,true);
2.546 + _pred->set(m,e);
2.547 + _dist->set(m,_curr_dist);
2.548 + if (nm[m] && rnode == INVALID) rnode = m;
2.549 + }
2.550 + return n;
2.551 + }
2.552 +
2.553 + ///Next node to be processed.
2.554 +
2.555 + ///Next node to be processed.
2.556 + ///
2.557 + ///\return The next node to be processed or INVALID if the queue is
2.558 + /// empty.
2.559 + Node nextNode()
2.560 + {
2.561 + return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
2.562 + }
2.563 +
2.564 + ///\brief Returns \c false if there are nodes
2.565 + ///to be processed in the queue
2.566 + ///
2.567 + ///Returns \c false if there are nodes
2.568 + ///to be processed in the queue
2.569 + bool emptyQueue() { return _queue_tail==_queue_head; }
2.570 + ///Returns the number of the nodes to be processed.
2.571 +
2.572 + ///Returns the number of the nodes to be processed in the queue.
2.573 + int queueSize() { return _queue_head-_queue_tail; }
2.574 +
2.575 + ///Executes the algorithm.
2.576 +
2.577 + ///Executes the algorithm.
2.578 + ///
2.579 + ///\pre init() must be called and at least one node should be added
2.580 + ///with addSource() before using this function.
2.581 + ///
2.582 + ///This method runs the %BFS algorithm from the root node(s)
2.583 + ///in order to
2.584 + ///compute the
2.585 + ///shortest path to each node. The algorithm computes
2.586 + ///- The shortest path tree.
2.587 + ///- The distance of each node from the root(s).
2.588 + void start()
2.589 + {
2.590 + while ( !emptyQueue() ) processNextNode();
2.591 + }
2.592 +
2.593 + ///Executes the algorithm until \c dest is reached.
2.594 +
2.595 + ///Executes the algorithm until \c dest is reached.
2.596 + ///
2.597 + ///\pre init() must be called and at least one node should be added
2.598 + ///with addSource() before using this function.
2.599 + ///
2.600 + ///This method runs the %BFS algorithm from the root node(s)
2.601 + ///in order to compute the shortest path to \c dest.
2.602 + ///The algorithm computes
2.603 + ///- The shortest path to \c dest.
2.604 + ///- The distance of \c dest from the root(s).
2.605 + void start(Node dest)
2.606 + {
2.607 + bool reach = false;
2.608 + while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
2.609 + }
2.610 +
2.611 + ///Executes the algorithm until a condition is met.
2.612 +
2.613 + ///Executes the algorithm until a condition is met.
2.614 + ///
2.615 + ///\pre init() must be called and at least one node should be added
2.616 + ///with addSource() before using this function.
2.617 + ///
2.618 + ///\param nm must be a bool (or convertible) node map. The
2.619 + ///algorithm will stop when it reaches a node \c v with
2.620 + /// <tt>nm[v]</tt> true.
2.621 + ///
2.622 + ///\return The reached node \c v with <tt>nm[v]</tt> true or
2.623 + ///\c INVALID if no such node was found.
2.624 + template<class NM>
2.625 + Node start(const NM &nm)
2.626 + {
2.627 + Node rnode = INVALID;
2.628 + while ( !emptyQueue() && rnode == INVALID ) {
2.629 + processNextNode(nm, rnode);
2.630 + }
2.631 + return rnode;
2.632 + }
2.633 +
2.634 + ///Runs %BFS algorithm from node \c s.
2.635 +
2.636 + ///This method runs the %BFS algorithm from a root node \c s
2.637 + ///in order to
2.638 + ///compute the
2.639 + ///shortest path to each node. The algorithm computes
2.640 + ///- The shortest path tree.
2.641 + ///- The distance of each node from the root.
2.642 + ///
2.643 + ///\note b.run(s) is just a shortcut of the following code.
2.644 + ///\code
2.645 + /// b.init();
2.646 + /// b.addSource(s);
2.647 + /// b.start();
2.648 + ///\endcode
2.649 + void run(Node s) {
2.650 + init();
2.651 + addSource(s);
2.652 + start();
2.653 + }
2.654 +
2.655 + ///Finds the shortest path between \c s and \c t.
2.656 +
2.657 + ///Finds the shortest path between \c s and \c t.
2.658 + ///
2.659 + ///\return The length of the shortest s---t path if there exists one,
2.660 + ///0 otherwise.
2.661 + ///\note Apart from the return value, b.run(s) is
2.662 + ///just a shortcut of the following code.
2.663 + ///\code
2.664 + /// b.init();
2.665 + /// b.addSource(s);
2.666 + /// b.start(t);
2.667 + ///\endcode
2.668 + int run(Node s,Node t) {
2.669 + init();
2.670 + addSource(s);
2.671 + start(t);
2.672 + return reached(t) ? _curr_dist : 0;
2.673 + }
2.674 +
2.675 + ///@}
2.676 +
2.677 + ///\name Query Functions
2.678 + ///The result of the %BFS algorithm can be obtained using these
2.679 + ///functions.\n
2.680 + ///Before the use of these functions,
2.681 + ///either run() or start() must be calleb.
2.682 +
2.683 + ///@{
2.684 +
2.685 + typedef PredMapPath<Digraph, PredMap> Path;
2.686 +
2.687 + ///Gives back the shortest path.
2.688 +
2.689 + ///Gives back the shortest path.
2.690 + ///\pre The \c t should be reachable from the source.
2.691 + Path path(Node t)
2.692 + {
2.693 + return Path(*G, *_pred, t);
2.694 + }
2.695 +
2.696 + ///The distance of a node from the root(s).
2.697 +
2.698 + ///Returns the distance of a node from the root(s).
2.699 + ///\pre \ref run() must be called before using this function.
2.700 + ///\warning If node \c v in unreachable from the root(s) the return value
2.701 + ///of this function is undefined.
2.702 + int dist(Node v) const { return (*_dist)[v]; }
2.703 +
2.704 + ///Returns the 'previous arc' of the shortest path tree.
2.705 +
2.706 + ///For a node \c v it returns the 'previous arc'
2.707 + ///of the shortest path tree,
2.708 + ///i.e. it returns the last arc of a shortest path from the root(s) to \c
2.709 + ///v. It is \ref INVALID
2.710 + ///if \c v is unreachable from the root(s) or \c v is a root. The
2.711 + ///shortest path tree used here is equal to the shortest path tree used in
2.712 + ///\ref predNode().
2.713 + ///\pre Either \ref run() or \ref start() must be called before using
2.714 + ///this function.
2.715 + Arc predArc(Node v) const { return (*_pred)[v];}
2.716 +
2.717 + ///Returns the 'previous node' of the shortest path tree.
2.718 +
2.719 + ///For a node \c v it returns the 'previous node'
2.720 + ///of the shortest path tree,
2.721 + ///i.e. it returns the last but one node from a shortest path from the
2.722 + ///root(a) to \c /v.
2.723 + ///It is INVALID if \c v is unreachable from the root(s) or
2.724 + ///if \c v itself a root.
2.725 + ///The shortest path tree used here is equal to the shortest path
2.726 + ///tree used in \ref predArc().
2.727 + ///\pre Either \ref run() or \ref start() must be called before
2.728 + ///using this function.
2.729 + Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
2.730 + G->source((*_pred)[v]); }
2.731 +
2.732 + ///Returns a reference to the NodeMap of distances.
2.733 +
2.734 + ///Returns a reference to the NodeMap of distances.
2.735 + ///\pre Either \ref run() or \ref init() must
2.736 + ///be called before using this function.
2.737 + const DistMap &distMap() const { return *_dist;}
2.738 +
2.739 + ///Returns a reference to the shortest path tree map.
2.740 +
2.741 + ///Returns a reference to the NodeMap of the arcs of the
2.742 + ///shortest path tree.
2.743 + ///\pre Either \ref run() or \ref init()
2.744 + ///must be called before using this function.
2.745 + const PredMap &predMap() const { return *_pred;}
2.746 +
2.747 + ///Checks if a node is reachable from the root.
2.748 +
2.749 + ///Returns \c true if \c v is reachable from the root.
2.750 + ///\warning The source nodes are indicated as unreached.
2.751 + ///\pre Either \ref run() or \ref start()
2.752 + ///must be called before using this function.
2.753 + ///
2.754 + bool reached(Node v) { return (*_reached)[v]; }
2.755 +
2.756 + ///@}
2.757 + };
2.758 +
2.759 + ///Default traits class of Bfs function.
2.760 +
2.761 + ///Default traits class of Bfs function.
2.762 + ///\param GR Digraph type.
2.763 + template<class GR>
2.764 + struct BfsWizardDefaultTraits
2.765 + {
2.766 + ///The digraph type the algorithm runs on.
2.767 + typedef GR Digraph;
2.768 + ///\brief The type of the map that stores the last
2.769 + ///arcs of the shortest paths.
2.770 + ///
2.771 + ///The type of the map that stores the last
2.772 + ///arcs of the shortest paths.
2.773 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
2.774 + ///
2.775 + typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap;
2.776 + ///Instantiates a PredMap.
2.777 +
2.778 + ///This function instantiates a \ref PredMap.
2.779 + ///\param g is the digraph, to which we would like to define the PredMap.
2.780 + ///\todo The digraph alone may be insufficient to initialize
2.781 +#ifdef DOXYGEN
2.782 + static PredMap *createPredMap(const GR &g)
2.783 +#else
2.784 + static PredMap *createPredMap(const GR &)
2.785 +#endif
2.786 + {
2.787 + return new PredMap();
2.788 + }
2.789 +
2.790 + ///The type of the map that indicates which nodes are processed.
2.791 +
2.792 + ///The type of the map that indicates which nodes are processed.
2.793 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
2.794 + ///\todo named parameter to set this type, function to read and write.
2.795 + typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
2.796 + ///Instantiates a ProcessedMap.
2.797 +
2.798 + ///This function instantiates a \ref ProcessedMap.
2.799 + ///\param g is the digraph, to which
2.800 + ///we would like to define the \ref ProcessedMap
2.801 +#ifdef DOXYGEN
2.802 + static ProcessedMap *createProcessedMap(const GR &g)
2.803 +#else
2.804 + static ProcessedMap *createProcessedMap(const GR &)
2.805 +#endif
2.806 + {
2.807 + return new ProcessedMap();
2.808 + }
2.809 + ///The type of the map that indicates which nodes are reached.
2.810 +
2.811 + ///The type of the map that indicates which nodes are reached.
2.812 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
2.813 + ///\todo named parameter to set this type, function to read and write.
2.814 + typedef typename Digraph::template NodeMap<bool> ReachedMap;
2.815 + ///Instantiates a ReachedMap.
2.816 +
2.817 + ///This function instantiates a \ref ReachedMap.
2.818 + ///\param G is the digraph, to which
2.819 + ///we would like to define the \ref ReachedMap.
2.820 + static ReachedMap *createReachedMap(const GR &G)
2.821 + {
2.822 + return new ReachedMap(G);
2.823 + }
2.824 + ///The type of the map that stores the dists of the nodes.
2.825 +
2.826 + ///The type of the map that stores the dists of the nodes.
2.827 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
2.828 + ///
2.829 + typedef NullMap<typename Digraph::Node,int> DistMap;
2.830 + ///Instantiates a DistMap.
2.831 +
2.832 + ///This function instantiates a \ref DistMap.
2.833 + ///\param g is the digraph, to which we would like to define the \ref DistMap
2.834 +#ifdef DOXYGEN
2.835 + static DistMap *createDistMap(const GR &g)
2.836 +#else
2.837 + static DistMap *createDistMap(const GR &)
2.838 +#endif
2.839 + {
2.840 + return new DistMap();
2.841 + }
2.842 + };
2.843 +
2.844 + /// Default traits used by \ref BfsWizard
2.845 +
2.846 + /// To make it easier to use Bfs algorithm
2.847 + ///we have created a wizard class.
2.848 + /// This \ref BfsWizard class needs default traits,
2.849 + ///as well as the \ref Bfs class.
2.850 + /// The \ref BfsWizardBase is a class to be the default traits of the
2.851 + /// \ref BfsWizard class.
2.852 + template<class GR>
2.853 + class BfsWizardBase : public BfsWizardDefaultTraits<GR>
2.854 + {
2.855 +
2.856 + typedef BfsWizardDefaultTraits<GR> Base;
2.857 + protected:
2.858 + /// Type of the nodes in the digraph.
2.859 + typedef typename Base::Digraph::Node Node;
2.860 +
2.861 + /// Pointer to the underlying digraph.
2.862 + void *_g;
2.863 + ///Pointer to the map of reached nodes.
2.864 + void *_reached;
2.865 + ///Pointer to the map of processed nodes.
2.866 + void *_processed;
2.867 + ///Pointer to the map of predecessors arcs.
2.868 + void *_pred;
2.869 + ///Pointer to the map of distances.
2.870 + void *_dist;
2.871 + ///Pointer to the source node.
2.872 + Node _source;
2.873 +
2.874 + public:
2.875 + /// Constructor.
2.876 +
2.877 + /// This constructor does not require parameters, therefore it initiates
2.878 + /// all of the attributes to default values (0, INVALID).
2.879 + BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
2.880 + _dist(0), _source(INVALID) {}
2.881 +
2.882 + /// Constructor.
2.883 +
2.884 + /// This constructor requires some parameters,
2.885 + /// listed in the parameters list.
2.886 + /// Others are initiated to 0.
2.887 + /// \param g is the initial value of \ref _g
2.888 + /// \param s is the initial value of \ref _source
2.889 + BfsWizardBase(const GR &g, Node s=INVALID) :
2.890 + _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
2.891 + _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
2.892 +
2.893 + };
2.894 +
2.895 + /// A class to make the usage of Bfs algorithm easier
2.896 +
2.897 + /// This class is created to make it easier to use Bfs algorithm.
2.898 + /// It uses the functions and features of the plain \ref Bfs,
2.899 + /// but it is much simpler to use it.
2.900 + ///
2.901 + /// Simplicity means that the way to change the types defined
2.902 + /// in the traits class is based on functions that returns the new class
2.903 + /// and not on templatable built-in classes.
2.904 + /// When using the plain \ref Bfs
2.905 + /// the new class with the modified type comes from
2.906 + /// the original class by using the ::
2.907 + /// operator. In the case of \ref BfsWizard only
2.908 + /// a function have to be called and it will
2.909 + /// return the needed class.
2.910 + ///
2.911 + /// It does not have own \ref run method. When its \ref run method is called
2.912 + /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
2.913 + /// method of it.
2.914 + template<class TR>
2.915 + class BfsWizard : public TR
2.916 + {
2.917 + typedef TR Base;
2.918 +
2.919 + ///The type of the underlying digraph.
2.920 + typedef typename TR::Digraph Digraph;
2.921 + //\e
2.922 + typedef typename Digraph::Node Node;
2.923 + //\e
2.924 + typedef typename Digraph::NodeIt NodeIt;
2.925 + //\e
2.926 + typedef typename Digraph::Arc Arc;
2.927 + //\e
2.928 + typedef typename Digraph::OutArcIt OutArcIt;
2.929 +
2.930 + ///\brief The type of the map that stores
2.931 + ///the reached nodes
2.932 + typedef typename TR::ReachedMap ReachedMap;
2.933 + ///\brief The type of the map that stores
2.934 + ///the processed nodes
2.935 + typedef typename TR::ProcessedMap ProcessedMap;
2.936 + ///\brief The type of the map that stores the last
2.937 + ///arcs of the shortest paths.
2.938 + typedef typename TR::PredMap PredMap;
2.939 + ///The type of the map that stores the dists of the nodes.
2.940 + typedef typename TR::DistMap DistMap;
2.941 +
2.942 + public:
2.943 + /// Constructor.
2.944 + BfsWizard() : TR() {}
2.945 +
2.946 + /// Constructor that requires parameters.
2.947 +
2.948 + /// Constructor that requires parameters.
2.949 + /// These parameters will be the default values for the traits class.
2.950 + BfsWizard(const Digraph &g, Node s=INVALID) :
2.951 + TR(g,s) {}
2.952 +
2.953 + ///Copy constructor
2.954 + BfsWizard(const TR &b) : TR(b) {}
2.955 +
2.956 + ~BfsWizard() {}
2.957 +
2.958 + ///Runs Bfs algorithm from a given node.
2.959 +
2.960 + ///Runs Bfs algorithm from a given node.
2.961 + ///The node can be given by the \ref source function.
2.962 + void run()
2.963 + {
2.964 + if(Base::_source==INVALID) throw UninitializedParameter();
2.965 + Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
2.966 + if(Base::_reached)
2.967 + alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
2.968 + if(Base::_processed)
2.969 + alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
2.970 + if(Base::_pred)
2.971 + alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
2.972 + if(Base::_dist)
2.973 + alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
2.974 + alg.run(Base::_source);
2.975 + }
2.976 +
2.977 + ///Runs Bfs algorithm from the given node.
2.978 +
2.979 + ///Runs Bfs algorithm from the given node.
2.980 + ///\param s is the given source.
2.981 + void run(Node s)
2.982 + {
2.983 + Base::_source=s;
2.984 + run();
2.985 + }
2.986 +
2.987 + template<class T>
2.988 + struct DefPredMapBase : public Base {
2.989 + typedef T PredMap;
2.990 + static PredMap *createPredMap(const Digraph &) { return 0; };
2.991 + DefPredMapBase(const TR &b) : TR(b) {}
2.992 + };
2.993 +
2.994 + ///\brief \ref named-templ-param "Named parameter"
2.995 + ///function for setting PredMap
2.996 + ///
2.997 + /// \ref named-templ-param "Named parameter"
2.998 + ///function for setting PredMap
2.999 + ///
2.1000 + template<class T>
2.1001 + BfsWizard<DefPredMapBase<T> > predMap(const T &t)
2.1002 + {
2.1003 + Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
2.1004 + return BfsWizard<DefPredMapBase<T> >(*this);
2.1005 + }
2.1006 +
2.1007 +
2.1008 + template<class T>
2.1009 + struct DefReachedMapBase : public Base {
2.1010 + typedef T ReachedMap;
2.1011 + static ReachedMap *createReachedMap(const Digraph &) { return 0; };
2.1012 + DefReachedMapBase(const TR &b) : TR(b) {}
2.1013 + };
2.1014 +
2.1015 + ///\brief \ref named-templ-param "Named parameter"
2.1016 + ///function for setting ReachedMap
2.1017 + ///
2.1018 + /// \ref named-templ-param "Named parameter"
2.1019 + ///function for setting ReachedMap
2.1020 + ///
2.1021 + template<class T>
2.1022 + BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
2.1023 + {
2.1024 + Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
2.1025 + return BfsWizard<DefReachedMapBase<T> >(*this);
2.1026 + }
2.1027 +
2.1028 +
2.1029 + template<class T>
2.1030 + struct DefProcessedMapBase : public Base {
2.1031 + typedef T ProcessedMap;
2.1032 + static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
2.1033 + DefProcessedMapBase(const TR &b) : TR(b) {}
2.1034 + };
2.1035 +
2.1036 + ///\brief \ref named-templ-param "Named parameter"
2.1037 + ///function for setting ProcessedMap
2.1038 + ///
2.1039 + /// \ref named-templ-param "Named parameter"
2.1040 + ///function for setting ProcessedMap
2.1041 + ///
2.1042 + template<class T>
2.1043 + BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
2.1044 + {
2.1045 + Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
2.1046 + return BfsWizard<DefProcessedMapBase<T> >(*this);
2.1047 + }
2.1048 +
2.1049 +
2.1050 + template<class T>
2.1051 + struct DefDistMapBase : public Base {
2.1052 + typedef T DistMap;
2.1053 + static DistMap *createDistMap(const Digraph &) { return 0; };
2.1054 + DefDistMapBase(const TR &b) : TR(b) {}
2.1055 + };
2.1056 +
2.1057 + ///\brief \ref named-templ-param "Named parameter"
2.1058 + ///function for setting DistMap type
2.1059 + ///
2.1060 + /// \ref named-templ-param "Named parameter"
2.1061 + ///function for setting DistMap type
2.1062 + ///
2.1063 + template<class T>
2.1064 + BfsWizard<DefDistMapBase<T> > distMap(const T &t)
2.1065 + {
2.1066 + Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
2.1067 + return BfsWizard<DefDistMapBase<T> >(*this);
2.1068 + }
2.1069 +
2.1070 + /// Sets the source node, from which the Bfs algorithm runs.
2.1071 +
2.1072 + /// Sets the source node, from which the Bfs algorithm runs.
2.1073 + /// \param s is the source node.
2.1074 + BfsWizard<TR> &source(Node s)
2.1075 + {
2.1076 + Base::_source=s;
2.1077 + return *this;
2.1078 + }
2.1079 +
2.1080 + };
2.1081 +
2.1082 + ///Function type interface for Bfs algorithm.
2.1083 +
2.1084 + /// \ingroup search
2.1085 + ///Function type interface for Bfs algorithm.
2.1086 + ///
2.1087 + ///This function also has several
2.1088 + ///\ref named-templ-func-param "named parameters",
2.1089 + ///they are declared as the members of class \ref BfsWizard.
2.1090 + ///The following
2.1091 + ///example shows how to use these parameters.
2.1092 + ///\code
2.1093 + /// bfs(g,source).predMap(preds).run();
2.1094 + ///\endcode
2.1095 + ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
2.1096 + ///to the end of the parameter list.
2.1097 + ///\sa BfsWizard
2.1098 + ///\sa Bfs
2.1099 + template<class GR>
2.1100 + BfsWizard<BfsWizardBase<GR> >
2.1101 + bfs(const GR &g,typename GR::Node s=INVALID)
2.1102 + {
2.1103 + return BfsWizard<BfsWizardBase<GR> >(g,s);
2.1104 + }
2.1105 +
2.1106 +#ifdef DOXYGEN
2.1107 + /// \brief Visitor class for bfs.
2.1108 + ///
2.1109 + /// This class defines the interface of the BfsVisit events, and
2.1110 + /// it could be the base of a real Visitor class.
2.1111 + template <typename _Digraph>
2.1112 + struct BfsVisitor {
2.1113 + typedef _Digraph Digraph;
2.1114 + typedef typename Digraph::Arc Arc;
2.1115 + typedef typename Digraph::Node Node;
2.1116 + /// \brief Called when the arc reach a node.
2.1117 + ///
2.1118 + /// It is called when the bfs find an arc which target is not
2.1119 + /// reached yet.
2.1120 + void discover(const Arc& arc) {}
2.1121 + /// \brief Called when the node reached first time.
2.1122 + ///
2.1123 + /// It is Called when the node reached first time.
2.1124 + void reach(const Node& node) {}
2.1125 + /// \brief Called when the arc examined but target of the arc
2.1126 + /// already discovered.
2.1127 + ///
2.1128 + /// It called when the arc examined but the target of the arc
2.1129 + /// already discovered.
2.1130 + void examine(const Arc& arc) {}
2.1131 + /// \brief Called for the source node of the bfs.
2.1132 + ///
2.1133 + /// It is called for the source node of the bfs.
2.1134 + void start(const Node& node) {}
2.1135 + /// \brief Called when the node processed.
2.1136 + ///
2.1137 + /// It is Called when the node processed.
2.1138 + void process(const Node& node) {}
2.1139 + };
2.1140 +#else
2.1141 + template <typename _Digraph>
2.1142 + struct BfsVisitor {
2.1143 + typedef _Digraph Digraph;
2.1144 + typedef typename Digraph::Arc Arc;
2.1145 + typedef typename Digraph::Node Node;
2.1146 + void discover(const Arc&) {}
2.1147 + void reach(const Node&) {}
2.1148 + void examine(const Arc&) {}
2.1149 + void start(const Node&) {}
2.1150 + void process(const Node&) {}
2.1151 +
2.1152 + template <typename _Visitor>
2.1153 + struct Constraints {
2.1154 + void constraints() {
2.1155 + Arc arc;
2.1156 + Node node;
2.1157 + visitor.discover(arc);
2.1158 + visitor.reach(node);
2.1159 + visitor.examine(arc);
2.1160 + visitor.start(node);
2.1161 + visitor.process(node);
2.1162 + }
2.1163 + _Visitor& visitor;
2.1164 + };
2.1165 + };
2.1166 +#endif
2.1167 +
2.1168 + /// \brief Default traits class of BfsVisit class.
2.1169 + ///
2.1170 + /// Default traits class of BfsVisit class.
2.1171 + /// \param _Digraph Digraph type.
2.1172 + template<class _Digraph>
2.1173 + struct BfsVisitDefaultTraits {
2.1174 +
2.1175 + /// \brief The digraph type the algorithm runs on.
2.1176 + typedef _Digraph Digraph;
2.1177 +
2.1178 + /// \brief The type of the map that indicates which nodes are reached.
2.1179 + ///
2.1180 + /// The type of the map that indicates which nodes are reached.
2.1181 + /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
2.1182 + /// \todo named parameter to set this type, function to read and write.
2.1183 + typedef typename Digraph::template NodeMap<bool> ReachedMap;
2.1184 +
2.1185 + /// \brief Instantiates a ReachedMap.
2.1186 + ///
2.1187 + /// This function instantiates a \ref ReachedMap.
2.1188 + /// \param digraph is the digraph, to which
2.1189 + /// we would like to define the \ref ReachedMap.
2.1190 + static ReachedMap *createReachedMap(const Digraph &digraph) {
2.1191 + return new ReachedMap(digraph);
2.1192 + }
2.1193 +
2.1194 + };
2.1195 +
2.1196 + /// \ingroup search
2.1197 + ///
2.1198 + /// \brief %BFS Visit algorithm class.
2.1199 + ///
2.1200 + /// This class provides an efficient implementation of the %BFS algorithm
2.1201 + /// with visitor interface.
2.1202 + ///
2.1203 + /// The %BfsVisit class provides an alternative interface to the Bfs
2.1204 + /// class. It works with callback mechanism, the BfsVisit object calls
2.1205 + /// on every bfs event the \c Visitor class member functions.
2.1206 + ///
2.1207 + /// \param _Digraph The digraph type the algorithm runs on. The default value is
2.1208 + /// \ref ListDigraph. The value of _Digraph is not used directly by Bfs, it
2.1209 + /// is only passed to \ref BfsDefaultTraits.
2.1210 + /// \param _Visitor The Visitor object for the algorithm. The
2.1211 + /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty Visitor which
2.1212 + /// does not observe the Bfs events. If you want to observe the bfs
2.1213 + /// events you should implement your own Visitor class.
2.1214 + /// \param _Traits Traits class to set various data types used by the
2.1215 + /// algorithm. The default traits class is
2.1216 + /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
2.1217 + /// See \ref BfsVisitDefaultTraits for the documentation of
2.1218 + /// a Bfs visit traits class.
2.1219 + ///
2.1220 + /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
2.1221 +#ifdef DOXYGEN
2.1222 + template <typename _Digraph, typename _Visitor, typename _Traits>
2.1223 +#else
2.1224 + template <typename _Digraph = ListDigraph,
2.1225 + typename _Visitor = BfsVisitor<_Digraph>,
2.1226 + typename _Traits = BfsDefaultTraits<_Digraph> >
2.1227 +#endif
2.1228 + class BfsVisit {
2.1229 + public:
2.1230 +
2.1231 + /// \brief \ref Exception for uninitialized parameters.
2.1232 + ///
2.1233 + /// This error represents problems in the initialization
2.1234 + /// of the parameters of the algorithms.
2.1235 + class UninitializedParameter : public lemon::UninitializedParameter {
2.1236 + public:
2.1237 + virtual const char* what() const throw()
2.1238 + {
2.1239 + return "lemon::BfsVisit::UninitializedParameter";
2.1240 + }
2.1241 + };
2.1242 +
2.1243 + typedef _Traits Traits;
2.1244 +
2.1245 + typedef typename Traits::Digraph Digraph;
2.1246 +
2.1247 + typedef _Visitor Visitor;
2.1248 +
2.1249 + ///The type of the map indicating which nodes are reached.
2.1250 + typedef typename Traits::ReachedMap ReachedMap;
2.1251 +
2.1252 + private:
2.1253 +
2.1254 + typedef typename Digraph::Node Node;
2.1255 + typedef typename Digraph::NodeIt NodeIt;
2.1256 + typedef typename Digraph::Arc Arc;
2.1257 + typedef typename Digraph::OutArcIt OutArcIt;
2.1258 +
2.1259 + /// Pointer to the underlying digraph.
2.1260 + const Digraph *_digraph;
2.1261 + /// Pointer to the visitor object.
2.1262 + Visitor *_visitor;
2.1263 + ///Pointer to the map of reached status of the nodes.
2.1264 + ReachedMap *_reached;
2.1265 + ///Indicates if \ref _reached is locally allocated (\c true) or not.
2.1266 + bool local_reached;
2.1267 +
2.1268 + std::vector<typename Digraph::Node> _list;
2.1269 + int _list_front, _list_back;
2.1270 +
2.1271 + /// \brief Creates the maps if necessary.
2.1272 + ///
2.1273 + /// Creates the maps if necessary.
2.1274 + void create_maps() {
2.1275 + if(!_reached) {
2.1276 + local_reached = true;
2.1277 + _reached = Traits::createReachedMap(*_digraph);
2.1278 + }
2.1279 + }
2.1280 +
2.1281 + protected:
2.1282 +
2.1283 + BfsVisit() {}
2.1284 +
2.1285 + public:
2.1286 +
2.1287 + typedef BfsVisit Create;
2.1288 +
2.1289 + /// \name Named template parameters
2.1290 +
2.1291 + ///@{
2.1292 + template <class T>
2.1293 + struct DefReachedMapTraits : public Traits {
2.1294 + typedef T ReachedMap;
2.1295 + static ReachedMap *createReachedMap(const Digraph &digraph) {
2.1296 + throw UninitializedParameter();
2.1297 + }
2.1298 + };
2.1299 + /// \brief \ref named-templ-param "Named parameter" for setting
2.1300 + /// ReachedMap type
2.1301 + ///
2.1302 + /// \ref named-templ-param "Named parameter" for setting ReachedMap type
2.1303 + template <class T>
2.1304 + struct DefReachedMap : public BfsVisit< Digraph, Visitor,
2.1305 + DefReachedMapTraits<T> > {
2.1306 + typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;
2.1307 + };
2.1308 + ///@}
2.1309 +
2.1310 + public:
2.1311 +
2.1312 + /// \brief Constructor.
2.1313 + ///
2.1314 + /// Constructor.
2.1315 + ///
2.1316 + /// \param digraph the digraph the algorithm will run on.
2.1317 + /// \param visitor The visitor of the algorithm.
2.1318 + ///
2.1319 + BfsVisit(const Digraph& digraph, Visitor& visitor)
2.1320 + : _digraph(&digraph), _visitor(&visitor),
2.1321 + _reached(0), local_reached(false) {}
2.1322 +
2.1323 + /// \brief Destructor.
2.1324 + ///
2.1325 + /// Destructor.
2.1326 + ~BfsVisit() {
2.1327 + if(local_reached) delete _reached;
2.1328 + }
2.1329 +
2.1330 + /// \brief Sets the map indicating if a node is reached.
2.1331 + ///
2.1332 + /// Sets the map indicating if a node is reached.
2.1333 + /// If you don't use this function before calling \ref run(),
2.1334 + /// it will allocate one. The destuctor deallocates this
2.1335 + /// automatically allocated map, of course.
2.1336 + /// \return <tt> (*this) </tt>
2.1337 + BfsVisit &reachedMap(ReachedMap &m) {
2.1338 + if(local_reached) {
2.1339 + delete _reached;
2.1340 + local_reached = false;
2.1341 + }
2.1342 + _reached = &m;
2.1343 + return *this;
2.1344 + }
2.1345 +
2.1346 + public:
2.1347 + /// \name Execution control
2.1348 + /// The simplest way to execute the algorithm is to use
2.1349 + /// one of the member functions called \c run(...).
2.1350 + /// \n
2.1351 + /// If you need more control on the execution,
2.1352 + /// first you must call \ref init(), then you can adda source node
2.1353 + /// with \ref addSource().
2.1354 + /// Finally \ref start() will perform the actual path
2.1355 + /// computation.
2.1356 +
2.1357 + /// @{
2.1358 + /// \brief Initializes the internal data structures.
2.1359 + ///
2.1360 + /// Initializes the internal data structures.
2.1361 + ///
2.1362 + void init() {
2.1363 + create_maps();
2.1364 + _list.resize(countNodes(*_digraph));
2.1365 + _list_front = _list_back = -1;
2.1366 + for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
2.1367 + _reached->set(u, false);
2.1368 + }
2.1369 + }
2.1370 +
2.1371 + /// \brief Adds a new source node.
2.1372 + ///
2.1373 + /// Adds a new source node to the set of nodes to be processed.
2.1374 + void addSource(Node s) {
2.1375 + if(!(*_reached)[s]) {
2.1376 + _reached->set(s,true);
2.1377 + _visitor->start(s);
2.1378 + _visitor->reach(s);
2.1379 + _list[++_list_back] = s;
2.1380 + }
2.1381 + }
2.1382 +
2.1383 + /// \brief Processes the next node.
2.1384 + ///
2.1385 + /// Processes the next node.
2.1386 + ///
2.1387 + /// \return The processed node.
2.1388 + ///
2.1389 + /// \pre The queue must not be empty!
2.1390 + Node processNextNode() {
2.1391 + Node n = _list[++_list_front];
2.1392 + _visitor->process(n);
2.1393 + Arc e;
2.1394 + for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
2.1395 + Node m = _digraph->target(e);
2.1396 + if (!(*_reached)[m]) {
2.1397 + _visitor->discover(e);
2.1398 + _visitor->reach(m);
2.1399 + _reached->set(m, true);
2.1400 + _list[++_list_back] = m;
2.1401 + } else {
2.1402 + _visitor->examine(e);
2.1403 + }
2.1404 + }
2.1405 + return n;
2.1406 + }
2.1407 +
2.1408 + /// \brief Processes the next node.
2.1409 + ///
2.1410 + /// Processes the next node. And checks that the given target node
2.1411 + /// is reached. If the target node is reachable from the processed
2.1412 + /// node then the reached parameter will be set true. The reached
2.1413 + /// parameter should be initially false.
2.1414 + ///
2.1415 + /// \param target The target node.
2.1416 + /// \retval reach Indicates that the target node is reached.
2.1417 + /// \return The processed node.
2.1418 + ///
2.1419 + /// \warning The queue must not be empty!
2.1420 + Node processNextNode(Node target, bool& reach) {
2.1421 + Node n = _list[++_list_front];
2.1422 + _visitor->process(n);
2.1423 + Arc e;
2.1424 + for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
2.1425 + Node m = _digraph->target(e);
2.1426 + if (!(*_reached)[m]) {
2.1427 + _visitor->discover(e);
2.1428 + _visitor->reach(m);
2.1429 + _reached->set(m, true);
2.1430 + _list[++_list_back] = m;
2.1431 + reach = reach || (target == m);
2.1432 + } else {
2.1433 + _visitor->examine(e);
2.1434 + }
2.1435 + }
2.1436 + return n;
2.1437 + }
2.1438 +
2.1439 + /// \brief Processes the next node.
2.1440 + ///
2.1441 + /// Processes the next node. And checks that at least one of
2.1442 + /// reached node has true value in the \c nm node map. If one node
2.1443 + /// with true value is reachable from the processed node then the
2.1444 + /// rnode parameter will be set to the first of such nodes.
2.1445 + ///
2.1446 + /// \param nm The node map of possible targets.
2.1447 + /// \retval rnode The reached target node.
2.1448 + /// \return The processed node.
2.1449 + ///
2.1450 + /// \warning The queue must not be empty!
2.1451 + template <typename NM>
2.1452 + Node processNextNode(const NM& nm, Node& rnode) {
2.1453 + Node n = _list[++_list_front];
2.1454 + _visitor->process(n);
2.1455 + Arc e;
2.1456 + for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
2.1457 + Node m = _digraph->target(e);
2.1458 + if (!(*_reached)[m]) {
2.1459 + _visitor->discover(e);
2.1460 + _visitor->reach(m);
2.1461 + _reached->set(m, true);
2.1462 + _list[++_list_back] = m;
2.1463 + if (nm[m] && rnode == INVALID) rnode = m;
2.1464 + } else {
2.1465 + _visitor->examine(e);
2.1466 + }
2.1467 + }
2.1468 + return n;
2.1469 + }
2.1470 +
2.1471 + /// \brief Next node to be processed.
2.1472 + ///
2.1473 + /// Next node to be processed.
2.1474 + ///
2.1475 + /// \return The next node to be processed or INVALID if the stack is
2.1476 + /// empty.
2.1477 + Node nextNode() {
2.1478 + return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
2.1479 + }
2.1480 +
2.1481 + /// \brief Returns \c false if there are nodes
2.1482 + /// to be processed in the queue
2.1483 + ///
2.1484 + /// Returns \c false if there are nodes
2.1485 + /// to be processed in the queue
2.1486 + bool emptyQueue() { return _list_front == _list_back; }
2.1487 +
2.1488 + /// \brief Returns the number of the nodes to be processed.
2.1489 + ///
2.1490 + /// Returns the number of the nodes to be processed in the queue.
2.1491 + int queueSize() { return _list_back - _list_front; }
2.1492 +
2.1493 + /// \brief Executes the algorithm.
2.1494 + ///
2.1495 + /// Executes the algorithm.
2.1496 + ///
2.1497 + /// \pre init() must be called and at least one node should be added
2.1498 + /// with addSource() before using this function.
2.1499 + void start() {
2.1500 + while ( !emptyQueue() ) processNextNode();
2.1501 + }
2.1502 +
2.1503 + /// \brief Executes the algorithm until \c dest is reached.
2.1504 + ///
2.1505 + /// Executes the algorithm until \c dest is reached.
2.1506 + ///
2.1507 + /// \pre init() must be called and at least one node should be added
2.1508 + /// with addSource() before using this function.
2.1509 + void start(Node dest) {
2.1510 + bool reach = false;
2.1511 + while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
2.1512 + }
2.1513 +
2.1514 + /// \brief Executes the algorithm until a condition is met.
2.1515 + ///
2.1516 + /// Executes the algorithm until a condition is met.
2.1517 + ///
2.1518 + /// \pre init() must be called and at least one node should be added
2.1519 + /// with addSource() before using this function.
2.1520 + ///
2.1521 + ///\param nm must be a bool (or convertible) node map. The
2.1522 + ///algorithm will stop when it reaches a node \c v with
2.1523 + /// <tt>nm[v]</tt> true.
2.1524 + ///
2.1525 + ///\return The reached node \c v with <tt>nm[v]</tt> true or
2.1526 + ///\c INVALID if no such node was found.
2.1527 + template <typename NM>
2.1528 + Node start(const NM &nm) {
2.1529 + Node rnode = INVALID;
2.1530 + while ( !emptyQueue() && rnode == INVALID ) {
2.1531 + processNextNode(nm, rnode);
2.1532 + }
2.1533 + return rnode;
2.1534 + }
2.1535 +
2.1536 + /// \brief Runs %BFSVisit algorithm from node \c s.
2.1537 + ///
2.1538 + /// This method runs the %BFS algorithm from a root node \c s.
2.1539 + /// \note b.run(s) is just a shortcut of the following code.
2.1540 + ///\code
2.1541 + /// b.init();
2.1542 + /// b.addSource(s);
2.1543 + /// b.start();
2.1544 + ///\endcode
2.1545 + void run(Node s) {
2.1546 + init();
2.1547 + addSource(s);
2.1548 + start();
2.1549 + }
2.1550 +
2.1551 + /// \brief Runs %BFSVisit algorithm to visit all nodes in the digraph.
2.1552 + ///
2.1553 + /// This method runs the %BFS algorithm in order to
2.1554 + /// compute the %BFS path to each node. The algorithm computes
2.1555 + /// - The %BFS tree.
2.1556 + /// - The distance of each node from the root in the %BFS tree.
2.1557 + ///
2.1558 + ///\note b.run() is just a shortcut of the following code.
2.1559 + ///\code
2.1560 + /// b.init();
2.1561 + /// for (NodeIt it(digraph); it != INVALID; ++it) {
2.1562 + /// if (!b.reached(it)) {
2.1563 + /// b.addSource(it);
2.1564 + /// b.start();
2.1565 + /// }
2.1566 + /// }
2.1567 + ///\endcode
2.1568 + void run() {
2.1569 + init();
2.1570 + for (NodeIt it(*_digraph); it != INVALID; ++it) {
2.1571 + if (!reached(it)) {
2.1572 + addSource(it);
2.1573 + start();
2.1574 + }
2.1575 + }
2.1576 + }
2.1577 + ///@}
2.1578 +
2.1579 + /// \name Query Functions
2.1580 + /// The result of the %BFS algorithm can be obtained using these
2.1581 + /// functions.\n
2.1582 + /// Before the use of these functions,
2.1583 + /// either run() or start() must be called.
2.1584 + ///@{
2.1585 +
2.1586 + /// \brief Checks if a node is reachable from the root.
2.1587 + ///
2.1588 + /// Returns \c true if \c v is reachable from the root(s).
2.1589 + /// \warning The source nodes are inditated as unreachable.
2.1590 + /// \pre Either \ref run() or \ref start()
2.1591 + /// must be called before using this function.
2.1592 + ///
2.1593 + bool reached(Node v) { return (*_reached)[v]; }
2.1594 + ///@}
2.1595 + };
2.1596 +
2.1597 +} //END OF NAMESPACE LEMON
2.1598 +
2.1599 +#endif
2.1600 +
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/lemon/bin_heap.h Thu Feb 07 21:37:07 2008 +0000
3.3 @@ -0,0 +1,346 @@
3.4 +/* -*- C++ -*-
3.5 + *
3.6 + * This file is a part of LEMON, a generic C++ optimization library
3.7 + *
3.8 + * Copyright (C) 2003-2008
3.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
3.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
3.11 + *
3.12 + * Permission to use, modify and distribute this software is granted
3.13 + * provided that this copyright notice appears in all copies. For
3.14 + * precise terms see the accompanying LICENSE file.
3.15 + *
3.16 + * This software is provided "AS IS" with no warranty of any kind,
3.17 + * express or implied, and with no claim as to its suitability for any
3.18 + * purpose.
3.19 + *
3.20 + */
3.21 +
3.22 +#ifndef LEMON_BIN_HEAP_H
3.23 +#define LEMON_BIN_HEAP_H
3.24 +
3.25 +///\ingroup auxdat
3.26 +///\file
3.27 +///\brief Binary Heap implementation.
3.28 +
3.29 +#include <vector>
3.30 +#include <utility>
3.31 +#include <functional>
3.32 +
3.33 +namespace lemon {
3.34 +
3.35 + ///\ingroup auxdat
3.36 + ///
3.37 + ///\brief A Binary Heap implementation.
3.38 + ///
3.39 + ///This class implements the \e binary \e heap data structure. A \e heap
3.40 + ///is a data structure for storing items with specified values called \e
3.41 + ///priorities in such a way that finding the item with minimum priority is
3.42 + ///efficient. \c Compare specifies the ordering of the priorities. In a heap
3.43 + ///one can change the priority of an item, add or erase an item, etc.
3.44 + ///
3.45 + ///\param _Prio Type of the priority of the items.
3.46 + ///\param _ItemIntMap A read and writable Item int map, used internally
3.47 + ///to handle the cross references.
3.48 + ///\param _Compare A class for the ordering of the priorities. The
3.49 + ///default is \c std::less<_Prio>.
3.50 + ///
3.51 + ///\sa FibHeap
3.52 + ///\sa Dijkstra
3.53 + template <typename _Prio, typename _ItemIntMap,
3.54 + typename _Compare = std::less<_Prio> >
3.55 + class BinHeap {
3.56 +
3.57 + public:
3.58 + ///\e
3.59 + typedef _ItemIntMap ItemIntMap;
3.60 + ///\e
3.61 + typedef _Prio Prio;
3.62 + ///\e
3.63 + typedef typename ItemIntMap::Key Item;
3.64 + ///\e
3.65 + typedef std::pair<Item,Prio> Pair;
3.66 + ///\e
3.67 + typedef _Compare Compare;
3.68 +
3.69 + /// \brief Type to represent the items states.
3.70 + ///
3.71 + /// Each Item element have a state associated to it. It may be "in heap",
3.72 + /// "pre heap" or "post heap". The latter two are indifferent from the
3.73 + /// heap's point of view, but may be useful to the user.
3.74 + ///
3.75 + /// The ItemIntMap \e should be initialized in such way that it maps
3.76 + /// PRE_HEAP (-1) to any element to be put in the heap...
3.77 + enum State {
3.78 + IN_HEAP = 0,
3.79 + PRE_HEAP = -1,
3.80 + POST_HEAP = -2
3.81 + };
3.82 +
3.83 + private:
3.84 + std::vector<Pair> data;
3.85 + Compare comp;
3.86 + ItemIntMap &iim;
3.87 +
3.88 + public:
3.89 + /// \brief The constructor.
3.90 + ///
3.91 + /// The constructor.
3.92 + /// \param _iim should be given to the constructor, since it is used
3.93 + /// internally to handle the cross references. The value of the map
3.94 + /// should be PRE_HEAP (-1) for each element.
3.95 + explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
3.96 +
3.97 + /// \brief The constructor.
3.98 + ///
3.99 + /// The constructor.
3.100 + /// \param _iim should be given to the constructor, since it is used
3.101 + /// internally to handle the cross references. The value of the map
3.102 + /// should be PRE_HEAP (-1) for each element.
3.103 + ///
3.104 + /// \param _comp The comparator function object.
3.105 + BinHeap(ItemIntMap &_iim, const Compare &_comp)
3.106 + : iim(_iim), comp(_comp) {}
3.107 +
3.108 +
3.109 + /// The number of items stored in the heap.
3.110 + ///
3.111 + /// \brief Returns the number of items stored in the heap.
3.112 + int size() const { return data.size(); }
3.113 +
3.114 + /// \brief Checks if the heap stores no items.
3.115 + ///
3.116 + /// Returns \c true if and only if the heap stores no items.
3.117 + bool empty() const { return data.empty(); }
3.118 +
3.119 + /// \brief Make empty this heap.
3.120 + ///
3.121 + /// Make empty this heap. It does not change the cross reference map.
3.122 + /// If you want to reuse what is not surely empty you should first clear
3.123 + /// the heap and after that you should set the cross reference map for
3.124 + /// each item to \c PRE_HEAP.
3.125 + void clear() {
3.126 + data.clear();
3.127 + }
3.128 +
3.129 + private:
3.130 + static int parent(int i) { return (i-1)/2; }
3.131 +
3.132 + static int second_child(int i) { return 2*i+2; }
3.133 + bool less(const Pair &p1, const Pair &p2) const {
3.134 + return comp(p1.second, p2.second);
3.135 + }
3.136 +
3.137 + int bubble_up(int hole, Pair p) {
3.138 + int par = parent(hole);
3.139 + while( hole>0 && less(p,data[par]) ) {
3.140 + move(data[par],hole);
3.141 + hole = par;
3.142 + par = parent(hole);
3.143 + }
3.144 + move(p, hole);
3.145 + return hole;
3.146 + }
3.147 +
3.148 + int bubble_down(int hole, Pair p, int length) {
3.149 + int child = second_child(hole);
3.150 + while(child < length) {
3.151 + if( less(data[child-1], data[child]) ) {
3.152 + --child;
3.153 + }
3.154 + if( !less(data[child], p) )
3.155 + goto ok;
3.156 + move(data[child], hole);
3.157 + hole = child;
3.158 + child = second_child(hole);
3.159 + }
3.160 + child--;
3.161 + if( child<length && less(data[child], p) ) {
3.162 + move(data[child], hole);
3.163 + hole=child;
3.164 + }
3.165 + ok:
3.166 + move(p, hole);
3.167 + return hole;
3.168 + }
3.169 +
3.170 + void move(const Pair &p, int i) {
3.171 + data[i] = p;
3.172 + iim.set(p.first, i);
3.173 + }
3.174 +
3.175 + public:
3.176 + /// \brief Insert a pair of item and priority into the heap.
3.177 + ///
3.178 + /// Adds \c p.first to the heap with priority \c p.second.
3.179 + /// \param p The pair to insert.
3.180 + void push(const Pair &p) {
3.181 + int n = data.size();
3.182 + data.resize(n+1);
3.183 + bubble_up(n, p);
3.184 + }
3.185 +
3.186 + /// \brief Insert an item into the heap with the given heap.
3.187 + ///
3.188 + /// Adds \c i to the heap with priority \c p.
3.189 + /// \param i The item to insert.
3.190 + /// \param p The priority of the item.
3.191 + void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
3.192 +
3.193 + /// \brief Returns the item with minimum priority relative to \c Compare.
3.194 + ///
3.195 + /// This method returns the item with minimum priority relative to \c
3.196 + /// Compare.
3.197 + /// \pre The heap must be nonempty.
3.198 + Item top() const {
3.199 + return data[0].first;
3.200 + }
3.201 +
3.202 + /// \brief Returns the minimum priority relative to \c Compare.
3.203 + ///
3.204 + /// It returns the minimum priority relative to \c Compare.
3.205 + /// \pre The heap must be nonempty.
3.206 + Prio prio() const {
3.207 + return data[0].second;
3.208 + }
3.209 +
3.210 + /// \brief Deletes the item with minimum priority relative to \c Compare.
3.211 + ///
3.212 + /// This method deletes the item with minimum priority relative to \c
3.213 + /// Compare from the heap.
3.214 + /// \pre The heap must be non-empty.
3.215 + void pop() {
3.216 + int n = data.size()-1;
3.217 + iim.set(data[0].first, POST_HEAP);
3.218 + if (n > 0) {
3.219 + bubble_down(0, data[n], n);
3.220 + }
3.221 + data.pop_back();
3.222 + }
3.223 +
3.224 + /// \brief Deletes \c i from the heap.
3.225 + ///
3.226 + /// This method deletes item \c i from the heap.
3.227 + /// \param i The item to erase.
3.228 + /// \pre The item should be in the heap.
3.229 + void erase(const Item &i) {
3.230 + int h = iim[i];
3.231 + int n = data.size()-1;
3.232 + iim.set(data[h].first, POST_HEAP);
3.233 + if( h < n ) {
3.234 + if ( bubble_up(h, data[n]) == h) {
3.235 + bubble_down(h, data[n], n);
3.236 + }
3.237 + }
3.238 + data.pop_back();
3.239 + }
3.240 +
3.241 +
3.242 + /// \brief Returns the priority of \c i.
3.243 + ///
3.244 + /// This function returns the priority of item \c i.
3.245 + /// \pre \c i must be in the heap.
3.246 + /// \param i The item.
3.247 + Prio operator[](const Item &i) const {
3.248 + int idx = iim[i];
3.249 + return data[idx].second;
3.250 + }
3.251 +
3.252 + /// \brief \c i gets to the heap with priority \c p independently
3.253 + /// if \c i was already there.
3.254 + ///
3.255 + /// This method calls \ref push(\c i, \c p) if \c i is not stored
3.256 + /// in the heap and sets the priority of \c i to \c p otherwise.
3.257 + /// \param i The item.
3.258 + /// \param p The priority.
3.259 + void set(const Item &i, const Prio &p) {
3.260 + int idx = iim[i];
3.261 + if( idx < 0 ) {
3.262 + push(i,p);
3.263 + }
3.264 + else if( comp(p, data[idx].second) ) {
3.265 + bubble_up(idx, Pair(i,p));
3.266 + }
3.267 + else {
3.268 + bubble_down(idx, Pair(i,p), data.size());
3.269 + }
3.270 + }
3.271 +
3.272 + /// \brief Decreases the priority of \c i to \c p.
3.273 + ///
3.274 + /// This method decreases the priority of item \c i to \c p.
3.275 + /// \pre \c i must be stored in the heap with priority at least \c
3.276 + /// p relative to \c Compare.
3.277 + /// \param i The item.
3.278 + /// \param p The priority.
3.279 + void decrease(const Item &i, const Prio &p) {
3.280 + int idx = iim[i];
3.281 + bubble_up(idx, Pair(i,p));
3.282 + }
3.283 +
3.284 + /// \brief Increases the priority of \c i to \c p.
3.285 + ///
3.286 + /// This method sets the priority of item \c i to \c p.
3.287 + /// \pre \c i must be stored in the heap with priority at most \c
3.288 + /// p relative to \c Compare.
3.289 + /// \param i The item.
3.290 + /// \param p The priority.
3.291 + void increase(const Item &i, const Prio &p) {
3.292 + int idx = iim[i];
3.293 + bubble_down(idx, Pair(i,p), data.size());
3.294 + }
3.295 +
3.296 + /// \brief Returns if \c item is in, has already been in, or has
3.297 + /// never been in the heap.
3.298 + ///
3.299 + /// This method returns PRE_HEAP if \c item has never been in the
3.300 + /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
3.301 + /// otherwise. In the latter case it is possible that \c item will
3.302 + /// get back to the heap again.
3.303 + /// \param i The item.
3.304 + State state(const Item &i) const {
3.305 + int s = iim[i];
3.306 + if( s>=0 )
3.307 + s=0;
3.308 + return State(s);
3.309 + }
3.310 +
3.311 + /// \brief Sets the state of the \c item in the heap.
3.312 + ///
3.313 + /// Sets the state of the \c item in the heap. It can be used to
3.314 + /// manually clear the heap when it is important to achive the
3.315 + /// better time complexity.
3.316 + /// \param i The item.
3.317 + /// \param st The state. It should not be \c IN_HEAP.
3.318 + void state(const Item& i, State st) {
3.319 + switch (st) {
3.320 + case POST_HEAP:
3.321 + case PRE_HEAP:
3.322 + if (state(i) == IN_HEAP) {
3.323 + erase(i);
3.324 + }
3.325 + iim[i] = st;
3.326 + break;
3.327 + case IN_HEAP:
3.328 + break;
3.329 + }
3.330 + }
3.331 +
3.332 + /// \brief Replaces an item in the heap.
3.333 + ///
3.334 + /// The \c i item is replaced with \c j item. The \c i item should
3.335 + /// be in the heap, while the \c j should be out of the heap. The
3.336 + /// \c i item will out of the heap and \c j will be in the heap
3.337 + /// with the same prioriority as prevoiusly the \c i item.
3.338 + void replace(const Item& i, const Item& j) {
3.339 + int idx = iim[i];
3.340 + iim.set(i, iim[j]);
3.341 + iim.set(j, idx);
3.342 + data[idx].first = j;
3.343 + }
3.344 +
3.345 + }; // class BinHeap
3.346 +
3.347 +} // namespace lemon
3.348 +
3.349 +#endif // LEMON_BIN_HEAP_H
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/lemon/bits/path_dump.h Thu Feb 07 21:37:07 2008 +0000
4.3 @@ -0,0 +1,174 @@
4.4 +/* -*- C++ -*-
4.5 + *
4.6 + * This file is a part of LEMON, a generic C++ optimization library
4.7 + *
4.8 + * Copyright (C) 2003-2008
4.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
4.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
4.11 + *
4.12 + * Permission to use, modify and distribute this software is granted
4.13 + * provided that this copyright notice appears in all copies. For
4.14 + * precise terms see the accompanying LICENSE file.
4.15 + *
4.16 + * This software is provided "AS IS" with no warranty of any kind,
4.17 + * express or implied, and with no claim as to its suitability for any
4.18 + * purpose.
4.19 + *
4.20 + */
4.21 +
4.22 +#ifndef LEMON_BITS_PRED_MAP_PATH_H
4.23 +#define LEMON_BITS_PRED_MAP_PATH_H
4.24 +
4.25 +namespace lemon {
4.26 +
4.27 + template <typename _Digraph, typename _PredMap>
4.28 + class PredMapPath {
4.29 + public:
4.30 + typedef True RevPathTag;
4.31 +
4.32 + typedef _Digraph Digraph;
4.33 + typedef typename Digraph::Arc Arc;
4.34 + typedef _PredMap PredMap;
4.35 +
4.36 + PredMapPath(const Digraph& _digraph, const PredMap& _predMap,
4.37 + typename Digraph::Node _target)
4.38 + : digraph(_digraph), predMap(_predMap), target(_target) {}
4.39 +
4.40 + int length() const {
4.41 + int len = 0;
4.42 + typename Digraph::Node node = target;
4.43 + typename Digraph::Arc arc;
4.44 + while ((arc = predMap[node]) != INVALID) {
4.45 + node = digraph.source(arc);
4.46 + ++len;
4.47 + }
4.48 + return len;
4.49 + }
4.50 +
4.51 + bool empty() const {
4.52 + return predMap[target] != INVALID;
4.53 + }
4.54 +
4.55 + class RevArcIt {
4.56 + public:
4.57 + RevArcIt() {}
4.58 + RevArcIt(Invalid) : path(0), current(INVALID) {}
4.59 + RevArcIt(const PredMapPath& _path)
4.60 + : path(&_path), current(_path.target) {
4.61 + if (path->predMap[current] == INVALID) current = INVALID;
4.62 + }
4.63 +
4.64 + operator const typename Digraph::Arc() const {
4.65 + return path->predMap[current];
4.66 + }
4.67 +
4.68 + RevArcIt& operator++() {
4.69 + current = path->digraph.source(path->predMap[current]);
4.70 + if (path->predMap[current] == INVALID) current = INVALID;
4.71 + return *this;
4.72 + }
4.73 +
4.74 + bool operator==(const RevArcIt& e) const {
4.75 + return current == e.current;
4.76 + }
4.77 +
4.78 + bool operator!=(const RevArcIt& e) const {
4.79 + return current != e.current;
4.80 + }
4.81 +
4.82 + bool operator<(const RevArcIt& e) const {
4.83 + return current < e.current;
4.84 + }
4.85 +
4.86 + private:
4.87 + const PredMapPath* path;
4.88 + typename Digraph::Node current;
4.89 + };
4.90 +
4.91 + private:
4.92 + const Digraph& digraph;
4.93 + const PredMap& predMap;
4.94 + typename Digraph::Node target;
4.95 + };
4.96 +
4.97 +
4.98 + template <typename _Digraph, typename _PredMatrixMap>
4.99 + class PredMatrixMapPath {
4.100 + public:
4.101 + typedef True RevPathTag;
4.102 +
4.103 + typedef _Digraph Digraph;
4.104 + typedef typename Digraph::Arc Arc;
4.105 + typedef _PredMatrixMap PredMatrixMap;
4.106 +
4.107 + PredMatrixMapPath(const Digraph& _digraph,
4.108 + const PredMatrixMap& _predMatrixMap,
4.109 + typename Digraph::Node _source,
4.110 + typename Digraph::Node _target)
4.111 + : digraph(_digraph), predMatrixMap(_predMatrixMap),
4.112 + source(_source), target(_target) {}
4.113 +
4.114 + int length() const {
4.115 + int len = 0;
4.116 + typename Digraph::Node node = target;
4.117 + typename Digraph::Arc arc;
4.118 + while ((arc = predMatrixMap(source, node)) != INVALID) {
4.119 + node = digraph.source(arc);
4.120 + ++len;
4.121 + }
4.122 + return len;
4.123 + }
4.124 +
4.125 + bool empty() const {
4.126 + return source != target;
4.127 + }
4.128 +
4.129 + class RevArcIt {
4.130 + public:
4.131 + RevArcIt() {}
4.132 + RevArcIt(Invalid) : path(0), current(INVALID) {}
4.133 + RevArcIt(const PredMatrixMapPath& _path)
4.134 + : path(&_path), current(_path.target) {
4.135 + if (path->predMatrixMap(path->source, current) == INVALID)
4.136 + current = INVALID;
4.137 + }
4.138 +
4.139 + operator const typename Digraph::Arc() const {
4.140 + return path->predMatrixMap(path->source, current);
4.141 + }
4.142 +
4.143 + RevArcIt& operator++() {
4.144 + current =
4.145 + path->digraph.source(path->predMatrixMap(path->source, current));
4.146 + if (path->predMatrixMap(path->source, current) == INVALID)
4.147 + current = INVALID;
4.148 + return *this;
4.149 + }
4.150 +
4.151 + bool operator==(const RevArcIt& e) const {
4.152 + return current == e.current;
4.153 + }
4.154 +
4.155 + bool operator!=(const RevArcIt& e) const {
4.156 + return current != e.current;
4.157 + }
4.158 +
4.159 + bool operator<(const RevArcIt& e) const {
4.160 + return current < e.current;
4.161 + }
4.162 +
4.163 + private:
4.164 + const PredMatrixMapPath* path;
4.165 + typename Digraph::Node current;
4.166 + };
4.167 +
4.168 + private:
4.169 + const Digraph& digraph;
4.170 + const PredMatrixMap& predMatrixMap;
4.171 + typename Digraph::Node source;
4.172 + typename Digraph::Node target;
4.173 + };
4.174 +
4.175 +}
4.176 +
4.177 +#endif
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/lemon/concepts/heap.h Thu Feb 07 21:37:07 2008 +0000
5.3 @@ -0,0 +1,226 @@
5.4 +/* -*- C++ -*-
5.5 + *
5.6 + * This file is a part of LEMON, a generic C++ optimization library
5.7 + *
5.8 + * Copyright (C) 2003-2008
5.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
5.11 + *
5.12 + * Permission to use, modify and distribute this software is granted
5.13 + * provided that this copyright notice appears in all copies. For
5.14 + * precise terms see the accompanying LICENSE file.
5.15 + *
5.16 + * This software is provided "AS IS" with no warranty of any kind,
5.17 + * express or implied, and with no claim as to its suitability for any
5.18 + * purpose.
5.19 + *
5.20 + */
5.21 +
5.22 +///\ingroup concept
5.23 +///\file
5.24 +///\brief Classes for representing heaps.
5.25 +///
5.26 +
5.27 +#ifndef LEMON_CONCEPT_HEAP_H
5.28 +#define LEMON_CONCEPT_HEAP_H
5.29 +
5.30 +#include <lemon/bits/invalid.h>
5.31 +
5.32 +namespace lemon {
5.33 + namespace concepts {
5.34 + /// \addtogroup concept
5.35 + /// @{
5.36 +
5.37 +
5.38 + /// \brief A concept structure describes the main interface of heaps.
5.39 + ///
5.40 + /// A concept structure describes the main interface of heaps.
5.41 + ///
5.42 + template <typename Prio, typename ItemIntMap>
5.43 + class Heap {
5.44 + public:
5.45 +
5.46 + ///\brief Type of the items stored in the heap.
5.47 + typedef typename ItemIntMap::Key Item;
5.48 +
5.49 +
5.50 + /// \brief Type to represent the items states.
5.51 + ///
5.52 + /// Each Item element have a state associated to it. It may be "in heap",
5.53 + /// "pre heap" or "post heap". The later two are indifferent from the
5.54 + /// heap's point of view, but may be useful to the user.
5.55 + ///
5.56 + /// The ItemIntMap _should_ be initialized in such way, that it maps
5.57 + /// PRE_HEAP (-1) to any element to be put in the heap...
5.58 + enum State {
5.59 + IN_HEAP = 0,
5.60 + PRE_HEAP = -1,
5.61 + POST_HEAP = -2
5.62 + };
5.63 +
5.64 + /// \brief The constructor.
5.65 + ///
5.66 + /// The constructor.
5.67 + /// \param _iim should be given to the constructor, since it is used
5.68 + /// internally to handle the cross references. The value of the map
5.69 + /// should be PRE_HEAP (-1) for each element.
5.70 + explicit Heap(ItemIntMap &_iim) {}
5.71 +
5.72 + /// \brief The number of items stored in the heap.
5.73 + ///
5.74 + /// Returns the number of items stored in the heap.
5.75 + int size() const { return 0; }
5.76 +
5.77 + /// \brief Checks if the heap stores no items.
5.78 + ///
5.79 + /// Returns \c true if and only if the heap stores no items.
5.80 + bool empty() const { return false; }
5.81 +
5.82 + /// \brief Makes empty this heap.
5.83 + ///
5.84 + /// Makes this heap empty.
5.85 + void clear();
5.86 +
5.87 + /// \brief Insert an item into the heap with the given heap.
5.88 + ///
5.89 + /// Adds \c i to the heap with priority \c p.
5.90 + /// \param i The item to insert.
5.91 + /// \param p The priority of the item.
5.92 + void push(const Item &i, const Prio &p) {}
5.93 +
5.94 + /// \brief Returns the item with minimum priority.
5.95 + ///
5.96 + /// This method returns the item with minimum priority.
5.97 + /// \pre The heap must be nonempty.
5.98 + Item top() const {}
5.99 +
5.100 + /// \brief Returns the minimum priority.
5.101 + ///
5.102 + /// It returns the minimum priority.
5.103 + /// \pre The heap must be nonempty.
5.104 + Prio prio() const {}
5.105 +
5.106 + /// \brief Deletes the item with minimum priority.
5.107 + ///
5.108 + /// This method deletes the item with minimum priority.
5.109 + /// \pre The heap must be non-empty.
5.110 + void pop() {}
5.111 +
5.112 + /// \brief Deletes \c i from the heap.
5.113 + ///
5.114 + /// This method deletes item \c i from the heap, if \c i was
5.115 + /// already stored in the heap.
5.116 + /// \param i The item to erase.
5.117 + void erase(const Item &i) {}
5.118 +
5.119 + /// \brief Returns the priority of \c i.
5.120 + ///
5.121 + /// This function returns the priority of item \c i.
5.122 + /// \pre \c i must be in the heap.
5.123 + /// \param i The item.
5.124 + Prio operator[](const Item &i) const {}
5.125 +
5.126 + /// \brief \c i gets to the heap with priority \c p independently
5.127 + /// if \c i was already there.
5.128 + ///
5.129 + /// This method calls \ref push(\c i, \c p) if \c i is not stored
5.130 + /// in the heap and sets the priority of \c i to \c p otherwise.
5.131 + /// It may throw an \e UnderFlowPriorityException.
5.132 + /// \param i The item.
5.133 + /// \param p The priority.
5.134 + void set(const Item &i, const Prio &p) {}
5.135 +
5.136 + /// \brief Decreases the priority of \c i to \c p.
5.137 + ///
5.138 + /// This method decreases the priority of item \c i to \c p.
5.139 + /// \pre \c i must be stored in the heap with priority at least \c p.
5.140 + /// \param i The item.
5.141 + /// \param p The priority.
5.142 + void decrease(const Item &i, const Prio &p) {}
5.143 +
5.144 + /// \brief Increases the priority of \c i to \c p.
5.145 + ///
5.146 + /// This method sets the priority of item \c i to \c p.
5.147 + /// \pre \c i must be stored in the heap with priority at most \c
5.148 + /// p relative to \c Compare.
5.149 + /// \param i The item.
5.150 + /// \param p The priority.
5.151 + void increase(const Item &i, const Prio &p) {}
5.152 +
5.153 + /// \brief Returns if \c item is in, has already been in, or has
5.154 + /// never been in the heap.
5.155 + ///
5.156 + /// This method returns PRE_HEAP if \c item has never been in the
5.157 + /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
5.158 + /// otherwise. In the latter case it is possible that \c item will
5.159 + /// get back to the heap again.
5.160 + /// \param i The item.
5.161 + State state(const Item &i) const {}
5.162 +
5.163 + /// \brief Sets the state of the \c item in the heap.
5.164 + ///
5.165 + /// Sets the state of the \c item in the heap. It can be used to
5.166 + /// manually clear the heap when it is important to achive the
5.167 + /// better time complexity.
5.168 + /// \param i The item.
5.169 + /// \param st The state. It should not be \c IN_HEAP.
5.170 + void state(const Item& i, State st) {}
5.171 +
5.172 +
5.173 + template <typename _Heap>
5.174 + struct Constraints {
5.175 + public:
5.176 +
5.177 + void constraints() {
5.178 + Item item;
5.179 + Prio prio;
5.180 +
5.181 + item=Item();
5.182 + prio=Prio();
5.183 +
5.184 + ignore_unused_variable_warning(item);
5.185 + ignore_unused_variable_warning(prio);
5.186 +
5.187 + typedef typename _Heap::State State;
5.188 + State state;
5.189 +
5.190 + ignore_unused_variable_warning(state);
5.191 +
5.192 + _Heap heap1 = _Heap(map);
5.193 +
5.194 + ignore_unused_variable_warning(heap1);
5.195 +
5.196 + heap.push(item, prio);
5.197 +
5.198 + prio = heap.prio();
5.199 + item = heap.top();
5.200 +
5.201 + heap.pop();
5.202 +
5.203 + heap.set(item, prio);
5.204 + heap.decrease(item, prio);
5.205 + heap.increase(item, prio);
5.206 + prio = heap[item];
5.207 +
5.208 + heap.erase(item);
5.209 +
5.210 + state = heap.state(item);
5.211 +
5.212 + state = _Heap::PRE_HEAP;
5.213 + state = _Heap::IN_HEAP;
5.214 + state = _Heap::POST_HEAP;
5.215 +
5.216 + heap.clear();
5.217 + }
5.218 +
5.219 + _Heap& heap;
5.220 + ItemIntMap& map;
5.221 +
5.222 + Constraints() : heap(0), map(0) {}
5.223 + };
5.224 + };
5.225 +
5.226 + /// @}
5.227 + } // namespace lemon
5.228 +}
5.229 +#endif // LEMON_CONCEPT_PATH_H
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/lemon/dfs.h Thu Feb 07 21:37:07 2008 +0000
6.3 @@ -0,0 +1,1543 @@
6.4 +/* -*- C++ -*-
6.5 + *
6.6 + * This file is a part of LEMON, a generic C++ optimization library
6.7 + *
6.8 + * Copyright (C) 2003-2008
6.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
6.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
6.11 + *
6.12 + * Permission to use, modify and distribute this software is granted
6.13 + * provided that this copyright notice appears in all copies. For
6.14 + * precise terms see the accompanying LICENSE file.
6.15 + *
6.16 + * This software is provided "AS IS" with no warranty of any kind,
6.17 + * express or implied, and with no claim as to its suitability for any
6.18 + * purpose.
6.19 + *
6.20 + */
6.21 +
6.22 +#ifndef LEMON_DFS_H
6.23 +#define LEMON_DFS_H
6.24 +
6.25 +///\ingroup search
6.26 +///\file
6.27 +///\brief Dfs algorithm.
6.28 +
6.29 +#include <lemon/list_graph.h>
6.30 +#include <lemon/graph_utils.h>
6.31 +#include <lemon/bits/path_dump.h>
6.32 +#include <lemon/bits/invalid.h>
6.33 +#include <lemon/error.h>
6.34 +#include <lemon/maps.h>
6.35 +
6.36 +#include <lemon/concept_check.h>
6.37 +
6.38 +namespace lemon {
6.39 +
6.40 +
6.41 + ///Default traits class of Dfs class.
6.42 +
6.43 + ///Default traits class of Dfs class.
6.44 + ///\param GR Digraph type.
6.45 + template<class GR>
6.46 + struct DfsDefaultTraits
6.47 + {
6.48 + ///The digraph type the algorithm runs on.
6.49 + typedef GR Digraph;
6.50 + ///\brief The type of the map that stores the last
6.51 + ///arcs of the %DFS paths.
6.52 + ///
6.53 + ///The type of the map that stores the last
6.54 + ///arcs of the %DFS paths.
6.55 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
6.56 + ///
6.57 + typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap;
6.58 + ///Instantiates a PredMap.
6.59 +
6.60 + ///This function instantiates a \ref PredMap.
6.61 + ///\param G is the digraph, to which we would like to define the PredMap.
6.62 + ///\todo The digraph alone may be insufficient to initialize
6.63 + static PredMap *createPredMap(const GR &G)
6.64 + {
6.65 + return new PredMap(G);
6.66 + }
6.67 +
6.68 + ///The type of the map that indicates which nodes are processed.
6.69 +
6.70 + ///The type of the map that indicates which nodes are processed.
6.71 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
6.72 + ///\todo named parameter to set this type, function to read and write.
6.73 + typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
6.74 + ///Instantiates a ProcessedMap.
6.75 +
6.76 + ///This function instantiates a \ref ProcessedMap.
6.77 + ///\param g is the digraph, to which
6.78 + ///we would like to define the \ref ProcessedMap
6.79 +#ifdef DOXYGEN
6.80 + static ProcessedMap *createProcessedMap(const GR &g)
6.81 +#else
6.82 + static ProcessedMap *createProcessedMap(const GR &)
6.83 +#endif
6.84 + {
6.85 + return new ProcessedMap();
6.86 + }
6.87 + ///The type of the map that indicates which nodes are reached.
6.88 +
6.89 + ///The type of the map that indicates which nodes are reached.
6.90 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
6.91 + ///\todo named parameter to set this type, function to read and write.
6.92 + typedef typename Digraph::template NodeMap<bool> ReachedMap;
6.93 + ///Instantiates a ReachedMap.
6.94 +
6.95 + ///This function instantiates a \ref ReachedMap.
6.96 + ///\param G is the digraph, to which
6.97 + ///we would like to define the \ref ReachedMap.
6.98 + static ReachedMap *createReachedMap(const GR &G)
6.99 + {
6.100 + return new ReachedMap(G);
6.101 + }
6.102 + ///The type of the map that stores the dists of the nodes.
6.103 +
6.104 + ///The type of the map that stores the dists of the nodes.
6.105 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
6.106 + ///
6.107 + typedef typename Digraph::template NodeMap<int> DistMap;
6.108 + ///Instantiates a DistMap.
6.109 +
6.110 + ///This function instantiates a \ref DistMap.
6.111 + ///\param G is the digraph, to which we would like to define the \ref DistMap
6.112 + static DistMap *createDistMap(const GR &G)
6.113 + {
6.114 + return new DistMap(G);
6.115 + }
6.116 + };
6.117 +
6.118 + ///%DFS algorithm class.
6.119 +
6.120 + ///\ingroup search
6.121 + ///This class provides an efficient implementation of the %DFS algorithm.
6.122 + ///
6.123 + ///\param GR The digraph type the algorithm runs on. The default value is
6.124 + ///\ref ListDigraph. The value of GR is not used directly by Dfs, it
6.125 + ///is only passed to \ref DfsDefaultTraits.
6.126 + ///\param TR Traits class to set various data types used by the algorithm.
6.127 + ///The default traits class is
6.128 + ///\ref DfsDefaultTraits "DfsDefaultTraits<GR>".
6.129 + ///See \ref DfsDefaultTraits for the documentation of
6.130 + ///a Dfs traits class.
6.131 + ///
6.132 + ///\author Jacint Szabo and Alpar Juttner
6.133 +#ifdef DOXYGEN
6.134 + template <typename GR,
6.135 + typename TR>
6.136 +#else
6.137 + template <typename GR=ListDigraph,
6.138 + typename TR=DfsDefaultTraits<GR> >
6.139 +#endif
6.140 + class Dfs {
6.141 + public:
6.142 + /**
6.143 + * \brief \ref Exception for uninitialized parameters.
6.144 + *
6.145 + * This error represents problems in the initialization
6.146 + * of the parameters of the algorithms.
6.147 + */
6.148 + class UninitializedParameter : public lemon::UninitializedParameter {
6.149 + public:
6.150 + virtual const char* what() const throw() {
6.151 + return "lemon::Dfs::UninitializedParameter";
6.152 + }
6.153 + };
6.154 +
6.155 + typedef TR Traits;
6.156 + ///The type of the underlying digraph.
6.157 + typedef typename TR::Digraph Digraph;
6.158 + ///\e
6.159 + typedef typename Digraph::Node Node;
6.160 + ///\e
6.161 + typedef typename Digraph::NodeIt NodeIt;
6.162 + ///\e
6.163 + typedef typename Digraph::Arc Arc;
6.164 + ///\e
6.165 + typedef typename Digraph::OutArcIt OutArcIt;
6.166 +
6.167 + ///\brief The type of the map that stores the last
6.168 + ///arcs of the %DFS paths.
6.169 + typedef typename TR::PredMap PredMap;
6.170 + ///The type of the map indicating which nodes are reached.
6.171 + typedef typename TR::ReachedMap ReachedMap;
6.172 + ///The type of the map indicating which nodes are processed.
6.173 + typedef typename TR::ProcessedMap ProcessedMap;
6.174 + ///The type of the map that stores the dists of the nodes.
6.175 + typedef typename TR::DistMap DistMap;
6.176 + private:
6.177 + /// Pointer to the underlying digraph.
6.178 + const Digraph *G;
6.179 + ///Pointer to the map of predecessors arcs.
6.180 + PredMap *_pred;
6.181 + ///Indicates if \ref _pred is locally allocated (\c true) or not.
6.182 + bool local_pred;
6.183 + ///Pointer to the map of distances.
6.184 + DistMap *_dist;
6.185 + ///Indicates if \ref _dist is locally allocated (\c true) or not.
6.186 + bool local_dist;
6.187 + ///Pointer to the map of reached status of the nodes.
6.188 + ReachedMap *_reached;
6.189 + ///Indicates if \ref _reached is locally allocated (\c true) or not.
6.190 + bool local_reached;
6.191 + ///Pointer to the map of processed status of the nodes.
6.192 + ProcessedMap *_processed;
6.193 + ///Indicates if \ref _processed is locally allocated (\c true) or not.
6.194 + bool local_processed;
6.195 +
6.196 + std::vector<typename Digraph::OutArcIt> _stack;
6.197 + int _stack_head;
6.198 +
6.199 + ///Creates the maps if necessary.
6.200 +
6.201 + ///\todo Better memory allocation (instead of new).
6.202 + void create_maps()
6.203 + {
6.204 + if(!_pred) {
6.205 + local_pred = true;
6.206 + _pred = Traits::createPredMap(*G);
6.207 + }
6.208 + if(!_dist) {
6.209 + local_dist = true;
6.210 + _dist = Traits::createDistMap(*G);
6.211 + }
6.212 + if(!_reached) {
6.213 + local_reached = true;
6.214 + _reached = Traits::createReachedMap(*G);
6.215 + }
6.216 + if(!_processed) {
6.217 + local_processed = true;
6.218 + _processed = Traits::createProcessedMap(*G);
6.219 + }
6.220 + }
6.221 +
6.222 + protected:
6.223 +
6.224 + Dfs() {}
6.225 +
6.226 + public:
6.227 +
6.228 + typedef Dfs Create;
6.229 +
6.230 + ///\name Named template parameters
6.231 +
6.232 + ///@{
6.233 +
6.234 + template <class T>
6.235 + struct DefPredMapTraits : public Traits {
6.236 + typedef T PredMap;
6.237 + static PredMap *createPredMap(const Digraph &G)
6.238 + {
6.239 + throw UninitializedParameter();
6.240 + }
6.241 + };
6.242 + ///\brief \ref named-templ-param "Named parameter" for setting
6.243 + ///PredMap type
6.244 + ///
6.245 + ///\ref named-templ-param "Named parameter" for setting PredMap type
6.246 + ///
6.247 + template <class T>
6.248 + struct DefPredMap : public Dfs<Digraph, DefPredMapTraits<T> > {
6.249 + typedef Dfs<Digraph, DefPredMapTraits<T> > Create;
6.250 + };
6.251 +
6.252 +
6.253 + template <class T>
6.254 + struct DefDistMapTraits : public Traits {
6.255 + typedef T DistMap;
6.256 + static DistMap *createDistMap(const Digraph &)
6.257 + {
6.258 + throw UninitializedParameter();
6.259 + }
6.260 + };
6.261 + ///\brief \ref named-templ-param "Named parameter" for setting
6.262 + ///DistMap type
6.263 + ///
6.264 + ///\ref named-templ-param "Named parameter" for setting DistMap
6.265 + ///type
6.266 + template <class T>
6.267 + struct DefDistMap {
6.268 + typedef Dfs<Digraph, DefDistMapTraits<T> > Create;
6.269 + };
6.270 +
6.271 + template <class T>
6.272 + struct DefReachedMapTraits : public Traits {
6.273 + typedef T ReachedMap;
6.274 + static ReachedMap *createReachedMap(const Digraph &)
6.275 + {
6.276 + throw UninitializedParameter();
6.277 + }
6.278 + };
6.279 + ///\brief \ref named-templ-param "Named parameter" for setting
6.280 + ///ReachedMap type
6.281 + ///
6.282 + ///\ref named-templ-param "Named parameter" for setting ReachedMap type
6.283 + ///
6.284 + template <class T>
6.285 + struct DefReachedMap : public Dfs< Digraph, DefReachedMapTraits<T> > {
6.286 + typedef Dfs< Digraph, DefReachedMapTraits<T> > Create;
6.287 + };
6.288 +
6.289 + template <class T>
6.290 + struct DefProcessedMapTraits : public Traits {
6.291 + typedef T ProcessedMap;
6.292 + static ProcessedMap *createProcessedMap(const Digraph &)
6.293 + {
6.294 + throw UninitializedParameter();
6.295 + }
6.296 + };
6.297 + ///\brief \ref named-templ-param "Named parameter" for setting
6.298 + ///ProcessedMap type
6.299 + ///
6.300 + ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
6.301 + ///
6.302 + template <class T>
6.303 + struct DefProcessedMap : public Dfs< Digraph, DefProcessedMapTraits<T> > {
6.304 + typedef Dfs< Digraph, DefProcessedMapTraits<T> > Create;
6.305 + };
6.306 +
6.307 + struct DefDigraphProcessedMapTraits : public Traits {
6.308 + typedef typename Digraph::template NodeMap<bool> ProcessedMap;
6.309 + static ProcessedMap *createProcessedMap(const Digraph &G)
6.310 + {
6.311 + return new ProcessedMap(G);
6.312 + }
6.313 + };
6.314 + ///\brief \ref named-templ-param "Named parameter"
6.315 + ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
6.316 + ///
6.317 + ///\ref named-templ-param "Named parameter"
6.318 + ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
6.319 + ///If you don't set it explicitely, it will be automatically allocated.
6.320 + template <class T>
6.321 + class DefProcessedMapToBeDefaultMap :
6.322 + public Dfs< Digraph, DefDigraphProcessedMapTraits> {
6.323 + typedef Dfs< Digraph, DefDigraphProcessedMapTraits> Create;
6.324 + };
6.325 +
6.326 + ///@}
6.327 +
6.328 + public:
6.329 +
6.330 + ///Constructor.
6.331 +
6.332 + ///\param _G the digraph the algorithm will run on.
6.333 + ///
6.334 + Dfs(const Digraph& _G) :
6.335 + G(&_G),
6.336 + _pred(NULL), local_pred(false),
6.337 + _dist(NULL), local_dist(false),
6.338 + _reached(NULL), local_reached(false),
6.339 + _processed(NULL), local_processed(false)
6.340 + { }
6.341 +
6.342 + ///Destructor.
6.343 + ~Dfs()
6.344 + {
6.345 + if(local_pred) delete _pred;
6.346 + if(local_dist) delete _dist;
6.347 + if(local_reached) delete _reached;
6.348 + if(local_processed) delete _processed;
6.349 + }
6.350 +
6.351 + ///Sets the map storing the predecessor arcs.
6.352 +
6.353 + ///Sets the map storing the predecessor arcs.
6.354 + ///If you don't use this function before calling \ref run(),
6.355 + ///it will allocate one. The destuctor deallocates this
6.356 + ///automatically allocated map, of course.
6.357 + ///\return <tt> (*this) </tt>
6.358 + Dfs &predMap(PredMap &m)
6.359 + {
6.360 + if(local_pred) {
6.361 + delete _pred;
6.362 + local_pred=false;
6.363 + }
6.364 + _pred = &m;
6.365 + return *this;
6.366 + }
6.367 +
6.368 + ///Sets the map storing the distances calculated by the algorithm.
6.369 +
6.370 + ///Sets the map storing the distances calculated by the algorithm.
6.371 + ///If you don't use this function before calling \ref run(),
6.372 + ///it will allocate one. The destuctor deallocates this
6.373 + ///automatically allocated map, of course.
6.374 + ///\return <tt> (*this) </tt>
6.375 + Dfs &distMap(DistMap &m)
6.376 + {
6.377 + if(local_dist) {
6.378 + delete _dist;
6.379 + local_dist=false;
6.380 + }
6.381 + _dist = &m;
6.382 + return *this;
6.383 + }
6.384 +
6.385 + ///Sets the map indicating if a node is reached.
6.386 +
6.387 + ///Sets the map indicating if a node is reached.
6.388 + ///If you don't use this function before calling \ref run(),
6.389 + ///it will allocate one. The destuctor deallocates this
6.390 + ///automatically allocated map, of course.
6.391 + ///\return <tt> (*this) </tt>
6.392 + Dfs &reachedMap(ReachedMap &m)
6.393 + {
6.394 + if(local_reached) {
6.395 + delete _reached;
6.396 + local_reached=false;
6.397 + }
6.398 + _reached = &m;
6.399 + return *this;
6.400 + }
6.401 +
6.402 + ///Sets the map indicating if a node is processed.
6.403 +
6.404 + ///Sets the map indicating if a node is processed.
6.405 + ///If you don't use this function before calling \ref run(),
6.406 + ///it will allocate one. The destuctor deallocates this
6.407 + ///automatically allocated map, of course.
6.408 + ///\return <tt> (*this) </tt>
6.409 + Dfs &processedMap(ProcessedMap &m)
6.410 + {
6.411 + if(local_processed) {
6.412 + delete _processed;
6.413 + local_processed=false;
6.414 + }
6.415 + _processed = &m;
6.416 + return *this;
6.417 + }
6.418 +
6.419 + public:
6.420 + ///\name Execution control
6.421 + ///The simplest way to execute the algorithm is to use
6.422 + ///one of the member functions called \c run(...).
6.423 + ///\n
6.424 + ///If you need more control on the execution,
6.425 + ///first you must call \ref init(), then you can add a source node
6.426 + ///with \ref addSource().
6.427 + ///Finally \ref start() will perform the actual path
6.428 + ///computation.
6.429 +
6.430 + ///@{
6.431 +
6.432 + ///Initializes the internal data structures.
6.433 +
6.434 + ///Initializes the internal data structures.
6.435 + ///
6.436 + void init()
6.437 + {
6.438 + create_maps();
6.439 + _stack.resize(countNodes(*G));
6.440 + _stack_head=-1;
6.441 + for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
6.442 + _pred->set(u,INVALID);
6.443 + // _predNode->set(u,INVALID);
6.444 + _reached->set(u,false);
6.445 + _processed->set(u,false);
6.446 + }
6.447 + }
6.448 +
6.449 + ///Adds a new source node.
6.450 +
6.451 + ///Adds a new source node to the set of nodes to be processed.
6.452 + ///
6.453 + ///\warning dists are wrong (or at least strange)
6.454 + ///in case of multiple sources.
6.455 + void addSource(Node s)
6.456 + {
6.457 + if(!(*_reached)[s])
6.458 + {
6.459 + _reached->set(s,true);
6.460 + _pred->set(s,INVALID);
6.461 + OutArcIt e(*G,s);
6.462 + if(e!=INVALID) {
6.463 + _stack[++_stack_head]=e;
6.464 + _dist->set(s,_stack_head);
6.465 + }
6.466 + else {
6.467 + _processed->set(s,true);
6.468 + _dist->set(s,0);
6.469 + }
6.470 + }
6.471 + }
6.472 +
6.473 + ///Processes the next arc.
6.474 +
6.475 + ///Processes the next arc.
6.476 + ///
6.477 + ///\return The processed arc.
6.478 + ///
6.479 + ///\pre The stack must not be empty!
6.480 + Arc processNextArc()
6.481 + {
6.482 + Node m;
6.483 + Arc e=_stack[_stack_head];
6.484 + if(!(*_reached)[m=G->target(e)]) {
6.485 + _pred->set(m,e);
6.486 + _reached->set(m,true);
6.487 + ++_stack_head;
6.488 + _stack[_stack_head] = OutArcIt(*G, m);
6.489 + _dist->set(m,_stack_head);
6.490 + }
6.491 + else {
6.492 + m=G->source(e);
6.493 + ++_stack[_stack_head];
6.494 + }
6.495 + while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
6.496 + _processed->set(m,true);
6.497 + --_stack_head;
6.498 + if(_stack_head>=0) {
6.499 + m=G->source(_stack[_stack_head]);
6.500 + ++_stack[_stack_head];
6.501 + }
6.502 + }
6.503 + return e;
6.504 + }
6.505 + ///Next arc to be processed.
6.506 +
6.507 + ///Next arc to be processed.
6.508 + ///
6.509 + ///\return The next arc to be processed or INVALID if the stack is
6.510 + /// empty.
6.511 + OutArcIt nextArc()
6.512 + {
6.513 + return _stack_head>=0?_stack[_stack_head]:INVALID;
6.514 + }
6.515 +
6.516 + ///\brief Returns \c false if there are nodes
6.517 + ///to be processed in the queue
6.518 + ///
6.519 + ///Returns \c false if there are nodes
6.520 + ///to be processed in the queue
6.521 + bool emptyQueue() { return _stack_head<0; }
6.522 + ///Returns the number of the nodes to be processed.
6.523 +
6.524 + ///Returns the number of the nodes to be processed in the queue.
6.525 + int queueSize() { return _stack_head+1; }
6.526 +
6.527 + ///Executes the algorithm.
6.528 +
6.529 + ///Executes the algorithm.
6.530 + ///
6.531 + ///\pre init() must be called and at least one node should be added
6.532 + ///with addSource() before using this function.
6.533 + ///
6.534 + ///This method runs the %DFS algorithm from the root node(s)
6.535 + ///in order to
6.536 + ///compute the
6.537 + ///%DFS path to each node. The algorithm computes
6.538 + ///- The %DFS tree.
6.539 + ///- The distance of each node from the root(s) in the %DFS tree.
6.540 + ///
6.541 + void start()
6.542 + {
6.543 + while ( !emptyQueue() ) processNextArc();
6.544 + }
6.545 +
6.546 + ///Executes the algorithm until \c dest is reached.
6.547 +
6.548 + ///Executes the algorithm until \c dest is reached.
6.549 + ///
6.550 + ///\pre init() must be called and at least one node should be added
6.551 + ///with addSource() before using this function.
6.552 + ///
6.553 + ///This method runs the %DFS algorithm from the root node(s)
6.554 + ///in order to
6.555 + ///compute the
6.556 + ///%DFS path to \c dest. The algorithm computes
6.557 + ///- The %DFS path to \c dest.
6.558 + ///- The distance of \c dest from the root(s) in the %DFS tree.
6.559 + ///
6.560 + void start(Node dest)
6.561 + {
6.562 + while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest )
6.563 + processNextArc();
6.564 + }
6.565 +
6.566 + ///Executes the algorithm until a condition is met.
6.567 +
6.568 + ///Executes the algorithm until a condition is met.
6.569 + ///
6.570 + ///\pre init() must be called and at least one node should be added
6.571 + ///with addSource() before using this function.
6.572 + ///
6.573 + ///\param em must be a bool (or convertible) arc map. The algorithm
6.574 + ///will stop when it reaches an arc \c e with <tt>em[e]</tt> true.
6.575 + ///
6.576 + ///\return The reached arc \c e with <tt>em[e]</tt> true or
6.577 + ///\c INVALID if no such arc was found.
6.578 + ///
6.579 + ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an arc map,
6.580 + ///not a node map.
6.581 + template<class EM>
6.582 + Arc start(const EM &em)
6.583 + {
6.584 + while ( !emptyQueue() && !em[_stack[_stack_head]] )
6.585 + processNextArc();
6.586 + return emptyQueue() ? INVALID : _stack[_stack_head];
6.587 + }
6.588 +
6.589 + ///Runs %DFS algorithm to visit all nodes in the digraph.
6.590 +
6.591 + ///This method runs the %DFS algorithm in order to
6.592 + ///compute the
6.593 + ///%DFS path to each node. The algorithm computes
6.594 + ///- The %DFS tree.
6.595 + ///- The distance of each node from the root in the %DFS tree.
6.596 + ///
6.597 + ///\note d.run() is just a shortcut of the following code.
6.598 + ///\code
6.599 + /// d.init();
6.600 + /// for (NodeIt it(digraph); it != INVALID; ++it) {
6.601 + /// if (!d.reached(it)) {
6.602 + /// d.addSource(it);
6.603 + /// d.start();
6.604 + /// }
6.605 + /// }
6.606 + ///\endcode
6.607 + void run() {
6.608 + init();
6.609 + for (NodeIt it(*G); it != INVALID; ++it) {
6.610 + if (!reached(it)) {
6.611 + addSource(it);
6.612 + start();
6.613 + }
6.614 + }
6.615 + }
6.616 +
6.617 + ///Runs %DFS algorithm from node \c s.
6.618 +
6.619 + ///This method runs the %DFS algorithm from a root node \c s
6.620 + ///in order to
6.621 + ///compute the
6.622 + ///%DFS path to each node. The algorithm computes
6.623 + ///- The %DFS tree.
6.624 + ///- The distance of each node from the root in the %DFS tree.
6.625 + ///
6.626 + ///\note d.run(s) is just a shortcut of the following code.
6.627 + ///\code
6.628 + /// d.init();
6.629 + /// d.addSource(s);
6.630 + /// d.start();
6.631 + ///\endcode
6.632 + void run(Node s) {
6.633 + init();
6.634 + addSource(s);
6.635 + start();
6.636 + }
6.637 +
6.638 + ///Finds the %DFS path between \c s and \c t.
6.639 +
6.640 + ///Finds the %DFS path between \c s and \c t.
6.641 + ///
6.642 + ///\return The length of the %DFS s---t path if there exists one,
6.643 + ///0 otherwise.
6.644 + ///\note Apart from the return value, d.run(s,t) is
6.645 + ///just a shortcut of the following code.
6.646 + ///\code
6.647 + /// d.init();
6.648 + /// d.addSource(s);
6.649 + /// d.start(t);
6.650 + ///\endcode
6.651 + int run(Node s,Node t) {
6.652 + init();
6.653 + addSource(s);
6.654 + start(t);
6.655 + return reached(t)?_stack_head+1:0;
6.656 + }
6.657 +
6.658 + ///@}
6.659 +
6.660 + ///\name Query Functions
6.661 + ///The result of the %DFS algorithm can be obtained using these
6.662 + ///functions.\n
6.663 + ///Before the use of these functions,
6.664 + ///either run() or start() must be called.
6.665 +
6.666 + ///@{
6.667 +
6.668 + typedef PredMapPath<Digraph, PredMap> Path;
6.669 +
6.670 + ///Gives back the shortest path.
6.671 +
6.672 + ///Gives back the shortest path.
6.673 + ///\pre The \c t should be reachable from the source.
6.674 + Path path(Node t)
6.675 + {
6.676 + return Path(*G, *_pred, t);
6.677 + }
6.678 +
6.679 + ///The distance of a node from the root(s).
6.680 +
6.681 + ///Returns the distance of a node from the root(s).
6.682 + ///\pre \ref run() must be called before using this function.
6.683 + ///\warning If node \c v is unreachable from the root(s) then the return
6.684 + ///value of this funcion is undefined.
6.685 + int dist(Node v) const { return (*_dist)[v]; }
6.686 +
6.687 + ///Returns the 'previous arc' of the %DFS tree.
6.688 +
6.689 + ///For a node \c v it returns the 'previous arc'
6.690 + ///of the %DFS path,
6.691 + ///i.e. it returns the last arc of a %DFS path from the root(s) to \c
6.692 + ///v. It is \ref INVALID
6.693 + ///if \c v is unreachable from the root(s) or \c v is a root. The
6.694 + ///%DFS tree used here is equal to the %DFS tree used in
6.695 + ///\ref predNode().
6.696 + ///\pre Either \ref run() or \ref start() must be called before using
6.697 + ///this function.
6.698 + Arc predArc(Node v) const { return (*_pred)[v];}
6.699 +
6.700 + ///Returns the 'previous node' of the %DFS tree.
6.701 +
6.702 + ///For a node \c v it returns the 'previous node'
6.703 + ///of the %DFS tree,
6.704 + ///i.e. it returns the last but one node from a %DFS path from the
6.705 + ///root(s) to \c v.
6.706 + ///It is INVALID if \c v is unreachable from the root(s) or
6.707 + ///if \c v itself a root.
6.708 + ///The %DFS tree used here is equal to the %DFS
6.709 + ///tree used in \ref predArc().
6.710 + ///\pre Either \ref run() or \ref start() must be called before
6.711 + ///using this function.
6.712 + Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
6.713 + G->source((*_pred)[v]); }
6.714 +
6.715 + ///Returns a reference to the NodeMap of distances.
6.716 +
6.717 + ///Returns a reference to the NodeMap of distances.
6.718 + ///\pre Either \ref run() or \ref init() must
6.719 + ///be called before using this function.
6.720 + const DistMap &distMap() const { return *_dist;}
6.721 +
6.722 + ///Returns a reference to the %DFS arc-tree map.
6.723 +
6.724 + ///Returns a reference to the NodeMap of the arcs of the
6.725 + ///%DFS tree.
6.726 + ///\pre Either \ref run() or \ref init()
6.727 + ///must be called before using this function.
6.728 + const PredMap &predMap() const { return *_pred;}
6.729 +
6.730 + ///Checks if a node is reachable from the root.
6.731 +
6.732 + ///Returns \c true if \c v is reachable from the root(s).
6.733 + ///\warning The source nodes are inditated as unreachable.
6.734 + ///\pre Either \ref run() or \ref start()
6.735 + ///must be called before using this function.
6.736 + ///
6.737 + bool reached(Node v) { return (*_reached)[v]; }
6.738 +
6.739 + ///@}
6.740 + };
6.741 +
6.742 + ///Default traits class of Dfs function.
6.743 +
6.744 + ///Default traits class of Dfs function.
6.745 + ///\param GR Digraph type.
6.746 + template<class GR>
6.747 + struct DfsWizardDefaultTraits
6.748 + {
6.749 + ///The digraph type the algorithm runs on.
6.750 + typedef GR Digraph;
6.751 + ///\brief The type of the map that stores the last
6.752 + ///arcs of the %DFS paths.
6.753 + ///
6.754 + ///The type of the map that stores the last
6.755 + ///arcs of the %DFS paths.
6.756 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
6.757 + ///
6.758 + typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap;
6.759 + ///Instantiates a PredMap.
6.760 +
6.761 + ///This function instantiates a \ref PredMap.
6.762 + ///\param g is the digraph, to which we would like to define the PredMap.
6.763 + ///\todo The digraph alone may be insufficient to initialize
6.764 +#ifdef DOXYGEN
6.765 + static PredMap *createPredMap(const GR &g)
6.766 +#else
6.767 + static PredMap *createPredMap(const GR &)
6.768 +#endif
6.769 + {
6.770 + return new PredMap();
6.771 + }
6.772 +
6.773 + ///The type of the map that indicates which nodes are processed.
6.774 +
6.775 + ///The type of the map that indicates which nodes are processed.
6.776 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
6.777 + ///\todo named parameter to set this type, function to read and write.
6.778 + typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
6.779 + ///Instantiates a ProcessedMap.
6.780 +
6.781 + ///This function instantiates a \ref ProcessedMap.
6.782 + ///\param g is the digraph, to which
6.783 + ///we would like to define the \ref ProcessedMap
6.784 +#ifdef DOXYGEN
6.785 + static ProcessedMap *createProcessedMap(const GR &g)
6.786 +#else
6.787 + static ProcessedMap *createProcessedMap(const GR &)
6.788 +#endif
6.789 + {
6.790 + return new ProcessedMap();
6.791 + }
6.792 + ///The type of the map that indicates which nodes are reached.
6.793 +
6.794 + ///The type of the map that indicates which nodes are reached.
6.795 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
6.796 + ///\todo named parameter to set this type, function to read and write.
6.797 + typedef typename Digraph::template NodeMap<bool> ReachedMap;
6.798 + ///Instantiates a ReachedMap.
6.799 +
6.800 + ///This function instantiates a \ref ReachedMap.
6.801 + ///\param G is the digraph, to which
6.802 + ///we would like to define the \ref ReachedMap.
6.803 + static ReachedMap *createReachedMap(const GR &G)
6.804 + {
6.805 + return new ReachedMap(G);
6.806 + }
6.807 + ///The type of the map that stores the dists of the nodes.
6.808 +
6.809 + ///The type of the map that stores the dists of the nodes.
6.810 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
6.811 + ///
6.812 + typedef NullMap<typename Digraph::Node,int> DistMap;
6.813 + ///Instantiates a DistMap.
6.814 +
6.815 + ///This function instantiates a \ref DistMap.
6.816 + ///\param g is the digraph, to which we would like to define the \ref DistMap
6.817 +#ifdef DOXYGEN
6.818 + static DistMap *createDistMap(const GR &g)
6.819 +#else
6.820 + static DistMap *createDistMap(const GR &)
6.821 +#endif
6.822 + {
6.823 + return new DistMap();
6.824 + }
6.825 + };
6.826 +
6.827 + /// Default traits used by \ref DfsWizard
6.828 +
6.829 + /// To make it easier to use Dfs algorithm
6.830 + ///we have created a wizard class.
6.831 + /// This \ref DfsWizard class needs default traits,
6.832 + ///as well as the \ref Dfs class.
6.833 + /// The \ref DfsWizardBase is a class to be the default traits of the
6.834 + /// \ref DfsWizard class.
6.835 + template<class GR>
6.836 + class DfsWizardBase : public DfsWizardDefaultTraits<GR>
6.837 + {
6.838 +
6.839 + typedef DfsWizardDefaultTraits<GR> Base;
6.840 + protected:
6.841 + /// Type of the nodes in the digraph.
6.842 + typedef typename Base::Digraph::Node Node;
6.843 +
6.844 + /// Pointer to the underlying digraph.
6.845 + void *_g;
6.846 + ///Pointer to the map of reached nodes.
6.847 + void *_reached;
6.848 + ///Pointer to the map of processed nodes.
6.849 + void *_processed;
6.850 + ///Pointer to the map of predecessors arcs.
6.851 + void *_pred;
6.852 + ///Pointer to the map of distances.
6.853 + void *_dist;
6.854 + ///Pointer to the source node.
6.855 + Node _source;
6.856 +
6.857 + public:
6.858 + /// Constructor.
6.859 +
6.860 + /// This constructor does not require parameters, therefore it initiates
6.861 + /// all of the attributes to default values (0, INVALID).
6.862 + DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
6.863 + _dist(0), _source(INVALID) {}
6.864 +
6.865 + /// Constructor.
6.866 +
6.867 + /// This constructor requires some parameters,
6.868 + /// listed in the parameters list.
6.869 + /// Others are initiated to 0.
6.870 + /// \param g is the initial value of \ref _g
6.871 + /// \param s is the initial value of \ref _source
6.872 + DfsWizardBase(const GR &g, Node s=INVALID) :
6.873 + _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
6.874 + _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
6.875 +
6.876 + };
6.877 +
6.878 + /// A class to make the usage of the Dfs algorithm easier
6.879 +
6.880 + /// This class is created to make it easier to use the Dfs algorithm.
6.881 + /// It uses the functions and features of the plain \ref Dfs,
6.882 + /// but it is much simpler to use it.
6.883 + ///
6.884 + /// Simplicity means that the way to change the types defined
6.885 + /// in the traits class is based on functions that returns the new class
6.886 + /// and not on templatable built-in classes.
6.887 + /// When using the plain \ref Dfs
6.888 + /// the new class with the modified type comes from
6.889 + /// the original class by using the ::
6.890 + /// operator. In the case of \ref DfsWizard only
6.891 + /// a function have to be called and it will
6.892 + /// return the needed class.
6.893 + ///
6.894 + /// It does not have own \ref run method. When its \ref run method is called
6.895 + /// it initiates a plain \ref Dfs object, and calls the \ref Dfs::run
6.896 + /// method of it.
6.897 + template<class TR>
6.898 + class DfsWizard : public TR
6.899 + {
6.900 + typedef TR Base;
6.901 +
6.902 + ///The type of the underlying digraph.
6.903 + typedef typename TR::Digraph Digraph;
6.904 + //\e
6.905 + typedef typename Digraph::Node Node;
6.906 + //\e
6.907 + typedef typename Digraph::NodeIt NodeIt;
6.908 + //\e
6.909 + typedef typename Digraph::Arc Arc;
6.910 + //\e
6.911 + typedef typename Digraph::OutArcIt OutArcIt;
6.912 +
6.913 + ///\brief The type of the map that stores
6.914 + ///the reached nodes
6.915 + typedef typename TR::ReachedMap ReachedMap;
6.916 + ///\brief The type of the map that stores
6.917 + ///the processed nodes
6.918 + typedef typename TR::ProcessedMap ProcessedMap;
6.919 + ///\brief The type of the map that stores the last
6.920 + ///arcs of the %DFS paths.
6.921 + typedef typename TR::PredMap PredMap;
6.922 + ///The type of the map that stores the distances of the nodes.
6.923 + typedef typename TR::DistMap DistMap;
6.924 +
6.925 + public:
6.926 + /// Constructor.
6.927 + DfsWizard() : TR() {}
6.928 +
6.929 + /// Constructor that requires parameters.
6.930 +
6.931 + /// Constructor that requires parameters.
6.932 + /// These parameters will be the default values for the traits class.
6.933 + DfsWizard(const Digraph &g, Node s=INVALID) :
6.934 + TR(g,s) {}
6.935 +
6.936 + ///Copy constructor
6.937 + DfsWizard(const TR &b) : TR(b) {}
6.938 +
6.939 + ~DfsWizard() {}
6.940 +
6.941 + ///Runs Dfs algorithm from a given node.
6.942 +
6.943 + ///Runs Dfs algorithm from a given node.
6.944 + ///The node can be given by the \ref source function.
6.945 + void run()
6.946 + {
6.947 + if(Base::_source==INVALID) throw UninitializedParameter();
6.948 + Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
6.949 + if(Base::_reached)
6.950 + alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
6.951 + if(Base::_processed)
6.952 + alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
6.953 + if(Base::_pred)
6.954 + alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
6.955 + if(Base::_dist)
6.956 + alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
6.957 + alg.run(Base::_source);
6.958 + }
6.959 +
6.960 + ///Runs Dfs algorithm from the given node.
6.961 +
6.962 + ///Runs Dfs algorithm from the given node.
6.963 + ///\param s is the given source.
6.964 + void run(Node s)
6.965 + {
6.966 + Base::_source=s;
6.967 + run();
6.968 + }
6.969 +
6.970 + template<class T>
6.971 + struct DefPredMapBase : public Base {
6.972 + typedef T PredMap;
6.973 + static PredMap *createPredMap(const Digraph &) { return 0; };
6.974 + DefPredMapBase(const TR &b) : TR(b) {}
6.975 + };
6.976 +
6.977 + ///\brief \ref named-templ-param "Named parameter"
6.978 + ///function for setting PredMap type
6.979 + ///
6.980 + /// \ref named-templ-param "Named parameter"
6.981 + ///function for setting PredMap type
6.982 + ///
6.983 + template<class T>
6.984 + DfsWizard<DefPredMapBase<T> > predMap(const T &t)
6.985 + {
6.986 + Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
6.987 + return DfsWizard<DefPredMapBase<T> >(*this);
6.988 + }
6.989 +
6.990 +
6.991 + template<class T>
6.992 + struct DefReachedMapBase : public Base {
6.993 + typedef T ReachedMap;
6.994 + static ReachedMap *createReachedMap(const Digraph &) { return 0; };
6.995 + DefReachedMapBase(const TR &b) : TR(b) {}
6.996 + };
6.997 +
6.998 + ///\brief \ref named-templ-param "Named parameter"
6.999 + ///function for setting ReachedMap
6.1000 + ///
6.1001 + /// \ref named-templ-param "Named parameter"
6.1002 + ///function for setting ReachedMap
6.1003 + ///
6.1004 + template<class T>
6.1005 + DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
6.1006 + {
6.1007 + Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
6.1008 + return DfsWizard<DefReachedMapBase<T> >(*this);
6.1009 + }
6.1010 +
6.1011 +
6.1012 + template<class T>
6.1013 + struct DefProcessedMapBase : public Base {
6.1014 + typedef T ProcessedMap;
6.1015 + static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
6.1016 + DefProcessedMapBase(const TR &b) : TR(b) {}
6.1017 + };
6.1018 +
6.1019 + ///\brief \ref named-templ-param "Named parameter"
6.1020 + ///function for setting ProcessedMap
6.1021 + ///
6.1022 + /// \ref named-templ-param "Named parameter"
6.1023 + ///function for setting ProcessedMap
6.1024 + ///
6.1025 + template<class T>
6.1026 + DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
6.1027 + {
6.1028 + Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
6.1029 + return DfsWizard<DefProcessedMapBase<T> >(*this);
6.1030 + }
6.1031 +
6.1032 + template<class T>
6.1033 + struct DefDistMapBase : public Base {
6.1034 + typedef T DistMap;
6.1035 + static DistMap *createDistMap(const Digraph &) { return 0; };
6.1036 + DefDistMapBase(const TR &b) : TR(b) {}
6.1037 + };
6.1038 +
6.1039 + ///\brief \ref named-templ-param "Named parameter"
6.1040 + ///function for setting DistMap type
6.1041 + ///
6.1042 + /// \ref named-templ-param "Named parameter"
6.1043 + ///function for setting DistMap type
6.1044 + ///
6.1045 + template<class T>
6.1046 + DfsWizard<DefDistMapBase<T> > distMap(const T &t)
6.1047 + {
6.1048 + Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
6.1049 + return DfsWizard<DefDistMapBase<T> >(*this);
6.1050 + }
6.1051 +
6.1052 + /// Sets the source node, from which the Dfs algorithm runs.
6.1053 +
6.1054 + /// Sets the source node, from which the Dfs algorithm runs.
6.1055 + /// \param s is the source node.
6.1056 + DfsWizard<TR> &source(Node s)
6.1057 + {
6.1058 + Base::_source=s;
6.1059 + return *this;
6.1060 + }
6.1061 +
6.1062 + };
6.1063 +
6.1064 + ///Function type interface for Dfs algorithm.
6.1065 +
6.1066 + ///\ingroup search
6.1067 + ///Function type interface for Dfs algorithm.
6.1068 + ///
6.1069 + ///This function also has several
6.1070 + ///\ref named-templ-func-param "named parameters",
6.1071 + ///they are declared as the members of class \ref DfsWizard.
6.1072 + ///The following
6.1073 + ///example shows how to use these parameters.
6.1074 + ///\code
6.1075 + /// dfs(g,source).predMap(preds).run();
6.1076 + ///\endcode
6.1077 + ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
6.1078 + ///to the end of the parameter list.
6.1079 + ///\sa DfsWizard
6.1080 + ///\sa Dfs
6.1081 + template<class GR>
6.1082 + DfsWizard<DfsWizardBase<GR> >
6.1083 + dfs(const GR &g,typename GR::Node s=INVALID)
6.1084 + {
6.1085 + return DfsWizard<DfsWizardBase<GR> >(g,s);
6.1086 + }
6.1087 +
6.1088 +#ifdef DOXYGEN
6.1089 + /// \brief Visitor class for dfs.
6.1090 + ///
6.1091 + /// It gives a simple interface for a functional interface for dfs
6.1092 + /// traversal. The traversal on a linear data structure.
6.1093 + template <typename _Digraph>
6.1094 + struct DfsVisitor {
6.1095 + typedef _Digraph Digraph;
6.1096 + typedef typename Digraph::Arc Arc;
6.1097 + typedef typename Digraph::Node Node;
6.1098 + /// \brief Called when the arc reach a node.
6.1099 + ///
6.1100 + /// It is called when the dfs find an arc which target is not
6.1101 + /// reached yet.
6.1102 + void discover(const Arc& arc) {}
6.1103 + /// \brief Called when the node reached first time.
6.1104 + ///
6.1105 + /// It is Called when the node reached first time.
6.1106 + void reach(const Node& node) {}
6.1107 + /// \brief Called when we step back on an arc.
6.1108 + ///
6.1109 + /// It is called when the dfs should step back on the arc.
6.1110 + void backtrack(const Arc& arc) {}
6.1111 + /// \brief Called when we step back from the node.
6.1112 + ///
6.1113 + /// It is called when we step back from the node.
6.1114 + void leave(const Node& node) {}
6.1115 + /// \brief Called when the arc examined but target of the arc
6.1116 + /// already discovered.
6.1117 + ///
6.1118 + /// It called when the arc examined but the target of the arc
6.1119 + /// already discovered.
6.1120 + void examine(const Arc& arc) {}
6.1121 + /// \brief Called for the source node of the dfs.
6.1122 + ///
6.1123 + /// It is called for the source node of the dfs.
6.1124 + void start(const Node& node) {}
6.1125 + /// \brief Called when we leave the source node of the dfs.
6.1126 + ///
6.1127 + /// It is called when we leave the source node of the dfs.
6.1128 + void stop(const Node& node) {}
6.1129 +
6.1130 + };
6.1131 +#else
6.1132 + template <typename _Digraph>
6.1133 + struct DfsVisitor {
6.1134 + typedef _Digraph Digraph;
6.1135 + typedef typename Digraph::Arc Arc;
6.1136 + typedef typename Digraph::Node Node;
6.1137 + void discover(const Arc&) {}
6.1138 + void reach(const Node&) {}
6.1139 + void backtrack(const Arc&) {}
6.1140 + void leave(const Node&) {}
6.1141 + void examine(const Arc&) {}
6.1142 + void start(const Node&) {}
6.1143 + void stop(const Node&) {}
6.1144 +
6.1145 + template <typename _Visitor>
6.1146 + struct Constraints {
6.1147 + void constraints() {
6.1148 + Arc arc;
6.1149 + Node node;
6.1150 + visitor.discover(arc);
6.1151 + visitor.reach(node);
6.1152 + visitor.backtrack(arc);
6.1153 + visitor.leave(node);
6.1154 + visitor.examine(arc);
6.1155 + visitor.start(node);
6.1156 + visitor.stop(arc);
6.1157 + }
6.1158 + _Visitor& visitor;
6.1159 + };
6.1160 + };
6.1161 +#endif
6.1162 +
6.1163 + /// \brief Default traits class of DfsVisit class.
6.1164 + ///
6.1165 + /// Default traits class of DfsVisit class.
6.1166 + /// \param _Digraph Digraph type.
6.1167 + template<class _Digraph>
6.1168 + struct DfsVisitDefaultTraits {
6.1169 +
6.1170 + /// \brief The digraph type the algorithm runs on.
6.1171 + typedef _Digraph Digraph;
6.1172 +
6.1173 + /// \brief The type of the map that indicates which nodes are reached.
6.1174 + ///
6.1175 + /// The type of the map that indicates which nodes are reached.
6.1176 + /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
6.1177 + /// \todo named parameter to set this type, function to read and write.
6.1178 + typedef typename Digraph::template NodeMap<bool> ReachedMap;
6.1179 +
6.1180 + /// \brief Instantiates a ReachedMap.
6.1181 + ///
6.1182 + /// This function instantiates a \ref ReachedMap.
6.1183 + /// \param digraph is the digraph, to which
6.1184 + /// we would like to define the \ref ReachedMap.
6.1185 + static ReachedMap *createReachedMap(const Digraph &digraph) {
6.1186 + return new ReachedMap(digraph);
6.1187 + }
6.1188 +
6.1189 + };
6.1190 +
6.1191 + /// %DFS Visit algorithm class.
6.1192 +
6.1193 + /// \ingroup search
6.1194 + /// This class provides an efficient implementation of the %DFS algorithm
6.1195 + /// with visitor interface.
6.1196 + ///
6.1197 + /// The %DfsVisit class provides an alternative interface to the Dfs
6.1198 + /// class. It works with callback mechanism, the DfsVisit object calls
6.1199 + /// on every dfs event the \c Visitor class member functions.
6.1200 + ///
6.1201 + /// \param _Digraph The digraph type the algorithm runs on. The default value is
6.1202 + /// \ref ListDigraph. The value of _Digraph is not used directly by Dfs, it
6.1203 + /// is only passed to \ref DfsDefaultTraits.
6.1204 + /// \param _Visitor The Visitor object for the algorithm. The
6.1205 + /// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty Visitor which
6.1206 + /// does not observe the Dfs events. If you want to observe the dfs
6.1207 + /// events you should implement your own Visitor class.
6.1208 + /// \param _Traits Traits class to set various data types used by the
6.1209 + /// algorithm. The default traits class is
6.1210 + /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>".
6.1211 + /// See \ref DfsVisitDefaultTraits for the documentation of
6.1212 + /// a Dfs visit traits class.
6.1213 + ///
6.1214 + /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso
6.1215 +#ifdef DOXYGEN
6.1216 + template <typename _Digraph, typename _Visitor, typename _Traits>
6.1217 +#else
6.1218 + template <typename _Digraph = ListDigraph,
6.1219 + typename _Visitor = DfsVisitor<_Digraph>,
6.1220 + typename _Traits = DfsDefaultTraits<_Digraph> >
6.1221 +#endif
6.1222 + class DfsVisit {
6.1223 + public:
6.1224 +
6.1225 + /// \brief \ref Exception for uninitialized parameters.
6.1226 + ///
6.1227 + /// This error represents problems in the initialization
6.1228 + /// of the parameters of the algorithms.
6.1229 + class UninitializedParameter : public lemon::UninitializedParameter {
6.1230 + public:
6.1231 + virtual const char* what() const throw()
6.1232 + {
6.1233 + return "lemon::DfsVisit::UninitializedParameter";
6.1234 + }
6.1235 + };
6.1236 +
6.1237 + typedef _Traits Traits;
6.1238 +
6.1239 + typedef typename Traits::Digraph Digraph;
6.1240 +
6.1241 + typedef _Visitor Visitor;
6.1242 +
6.1243 + ///The type of the map indicating which nodes are reached.
6.1244 + typedef typename Traits::ReachedMap ReachedMap;
6.1245 +
6.1246 + private:
6.1247 +
6.1248 + typedef typename Digraph::Node Node;
6.1249 + typedef typename Digraph::NodeIt NodeIt;
6.1250 + typedef typename Digraph::Arc Arc;
6.1251 + typedef typename Digraph::OutArcIt OutArcIt;
6.1252 +
6.1253 + /// Pointer to the underlying digraph.
6.1254 + const Digraph *_digraph;
6.1255 + /// Pointer to the visitor object.
6.1256 + Visitor *_visitor;
6.1257 + ///Pointer to the map of reached status of the nodes.
6.1258 + ReachedMap *_reached;
6.1259 + ///Indicates if \ref _reached is locally allocated (\c true) or not.
6.1260 + bool local_reached;
6.1261 +
6.1262 + std::vector<typename Digraph::Arc> _stack;
6.1263 + int _stack_head;
6.1264 +
6.1265 + /// \brief Creates the maps if necessary.
6.1266 + ///
6.1267 + /// Creates the maps if necessary.
6.1268 + void create_maps() {
6.1269 + if(!_reached) {
6.1270 + local_reached = true;
6.1271 + _reached = Traits::createReachedMap(*_digraph);
6.1272 + }
6.1273 + }
6.1274 +
6.1275 + protected:
6.1276 +
6.1277 + DfsVisit() {}
6.1278 +
6.1279 + public:
6.1280 +
6.1281 + typedef DfsVisit Create;
6.1282 +
6.1283 + /// \name Named template parameters
6.1284 +
6.1285 + ///@{
6.1286 + template <class T>
6.1287 + struct DefReachedMapTraits : public Traits {
6.1288 + typedef T ReachedMap;
6.1289 + static ReachedMap *createReachedMap(const Digraph &digraph) {
6.1290 + throw UninitializedParameter();
6.1291 + }
6.1292 + };
6.1293 + /// \brief \ref named-templ-param "Named parameter" for setting
6.1294 + /// ReachedMap type
6.1295 + ///
6.1296 + /// \ref named-templ-param "Named parameter" for setting ReachedMap type
6.1297 + template <class T>
6.1298 + struct DefReachedMap : public DfsVisit< Digraph, Visitor,
6.1299 + DefReachedMapTraits<T> > {
6.1300 + typedef DfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;
6.1301 + };
6.1302 + ///@}
6.1303 +
6.1304 + public:
6.1305 +
6.1306 + /// \brief Constructor.
6.1307 + ///
6.1308 + /// Constructor.
6.1309 + ///
6.1310 + /// \param digraph the digraph the algorithm will run on.
6.1311 + /// \param visitor The visitor of the algorithm.
6.1312 + ///
6.1313 + DfsVisit(const Digraph& digraph, Visitor& visitor)
6.1314 + : _digraph(&digraph), _visitor(&visitor),
6.1315 + _reached(0), local_reached(false) {}
6.1316 +
6.1317 + /// \brief Destructor.
6.1318 + ///
6.1319 + /// Destructor.
6.1320 + ~DfsVisit() {
6.1321 + if(local_reached) delete _reached;
6.1322 + }
6.1323 +
6.1324 + /// \brief Sets the map indicating if a node is reached.
6.1325 + ///
6.1326 + /// Sets the map indicating if a node is reached.
6.1327 + /// If you don't use this function before calling \ref run(),
6.1328 + /// it will allocate one. The destuctor deallocates this
6.1329 + /// automatically allocated map, of course.
6.1330 + /// \return <tt> (*this) </tt>
6.1331 + DfsVisit &reachedMap(ReachedMap &m) {
6.1332 + if(local_reached) {
6.1333 + delete _reached;
6.1334 + local_reached=false;
6.1335 + }
6.1336 + _reached = &m;
6.1337 + return *this;
6.1338 + }
6.1339 +
6.1340 + public:
6.1341 + /// \name Execution control
6.1342 + /// The simplest way to execute the algorithm is to use
6.1343 + /// one of the member functions called \c run(...).
6.1344 + /// \n
6.1345 + /// If you need more control on the execution,
6.1346 + /// first you must call \ref init(), then you can adda source node
6.1347 + /// with \ref addSource().
6.1348 + /// Finally \ref start() will perform the actual path
6.1349 + /// computation.
6.1350 +
6.1351 + /// @{
6.1352 + /// \brief Initializes the internal data structures.
6.1353 + ///
6.1354 + /// Initializes the internal data structures.
6.1355 + ///
6.1356 + void init() {
6.1357 + create_maps();
6.1358 + _stack.resize(countNodes(*_digraph));
6.1359 + _stack_head = -1;
6.1360 + for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
6.1361 + _reached->set(u, false);
6.1362 + }
6.1363 + }
6.1364 +
6.1365 + /// \brief Adds a new source node.
6.1366 + ///
6.1367 + /// Adds a new source node to the set of nodes to be processed.
6.1368 + void addSource(Node s) {
6.1369 + if(!(*_reached)[s]) {
6.1370 + _reached->set(s,true);
6.1371 + _visitor->start(s);
6.1372 + _visitor->reach(s);
6.1373 + Arc e;
6.1374 + _digraph->firstOut(e, s);
6.1375 + if (e != INVALID) {
6.1376 + _stack[++_stack_head] = e;
6.1377 + } else {
6.1378 + _visitor->leave(s);
6.1379 + }
6.1380 + }
6.1381 + }
6.1382 +
6.1383 + /// \brief Processes the next arc.
6.1384 + ///
6.1385 + /// Processes the next arc.
6.1386 + ///
6.1387 + /// \return The processed arc.
6.1388 + ///
6.1389 + /// \pre The stack must not be empty!
6.1390 + Arc processNextArc() {
6.1391 + Arc e = _stack[_stack_head];
6.1392 + Node m = _digraph->target(e);
6.1393 + if(!(*_reached)[m]) {
6.1394 + _visitor->discover(e);
6.1395 + _visitor->reach(m);
6.1396 + _reached->set(m, true);
6.1397 + _digraph->firstOut(_stack[++_stack_head], m);
6.1398 + } else {
6.1399 + _visitor->examine(e);
6.1400 + m = _digraph->source(e);
6.1401 + _digraph->nextOut(_stack[_stack_head]);
6.1402 + }
6.1403 + while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
6.1404 + _visitor->leave(m);
6.1405 + --_stack_head;
6.1406 + if (_stack_head >= 0) {
6.1407 + _visitor->backtrack(_stack[_stack_head]);
6.1408 + m = _digraph->source(_stack[_stack_head]);
6.1409 + _digraph->nextOut(_stack[_stack_head]);
6.1410 + } else {
6.1411 + _visitor->stop(m);
6.1412 + }
6.1413 + }
6.1414 + return e;
6.1415 + }
6.1416 +
6.1417 + /// \brief Next arc to be processed.
6.1418 + ///
6.1419 + /// Next arc to be processed.
6.1420 + ///
6.1421 + /// \return The next arc to be processed or INVALID if the stack is
6.1422 + /// empty.
6.1423 + Arc nextArc() {
6.1424 + return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
6.1425 + }
6.1426 +
6.1427 + /// \brief Returns \c false if there are nodes
6.1428 + /// to be processed in the queue
6.1429 + ///
6.1430 + /// Returns \c false if there are nodes
6.1431 + /// to be processed in the queue
6.1432 + bool emptyQueue() { return _stack_head < 0; }
6.1433 +
6.1434 + /// \brief Returns the number of the nodes to be processed.
6.1435 + ///
6.1436 + /// Returns the number of the nodes to be processed in the queue.
6.1437 + int queueSize() { return _stack_head + 1; }
6.1438 +
6.1439 + /// \brief Executes the algorithm.
6.1440 + ///
6.1441 + /// Executes the algorithm.
6.1442 + ///
6.1443 + /// \pre init() must be called and at least one node should be added
6.1444 + /// with addSource() before using this function.
6.1445 + void start() {
6.1446 + while ( !emptyQueue() ) processNextArc();
6.1447 + }
6.1448 +
6.1449 + /// \brief Executes the algorithm until \c dest is reached.
6.1450 + ///
6.1451 + /// Executes the algorithm until \c dest is reached.
6.1452 + ///
6.1453 + /// \pre init() must be called and at least one node should be added
6.1454 + /// with addSource() before using this function.
6.1455 + void start(Node dest) {
6.1456 + while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest )
6.1457 + processNextArc();
6.1458 + }
6.1459 +
6.1460 + /// \brief Executes the algorithm until a condition is met.
6.1461 + ///
6.1462 + /// Executes the algorithm until a condition is met.
6.1463 + ///
6.1464 + /// \pre init() must be called and at least one node should be added
6.1465 + /// with addSource() before using this function.
6.1466 + ///
6.1467 + /// \param em must be a bool (or convertible) arc map. The algorithm
6.1468 + /// will stop when it reaches an arc \c e with <tt>em[e]</tt> true.
6.1469 + ///
6.1470 + ///\return The reached arc \c e with <tt>em[e]</tt> true or
6.1471 + ///\c INVALID if no such arc was found.
6.1472 + ///
6.1473 + /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an arc map,
6.1474 + /// not a node map.
6.1475 + template <typename EM>
6.1476 + Arc start(const EM &em) {
6.1477 + while ( !emptyQueue() && !em[_stack[_stack_head]] )
6.1478 + processNextArc();
6.1479 + return emptyQueue() ? INVALID : _stack[_stack_head];
6.1480 + }
6.1481 +
6.1482 + /// \brief Runs %DFSVisit algorithm from node \c s.
6.1483 + ///
6.1484 + /// This method runs the %DFS algorithm from a root node \c s.
6.1485 + /// \note d.run(s) is just a shortcut of the following code.
6.1486 + ///\code
6.1487 + /// d.init();
6.1488 + /// d.addSource(s);
6.1489 + /// d.start();
6.1490 + ///\endcode
6.1491 + void run(Node s) {
6.1492 + init();
6.1493 + addSource(s);
6.1494 + start();
6.1495 + }
6.1496 +
6.1497 + /// \brief Runs %DFSVisit algorithm to visit all nodes in the digraph.
6.1498 +
6.1499 + /// This method runs the %DFS algorithm in order to
6.1500 + /// compute the %DFS path to each node. The algorithm computes
6.1501 + /// - The %DFS tree.
6.1502 + /// - The distance of each node from the root in the %DFS tree.
6.1503 + ///
6.1504 + ///\note d.run() is just a shortcut of the following code.
6.1505 + ///\code
6.1506 + /// d.init();
6.1507 + /// for (NodeIt it(digraph); it != INVALID; ++it) {
6.1508 + /// if (!d.reached(it)) {
6.1509 + /// d.addSource(it);
6.1510 + /// d.start();
6.1511 + /// }
6.1512 + /// }
6.1513 + ///\endcode
6.1514 + void run() {
6.1515 + init();
6.1516 + for (NodeIt it(*_digraph); it != INVALID; ++it) {
6.1517 + if (!reached(it)) {
6.1518 + addSource(it);
6.1519 + start();
6.1520 + }
6.1521 + }
6.1522 + }
6.1523 + ///@}
6.1524 +
6.1525 + /// \name Query Functions
6.1526 + /// The result of the %DFS algorithm can be obtained using these
6.1527 + /// functions.\n
6.1528 + /// Before the use of these functions,
6.1529 + /// either run() or start() must be called.
6.1530 + ///@{
6.1531 + /// \brief Checks if a node is reachable from the root.
6.1532 + ///
6.1533 + /// Returns \c true if \c v is reachable from the root(s).
6.1534 + /// \warning The source nodes are inditated as unreachable.
6.1535 + /// \pre Either \ref run() or \ref start()
6.1536 + /// must be called before using this function.
6.1537 + ///
6.1538 + bool reached(Node v) { return (*_reached)[v]; }
6.1539 + ///@}
6.1540 + };
6.1541 +
6.1542 +
6.1543 +} //END OF NAMESPACE LEMON
6.1544 +
6.1545 +#endif
6.1546 +
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/lemon/dijkstra.h Thu Feb 07 21:37:07 2008 +0000
7.3 @@ -0,0 +1,1209 @@
7.4 +/* -*- C++ -*-
7.5 + *
7.6 + * This file is a part of LEMON, a generic C++ optimization library
7.7 + *
7.8 + * Copyright (C) 2003-2008
7.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
7.11 + *
7.12 + * Permission to use, modify and distribute this software is granted
7.13 + * provided that this copyright notice appears in all copies. For
7.14 + * precise terms see the accompanying LICENSE file.
7.15 + *
7.16 + * This software is provided "AS IS" with no warranty of any kind,
7.17 + * express or implied, and with no claim as to its suitability for any
7.18 + * purpose.
7.19 + *
7.20 + */
7.21 +
7.22 +#ifndef LEMON_DIJKSTRA_H
7.23 +#define LEMON_DIJKSTRA_H
7.24 +
7.25 +///\ingroup shortest_path
7.26 +///\file
7.27 +///\brief Dijkstra algorithm.
7.28 +///
7.29 +
7.30 +#include <lemon/list_digraph.h>
7.31 +#include <lemon/bin_heap.h>
7.32 +#include <lemon/bits/path_dump.h>
7.33 +#include <lemon/bits/invalid.h>
7.34 +#include <lemon/error.h>
7.35 +#include <lemon/maps.h>
7.36 +
7.37 +
7.38 +namespace lemon {
7.39 +
7.40 + /// \brief Default OperationTraits for the Dijkstra algorithm class.
7.41 + ///
7.42 + /// It defines all computational operations and constants which are
7.43 + /// used in the Dijkstra algorithm.
7.44 + template <typename Value>
7.45 + struct DijkstraDefaultOperationTraits {
7.46 + /// \brief Gives back the zero value of the type.
7.47 + static Value zero() {
7.48 + return static_cast<Value>(0);
7.49 + }
7.50 + /// \brief Gives back the sum of the given two elements.
7.51 + static Value plus(const Value& left, const Value& right) {
7.52 + return left + right;
7.53 + }
7.54 + /// \brief Gives back true only if the first value less than the second.
7.55 + static bool less(const Value& left, const Value& right) {
7.56 + return left < right;
7.57 + }
7.58 + };
7.59 +
7.60 + /// \brief Widest path OperationTraits for the Dijkstra algorithm class.
7.61 + ///
7.62 + /// It defines all computational operations and constants which are
7.63 + /// used in the Dijkstra algorithm for widest path computation.
7.64 + template <typename Value>
7.65 + struct DijkstraWidestPathOperationTraits {
7.66 + /// \brief Gives back the maximum value of the type.
7.67 + static Value zero() {
7.68 + return std::numeric_limits<Value>::max();
7.69 + }
7.70 + /// \brief Gives back the minimum of the given two elements.
7.71 + static Value plus(const Value& left, const Value& right) {
7.72 + return std::min(left, right);
7.73 + }
7.74 + /// \brief Gives back true only if the first value less than the second.
7.75 + static bool less(const Value& left, const Value& right) {
7.76 + return left < right;
7.77 + }
7.78 + };
7.79 +
7.80 + ///Default traits class of Dijkstra class.
7.81 +
7.82 + ///Default traits class of Dijkstra class.
7.83 + ///\param GR Digraph type.
7.84 + ///\param LM Type of length map.
7.85 + template<class GR, class LM>
7.86 + struct DijkstraDefaultTraits
7.87 + {
7.88 + ///The digraph type the algorithm runs on.
7.89 + typedef GR Digraph;
7.90 + ///The type of the map that stores the arc lengths.
7.91 +
7.92 + ///The type of the map that stores the arc lengths.
7.93 + ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
7.94 + typedef LM LengthMap;
7.95 + //The type of the length of the arcs.
7.96 + typedef typename LM::Value Value;
7.97 + /// Operation traits for Dijkstra algorithm.
7.98 +
7.99 + /// It defines the used operation by the algorithm.
7.100 + /// \see DijkstraDefaultOperationTraits
7.101 + typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
7.102 + /// The cross reference type used by heap.
7.103 +
7.104 +
7.105 + /// The cross reference type used by heap.
7.106 + /// Usually it is \c Digraph::NodeMap<int>.
7.107 + typedef typename Digraph::template NodeMap<int> HeapCrossRef;
7.108 + ///Instantiates a HeapCrossRef.
7.109 +
7.110 + ///This function instantiates a \c HeapCrossRef.
7.111 + /// \param G is the digraph, to which we would like to define the
7.112 + /// HeapCrossRef.
7.113 + static HeapCrossRef *createHeapCrossRef(const GR &G)
7.114 + {
7.115 + return new HeapCrossRef(G);
7.116 + }
7.117 +
7.118 + ///The heap type used by Dijkstra algorithm.
7.119 +
7.120 + ///The heap type used by Dijkstra algorithm.
7.121 + ///
7.122 + ///\sa BinHeap
7.123 + ///\sa Dijkstra
7.124 + typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap;
7.125 +
7.126 + static Heap *createHeap(HeapCrossRef& R)
7.127 + {
7.128 + return new Heap(R);
7.129 + }
7.130 +
7.131 + ///\brief The type of the map that stores the last
7.132 + ///arcs of the shortest paths.
7.133 + ///
7.134 + ///The type of the map that stores the last
7.135 + ///arcs of the shortest paths.
7.136 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
7.137 + ///
7.138 + typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap;
7.139 + ///Instantiates a PredMap.
7.140 +
7.141 + ///This function instantiates a \c PredMap.
7.142 + ///\param G is the digraph, to which we would like to define the PredMap.
7.143 + ///\todo The digraph alone may be insufficient for the initialization
7.144 + static PredMap *createPredMap(const GR &G)
7.145 + {
7.146 + return new PredMap(G);
7.147 + }
7.148 +
7.149 + ///The type of the map that stores whether a nodes is processed.
7.150 +
7.151 + ///The type of the map that stores whether a nodes is processed.
7.152 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
7.153 + ///By default it is a NullMap.
7.154 + ///\todo If it is set to a real map,
7.155 + ///Dijkstra::processed() should read this.
7.156 + ///\todo named parameter to set this type, function to read and write.
7.157 + typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
7.158 + ///Instantiates a ProcessedMap.
7.159 +
7.160 + ///This function instantiates a \c ProcessedMap.
7.161 + ///\param g is the digraph, to which
7.162 + ///we would like to define the \c ProcessedMap
7.163 +#ifdef DOXYGEN
7.164 + static ProcessedMap *createProcessedMap(const GR &g)
7.165 +#else
7.166 + static ProcessedMap *createProcessedMap(const GR &)
7.167 +#endif
7.168 + {
7.169 + return new ProcessedMap();
7.170 + }
7.171 + ///The type of the map that stores the dists of the nodes.
7.172 +
7.173 + ///The type of the map that stores the dists of the nodes.
7.174 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
7.175 + ///
7.176 + typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
7.177 + ///Instantiates a DistMap.
7.178 +
7.179 + ///This function instantiates a \ref DistMap.
7.180 + ///\param G is the digraph, to which we would like to define the \ref DistMap
7.181 + static DistMap *createDistMap(const GR &G)
7.182 + {
7.183 + return new DistMap(G);
7.184 + }
7.185 + };
7.186 +
7.187 + ///%Dijkstra algorithm class.
7.188 +
7.189 + /// \ingroup shortest_path
7.190 + ///This class provides an efficient implementation of %Dijkstra algorithm.
7.191 + ///The arc lengths are passed to the algorithm using a
7.192 + ///\ref concepts::ReadMap "ReadMap",
7.193 + ///so it is easy to change it to any kind of length.
7.194 + ///
7.195 + ///The type of the length is determined by the
7.196 + ///\ref concepts::ReadMap::Value "Value" of the length map.
7.197 + ///
7.198 + ///It is also possible to change the underlying priority heap.
7.199 + ///
7.200 + ///\param GR The digraph type the algorithm runs on. The default value
7.201 + ///is \ref ListDigraph. The value of GR is not used directly by
7.202 + ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits.
7.203 + ///\param LM This read-only ArcMap determines the lengths of the
7.204 + ///arcs. It is read once for each arc, so the map may involve in
7.205 + ///relatively time consuming process to compute the arc length if
7.206 + ///it is necessary. The default map type is \ref
7.207 + ///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". The value
7.208 + ///of LM is not used directly by Dijkstra, it is only passed to \ref
7.209 + ///DijkstraDefaultTraits. \param TR Traits class to set
7.210 + ///various data types used by the algorithm. The default traits
7.211 + ///class is \ref DijkstraDefaultTraits
7.212 + ///"DijkstraDefaultTraits<GR,LM>". See \ref
7.213 + ///DijkstraDefaultTraits for the documentation of a Dijkstra traits
7.214 + ///class.
7.215 + ///
7.216 + ///\author Jacint Szabo and Alpar Juttner
7.217 +
7.218 +#ifdef DOXYGEN
7.219 + template <typename GR, typename LM, typename TR>
7.220 +#else
7.221 + template <typename GR=ListDigraph,
7.222 + typename LM=typename GR::template ArcMap<int>,
7.223 + typename TR=DijkstraDefaultTraits<GR,LM> >
7.224 +#endif
7.225 + class Dijkstra {
7.226 + public:
7.227 + /**
7.228 + * \brief \ref Exception for uninitialized parameters.
7.229 + *
7.230 + * This error represents problems in the initialization
7.231 + * of the parameters of the algorithms.
7.232 + */
7.233 + class UninitializedParameter : public lemon::UninitializedParameter {
7.234 + public:
7.235 + virtual const char* what() const throw() {
7.236 + return "lemon::Dijkstra::UninitializedParameter";
7.237 + }
7.238 + };
7.239 +
7.240 + typedef TR Traits;
7.241 + ///The type of the underlying digraph.
7.242 + typedef typename TR::Digraph Digraph;
7.243 + ///\e
7.244 + typedef typename Digraph::Node Node;
7.245 + ///\e
7.246 + typedef typename Digraph::NodeIt NodeIt;
7.247 + ///\e
7.248 + typedef typename Digraph::Arc Arc;
7.249 + ///\e
7.250 + typedef typename Digraph::OutArcIt OutArcIt;
7.251 +
7.252 + ///The type of the length of the arcs.
7.253 + typedef typename TR::LengthMap::Value Value;
7.254 + ///The type of the map that stores the arc lengths.
7.255 + typedef typename TR::LengthMap LengthMap;
7.256 + ///\brief The type of the map that stores the last
7.257 + ///arcs of the shortest paths.
7.258 + typedef typename TR::PredMap PredMap;
7.259 + ///The type of the map indicating if a node is processed.
7.260 + typedef typename TR::ProcessedMap ProcessedMap;
7.261 + ///The type of the map that stores the dists of the nodes.
7.262 + typedef typename TR::DistMap DistMap;
7.263 + ///The cross reference type used for the current heap.
7.264 + typedef typename TR::HeapCrossRef HeapCrossRef;
7.265 + ///The heap type used by the dijkstra algorithm.
7.266 + typedef typename TR::Heap Heap;
7.267 + ///The operation traits.
7.268 + typedef typename TR::OperationTraits OperationTraits;
7.269 + private:
7.270 + /// Pointer to the underlying digraph.
7.271 + const Digraph *G;
7.272 + /// Pointer to the length map
7.273 + const LengthMap *length;
7.274 + ///Pointer to the map of predecessors arcs.
7.275 + PredMap *_pred;
7.276 + ///Indicates if \ref _pred is locally allocated (\c true) or not.
7.277 + bool local_pred;
7.278 + ///Pointer to the map of distances.
7.279 + DistMap *_dist;
7.280 + ///Indicates if \ref _dist is locally allocated (\c true) or not.
7.281 + bool local_dist;
7.282 + ///Pointer to the map of processed status of the nodes.
7.283 + ProcessedMap *_processed;
7.284 + ///Indicates if \ref _processed is locally allocated (\c true) or not.
7.285 + bool local_processed;
7.286 + ///Pointer to the heap cross references.
7.287 + HeapCrossRef *_heap_cross_ref;
7.288 + ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
7.289 + bool local_heap_cross_ref;
7.290 + ///Pointer to the heap.
7.291 + Heap *_heap;
7.292 + ///Indicates if \ref _heap is locally allocated (\c true) or not.
7.293 + bool local_heap;
7.294 +
7.295 + ///Creates the maps if necessary.
7.296 +
7.297 + ///\todo Better memory allocation (instead of new).
7.298 + void create_maps()
7.299 + {
7.300 + if(!_pred) {
7.301 + local_pred = true;
7.302 + _pred = Traits::createPredMap(*G);
7.303 + }
7.304 + if(!_dist) {
7.305 + local_dist = true;
7.306 + _dist = Traits::createDistMap(*G);
7.307 + }
7.308 + if(!_processed) {
7.309 + local_processed = true;
7.310 + _processed = Traits::createProcessedMap(*G);
7.311 + }
7.312 + if (!_heap_cross_ref) {
7.313 + local_heap_cross_ref = true;
7.314 + _heap_cross_ref = Traits::createHeapCrossRef(*G);
7.315 + }
7.316 + if (!_heap) {
7.317 + local_heap = true;
7.318 + _heap = Traits::createHeap(*_heap_cross_ref);
7.319 + }
7.320 + }
7.321 +
7.322 + public :
7.323 +
7.324 + typedef Dijkstra Create;
7.325 +
7.326 + ///\name Named template parameters
7.327 +
7.328 + ///@{
7.329 +
7.330 + template <class T>
7.331 + struct DefPredMapTraits : public Traits {
7.332 + typedef T PredMap;
7.333 + static PredMap *createPredMap(const Digraph &)
7.334 + {
7.335 + throw UninitializedParameter();
7.336 + }
7.337 + };
7.338 + ///\ref named-templ-param "Named parameter" for setting PredMap type
7.339 +
7.340 + ///\ref named-templ-param "Named parameter" for setting PredMap type
7.341 + ///
7.342 + template <class T>
7.343 + struct DefPredMap
7.344 + : public Dijkstra< Digraph, LengthMap, DefPredMapTraits<T> > {
7.345 + typedef Dijkstra< Digraph, LengthMap, DefPredMapTraits<T> > Create;
7.346 + };
7.347 +
7.348 + template <class T>
7.349 + struct DefDistMapTraits : public Traits {
7.350 + typedef T DistMap;
7.351 + static DistMap *createDistMap(const Digraph &)
7.352 + {
7.353 + throw UninitializedParameter();
7.354 + }
7.355 + };
7.356 + ///\ref named-templ-param "Named parameter" for setting DistMap type
7.357 +
7.358 + ///\ref named-templ-param "Named parameter" for setting DistMap type
7.359 + ///
7.360 + template <class T>
7.361 + struct DefDistMap
7.362 + : public Dijkstra< Digraph, LengthMap, DefDistMapTraits<T> > {
7.363 + typedef Dijkstra< Digraph, LengthMap, DefDistMapTraits<T> > Create;
7.364 + };
7.365 +
7.366 + template <class T>
7.367 + struct DefProcessedMapTraits : public Traits {
7.368 + typedef T ProcessedMap;
7.369 + static ProcessedMap *createProcessedMap(const Digraph &G)
7.370 + {
7.371 + throw UninitializedParameter();
7.372 + }
7.373 + };
7.374 + ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
7.375 +
7.376 + ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
7.377 + ///
7.378 + template <class T>
7.379 + struct DefProcessedMap
7.380 + : public Dijkstra< Digraph, LengthMap, DefProcessedMapTraits<T> > {
7.381 + typedef Dijkstra< Digraph, LengthMap, DefProcessedMapTraits<T> > Create;
7.382 + };
7.383 +
7.384 + struct DefDigraphProcessedMapTraits : public Traits {
7.385 + typedef typename Digraph::template NodeMap<bool> ProcessedMap;
7.386 + static ProcessedMap *createProcessedMap(const Digraph &G)
7.387 + {
7.388 + return new ProcessedMap(G);
7.389 + }
7.390 + };
7.391 + ///\brief \ref named-templ-param "Named parameter"
7.392 + ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
7.393 + ///
7.394 + ///\ref named-templ-param "Named parameter"
7.395 + ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
7.396 + ///If you don't set it explicitely, it will be automatically allocated.
7.397 + template <class T>
7.398 + struct DefProcessedMapToBeDefaultMap
7.399 + : public Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> {
7.400 + typedef Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> Create;
7.401 + };
7.402 +
7.403 + template <class H, class CR>
7.404 + struct DefHeapTraits : public Traits {
7.405 + typedef CR HeapCrossRef;
7.406 + typedef H Heap;
7.407 + static HeapCrossRef *createHeapCrossRef(const Digraph &) {
7.408 + throw UninitializedParameter();
7.409 + }
7.410 + static Heap *createHeap(HeapCrossRef &)
7.411 + {
7.412 + throw UninitializedParameter();
7.413 + }
7.414 + };
7.415 + ///\brief \ref named-templ-param "Named parameter" for setting
7.416 + ///heap and cross reference type
7.417 + ///
7.418 + ///\ref named-templ-param "Named parameter" for setting heap and cross
7.419 + ///reference type
7.420 + ///
7.421 + template <class H, class CR = typename Digraph::template NodeMap<int> >
7.422 + struct DefHeap
7.423 + : public Dijkstra< Digraph, LengthMap, DefHeapTraits<H, CR> > {
7.424 + typedef Dijkstra< Digraph, LengthMap, DefHeapTraits<H, CR> > Create;
7.425 + };
7.426 +
7.427 + template <class H, class CR>
7.428 + struct DefStandardHeapTraits : public Traits {
7.429 + typedef CR HeapCrossRef;
7.430 + typedef H Heap;
7.431 + static HeapCrossRef *createHeapCrossRef(const Digraph &G) {
7.432 + return new HeapCrossRef(G);
7.433 + }
7.434 + static Heap *createHeap(HeapCrossRef &R)
7.435 + {
7.436 + return new Heap(R);
7.437 + }
7.438 + };
7.439 + ///\brief \ref named-templ-param "Named parameter" for setting
7.440 + ///heap and cross reference type with automatic allocation
7.441 + ///
7.442 + ///\ref named-templ-param "Named parameter" for setting heap and cross
7.443 + ///reference type. It can allocate the heap and the cross reference
7.444 + ///object if the cross reference's constructor waits for the digraph as
7.445 + ///parameter and the heap's constructor waits for the cross reference.
7.446 + template <class H, class CR = typename Digraph::template NodeMap<int> >
7.447 + struct DefStandardHeap
7.448 + : public Dijkstra< Digraph, LengthMap, DefStandardHeapTraits<H, CR> > {
7.449 + typedef Dijkstra< Digraph, LengthMap, DefStandardHeapTraits<H, CR> >
7.450 + Create;
7.451 + };
7.452 +
7.453 + template <class T>
7.454 + struct DefOperationTraitsTraits : public Traits {
7.455 + typedef T OperationTraits;
7.456 + };
7.457 +
7.458 + /// \brief \ref named-templ-param "Named parameter" for setting
7.459 + /// OperationTraits type
7.460 + ///
7.461 + /// \ref named-templ-param "Named parameter" for setting OperationTraits
7.462 + /// type
7.463 + template <class T>
7.464 + struct DefOperationTraits
7.465 + : public Dijkstra<Digraph, LengthMap, DefOperationTraitsTraits<T> > {
7.466 + typedef Dijkstra<Digraph, LengthMap, DefOperationTraitsTraits<T> >
7.467 + Create;
7.468 + };
7.469 +
7.470 + ///@}
7.471 +
7.472 +
7.473 + protected:
7.474 +
7.475 + Dijkstra() {}
7.476 +
7.477 + public:
7.478 +
7.479 + ///Constructor.
7.480 +
7.481 + ///\param _G the digraph the algorithm will run on.
7.482 + ///\param _length the length map used by the algorithm.
7.483 + Dijkstra(const Digraph& _G, const LengthMap& _length) :
7.484 + G(&_G), length(&_length),
7.485 + _pred(NULL), local_pred(false),
7.486 + _dist(NULL), local_dist(false),
7.487 + _processed(NULL), local_processed(false),
7.488 + _heap_cross_ref(NULL), local_heap_cross_ref(false),
7.489 + _heap(NULL), local_heap(false)
7.490 + { }
7.491 +
7.492 + ///Destructor.
7.493 + ~Dijkstra()
7.494 + {
7.495 + if(local_pred) delete _pred;
7.496 + if(local_dist) delete _dist;
7.497 + if(local_processed) delete _processed;
7.498 + if(local_heap_cross_ref) delete _heap_cross_ref;
7.499 + if(local_heap) delete _heap;
7.500 + }
7.501 +
7.502 + ///Sets the length map.
7.503 +
7.504 + ///Sets the length map.
7.505 + ///\return <tt> (*this) </tt>
7.506 + Dijkstra &lengthMap(const LengthMap &m)
7.507 + {
7.508 + length = &m;
7.509 + return *this;
7.510 + }
7.511 +
7.512 + ///Sets the map storing the predecessor arcs.
7.513 +
7.514 + ///Sets the map storing the predecessor arcs.
7.515 + ///If you don't use this function before calling \ref run(),
7.516 + ///it will allocate one. The destuctor deallocates this
7.517 + ///automatically allocated map, of course.
7.518 + ///\return <tt> (*this) </tt>
7.519 + Dijkstra &predMap(PredMap &m)
7.520 + {
7.521 + if(local_pred) {
7.522 + delete _pred;
7.523 + local_pred=false;
7.524 + }
7.525 + _pred = &m;
7.526 + return *this;
7.527 + }
7.528 +
7.529 + ///Sets the map storing the distances calculated by the algorithm.
7.530 +
7.531 + ///Sets the map storing the distances calculated by the algorithm.
7.532 + ///If you don't use this function before calling \ref run(),
7.533 + ///it will allocate one. The destuctor deallocates this
7.534 + ///automatically allocated map, of course.
7.535 + ///\return <tt> (*this) </tt>
7.536 + Dijkstra &distMap(DistMap &m)
7.537 + {
7.538 + if(local_dist) {
7.539 + delete _dist;
7.540 + local_dist=false;
7.541 + }
7.542 + _dist = &m;
7.543 + return *this;
7.544 + }
7.545 +
7.546 + ///Sets the heap and the cross reference used by algorithm.
7.547 +
7.548 + ///Sets the heap and the cross reference used by algorithm.
7.549 + ///If you don't use this function before calling \ref run(),
7.550 + ///it will allocate one. The destuctor deallocates this
7.551 + ///automatically allocated heap and cross reference, of course.
7.552 + ///\return <tt> (*this) </tt>
7.553 + Dijkstra &heap(Heap& hp, HeapCrossRef &cr)
7.554 + {
7.555 + if(local_heap_cross_ref) {
7.556 + delete _heap_cross_ref;
7.557 + local_heap_cross_ref=false;
7.558 + }
7.559 + _heap_cross_ref = &cr;
7.560 + if(local_heap) {
7.561 + delete _heap;
7.562 + local_heap=false;
7.563 + }
7.564 + _heap = &hp;
7.565 + return *this;
7.566 + }
7.567 +
7.568 + private:
7.569 + void finalizeNodeData(Node v,Value dst)
7.570 + {
7.571 + _processed->set(v,true);
7.572 + _dist->set(v, dst);
7.573 + }
7.574 +
7.575 + public:
7.576 +
7.577 + typedef PredMapPath<Digraph, PredMap> Path;
7.578 +
7.579 + ///\name Execution control
7.580 + ///The simplest way to execute the algorithm is to use
7.581 + ///one of the member functions called \c run(...).
7.582 + ///\n
7.583 + ///If you need more control on the execution,
7.584 + ///first you must call \ref init(), then you can add several source nodes
7.585 + ///with \ref addSource().
7.586 + ///Finally \ref start() will perform the actual path
7.587 + ///computation.
7.588 +
7.589 + ///@{
7.590 +
7.591 + ///Initializes the internal data structures.
7.592 +
7.593 + ///Initializes the internal data structures.
7.594 + ///
7.595 + void init()
7.596 + {
7.597 + create_maps();
7.598 + _heap->clear();
7.599 + for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
7.600 + _pred->set(u,INVALID);
7.601 + _processed->set(u,false);
7.602 + _heap_cross_ref->set(u,Heap::PRE_HEAP);
7.603 + }
7.604 + }
7.605 +
7.606 + ///Adds a new source node.
7.607 +
7.608 + ///Adds a new source node to the priority heap.
7.609 + ///
7.610 + ///The optional second parameter is the initial distance of the node.
7.611 + ///
7.612 + ///It checks if the node has already been added to the heap and
7.613 + ///it is pushed to the heap only if either it was not in the heap
7.614 + ///or the shortest path found till then is shorter than \c dst.
7.615 + void addSource(Node s,Value dst=OperationTraits::zero())
7.616 + {
7.617 + if(_heap->state(s) != Heap::IN_HEAP) {
7.618 + _heap->push(s,dst);
7.619 + } else if(OperationTraits::less((*_heap)[s], dst)) {
7.620 + _heap->set(s,dst);
7.621 + _pred->set(s,INVALID);
7.622 + }
7.623 + }
7.624 +
7.625 + ///Processes the next node in the priority heap
7.626 +
7.627 + ///Processes the next node in the priority heap.
7.628 + ///
7.629 + ///\return The processed node.
7.630 + ///
7.631 + ///\warning The priority heap must not be empty!
7.632 + Node processNextNode()
7.633 + {
7.634 + Node v=_heap->top();
7.635 + Value oldvalue=_heap->prio();
7.636 + _heap->pop();
7.637 + finalizeNodeData(v,oldvalue);
7.638 +
7.639 + for(OutArcIt e(*G,v); e!=INVALID; ++e) {
7.640 + Node w=G->target(e);
7.641 + switch(_heap->state(w)) {
7.642 + case Heap::PRE_HEAP:
7.643 + _heap->push(w,OperationTraits::plus(oldvalue, (*length)[e]));
7.644 + _pred->set(w,e);
7.645 + break;
7.646 + case Heap::IN_HEAP:
7.647 + {
7.648 + Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]);
7.649 + if ( OperationTraits::less(newvalue, (*_heap)[w]) ) {
7.650 + _heap->decrease(w, newvalue);
7.651 + _pred->set(w,e);
7.652 + }
7.653 + }
7.654 + break;
7.655 + case Heap::POST_HEAP:
7.656 + break;
7.657 + }
7.658 + }
7.659 + return v;
7.660 + }
7.661 +
7.662 + ///Next node to be processed.
7.663 +
7.664 + ///Next node to be processed.
7.665 + ///
7.666 + ///\return The next node to be processed or INVALID if the priority heap
7.667 + /// is empty.
7.668 + Node nextNode()
7.669 + {
7.670 + return !_heap->empty()?_heap->top():INVALID;
7.671 + }
7.672 +
7.673 + ///\brief Returns \c false if there are nodes
7.674 + ///to be processed in the priority heap
7.675 + ///
7.676 + ///Returns \c false if there are nodes
7.677 + ///to be processed in the priority heap
7.678 + bool emptyQueue() { return _heap->empty(); }
7.679 + ///Returns the number of the nodes to be processed in the priority heap
7.680 +
7.681 + ///Returns the number of the nodes to be processed in the priority heap
7.682 + ///
7.683 + int queueSize() { return _heap->size(); }
7.684 +
7.685 + ///Executes the algorithm.
7.686 +
7.687 + ///Executes the algorithm.
7.688 + ///
7.689 + ///\pre init() must be called and at least one node should be added
7.690 + ///with addSource() before using this function.
7.691 + ///
7.692 + ///This method runs the %Dijkstra algorithm from the root node(s)
7.693 + ///in order to
7.694 + ///compute the
7.695 + ///shortest path to each node. The algorithm computes
7.696 + ///- The shortest path tree.
7.697 + ///- The distance of each node from the root(s).
7.698 + ///
7.699 + void start()
7.700 + {
7.701 + while ( !_heap->empty() ) processNextNode();
7.702 + }
7.703 +
7.704 + ///Executes the algorithm until \c dest is reached.
7.705 +
7.706 + ///Executes the algorithm until \c dest is reached.
7.707 + ///
7.708 + ///\pre init() must be called and at least one node should be added
7.709 + ///with addSource() before using this function.
7.710 + ///
7.711 + ///This method runs the %Dijkstra algorithm from the root node(s)
7.712 + ///in order to
7.713 + ///compute the
7.714 + ///shortest path to \c dest. The algorithm computes
7.715 + ///- The shortest path to \c dest.
7.716 + ///- The distance of \c dest from the root(s).
7.717 + ///
7.718 + void start(Node dest)
7.719 + {
7.720 + while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
7.721 + if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
7.722 + }
7.723 +
7.724 + ///Executes the algorithm until a condition is met.
7.725 +
7.726 + ///Executes the algorithm until a condition is met.
7.727 + ///
7.728 + ///\pre init() must be called and at least one node should be added
7.729 + ///with addSource() before using this function.
7.730 + ///
7.731 + ///\param nm must be a bool (or convertible) node map. The algorithm
7.732 + ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
7.733 + ///
7.734 + ///\return The reached node \c v with <tt>nm[v]</tt> true or
7.735 + ///\c INVALID if no such node was found.
7.736 + template<class NodeBoolMap>
7.737 + Node start(const NodeBoolMap &nm)
7.738 + {
7.739 + while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
7.740 + if ( _heap->empty() ) return INVALID;
7.741 + finalizeNodeData(_heap->top(),_heap->prio());
7.742 + return _heap->top();
7.743 + }
7.744 +
7.745 + ///Runs %Dijkstra algorithm from node \c s.
7.746 +
7.747 + ///This method runs the %Dijkstra algorithm from a root node \c s
7.748 + ///in order to
7.749 + ///compute the
7.750 + ///shortest path to each node. The algorithm computes
7.751 + ///- The shortest path tree.
7.752 + ///- The distance of each node from the root.
7.753 + ///
7.754 + ///\note d.run(s) is just a shortcut of the following code.
7.755 + ///\code
7.756 + /// d.init();
7.757 + /// d.addSource(s);
7.758 + /// d.start();
7.759 + ///\endcode
7.760 + void run(Node s) {
7.761 + init();
7.762 + addSource(s);
7.763 + start();
7.764 + }
7.765 +
7.766 + ///Finds the shortest path between \c s and \c t.
7.767 +
7.768 + ///Finds the shortest path between \c s and \c t.
7.769 + ///
7.770 + ///\return The length of the shortest s---t path if there exists one,
7.771 + ///0 otherwise.
7.772 + ///\note Apart from the return value, d.run(s) is
7.773 + ///just a shortcut of the following code.
7.774 + ///\code
7.775 + /// d.init();
7.776 + /// d.addSource(s);
7.777 + /// d.start(t);
7.778 + ///\endcode
7.779 + Value run(Node s,Node t) {
7.780 + init();
7.781 + addSource(s);
7.782 + start(t);
7.783 + return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t];
7.784 + }
7.785 +
7.786 + ///@}
7.787 +
7.788 + ///\name Query Functions
7.789 + ///The result of the %Dijkstra algorithm can be obtained using these
7.790 + ///functions.\n
7.791 + ///Before the use of these functions,
7.792 + ///either run() or start() must be called.
7.793 +
7.794 + ///@{
7.795 +
7.796 + ///Gives back the shortest path.
7.797 +
7.798 + ///Gives back the shortest path.
7.799 + ///\pre The \c t should be reachable from the source.
7.800 + Path path(Node t)
7.801 + {
7.802 + return Path(*G, *_pred, t);
7.803 + }
7.804 +
7.805 + ///The distance of a node from the root.
7.806 +
7.807 + ///Returns the distance of a node from the root.
7.808 + ///\pre \ref run() must be called before using this function.
7.809 + ///\warning If node \c v in unreachable from the root the return value
7.810 + ///of this funcion is undefined.
7.811 + Value dist(Node v) const { return (*_dist)[v]; }
7.812 +
7.813 + ///The current distance of a node from the root.
7.814 +
7.815 + ///Returns the current distance of a node from the root.
7.816 + ///It may be decreased in the following processes.
7.817 + ///\pre \c node should be reached but not processed
7.818 + Value currentDist(Node v) const { return (*_heap)[v]; }
7.819 +
7.820 + ///Returns the 'previous arc' of the shortest path tree.
7.821 +
7.822 + ///For a node \c v it returns the 'previous arc' of the shortest path tree,
7.823 + ///i.e. it returns the last arc of a shortest path from the root to \c
7.824 + ///v. It is \ref INVALID
7.825 + ///if \c v is unreachable from the root or if \c v=s. The
7.826 + ///shortest path tree used here is equal to the shortest path tree used in
7.827 + ///\ref predNode(). \pre \ref run() must be called before using
7.828 + ///this function.
7.829 + Arc predArc(Node v) const { return (*_pred)[v]; }
7.830 +
7.831 + ///Returns the 'previous node' of the shortest path tree.
7.832 +
7.833 + ///For a node \c v it returns the 'previous node' of the shortest path tree,
7.834 + ///i.e. it returns the last but one node from a shortest path from the
7.835 + ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
7.836 + ///\c v=s. The shortest path tree used here is equal to the shortest path
7.837 + ///tree used in \ref predArc(). \pre \ref run() must be called before
7.838 + ///using this function.
7.839 + Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
7.840 + G->source((*_pred)[v]); }
7.841 +
7.842 + ///Returns a reference to the NodeMap of distances.
7.843 +
7.844 + ///Returns a reference to the NodeMap of distances. \pre \ref run() must
7.845 + ///be called before using this function.
7.846 + const DistMap &distMap() const { return *_dist;}
7.847 +
7.848 + ///Returns a reference to the shortest path tree map.
7.849 +
7.850 + ///Returns a reference to the NodeMap of the arcs of the
7.851 + ///shortest path tree.
7.852 + ///\pre \ref run() must be called before using this function.
7.853 + const PredMap &predMap() const { return *_pred;}
7.854 +
7.855 + ///Checks if a node is reachable from the root.
7.856 +
7.857 + ///Returns \c true if \c v is reachable from the root.
7.858 + ///\warning The source nodes are inditated as unreached.
7.859 + ///\pre \ref run() must be called before using this function.
7.860 + ///
7.861 + bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
7.862 +
7.863 + ///Checks if a node is processed.
7.864 +
7.865 + ///Returns \c true if \c v is processed, i.e. the shortest
7.866 + ///path to \c v has already found.
7.867 + ///\pre \ref run() must be called before using this function.
7.868 + ///
7.869 + bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
7.870 +
7.871 + ///@}
7.872 + };
7.873 +
7.874 +
7.875 +
7.876 +
7.877 +
7.878 + ///Default traits class of Dijkstra function.
7.879 +
7.880 + ///Default traits class of Dijkstra function.
7.881 + ///\param GR Digraph type.
7.882 + ///\param LM Type of length map.
7.883 + template<class GR, class LM>
7.884 + struct DijkstraWizardDefaultTraits
7.885 + {
7.886 + ///The digraph type the algorithm runs on.
7.887 + typedef GR Digraph;
7.888 + ///The type of the map that stores the arc lengths.
7.889 +
7.890 + ///The type of the map that stores the arc lengths.
7.891 + ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
7.892 + typedef LM LengthMap;
7.893 + //The type of the length of the arcs.
7.894 + typedef typename LM::Value Value;
7.895 + /// Operation traits for Dijkstra algorithm.
7.896 +
7.897 + /// It defines the used operation by the algorithm.
7.898 + /// \see DijkstraDefaultOperationTraits
7.899 + typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
7.900 + ///The heap type used by Dijkstra algorithm.
7.901 +
7.902 + /// The cross reference type used by heap.
7.903 +
7.904 + /// The cross reference type used by heap.
7.905 + /// Usually it is \c Digraph::NodeMap<int>.
7.906 + typedef typename Digraph::template NodeMap<int> HeapCrossRef;
7.907 + ///Instantiates a HeapCrossRef.
7.908 +
7.909 + ///This function instantiates a \ref HeapCrossRef.
7.910 + /// \param G is the digraph, to which we would like to define the
7.911 + /// HeapCrossRef.
7.912 + /// \todo The digraph alone may be insufficient for the initialization
7.913 + static HeapCrossRef *createHeapCrossRef(const GR &G)
7.914 + {
7.915 + return new HeapCrossRef(G);
7.916 + }
7.917 +
7.918 + ///The heap type used by Dijkstra algorithm.
7.919 +
7.920 + ///The heap type used by Dijkstra algorithm.
7.921 + ///
7.922 + ///\sa BinHeap
7.923 + ///\sa Dijkstra
7.924 + typedef BinHeap<typename LM::Value, typename GR::template NodeMap<int>,
7.925 + std::less<Value> > Heap;
7.926 +
7.927 + static Heap *createHeap(HeapCrossRef& R)
7.928 + {
7.929 + return new Heap(R);
7.930 + }
7.931 +
7.932 + ///\brief The type of the map that stores the last
7.933 + ///arcs of the shortest paths.
7.934 + ///
7.935 + ///The type of the map that stores the last
7.936 + ///arcs of the shortest paths.
7.937 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
7.938 + ///
7.939 + typedef NullMap <typename GR::Node,typename GR::Arc> PredMap;
7.940 + ///Instantiates a PredMap.
7.941 +
7.942 + ///This function instantiates a \ref PredMap.
7.943 + ///\param g is the digraph, to which we would like to define the PredMap.
7.944 + ///\todo The digraph alone may be insufficient for the initialization
7.945 +#ifdef DOXYGEN
7.946 + static PredMap *createPredMap(const GR &g)
7.947 +#else
7.948 + static PredMap *createPredMap(const GR &)
7.949 +#endif
7.950 + {
7.951 + return new PredMap();
7.952 + }
7.953 + ///The type of the map that stores whether a nodes is processed.
7.954 +
7.955 + ///The type of the map that stores whether a nodes is processed.
7.956 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
7.957 + ///By default it is a NullMap.
7.958 + ///\todo If it is set to a real map,
7.959 + ///Dijkstra::processed() should read this.
7.960 + ///\todo named parameter to set this type, function to read and write.
7.961 + typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
7.962 + ///Instantiates a ProcessedMap.
7.963 +
7.964 + ///This function instantiates a \ref ProcessedMap.
7.965 + ///\param g is the digraph, to which
7.966 + ///we would like to define the \ref ProcessedMap
7.967 +#ifdef DOXYGEN
7.968 + static ProcessedMap *createProcessedMap(const GR &g)
7.969 +#else
7.970 + static ProcessedMap *createProcessedMap(const GR &)
7.971 +#endif
7.972 + {
7.973 + return new ProcessedMap();
7.974 + }
7.975 + ///The type of the map that stores the dists of the nodes.
7.976 +
7.977 + ///The type of the map that stores the dists of the nodes.
7.978 + ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
7.979 + ///
7.980 + typedef NullMap<typename Digraph::Node,typename LM::Value> DistMap;
7.981 + ///Instantiates a DistMap.
7.982 +
7.983 + ///This function instantiates a \ref DistMap.
7.984 + ///\param g is the digraph, to which we would like to define the \ref DistMap
7.985 +#ifdef DOXYGEN
7.986 + static DistMap *createDistMap(const GR &g)
7.987 +#else
7.988 + static DistMap *createDistMap(const GR &)
7.989 +#endif
7.990 + {
7.991 + return new DistMap();
7.992 + }
7.993 + };
7.994 +
7.995 + /// Default traits used by \ref DijkstraWizard
7.996 +
7.997 + /// To make it easier to use Dijkstra algorithm
7.998 + ///we have created a wizard class.
7.999 + /// This \ref DijkstraWizard class needs default traits,
7.1000 + ///as well as the \ref Dijkstra class.
7.1001 + /// The \ref DijkstraWizardBase is a class to be the default traits of the
7.1002 + /// \ref DijkstraWizard class.
7.1003 + /// \todo More named parameters are required...
7.1004 + template<class GR,class LM>
7.1005 + class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
7.1006 + {
7.1007 +
7.1008 + typedef DijkstraWizardDefaultTraits<GR,LM> Base;
7.1009 + protected:
7.1010 + /// Type of the nodes in the digraph.
7.1011 + typedef typename Base::Digraph::Node Node;
7.1012 +
7.1013 + /// Pointer to the underlying digraph.
7.1014 + void *_g;
7.1015 + /// Pointer to the length map
7.1016 + void *_length;
7.1017 + ///Pointer to the map of predecessors arcs.
7.1018 + void *_pred;
7.1019 + ///Pointer to the map of distances.
7.1020 + void *_dist;
7.1021 + ///Pointer to the source node.
7.1022 + Node _source;
7.1023 +
7.1024 + public:
7.1025 + /// Constructor.
7.1026 +
7.1027 + /// This constructor does not require parameters, therefore it initiates
7.1028 + /// all of the attributes to default values (0, INVALID).
7.1029 + DijkstraWizardBase() : _g(0), _length(0), _pred(0),
7.1030 + _dist(0), _source(INVALID) {}
7.1031 +
7.1032 + /// Constructor.
7.1033 +
7.1034 + /// This constructor requires some parameters,
7.1035 + /// listed in the parameters list.
7.1036 + /// Others are initiated to 0.
7.1037 + /// \param g is the initial value of \ref _g
7.1038 + /// \param l is the initial value of \ref _length
7.1039 + /// \param s is the initial value of \ref _source
7.1040 + DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
7.1041 + _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
7.1042 + _length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
7.1043 + _pred(0), _dist(0), _source(s) {}
7.1044 +
7.1045 + };
7.1046 +
7.1047 + /// A class to make the usage of Dijkstra algorithm easier
7.1048 +
7.1049 + /// This class is created to make it easier to use Dijkstra algorithm.
7.1050 + /// It uses the functions and features of the plain \ref Dijkstra,
7.1051 + /// but it is much simpler to use it.
7.1052 + ///
7.1053 + /// Simplicity means that the way to change the types defined
7.1054 + /// in the traits class is based on functions that returns the new class
7.1055 + /// and not on templatable built-in classes.
7.1056 + /// When using the plain \ref Dijkstra
7.1057 + /// the new class with the modified type comes from
7.1058 + /// the original class by using the ::
7.1059 + /// operator. In the case of \ref DijkstraWizard only
7.1060 + /// a function have to be called and it will
7.1061 + /// return the needed class.
7.1062 + ///
7.1063 + /// It does not have own \ref run method. When its \ref run method is called
7.1064 + /// it initiates a plain \ref Dijkstra class, and calls the \ref
7.1065 + /// Dijkstra::run method of it.
7.1066 + template<class TR>
7.1067 + class DijkstraWizard : public TR
7.1068 + {
7.1069 + typedef TR Base;
7.1070 +
7.1071 + ///The type of the underlying digraph.
7.1072 + typedef typename TR::Digraph Digraph;
7.1073 + //\e
7.1074 + typedef typename Digraph::Node Node;
7.1075 + //\e
7.1076 + typedef typename Digraph::NodeIt NodeIt;
7.1077 + //\e
7.1078 + typedef typename Digraph::Arc Arc;
7.1079 + //\e
7.1080 + typedef typename Digraph::OutArcIt OutArcIt;
7.1081 +
7.1082 + ///The type of the map that stores the arc lengths.
7.1083 + typedef typename TR::LengthMap LengthMap;
7.1084 + ///The type of the length of the arcs.
7.1085 + typedef typename LengthMap::Value Value;
7.1086 + ///\brief The type of the map that stores the last
7.1087 + ///arcs of the shortest paths.
7.1088 + typedef typename TR::PredMap PredMap;
7.1089 + ///The type of the map that stores the dists of the nodes.
7.1090 + typedef typename TR::DistMap DistMap;
7.1091 + ///The heap type used by the dijkstra algorithm.
7.1092 + typedef typename TR::Heap Heap;
7.1093 + public:
7.1094 + /// Constructor.
7.1095 + DijkstraWizard() : TR() {}
7.1096 +
7.1097 + /// Constructor that requires parameters.
7.1098 +
7.1099 + /// Constructor that requires parameters.
7.1100 + /// These parameters will be the default values for the traits class.
7.1101 + DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) :
7.1102 + TR(g,l,s) {}
7.1103 +
7.1104 + ///Copy constructor
7.1105 + DijkstraWizard(const TR &b) : TR(b) {}
7.1106 +
7.1107 + ~DijkstraWizard() {}
7.1108 +
7.1109 + ///Runs Dijkstra algorithm from a given node.
7.1110 +
7.1111 + ///Runs Dijkstra algorithm from a given node.
7.1112 + ///The node can be given by the \ref source function.
7.1113 + void run()
7.1114 + {
7.1115 + if(Base::_source==INVALID) throw UninitializedParameter();
7.1116 + Dijkstra<Digraph,LengthMap,TR>
7.1117 + dij(*reinterpret_cast<const Digraph*>(Base::_g),
7.1118 + *reinterpret_cast<const LengthMap*>(Base::_length));
7.1119 + if(Base::_pred) dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
7.1120 + if(Base::_dist) dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
7.1121 + dij.run(Base::_source);
7.1122 + }
7.1123 +
7.1124 + ///Runs Dijkstra algorithm from the given node.
7.1125 +
7.1126 + ///Runs Dijkstra algorithm from the given node.
7.1127 + ///\param s is the given source.
7.1128 + void run(Node s)
7.1129 + {
7.1130 + Base::_source=s;
7.1131 + run();
7.1132 + }
7.1133 +
7.1134 + template<class T>
7.1135 + struct DefPredMapBase : public Base {
7.1136 + typedef T PredMap;
7.1137 + static PredMap *createPredMap(const Digraph &) { return 0; };
7.1138 + DefPredMapBase(const TR &b) : TR(b) {}
7.1139 + };
7.1140 +
7.1141 + ///\brief \ref named-templ-param "Named parameter"
7.1142 + ///function for setting PredMap type
7.1143 + ///
7.1144 + /// \ref named-templ-param "Named parameter"
7.1145 + ///function for setting PredMap type
7.1146 + ///
7.1147 + template<class T>
7.1148 + DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
7.1149 + {
7.1150 + Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
7.1151 + return DijkstraWizard<DefPredMapBase<T> >(*this);
7.1152 + }
7.1153 +
7.1154 + template<class T>
7.1155 + struct DefDistMapBase : public Base {
7.1156 + typedef T DistMap;
7.1157 + static DistMap *createDistMap(const Digraph &) { return 0; };
7.1158 + DefDistMapBase(const TR &b) : TR(b) {}
7.1159 + };
7.1160 +
7.1161 + ///\brief \ref named-templ-param "Named parameter"
7.1162 + ///function for setting DistMap type
7.1163 + ///
7.1164 + /// \ref named-templ-param "Named parameter"
7.1165 + ///function for setting DistMap type
7.1166 + ///
7.1167 + template<class T>
7.1168 + DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
7.1169 + {
7.1170 + Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
7.1171 + return DijkstraWizard<DefDistMapBase<T> >(*this);
7.1172 + }
7.1173 +
7.1174 + /// Sets the source node, from which the Dijkstra algorithm runs.
7.1175 +
7.1176 + /// Sets the source node, from which the Dijkstra algorithm runs.
7.1177 + /// \param s is the source node.
7.1178 + DijkstraWizard<TR> &source(Node s)
7.1179 + {
7.1180 + Base::_source=s;
7.1181 + return *this;
7.1182 + }
7.1183 +
7.1184 + };
7.1185 +
7.1186 + ///Function type interface for Dijkstra algorithm.
7.1187 +
7.1188 + /// \ingroup shortest_path
7.1189 + ///Function type interface for Dijkstra algorithm.
7.1190 + ///
7.1191 + ///This function also has several
7.1192 + ///\ref named-templ-func-param "named parameters",
7.1193 + ///they are declared as the members of class \ref DijkstraWizard.
7.1194 + ///The following
7.1195 + ///example shows how to use these parameters.
7.1196 + ///\code
7.1197 + /// dijkstra(g,length,source).predMap(preds).run();
7.1198 + ///\endcode
7.1199 + ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
7.1200 + ///to the end of the parameter list.
7.1201 + ///\sa DijkstraWizard
7.1202 + ///\sa Dijkstra
7.1203 + template<class GR, class LM>
7.1204 + DijkstraWizard<DijkstraWizardBase<GR,LM> >
7.1205 + dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
7.1206 + {
7.1207 + return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
7.1208 + }
7.1209 +
7.1210 +} //END OF NAMESPACE LEMON
7.1211 +
7.1212 +#endif
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/lemon/graph_utils.h Thu Feb 07 21:37:07 2008 +0000
8.3 @@ -0,0 +1,3179 @@
8.4 +/* -*- C++ -*-
8.5 + *
8.6 + * This file is a part of LEMON, a generic C++ optimization library
8.7 + *
8.8 + * Copyright (C) 2003-2008
8.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
8.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
8.11 + *
8.12 + * Permission to use, modify and distribute this software is granted
8.13 + * provided that this copyright notice appears in all copies. For
8.14 + * precise terms see the accompanying LICENSE file.
8.15 + *
8.16 + * This software is provided "AS IS" with no warranty of any kind,
8.17 + * express or implied, and with no claim as to its suitability for any
8.18 + * purpose.
8.19 + *
8.20 + */
8.21 +
8.22 +#ifndef LEMON_GRAPH_UTILS_H
8.23 +#define LEMON_GRAPH_UTILS_H
8.24 +
8.25 +#include <iterator>
8.26 +#include <vector>
8.27 +#include <map>
8.28 +#include <cmath>
8.29 +#include <algorithm>
8.30 +
8.31 +#include <lemon/bits/invalid.h>
8.32 +#include <lemon/bits/utility.h>
8.33 +#include <lemon/maps.h>
8.34 +#include <lemon/bits/traits.h>
8.35 +
8.36 +#include <lemon/bits/alteration_notifier.h>
8.37 +#include <lemon/bits/default_map.h>
8.38 +
8.39 +///\ingroup gutils
8.40 +///\file
8.41 +///\brief Digraph utilities.
8.42 +
8.43 +namespace lemon {
8.44 +
8.45 + /// \addtogroup gutils
8.46 + /// @{
8.47 +
8.48 + ///Creates convenience typedefs for the digraph types and iterators
8.49 +
8.50 + ///This \c \#define creates convenience typedefs for the following types
8.51 + ///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
8.52 + ///\c OutArcIt
8.53 + ///\note If \c G it a template parameter, it should be used in this way.
8.54 + ///\code
8.55 + /// GRAPH_TYPEDEFS(typename G);
8.56 + ///\endcode
8.57 + ///
8.58 + ///\warning There are no typedefs for the digraph maps because of the lack of
8.59 + ///template typedefs in C++.
8.60 +#define GRAPH_TYPEDEFS(Digraph) \
8.61 + typedef Digraph:: Node Node; \
8.62 + typedef Digraph:: NodeIt NodeIt; \
8.63 + typedef Digraph:: Arc Arc; \
8.64 + typedef Digraph:: ArcIt ArcIt; \
8.65 + typedef Digraph:: InArcIt InArcIt; \
8.66 + typedef Digraph::OutArcIt OutArcIt
8.67 +
8.68 + ///Creates convenience typedefs for the graph types and iterators
8.69 +
8.70 + ///This \c \#define creates the same convenience typedefs as defined by
8.71 + ///\ref GRAPH_TYPEDEFS(Digraph) and three more, namely it creates
8.72 + ///\c Edge, \c EdgeIt, \c IncArcIt,
8.73 + ///
8.74 + ///\note If \c G it a template parameter, it should be used in this way.
8.75 + ///\code
8.76 + /// UGRAPH_TYPEDEFS(typename G);
8.77 + ///\endcode
8.78 + ///
8.79 + ///\warning There are no typedefs for the digraph maps because of the lack of
8.80 + ///template typedefs in C++.
8.81 +#define UGRAPH_TYPEDEFS(Digraph) \
8.82 + GRAPH_TYPEDEFS(Digraph); \
8.83 + typedef Digraph:: Edge Edge; \
8.84 + typedef Digraph:: EdgeIt EdgeIt; \
8.85 + typedef Digraph:: IncArcIt IncArcIt
8.86 +
8.87 + ///\brief Creates convenience typedefs for the bipartite digraph
8.88 + ///types and iterators
8.89 +
8.90 + ///This \c \#define creates the same convenience typedefs as defined by
8.91 + ///\ref UGRAPH_TYPEDEFS(Digraph) and two more, namely it creates
8.92 + ///\c RedIt, \c BlueIt,
8.93 + ///
8.94 + ///\note If \c G it a template parameter, it should be used in this way.
8.95 + ///\code
8.96 + /// BPUGRAPH_TYPEDEFS(typename G);
8.97 + ///\endcode
8.98 + ///
8.99 + ///\warning There are no typedefs for the digraph maps because of the lack of
8.100 + ///template typedefs in C++.
8.101 +#define BPUGRAPH_TYPEDEFS(Digraph) \
8.102 + UGRAPH_TYPEDEFS(Digraph); \
8.103 + typedef Digraph::Red Red; \
8.104 + typedef Digraph::Blue Blue; \
8.105 + typedef Digraph::RedIt RedIt; \
8.106 + typedef Digraph::BlueIt BlueIt
8.107 +
8.108 + /// \brief Function to count the items in the digraph.
8.109 + ///
8.110 + /// This function counts the items (nodes, arcs etc) in the digraph.
8.111 + /// The complexity of the function is O(n) because
8.112 + /// it iterates on all of the items.
8.113 +
8.114 + template <typename Digraph, typename Item>
8.115 + inline int countItems(const Digraph& g) {
8.116 + typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
8.117 + int num = 0;
8.118 + for (ItemIt it(g); it != INVALID; ++it) {
8.119 + ++num;
8.120 + }
8.121 + return num;
8.122 + }
8.123 +
8.124 + // Node counting:
8.125 +
8.126 + namespace _digraph_utils_bits {
8.127 +
8.128 + template <typename Digraph, typename Enable = void>
8.129 + struct CountNodesSelector {
8.130 + static int count(const Digraph &g) {
8.131 + return countItems<Digraph, typename Digraph::Node>(g);
8.132 + }
8.133 + };
8.134 +
8.135 + template <typename Digraph>
8.136 + struct CountNodesSelector<
8.137 + Digraph, typename
8.138 + enable_if<typename Digraph::NodeNumTag, void>::type>
8.139 + {
8.140 + static int count(const Digraph &g) {
8.141 + return g.nodeNum();
8.142 + }
8.143 + };
8.144 + }
8.145 +
8.146 + /// \brief Function to count the nodes in the digraph.
8.147 + ///
8.148 + /// This function counts the nodes in the digraph.
8.149 + /// The complexity of the function is O(n) but for some
8.150 + /// digraph structures it is specialized to run in O(1).
8.151 + ///
8.152 + /// If the digraph contains a \e nodeNum() member function and a
8.153 + /// \e NodeNumTag tag then this function calls directly the member
8.154 + /// function to query the cardinality of the node set.
8.155 + template <typename Digraph>
8.156 + inline int countNodes(const Digraph& g) {
8.157 + return _digraph_utils_bits::CountNodesSelector<Digraph>::count(g);
8.158 + }
8.159 +
8.160 + namespace _digraph_utils_bits {
8.161 +
8.162 + template <typename Digraph, typename Enable = void>
8.163 + struct CountRedsSelector {
8.164 + static int count(const Digraph &g) {
8.165 + return countItems<Digraph, typename Digraph::Red>(g);
8.166 + }
8.167 + };
8.168 +
8.169 + template <typename Digraph>
8.170 + struct CountRedsSelector<
8.171 + Digraph, typename
8.172 + enable_if<typename Digraph::NodeNumTag, void>::type>
8.173 + {
8.174 + static int count(const Digraph &g) {
8.175 + return g.redNum();
8.176 + }
8.177 + };
8.178 + }
8.179 +
8.180 + /// \brief Function to count the reds in the digraph.
8.181 + ///
8.182 + /// This function counts the reds in the digraph.
8.183 + /// The complexity of the function is O(an) but for some
8.184 + /// digraph structures it is specialized to run in O(1).
8.185 + ///
8.186 + /// If the digraph contains an \e redNum() member function and a
8.187 + /// \e NodeNumTag tag then this function calls directly the member
8.188 + /// function to query the cardinality of the A-node set.
8.189 + template <typename Digraph>
8.190 + inline int countReds(const Digraph& g) {
8.191 + return _digraph_utils_bits::CountRedsSelector<Digraph>::count(g);
8.192 + }
8.193 +
8.194 + namespace _digraph_utils_bits {
8.195 +
8.196 + template <typename Digraph, typename Enable = void>
8.197 + struct CountBluesSelector {
8.198 + static int count(const Digraph &g) {
8.199 + return countItems<Digraph, typename Digraph::Blue>(g);
8.200 + }
8.201 + };
8.202 +
8.203 + template <typename Digraph>
8.204 + struct CountBluesSelector<
8.205 + Digraph, typename
8.206 + enable_if<typename Digraph::NodeNumTag, void>::type>
8.207 + {
8.208 + static int count(const Digraph &g) {
8.209 + return g.blueNum();
8.210 + }
8.211 + };
8.212 + }
8.213 +
8.214 + /// \brief Function to count the blues in the digraph.
8.215 + ///
8.216 + /// This function counts the blues in the digraph.
8.217 + /// The complexity of the function is O(bn) but for some
8.218 + /// digraph structures it is specialized to run in O(1).
8.219 + ///
8.220 + /// If the digraph contains a \e blueNum() member function and a
8.221 + /// \e NodeNumTag tag then this function calls directly the member
8.222 + /// function to query the cardinality of the B-node set.
8.223 + template <typename Digraph>
8.224 + inline int countBlues(const Digraph& g) {
8.225 + return _digraph_utils_bits::CountBluesSelector<Digraph>::count(g);
8.226 + }
8.227 +
8.228 +
8.229 + // Arc counting:
8.230 +
8.231 + namespace _digraph_utils_bits {
8.232 +
8.233 + template <typename Digraph, typename Enable = void>
8.234 + struct CountArcsSelector {
8.235 + static int count(const Digraph &g) {
8.236 + return countItems<Digraph, typename Digraph::Arc>(g);
8.237 + }
8.238 + };
8.239 +
8.240 + template <typename Digraph>
8.241 + struct CountArcsSelector<
8.242 + Digraph,
8.243 + typename enable_if<typename Digraph::ArcNumTag, void>::type>
8.244 + {
8.245 + static int count(const Digraph &g) {
8.246 + return g.arcNum();
8.247 + }
8.248 + };
8.249 + }
8.250 +
8.251 + /// \brief Function to count the arcs in the digraph.
8.252 + ///
8.253 + /// This function counts the arcs in the digraph.
8.254 + /// The complexity of the function is O(e) but for some
8.255 + /// digraph structures it is specialized to run in O(1).
8.256 + ///
8.257 + /// If the digraph contains a \e arcNum() member function and a
8.258 + /// \e ArcNumTag tag then this function calls directly the member
8.259 + /// function to query the cardinality of the arc set.
8.260 + template <typename Digraph>
8.261 + inline int countArcs(const Digraph& g) {
8.262 + return _digraph_utils_bits::CountArcsSelector<Digraph>::count(g);
8.263 + }
8.264 +
8.265 + // Undirected arc counting:
8.266 + namespace _digraph_utils_bits {
8.267 +
8.268 + template <typename Digraph, typename Enable = void>
8.269 + struct CountEdgesSelector {
8.270 + static int count(const Digraph &g) {
8.271 + return countItems<Digraph, typename Digraph::Edge>(g);
8.272 + }
8.273 + };
8.274 +
8.275 + template <typename Digraph>
8.276 + struct CountEdgesSelector<
8.277 + Digraph,
8.278 + typename enable_if<typename Digraph::ArcNumTag, void>::type>
8.279 + {
8.280 + static int count(const Digraph &g) {
8.281 + return g.edgeNum();
8.282 + }
8.283 + };
8.284 + }
8.285 +
8.286 + /// \brief Function to count the edges in the digraph.
8.287 + ///
8.288 + /// This function counts the edges in the digraph.
8.289 + /// The complexity of the function is O(e) but for some
8.290 + /// digraph structures it is specialized to run in O(1).
8.291 + ///
8.292 + /// If the digraph contains a \e edgeNum() member function and a
8.293 + /// \e ArcNumTag tag then this function calls directly the member
8.294 + /// function to query the cardinality of the edge set.
8.295 + template <typename Digraph>
8.296 + inline int countEdges(const Digraph& g) {
8.297 + return _digraph_utils_bits::CountEdgesSelector<Digraph>::count(g);
8.298 +
8.299 + }
8.300 +
8.301 +
8.302 + template <typename Digraph, typename DegIt>
8.303 + inline int countNodeDegree(const Digraph& _g, const typename Digraph::Node& _n) {
8.304 + int num = 0;
8.305 + for (DegIt it(_g, _n); it != INVALID; ++it) {
8.306 + ++num;
8.307 + }
8.308 + return num;
8.309 + }
8.310 +
8.311 + /// \brief Function to count the number of the out-arcs from node \c n.
8.312 + ///
8.313 + /// This function counts the number of the out-arcs from node \c n
8.314 + /// in the digraph.
8.315 + template <typename Digraph>
8.316 + inline int countOutArcs(const Digraph& _g, const typename Digraph::Node& _n) {
8.317 + return countNodeDegree<Digraph, typename Digraph::OutArcIt>(_g, _n);
8.318 + }
8.319 +
8.320 + /// \brief Function to count the number of the in-arcs to node \c n.
8.321 + ///
8.322 + /// This function counts the number of the in-arcs to node \c n
8.323 + /// in the digraph.
8.324 + template <typename Digraph>
8.325 + inline int countInArcs(const Digraph& _g, const typename Digraph::Node& _n) {
8.326 + return countNodeDegree<Digraph, typename Digraph::InArcIt>(_g, _n);
8.327 + }
8.328 +
8.329 + /// \brief Function to count the number of the inc-arcs to node \c n.
8.330 + ///
8.331 + /// This function counts the number of the inc-arcs to node \c n
8.332 + /// in the digraph.
8.333 + template <typename Digraph>
8.334 + inline int countIncArcs(const Digraph& _g, const typename Digraph::Node& _n) {
8.335 + return countNodeDegree<Digraph, typename Digraph::IncArcIt>(_g, _n);
8.336 + }
8.337 +
8.338 + namespace _digraph_utils_bits {
8.339 +
8.340 + template <typename Digraph, typename Enable = void>
8.341 + struct FindArcSelector {
8.342 + typedef typename Digraph::Node Node;
8.343 + typedef typename Digraph::Arc Arc;
8.344 + static Arc find(const Digraph &g, Node u, Node v, Arc e) {
8.345 + if (e == INVALID) {
8.346 + g.firstOut(e, u);
8.347 + } else {
8.348 + g.nextOut(e);
8.349 + }
8.350 + while (e != INVALID && g.target(e) != v) {
8.351 + g.nextOut(e);
8.352 + }
8.353 + return e;
8.354 + }
8.355 + };
8.356 +
8.357 + template <typename Digraph>
8.358 + struct FindArcSelector<
8.359 + Digraph,
8.360 + typename enable_if<typename Digraph::FindArcTag, void>::type>
8.361 + {
8.362 + typedef typename Digraph::Node Node;
8.363 + typedef typename Digraph::Arc Arc;
8.364 + static Arc find(const Digraph &g, Node u, Node v, Arc prev) {
8.365 + return g.findArc(u, v, prev);
8.366 + }
8.367 + };
8.368 + }
8.369 +
8.370 + /// \brief Finds an arc between two nodes of a digraph.
8.371 + ///
8.372 + /// Finds an arc from node \c u to node \c v in digraph \c g.
8.373 + ///
8.374 + /// If \c prev is \ref INVALID (this is the default value), then
8.375 + /// it finds the first arc from \c u to \c v. Otherwise it looks for
8.376 + /// the next arc from \c u to \c v after \c prev.
8.377 + /// \return The found arc or \ref INVALID if there is no such an arc.
8.378 + ///
8.379 + /// Thus you can iterate through each arc from \c u to \c v as it follows.
8.380 + ///\code
8.381 + /// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) {
8.382 + /// ...
8.383 + /// }
8.384 + ///\endcode
8.385 + ///
8.386 + ///\sa ArcLookUp
8.387 + ///\sa AllArcLookUp
8.388 + ///\sa DynArcLookUp
8.389 + ///\sa ConArcIt
8.390 + template <typename Digraph>
8.391 + inline typename Digraph::Arc
8.392 + findArc(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v,
8.393 + typename Digraph::Arc prev = INVALID) {
8.394 + return _digraph_utils_bits::FindArcSelector<Digraph>::find(g, u, v, prev);
8.395 + }
8.396 +
8.397 + /// \brief Iterator for iterating on arcs connected the same nodes.
8.398 + ///
8.399 + /// Iterator for iterating on arcs connected the same nodes. It is
8.400 + /// higher level interface for the findArc() function. You can
8.401 + /// use it the following way:
8.402 + ///\code
8.403 + /// for (ConArcIt<Digraph> it(g, src, trg); it != INVALID; ++it) {
8.404 + /// ...
8.405 + /// }
8.406 + ///\endcode
8.407 + ///
8.408 + ///\sa findArc()
8.409 + ///\sa ArcLookUp
8.410 + ///\sa AllArcLookUp
8.411 + ///\sa DynArcLookUp
8.412 + ///
8.413 + /// \author Balazs Dezso
8.414 + template <typename _Digraph>
8.415 + class ConArcIt : public _Digraph::Arc {
8.416 + public:
8.417 +
8.418 + typedef _Digraph Digraph;
8.419 + typedef typename Digraph::Arc Parent;
8.420 +
8.421 + typedef typename Digraph::Arc Arc;
8.422 + typedef typename Digraph::Node Node;
8.423 +
8.424 + /// \brief Constructor.
8.425 + ///
8.426 + /// Construct a new ConArcIt iterating on the arcs which
8.427 + /// connects the \c u and \c v node.
8.428 + ConArcIt(const Digraph& g, Node u, Node v) : digraph(g) {
8.429 + Parent::operator=(findArc(digraph, u, v));
8.430 + }
8.431 +
8.432 + /// \brief Constructor.
8.433 + ///
8.434 + /// Construct a new ConArcIt which continues the iterating from
8.435 + /// the \c e arc.
8.436 + ConArcIt(const Digraph& g, Arc e) : Parent(e), digraph(g) {}
8.437 +
8.438 + /// \brief Increment operator.
8.439 + ///
8.440 + /// It increments the iterator and gives back the next arc.
8.441 + ConArcIt& operator++() {
8.442 + Parent::operator=(findArc(digraph, digraph.source(*this),
8.443 + digraph.target(*this), *this));
8.444 + return *this;
8.445 + }
8.446 + private:
8.447 + const Digraph& digraph;
8.448 + };
8.449 +
8.450 + namespace _digraph_utils_bits {
8.451 +
8.452 + template <typename Digraph, typename Enable = void>
8.453 + struct FindEdgeSelector {
8.454 + typedef typename Digraph::Node Node;
8.455 + typedef typename Digraph::Edge Edge;
8.456 + static Edge find(const Digraph &g, Node u, Node v, Edge e) {
8.457 + bool b;
8.458 + if (u != v) {
8.459 + if (e == INVALID) {
8.460 + g.firstInc(e, b, u);
8.461 + } else {
8.462 + b = g.source(e) == u;
8.463 + g.nextInc(e, b);
8.464 + }
8.465 + while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
8.466 + g.nextInc(e, b);
8.467 + }
8.468 + } else {
8.469 + if (e == INVALID) {
8.470 + g.firstInc(e, b, u);
8.471 + } else {
8.472 + b = true;
8.473 + g.nextInc(e, b);
8.474 + }
8.475 + while (e != INVALID && (!b || g.target(e) != v)) {
8.476 + g.nextInc(e, b);
8.477 + }
8.478 + }
8.479 + return e;
8.480 + }
8.481 + };
8.482 +
8.483 + template <typename Digraph>
8.484 + struct FindEdgeSelector<
8.485 + Digraph,
8.486 + typename enable_if<typename Digraph::FindArcTag, void>::type>
8.487 + {
8.488 + typedef typename Digraph::Node Node;
8.489 + typedef typename Digraph::Edge Edge;
8.490 + static Edge find(const Digraph &g, Node u, Node v, Edge prev) {
8.491 + return g.findEdge(u, v, prev);
8.492 + }
8.493 + };
8.494 + }
8.495 +
8.496 + /// \brief Finds an edge between two nodes of a digraph.
8.497 + ///
8.498 + /// Finds an edge from node \c u to node \c v in digraph \c g.
8.499 + /// If the node \c u and node \c v is equal then each loop arc
8.500 + /// will be enumerated.
8.501 + ///
8.502 + /// If \c prev is \ref INVALID (this is the default value), then
8.503 + /// it finds the first arc from \c u to \c v. Otherwise it looks for
8.504 + /// the next arc from \c u to \c v after \c prev.
8.505 + /// \return The found arc or \ref INVALID if there is no such an arc.
8.506 + ///
8.507 + /// Thus you can iterate through each arc from \c u to \c v as it follows.
8.508 + ///\code
8.509 + /// for(Edge e = findEdge(g,u,v); e != INVALID;
8.510 + /// e = findEdge(g,u,v,e)) {
8.511 + /// ...
8.512 + /// }
8.513 + ///\endcode
8.514 + ///
8.515 + ///\sa ConArcIt
8.516 +
8.517 + template <typename Digraph>
8.518 + inline typename Digraph::Edge
8.519 + findEdge(const Digraph &g, typename Digraph::Node u, typename Digraph::Node v,
8.520 + typename Digraph::Edge p = INVALID) {
8.521 + return _digraph_utils_bits::FindEdgeSelector<Digraph>::find(g, u, v, p);
8.522 + }
8.523 +
8.524 + /// \brief Iterator for iterating on edges connected the same nodes.
8.525 + ///
8.526 + /// Iterator for iterating on edges connected the same nodes. It is
8.527 + /// higher level interface for the findEdge() function. You can
8.528 + /// use it the following way:
8.529 + ///\code
8.530 + /// for (ConEdgeIt<Digraph> it(g, src, trg); it != INVALID; ++it) {
8.531 + /// ...
8.532 + /// }
8.533 + ///\endcode
8.534 + ///
8.535 + ///\sa findEdge()
8.536 + ///
8.537 + /// \author Balazs Dezso
8.538 + template <typename _Digraph>
8.539 + class ConEdgeIt : public _Digraph::Edge {
8.540 + public:
8.541 +
8.542 + typedef _Digraph Digraph;
8.543 + typedef typename Digraph::Edge Parent;
8.544 +
8.545 + typedef typename Digraph::Edge Edge;
8.546 + typedef typename Digraph::Node Node;
8.547 +
8.548 + /// \brief Constructor.
8.549 + ///
8.550 + /// Construct a new ConEdgeIt iterating on the arcs which
8.551 + /// connects the \c u and \c v node.
8.552 + ConEdgeIt(const Digraph& g, Node u, Node v) : digraph(g) {
8.553 + Parent::operator=(findEdge(digraph, u, v));
8.554 + }
8.555 +
8.556 + /// \brief Constructor.
8.557 + ///
8.558 + /// Construct a new ConEdgeIt which continues the iterating from
8.559 + /// the \c e arc.
8.560 + ConEdgeIt(const Digraph& g, Edge e) : Parent(e), digraph(g) {}
8.561 +
8.562 + /// \brief Increment operator.
8.563 + ///
8.564 + /// It increments the iterator and gives back the next arc.
8.565 + ConEdgeIt& operator++() {
8.566 + Parent::operator=(findEdge(digraph, digraph.source(*this),
8.567 + digraph.target(*this), *this));
8.568 + return *this;
8.569 + }
8.570 + private:
8.571 + const Digraph& digraph;
8.572 + };
8.573 +
8.574 + /// \brief Copy a map.
8.575 + ///
8.576 + /// This function copies the \c from map to the \c to map. It uses the
8.577 + /// given iterator to iterate on the data structure and it uses the \c ref
8.578 + /// mapping to convert the from's keys to the to's keys.
8.579 + template <typename To, typename From,
8.580 + typename ItemIt, typename Ref>
8.581 + void copyMap(To& to, const From& from,
8.582 + ItemIt it, const Ref& ref) {
8.583 + for (; it != INVALID; ++it) {
8.584 + to[ref[it]] = from[it];
8.585 + }
8.586 + }
8.587 +
8.588 + /// \brief Copy the from map to the to map.
8.589 + ///
8.590 + /// Copy the \c from map to the \c to map. It uses the given iterator
8.591 + /// to iterate on the data structure.
8.592 + template <typename To, typename From, typename ItemIt>
8.593 + void copyMap(To& to, const From& from, ItemIt it) {
8.594 + for (; it != INVALID; ++it) {
8.595 + to[it] = from[it];
8.596 + }
8.597 + }
8.598 +
8.599 + namespace _digraph_utils_bits {
8.600 +
8.601 + template <typename Digraph, typename Item, typename RefMap>
8.602 + class MapCopyBase {
8.603 + public:
8.604 + virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
8.605 +
8.606 + virtual ~MapCopyBase() {}
8.607 + };
8.608 +
8.609 + template <typename Digraph, typename Item, typename RefMap,
8.610 + typename ToMap, typename FromMap>
8.611 + class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
8.612 + public:
8.613 +
8.614 + MapCopy(ToMap& tmap, const FromMap& map)
8.615 + : _tmap(tmap), _map(map) {}
8.616 +
8.617 + virtual void copy(const Digraph& digraph, const RefMap& refMap) {
8.618 + typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
8.619 + for (ItemIt it(digraph); it != INVALID; ++it) {
8.620 + _tmap.set(refMap[it], _map[it]);
8.621 + }
8.622 + }
8.623 +
8.624 + private:
8.625 + ToMap& _tmap;
8.626 + const FromMap& _map;
8.627 + };
8.628 +
8.629 + template <typename Digraph, typename Item, typename RefMap, typename It>
8.630 + class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
8.631 + public:
8.632 +
8.633 + ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
8.634 +
8.635 + virtual void copy(const Digraph&, const RefMap& refMap) {
8.636 + _it = refMap[_item];
8.637 + }
8.638 +
8.639 + private:
8.640 + It& _it;
8.641 + Item _item;
8.642 + };
8.643 +
8.644 + template <typename Digraph, typename Item, typename RefMap, typename Ref>
8.645 + class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
8.646 + public:
8.647 +
8.648 + RefCopy(Ref& map) : _map(map) {}
8.649 +
8.650 + virtual void copy(const Digraph& digraph, const RefMap& refMap) {
8.651 + typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
8.652 + for (ItemIt it(digraph); it != INVALID; ++it) {
8.653 + _map.set(it, refMap[it]);
8.654 + }
8.655 + }
8.656 +
8.657 + private:
8.658 + Ref& _map;
8.659 + };
8.660 +
8.661 + template <typename Digraph, typename Item, typename RefMap,
8.662 + typename CrossRef>
8.663 + class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
8.664 + public:
8.665 +
8.666 + CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
8.667 +
8.668 + virtual void copy(const Digraph& digraph, const RefMap& refMap) {
8.669 + typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
8.670 + for (ItemIt it(digraph); it != INVALID; ++it) {
8.671 + _cmap.set(refMap[it], it);
8.672 + }
8.673 + }
8.674 +
8.675 + private:
8.676 + CrossRef& _cmap;
8.677 + };
8.678 +
8.679 + template <typename Digraph, typename Enable = void>
8.680 + struct DigraphCopySelector {
8.681 + template <typename From, typename NodeRefMap, typename ArcRefMap>
8.682 + static void copy(Digraph &to, const From& from,
8.683 + NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
8.684 + for (typename From::NodeIt it(from); it != INVALID; ++it) {
8.685 + nodeRefMap[it] = to.addNode();
8.686 + }
8.687 + for (typename From::ArcIt it(from); it != INVALID; ++it) {
8.688 + arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
8.689 + nodeRefMap[from.target(it)]);
8.690 + }
8.691 + }
8.692 + };
8.693 +
8.694 + template <typename Digraph>
8.695 + struct DigraphCopySelector<
8.696 + Digraph,
8.697 + typename enable_if<typename Digraph::BuildTag, void>::type>
8.698 + {
8.699 + template <typename From, typename NodeRefMap, typename ArcRefMap>
8.700 + static void copy(Digraph &to, const From& from,
8.701 + NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
8.702 + to.build(from, nodeRefMap, arcRefMap);
8.703 + }
8.704 + };
8.705 +
8.706 + template <typename Graph, typename Enable = void>
8.707 + struct GraphCopySelector {
8.708 + template <typename From, typename NodeRefMap, typename EdgeRefMap>
8.709 + static void copy(Graph &to, const From& from,
8.710 + NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
8.711 + for (typename From::NodeIt it(from); it != INVALID; ++it) {
8.712 + nodeRefMap[it] = to.addNode();
8.713 + }
8.714 + for (typename From::EdgeIt it(from); it != INVALID; ++it) {
8.715 + edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
8.716 + nodeRefMap[from.target(it)]);
8.717 + }
8.718 + }
8.719 + };
8.720 +
8.721 + template <typename Graph>
8.722 + struct GraphCopySelector<
8.723 + Graph,
8.724 + typename enable_if<typename Graph::BuildTag, void>::type>
8.725 + {
8.726 + template <typename From, typename NodeRefMap, typename EdgeRefMap>
8.727 + static void copy(Graph &to, const From& from,
8.728 + NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
8.729 + to.build(from, nodeRefMap, edgeRefMap);
8.730 + }
8.731 + };
8.732 +
8.733 + template <typename BpGraph, typename Enable = void>
8.734 + struct BpGraphCopySelector {
8.735 + template <typename From, typename RedRefMap,
8.736 + typename BlueRefMap, typename EdgeRefMap>
8.737 + static void copy(BpGraph &to, const From& from,
8.738 + RedRefMap& redRefMap, BlueRefMap& blueRefMap,
8.739 + EdgeRefMap& edgeRefMap) {
8.740 + for (typename From::RedIt it(from); it != INVALID; ++it) {
8.741 + redRefMap[it] = to.addRed();
8.742 + }
8.743 + for (typename From::BlueIt it(from); it != INVALID; ++it) {
8.744 + blueRefMap[it] = to.addBlue();
8.745 + }
8.746 + for (typename From::EdgeIt it(from); it != INVALID; ++it) {
8.747 + edgeRefMap[it] = to.addArc(redRefMap[from.red(it)],
8.748 + blueRefMap[from.blue(it)]);
8.749 + }
8.750 + }
8.751 + };
8.752 +
8.753 + template <typename BpGraph>
8.754 + struct BpGraphCopySelector<
8.755 + BpGraph,
8.756 + typename enable_if<typename BpGraph::BuildTag, void>::type>
8.757 + {
8.758 + template <typename From, typename RedRefMap,
8.759 + typename BlueRefMap, typename EdgeRefMap>
8.760 + static void copy(BpGraph &to, const From& from,
8.761 + RedRefMap& redRefMap, BlueRefMap& blueRefMap,
8.762 + EdgeRefMap& edgeRefMap) {
8.763 + to.build(from, redRefMap, blueRefMap, edgeRefMap);
8.764 + }
8.765 + };
8.766 +
8.767 +
8.768 + }
8.769 +
8.770 + /// \brief Class to copy a digraph.
8.771 + ///
8.772 + /// Class to copy a digraph to another digraph (duplicate a digraph). The
8.773 + /// simplest way of using it is through the \c copyDigraph() function.
8.774 + template <typename To, typename From>
8.775 + class DigraphCopy {
8.776 + private:
8.777 +
8.778 + typedef typename From::Node Node;
8.779 + typedef typename From::NodeIt NodeIt;
8.780 + typedef typename From::Arc Arc;
8.781 + typedef typename From::ArcIt ArcIt;
8.782 +
8.783 + typedef typename To::Node TNode;
8.784 + typedef typename To::Arc TArc;
8.785 +
8.786 + typedef typename From::template NodeMap<TNode> NodeRefMap;
8.787 + typedef typename From::template ArcMap<TArc> ArcRefMap;
8.788 +
8.789 +
8.790 + public:
8.791 +
8.792 +
8.793 + /// \brief Constructor for the DigraphCopy.
8.794 + ///
8.795 + /// It copies the content of the \c _from digraph into the
8.796 + /// \c _to digraph.
8.797 + DigraphCopy(To& _to, const From& _from)
8.798 + : from(_from), to(_to) {}
8.799 +
8.800 + /// \brief Destructor of the DigraphCopy
8.801 + ///
8.802 + /// Destructor of the DigraphCopy
8.803 + ~DigraphCopy() {
8.804 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
8.805 + delete nodeMapCopies[i];
8.806 + }
8.807 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
8.808 + delete arcMapCopies[i];
8.809 + }
8.810 +
8.811 + }
8.812 +
8.813 + /// \brief Copies the node references into the given map.
8.814 + ///
8.815 + /// Copies the node references into the given map.
8.816 + template <typename NodeRef>
8.817 + DigraphCopy& nodeRef(NodeRef& map) {
8.818 + nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node,
8.819 + NodeRefMap, NodeRef>(map));
8.820 + return *this;
8.821 + }
8.822 +
8.823 + /// \brief Copies the node cross references into the given map.
8.824 + ///
8.825 + /// Copies the node cross references (reverse references) into
8.826 + /// the given map.
8.827 + template <typename NodeCrossRef>
8.828 + DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
8.829 + nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
8.830 + NodeRefMap, NodeCrossRef>(map));
8.831 + return *this;
8.832 + }
8.833 +
8.834 + /// \brief Make copy of the given map.
8.835 + ///
8.836 + /// Makes copy of the given map for the newly created digraph.
8.837 + /// The new map's key type is the to digraph's node type,
8.838 + /// and the copied map's key type is the from digraph's node
8.839 + /// type.
8.840 + template <typename ToMap, typename FromMap>
8.841 + DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
8.842 + nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node,
8.843 + NodeRefMap, ToMap, FromMap>(tmap, map));
8.844 + return *this;
8.845 + }
8.846 +
8.847 + /// \brief Make a copy of the given node.
8.848 + ///
8.849 + /// Make a copy of the given node.
8.850 + DigraphCopy& node(TNode& tnode, const Node& snode) {
8.851 + nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node,
8.852 + NodeRefMap, TNode>(tnode, snode));
8.853 + return *this;
8.854 + }
8.855 +
8.856 + /// \brief Copies the arc references into the given map.
8.857 + ///
8.858 + /// Copies the arc references into the given map.
8.859 + template <typename ArcRef>
8.860 + DigraphCopy& arcRef(ArcRef& map) {
8.861 + arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc,
8.862 + ArcRefMap, ArcRef>(map));
8.863 + return *this;
8.864 + }
8.865 +
8.866 + /// \brief Copies the arc cross references into the given map.
8.867 + ///
8.868 + /// Copies the arc cross references (reverse references) into
8.869 + /// the given map.
8.870 + template <typename ArcCrossRef>
8.871 + DigraphCopy& arcCrossRef(ArcCrossRef& map) {
8.872 + arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
8.873 + ArcRefMap, ArcCrossRef>(map));
8.874 + return *this;
8.875 + }
8.876 +
8.877 + /// \brief Make copy of the given map.
8.878 + ///
8.879 + /// Makes copy of the given map for the newly created digraph.
8.880 + /// The new map's key type is the to digraph's arc type,
8.881 + /// and the copied map's key type is the from digraph's arc
8.882 + /// type.
8.883 + template <typename ToMap, typename FromMap>
8.884 + DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
8.885 + arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc,
8.886 + ArcRefMap, ToMap, FromMap>(tmap, map));
8.887 + return *this;
8.888 + }
8.889 +
8.890 + /// \brief Make a copy of the given arc.
8.891 + ///
8.892 + /// Make a copy of the given arc.
8.893 + DigraphCopy& arc(TArc& tarc, const Arc& sarc) {
8.894 + arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc,
8.895 + ArcRefMap, TArc>(tarc, sarc));
8.896 + return *this;
8.897 + }
8.898 +
8.899 + /// \brief Executes the copies.
8.900 + ///
8.901 + /// Executes the copies.
8.902 + void run() {
8.903 + NodeRefMap nodeRefMap(from);
8.904 + ArcRefMap arcRefMap(from);
8.905 + _digraph_utils_bits::DigraphCopySelector<To>::
8.906 + copy(to, from, nodeRefMap, arcRefMap);
8.907 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
8.908 + nodeMapCopies[i]->copy(from, nodeRefMap);
8.909 + }
8.910 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
8.911 + arcMapCopies[i]->copy(from, arcRefMap);
8.912 + }
8.913 + }
8.914 +
8.915 + protected:
8.916 +
8.917 +
8.918 + const From& from;
8.919 + To& to;
8.920 +
8.921 + std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
8.922 + nodeMapCopies;
8.923 +
8.924 + std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
8.925 + arcMapCopies;
8.926 +
8.927 + };
8.928 +
8.929 + /// \brief Copy a digraph to another digraph.
8.930 + ///
8.931 + /// Copy a digraph to another digraph.
8.932 + /// The usage of the function:
8.933 + ///
8.934 + ///\code
8.935 + /// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
8.936 + ///\endcode
8.937 + ///
8.938 + /// After the copy the \c nr map will contain the mapping from the
8.939 + /// nodes of the \c from digraph to the nodes of the \c to digraph and
8.940 + /// \c ecr will contain the mapping from the arcs of the \c to digraph
8.941 + /// to the arcs of the \c from digraph.
8.942 + ///
8.943 + /// \see DigraphCopy
8.944 + template <typename To, typename From>
8.945 + DigraphCopy<To, From> copyDigraph(To& to, const From& from) {
8.946 + return DigraphCopy<To, From>(to, from);
8.947 + }
8.948 +
8.949 + /// \brief Class to copy an graph.
8.950 + ///
8.951 + /// Class to copy an graph to another digraph (duplicate a digraph).
8.952 + /// The simplest way of using it is through the \c copyGraph() function.
8.953 + template <typename To, typename From>
8.954 + class GraphCopy {
8.955 + private:
8.956 +
8.957 + typedef typename From::Node Node;
8.958 + typedef typename From::NodeIt NodeIt;
8.959 + typedef typename From::Arc Arc;
8.960 + typedef typename From::ArcIt ArcIt;
8.961 + typedef typename From::Edge Edge;
8.962 + typedef typename From::EdgeIt EdgeIt;
8.963 +
8.964 + typedef typename To::Node TNode;
8.965 + typedef typename To::Arc TArc;
8.966 + typedef typename To::Edge TEdge;
8.967 +
8.968 + typedef typename From::template NodeMap<TNode> NodeRefMap;
8.969 + typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
8.970 +
8.971 + struct ArcRefMap {
8.972 + ArcRefMap(const To& _to, const From& _from,
8.973 + const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref)
8.974 + : to(_to), from(_from),
8.975 + edge_ref(_edge_ref), node_ref(_node_ref) {}
8.976 +
8.977 + typedef typename From::Arc Key;
8.978 + typedef typename To::Arc Value;
8.979 +
8.980 + Value operator[](const Key& key) const {
8.981 + bool forward =
8.982 + (from.direction(key) ==
8.983 + (node_ref[from.source(static_cast<const Edge&>(key))] ==
8.984 + to.source(edge_ref[static_cast<const Edge&>(key)])));
8.985 + return to.direct(edge_ref[key], forward);
8.986 + }
8.987 +
8.988 + const To& to;
8.989 + const From& from;
8.990 + const EdgeRefMap& edge_ref;
8.991 + const NodeRefMap& node_ref;
8.992 + };
8.993 +
8.994 +
8.995 + public:
8.996 +
8.997 +
8.998 + /// \brief Constructor for the DigraphCopy.
8.999 + ///
8.1000 + /// It copies the content of the \c _from digraph into the
8.1001 + /// \c _to digraph.
8.1002 + GraphCopy(To& _to, const From& _from)
8.1003 + : from(_from), to(_to) {}
8.1004 +
8.1005 + /// \brief Destructor of the DigraphCopy
8.1006 + ///
8.1007 + /// Destructor of the DigraphCopy
8.1008 + ~GraphCopy() {
8.1009 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
8.1010 + delete nodeMapCopies[i];
8.1011 + }
8.1012 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
8.1013 + delete arcMapCopies[i];
8.1014 + }
8.1015 + for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
8.1016 + delete edgeMapCopies[i];
8.1017 + }
8.1018 +
8.1019 + }
8.1020 +
8.1021 + /// \brief Copies the node references into the given map.
8.1022 + ///
8.1023 + /// Copies the node references into the given map.
8.1024 + template <typename NodeRef>
8.1025 + GraphCopy& nodeRef(NodeRef& map) {
8.1026 + nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node,
8.1027 + NodeRefMap, NodeRef>(map));
8.1028 + return *this;
8.1029 + }
8.1030 +
8.1031 + /// \brief Copies the node cross references into the given map.
8.1032 + ///
8.1033 + /// Copies the node cross references (reverse references) into
8.1034 + /// the given map.
8.1035 + template <typename NodeCrossRef>
8.1036 + GraphCopy& nodeCrossRef(NodeCrossRef& map) {
8.1037 + nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
8.1038 + NodeRefMap, NodeCrossRef>(map));
8.1039 + return *this;
8.1040 + }
8.1041 +
8.1042 + /// \brief Make copy of the given map.
8.1043 + ///
8.1044 + /// Makes copy of the given map for the newly created digraph.
8.1045 + /// The new map's key type is the to digraph's node type,
8.1046 + /// and the copied map's key type is the from digraph's node
8.1047 + /// type.
8.1048 + template <typename ToMap, typename FromMap>
8.1049 + GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
8.1050 + nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node,
8.1051 + NodeRefMap, ToMap, FromMap>(tmap, map));
8.1052 + return *this;
8.1053 + }
8.1054 +
8.1055 + /// \brief Make a copy of the given node.
8.1056 + ///
8.1057 + /// Make a copy of the given node.
8.1058 + GraphCopy& node(TNode& tnode, const Node& snode) {
8.1059 + nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node,
8.1060 + NodeRefMap, TNode>(tnode, snode));
8.1061 + return *this;
8.1062 + }
8.1063 +
8.1064 + /// \brief Copies the arc references into the given map.
8.1065 + ///
8.1066 + /// Copies the arc references into the given map.
8.1067 + template <typename ArcRef>
8.1068 + GraphCopy& arcRef(ArcRef& map) {
8.1069 + arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc,
8.1070 + ArcRefMap, ArcRef>(map));
8.1071 + return *this;
8.1072 + }
8.1073 +
8.1074 + /// \brief Copies the arc cross references into the given map.
8.1075 + ///
8.1076 + /// Copies the arc cross references (reverse references) into
8.1077 + /// the given map.
8.1078 + template <typename ArcCrossRef>
8.1079 + GraphCopy& arcCrossRef(ArcCrossRef& map) {
8.1080 + arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
8.1081 + ArcRefMap, ArcCrossRef>(map));
8.1082 + return *this;
8.1083 + }
8.1084 +
8.1085 + /// \brief Make copy of the given map.
8.1086 + ///
8.1087 + /// Makes copy of the given map for the newly created digraph.
8.1088 + /// The new map's key type is the to digraph's arc type,
8.1089 + /// and the copied map's key type is the from digraph's arc
8.1090 + /// type.
8.1091 + template <typename ToMap, typename FromMap>
8.1092 + GraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
8.1093 + arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc,
8.1094 + ArcRefMap, ToMap, FromMap>(tmap, map));
8.1095 + return *this;
8.1096 + }
8.1097 +
8.1098 + /// \brief Make a copy of the given arc.
8.1099 + ///
8.1100 + /// Make a copy of the given arc.
8.1101 + GraphCopy& arc(TArc& tarc, const Arc& sarc) {
8.1102 + arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc,
8.1103 + ArcRefMap, TArc>(tarc, sarc));
8.1104 + return *this;
8.1105 + }
8.1106 +
8.1107 + /// \brief Copies the edge references into the given map.
8.1108 + ///
8.1109 + /// Copies the edge references into the given map.
8.1110 + template <typename EdgeRef>
8.1111 + GraphCopy& edgeRef(EdgeRef& map) {
8.1112 + edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge,
8.1113 + EdgeRefMap, EdgeRef>(map));
8.1114 + return *this;
8.1115 + }
8.1116 +
8.1117 + /// \brief Copies the edge cross references into the given map.
8.1118 + ///
8.1119 + /// Copies the edge cross references (reverse
8.1120 + /// references) into the given map.
8.1121 + template <typename EdgeCrossRef>
8.1122 + GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
8.1123 + edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
8.1124 + Edge, EdgeRefMap, EdgeCrossRef>(map));
8.1125 + return *this;
8.1126 + }
8.1127 +
8.1128 + /// \brief Make copy of the given map.
8.1129 + ///
8.1130 + /// Makes copy of the given map for the newly created digraph.
8.1131 + /// The new map's key type is the to digraph's edge type,
8.1132 + /// and the copied map's key type is the from digraph's edge
8.1133 + /// type.
8.1134 + template <typename ToMap, typename FromMap>
8.1135 + GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
8.1136 + edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge,
8.1137 + EdgeRefMap, ToMap, FromMap>(tmap, map));
8.1138 + return *this;
8.1139 + }
8.1140 +
8.1141 + /// \brief Make a copy of the given edge.
8.1142 + ///
8.1143 + /// Make a copy of the given edge.
8.1144 + GraphCopy& edge(TEdge& tedge, const Edge& sedge) {
8.1145 + edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge,
8.1146 + EdgeRefMap, TEdge>(tedge, sedge));
8.1147 + return *this;
8.1148 + }
8.1149 +
8.1150 + /// \brief Executes the copies.
8.1151 + ///
8.1152 + /// Executes the copies.
8.1153 + void run() {
8.1154 + NodeRefMap nodeRefMap(from);
8.1155 + EdgeRefMap edgeRefMap(from);
8.1156 + ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap);
8.1157 + _digraph_utils_bits::GraphCopySelector<To>::
8.1158 + copy(to, from, nodeRefMap, edgeRefMap);
8.1159 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
8.1160 + nodeMapCopies[i]->copy(from, nodeRefMap);
8.1161 + }
8.1162 + for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
8.1163 + edgeMapCopies[i]->copy(from, edgeRefMap);
8.1164 + }
8.1165 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
8.1166 + arcMapCopies[i]->copy(from, arcRefMap);
8.1167 + }
8.1168 + }
8.1169 +
8.1170 + private:
8.1171 +
8.1172 + const From& from;
8.1173 + To& to;
8.1174 +
8.1175 + std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
8.1176 + nodeMapCopies;
8.1177 +
8.1178 + std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
8.1179 + arcMapCopies;
8.1180 +
8.1181 + std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
8.1182 + edgeMapCopies;
8.1183 +
8.1184 + };
8.1185 +
8.1186 + /// \brief Copy an graph to another digraph.
8.1187 + ///
8.1188 + /// Copy an graph to another digraph.
8.1189 + /// The usage of the function:
8.1190 + ///
8.1191 + ///\code
8.1192 + /// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
8.1193 + ///\endcode
8.1194 + ///
8.1195 + /// After the copy the \c nr map will contain the mapping from the
8.1196 + /// nodes of the \c from digraph to the nodes of the \c to digraph and
8.1197 + /// \c ecr will contain the mapping from the arcs of the \c to digraph
8.1198 + /// to the arcs of the \c from digraph.
8.1199 + ///
8.1200 + /// \see GraphCopy
8.1201 + template <typename To, typename From>
8.1202 + GraphCopy<To, From>
8.1203 + copyGraph(To& to, const From& from) {
8.1204 + return GraphCopy<To, From>(to, from);
8.1205 + }
8.1206 +
8.1207 + /// \brief Class to copy a bipartite digraph.
8.1208 + ///
8.1209 + /// Class to copy a bipartite digraph to another digraph
8.1210 + /// (duplicate a digraph). The simplest way of using it is through
8.1211 + /// the \c copyBpGraph() function.
8.1212 + template <typename To, typename From>
8.1213 + class BpGraphCopy {
8.1214 + private:
8.1215 +
8.1216 + typedef typename From::Node Node;
8.1217 + typedef typename From::Red Red;
8.1218 + typedef typename From::Blue Blue;
8.1219 + typedef typename From::NodeIt NodeIt;
8.1220 + typedef typename From::Arc Arc;
8.1221 + typedef typename From::ArcIt ArcIt;
8.1222 + typedef typename From::Edge Edge;
8.1223 + typedef typename From::EdgeIt EdgeIt;
8.1224 +
8.1225 + typedef typename To::Node TNode;
8.1226 + typedef typename To::Arc TArc;
8.1227 + typedef typename To::Edge TEdge;
8.1228 +
8.1229 + typedef typename From::template RedMap<TNode> RedRefMap;
8.1230 + typedef typename From::template BlueMap<TNode> BlueRefMap;
8.1231 + typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
8.1232 +
8.1233 + struct NodeRefMap {
8.1234 + NodeRefMap(const From& _from, const RedRefMap& _red_ref,
8.1235 + const BlueRefMap& _blue_ref)
8.1236 + : from(_from), red_ref(_red_ref), blue_ref(_blue_ref) {}
8.1237 +
8.1238 + typedef typename From::Node Key;
8.1239 + typedef typename To::Node Value;
8.1240 +
8.1241 + Value operator[](const Key& key) const {
8.1242 + return from.red(key) ? red_ref[key] : blue_ref[key];
8.1243 + }
8.1244 +
8.1245 + const From& from;
8.1246 + const RedRefMap& red_ref;
8.1247 + const BlueRefMap& blue_ref;
8.1248 + };
8.1249 +
8.1250 + struct ArcRefMap {
8.1251 + ArcRefMap(const To& _to, const From& _from,
8.1252 + const EdgeRefMap& _edge_ref, const NodeRefMap& _node_ref)
8.1253 + : to(_to), from(_from),
8.1254 + edge_ref(_edge_ref), node_ref(_node_ref) {}
8.1255 +
8.1256 + typedef typename From::Arc Key;
8.1257 + typedef typename To::Arc Value;
8.1258 +
8.1259 + Value operator[](const Key& key) const {
8.1260 + bool forward =
8.1261 + (from.direction(key) ==
8.1262 + (node_ref[from.source(static_cast<const Edge&>(key))] ==
8.1263 + to.source(edge_ref[static_cast<const Edge&>(key)])));
8.1264 + return to.direct(edge_ref[key], forward);
8.1265 + }
8.1266 +
8.1267 + const To& to;
8.1268 + const From& from;
8.1269 + const EdgeRefMap& edge_ref;
8.1270 + const NodeRefMap& node_ref;
8.1271 + };
8.1272 +
8.1273 + public:
8.1274 +
8.1275 +
8.1276 + /// \brief Constructor for the DigraphCopy.
8.1277 + ///
8.1278 + /// It copies the content of the \c _from digraph into the
8.1279 + /// \c _to digraph.
8.1280 + BpGraphCopy(To& _to, const From& _from)
8.1281 + : from(_from), to(_to) {}
8.1282 +
8.1283 + /// \brief Destructor of the DigraphCopy
8.1284 + ///
8.1285 + /// Destructor of the DigraphCopy
8.1286 + ~BpGraphCopy() {
8.1287 + for (int i = 0; i < int(redMapCopies.size()); ++i) {
8.1288 + delete redMapCopies[i];
8.1289 + }
8.1290 + for (int i = 0; i < int(blueMapCopies.size()); ++i) {
8.1291 + delete blueMapCopies[i];
8.1292 + }
8.1293 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
8.1294 + delete nodeMapCopies[i];
8.1295 + }
8.1296 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
8.1297 + delete arcMapCopies[i];
8.1298 + }
8.1299 + for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
8.1300 + delete edgeMapCopies[i];
8.1301 + }
8.1302 +
8.1303 + }
8.1304 +
8.1305 + /// \brief Copies the A-node references into the given map.
8.1306 + ///
8.1307 + /// Copies the A-node references into the given map.
8.1308 + template <typename RedRef>
8.1309 + BpGraphCopy& redRef(RedRef& map) {
8.1310 + redMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Red,
8.1311 + RedRefMap, RedRef>(map));
8.1312 + return *this;
8.1313 + }
8.1314 +
8.1315 + /// \brief Copies the A-node cross references into the given map.
8.1316 + ///
8.1317 + /// Copies the A-node cross references (reverse references) into
8.1318 + /// the given map.
8.1319 + template <typename RedCrossRef>
8.1320 + BpGraphCopy& redCrossRef(RedCrossRef& map) {
8.1321 + redMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
8.1322 + Red, RedRefMap, RedCrossRef>(map));
8.1323 + return *this;
8.1324 + }
8.1325 +
8.1326 + /// \brief Make copy of the given A-node map.
8.1327 + ///
8.1328 + /// Makes copy of the given map for the newly created digraph.
8.1329 + /// The new map's key type is the to digraph's node type,
8.1330 + /// and the copied map's key type is the from digraph's node
8.1331 + /// type.
8.1332 + template <typename ToMap, typename FromMap>
8.1333 + BpGraphCopy& redMap(ToMap& tmap, const FromMap& map) {
8.1334 + redMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Red,
8.1335 + RedRefMap, ToMap, FromMap>(tmap, map));
8.1336 + return *this;
8.1337 + }
8.1338 +
8.1339 + /// \brief Copies the B-node references into the given map.
8.1340 + ///
8.1341 + /// Copies the B-node references into the given map.
8.1342 + template <typename BlueRef>
8.1343 + BpGraphCopy& blueRef(BlueRef& map) {
8.1344 + blueMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Blue,
8.1345 + BlueRefMap, BlueRef>(map));
8.1346 + return *this;
8.1347 + }
8.1348 +
8.1349 + /// \brief Copies the B-node cross references into the given map.
8.1350 + ///
8.1351 + /// Copies the B-node cross references (reverse references) into
8.1352 + /// the given map.
8.1353 + template <typename BlueCrossRef>
8.1354 + BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
8.1355 + blueMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
8.1356 + Blue, BlueRefMap, BlueCrossRef>(map));
8.1357 + return *this;
8.1358 + }
8.1359 +
8.1360 + /// \brief Make copy of the given B-node map.
8.1361 + ///
8.1362 + /// Makes copy of the given map for the newly created digraph.
8.1363 + /// The new map's key type is the to digraph's node type,
8.1364 + /// and the copied map's key type is the from digraph's node
8.1365 + /// type.
8.1366 + template <typename ToMap, typename FromMap>
8.1367 + BpGraphCopy& blueMap(ToMap& tmap, const FromMap& map) {
8.1368 + blueMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Blue,
8.1369 + BlueRefMap, ToMap, FromMap>(tmap, map));
8.1370 + return *this;
8.1371 + }
8.1372 + /// \brief Copies the node references into the given map.
8.1373 + ///
8.1374 + /// Copies the node references into the given map.
8.1375 + template <typename NodeRef>
8.1376 + BpGraphCopy& nodeRef(NodeRef& map) {
8.1377 + nodeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Node,
8.1378 + NodeRefMap, NodeRef>(map));
8.1379 + return *this;
8.1380 + }
8.1381 +
8.1382 + /// \brief Copies the node cross references into the given map.
8.1383 + ///
8.1384 + /// Copies the node cross references (reverse references) into
8.1385 + /// the given map.
8.1386 + template <typename NodeCrossRef>
8.1387 + BpGraphCopy& nodeCrossRef(NodeCrossRef& map) {
8.1388 + nodeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Node,
8.1389 + NodeRefMap, NodeCrossRef>(map));
8.1390 + return *this;
8.1391 + }
8.1392 +
8.1393 + /// \brief Make copy of the given map.
8.1394 + ///
8.1395 + /// Makes copy of the given map for the newly created digraph.
8.1396 + /// The new map's key type is the to digraph's node type,
8.1397 + /// and the copied map's key type is the from digraph's node
8.1398 + /// type.
8.1399 + template <typename ToMap, typename FromMap>
8.1400 + BpGraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
8.1401 + nodeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Node,
8.1402 + NodeRefMap, ToMap, FromMap>(tmap, map));
8.1403 + return *this;
8.1404 + }
8.1405 +
8.1406 + /// \brief Make a copy of the given node.
8.1407 + ///
8.1408 + /// Make a copy of the given node.
8.1409 + BpGraphCopy& node(TNode& tnode, const Node& snode) {
8.1410 + nodeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Node,
8.1411 + NodeRefMap, TNode>(tnode, snode));
8.1412 + return *this;
8.1413 + }
8.1414 +
8.1415 + /// \brief Copies the arc references into the given map.
8.1416 + ///
8.1417 + /// Copies the arc references into the given map.
8.1418 + template <typename ArcRef>
8.1419 + BpGraphCopy& arcRef(ArcRef& map) {
8.1420 + arcMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Arc,
8.1421 + ArcRefMap, ArcRef>(map));
8.1422 + return *this;
8.1423 + }
8.1424 +
8.1425 + /// \brief Copies the arc cross references into the given map.
8.1426 + ///
8.1427 + /// Copies the arc cross references (reverse references) into
8.1428 + /// the given map.
8.1429 + template <typename ArcCrossRef>
8.1430 + BpGraphCopy& arcCrossRef(ArcCrossRef& map) {
8.1431 + arcMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From, Arc,
8.1432 + ArcRefMap, ArcCrossRef>(map));
8.1433 + return *this;
8.1434 + }
8.1435 +
8.1436 + /// \brief Make copy of the given map.
8.1437 + ///
8.1438 + /// Makes copy of the given map for the newly created digraph.
8.1439 + /// The new map's key type is the to digraph's arc type,
8.1440 + /// and the copied map's key type is the from digraph's arc
8.1441 + /// type.
8.1442 + template <typename ToMap, typename FromMap>
8.1443 + BpGraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
8.1444 + arcMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Arc,
8.1445 + ArcRefMap, ToMap, FromMap>(tmap, map));
8.1446 + return *this;
8.1447 + }
8.1448 +
8.1449 + /// \brief Make a copy of the given arc.
8.1450 + ///
8.1451 + /// Make a copy of the given arc.
8.1452 + BpGraphCopy& arc(TArc& tarc, const Arc& sarc) {
8.1453 + arcMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Arc,
8.1454 + ArcRefMap, TArc>(tarc, sarc));
8.1455 + return *this;
8.1456 + }
8.1457 +
8.1458 + /// \brief Copies the edge references into the given map.
8.1459 + ///
8.1460 + /// Copies the edge references into the given map.
8.1461 + template <typename EdgeRef>
8.1462 + BpGraphCopy& edgeRef(EdgeRef& map) {
8.1463 + edgeMapCopies.push_back(new _digraph_utils_bits::RefCopy<From, Edge,
8.1464 + EdgeRefMap, EdgeRef>(map));
8.1465 + return *this;
8.1466 + }
8.1467 +
8.1468 + /// \brief Copies the edge cross references into the given map.
8.1469 + ///
8.1470 + /// Copies the edge cross references (reverse
8.1471 + /// references) into the given map.
8.1472 + template <typename EdgeCrossRef>
8.1473 + BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
8.1474 + edgeMapCopies.push_back(new _digraph_utils_bits::CrossRefCopy<From,
8.1475 + Edge, EdgeRefMap, EdgeCrossRef>(map));
8.1476 + return *this;
8.1477 + }
8.1478 +
8.1479 + /// \brief Make copy of the given map.
8.1480 + ///
8.1481 + /// Makes copy of the given map for the newly created digraph.
8.1482 + /// The new map's key type is the to digraph's edge type,
8.1483 + /// and the copied map's key type is the from digraph's edge
8.1484 + /// type.
8.1485 + template <typename ToMap, typename FromMap>
8.1486 + BpGraphCopy& edgeMap(ToMap& tmap, const FromMap& map) {
8.1487 + edgeMapCopies.push_back(new _digraph_utils_bits::MapCopy<From, Edge,
8.1488 + EdgeRefMap, ToMap, FromMap>(tmap, map));
8.1489 + return *this;
8.1490 + }
8.1491 +
8.1492 + /// \brief Make a copy of the given edge.
8.1493 + ///
8.1494 + /// Make a copy of the given edge.
8.1495 + BpGraphCopy& edge(TEdge& tedge, const Edge& sedge) {
8.1496 + edgeMapCopies.push_back(new _digraph_utils_bits::ItemCopy<From, Edge,
8.1497 + EdgeRefMap, TEdge>(tedge, sedge));
8.1498 + return *this;
8.1499 + }
8.1500 +
8.1501 + /// \brief Executes the copies.
8.1502 + ///
8.1503 + /// Executes the copies.
8.1504 + void run() {
8.1505 + RedRefMap redRefMap(from);
8.1506 + BlueRefMap blueRefMap(from);
8.1507 + NodeRefMap nodeRefMap(from, redRefMap, blueRefMap);
8.1508 + EdgeRefMap edgeRefMap(from);
8.1509 + ArcRefMap arcRefMap(to, from, edgeRefMap, nodeRefMap);
8.1510 + _digraph_utils_bits::BpGraphCopySelector<To>::
8.1511 + copy(to, from, redRefMap, blueRefMap, edgeRefMap);
8.1512 + for (int i = 0; i < int(redMapCopies.size()); ++i) {
8.1513 + redMapCopies[i]->copy(from, redRefMap);
8.1514 + }
8.1515 + for (int i = 0; i < int(blueMapCopies.size()); ++i) {
8.1516 + blueMapCopies[i]->copy(from, blueRefMap);
8.1517 + }
8.1518 + for (int i = 0; i < int(nodeMapCopies.size()); ++i) {
8.1519 + nodeMapCopies[i]->copy(from, nodeRefMap);
8.1520 + }
8.1521 + for (int i = 0; i < int(edgeMapCopies.size()); ++i) {
8.1522 + edgeMapCopies[i]->copy(from, edgeRefMap);
8.1523 + }
8.1524 + for (int i = 0; i < int(arcMapCopies.size()); ++i) {
8.1525 + arcMapCopies[i]->copy(from, arcRefMap);
8.1526 + }
8.1527 + }
8.1528 +
8.1529 + private:
8.1530 +
8.1531 + const From& from;
8.1532 + To& to;
8.1533 +
8.1534 + std::vector<_digraph_utils_bits::MapCopyBase<From, Red, RedRefMap>* >
8.1535 + redMapCopies;
8.1536 +
8.1537 + std::vector<_digraph_utils_bits::MapCopyBase<From, Blue, BlueRefMap>* >
8.1538 + blueMapCopies;
8.1539 +
8.1540 + std::vector<_digraph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
8.1541 + nodeMapCopies;
8.1542 +
8.1543 + std::vector<_digraph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
8.1544 + arcMapCopies;
8.1545 +
8.1546 + std::vector<_digraph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
8.1547 + edgeMapCopies;
8.1548 +
8.1549 + };
8.1550 +
8.1551 + /// \brief Copy a bipartite digraph to another digraph.
8.1552 + ///
8.1553 + /// Copy a bipartite digraph to another digraph.
8.1554 + /// The usage of the function:
8.1555 + ///
8.1556 + ///\code
8.1557 + /// copyBpGraph(trg, src).redRef(anr).arcCrossRef(ecr).run();
8.1558 + ///\endcode
8.1559 + ///
8.1560 + /// After the copy the \c nr map will contain the mapping from the
8.1561 + /// nodes of the \c from digraph to the nodes of the \c to digraph and
8.1562 + /// \c ecr will contain the mapping from the arcs of the \c to digraph
8.1563 + /// to the arcs of the \c from digraph.
8.1564 + ///
8.1565 + /// \see BpGraphCopy
8.1566 + template <typename To, typename From>
8.1567 + BpGraphCopy<To, From>
8.1568 + copyBpGraph(To& to, const From& from) {
8.1569 + return BpGraphCopy<To, From>(to, from);
8.1570 + }
8.1571 +
8.1572 +
8.1573 + /// @}
8.1574 +
8.1575 + /// \addtogroup digraph_maps
8.1576 + /// @{
8.1577 +
8.1578 + /// Provides an immutable and unique id for each item in the digraph.
8.1579 +
8.1580 + /// The IdMap class provides a unique and immutable id for each item of the
8.1581 + /// same type (e.g. node) in the digraph. This id is <ul><li>\b unique:
8.1582 + /// different items (nodes) get different ids <li>\b immutable: the id of an
8.1583 + /// item (node) does not change (even if you delete other nodes). </ul>
8.1584 + /// Through this map you get access (i.e. can read) the inner id values of
8.1585 + /// the items stored in the digraph. This map can be inverted with its member
8.1586 + /// class \c InverseMap.
8.1587 + ///
8.1588 + template <typename _Digraph, typename _Item>
8.1589 + class IdMap {
8.1590 + public:
8.1591 + typedef _Digraph Digraph;
8.1592 + typedef int Value;
8.1593 + typedef _Item Item;
8.1594 + typedef _Item Key;
8.1595 +
8.1596 + /// \brief Constructor.
8.1597 + ///
8.1598 + /// Constructor of the map.
8.1599 + explicit IdMap(const Digraph& _digraph) : digraph(&_digraph) {}
8.1600 +
8.1601 + /// \brief Gives back the \e id of the item.
8.1602 + ///
8.1603 + /// Gives back the immutable and unique \e id of the item.
8.1604 + int operator[](const Item& item) const { return digraph->id(item);}
8.1605 +
8.1606 + /// \brief Gives back the item by its id.
8.1607 + ///
8.1608 + /// Gives back the item by its id.
8.1609 + Item operator()(int id) { return digraph->fromId(id, Item()); }
8.1610 +
8.1611 + private:
8.1612 + const Digraph* digraph;
8.1613 +
8.1614 + public:
8.1615 +
8.1616 + /// \brief The class represents the inverse of its owner (IdMap).
8.1617 + ///
8.1618 + /// The class represents the inverse of its owner (IdMap).
8.1619 + /// \see inverse()
8.1620 + class InverseMap {
8.1621 + public:
8.1622 +
8.1623 + /// \brief Constructor.
8.1624 + ///
8.1625 + /// Constructor for creating an id-to-item map.
8.1626 + explicit InverseMap(const Digraph& _digraph) : digraph(&_digraph) {}
8.1627 +
8.1628 + /// \brief Constructor.
8.1629 + ///
8.1630 + /// Constructor for creating an id-to-item map.
8.1631 + explicit InverseMap(const IdMap& idMap) : digraph(idMap.digraph) {}
8.1632 +
8.1633 + /// \brief Gives back the given item from its id.
8.1634 + ///
8.1635 + /// Gives back the given item from its id.
8.1636 + ///
8.1637 + Item operator[](int id) const { return digraph->fromId(id, Item());}
8.1638 +
8.1639 + private:
8.1640 + const Digraph* digraph;
8.1641 + };
8.1642 +
8.1643 + /// \brief Gives back the inverse of the map.
8.1644 + ///
8.1645 + /// Gives back the inverse of the IdMap.
8.1646 + InverseMap inverse() const { return InverseMap(*digraph);}
8.1647 +
8.1648 + };
8.1649 +
8.1650 +
8.1651 + /// \brief General invertable digraph-map type.
8.1652 +
8.1653 + /// This type provides simple invertable digraph-maps.
8.1654 + /// The InvertableMap wraps an arbitrary ReadWriteMap
8.1655 + /// and if a key is set to a new value then store it
8.1656 + /// in the inverse map.
8.1657 + ///
8.1658 + /// The values of the map can be accessed
8.1659 + /// with stl compatible forward iterator.
8.1660 + ///
8.1661 + /// \param _Digraph The digraph type.
8.1662 + /// \param _Item The item type of the digraph.
8.1663 + /// \param _Value The value type of the map.
8.1664 + ///
8.1665 + /// \see IterableValueMap
8.1666 + template <typename _Digraph, typename _Item, typename _Value>
8.1667 + class InvertableMap : protected DefaultMap<_Digraph, _Item, _Value> {
8.1668 + private:
8.1669 +
8.1670 + typedef DefaultMap<_Digraph, _Item, _Value> Map;
8.1671 + typedef _Digraph Digraph;
8.1672 +
8.1673 + typedef std::map<_Value, _Item> Container;
8.1674 + Container invMap;
8.1675 +
8.1676 + public:
8.1677 +
8.1678 + /// The key type of InvertableMap (Node, Arc, Edge).
8.1679 + typedef typename Map::Key Key;
8.1680 + /// The value type of the InvertableMap.
8.1681 + typedef typename Map::Value Value;
8.1682 +
8.1683 +
8.1684 +
8.1685 + /// \brief Constructor.
8.1686 + ///
8.1687 + /// Construct a new InvertableMap for the digraph.
8.1688 + ///
8.1689 + explicit InvertableMap(const Digraph& digraph) : Map(digraph) {}
8.1690 +
8.1691 + /// \brief Forward iterator for values.
8.1692 + ///
8.1693 + /// This iterator is an stl compatible forward
8.1694 + /// iterator on the values of the map. The values can
8.1695 + /// be accessed in the [beginValue, endValue) range.
8.1696 + ///
8.1697 + class ValueIterator
8.1698 + : public std::iterator<std::forward_iterator_tag, Value> {
8.1699 + friend class InvertableMap;
8.1700 + private:
8.1701 + ValueIterator(typename Container::const_iterator _it)
8.1702 + : it(_it) {}
8.1703 + public:
8.1704 +
8.1705 + ValueIterator() {}
8.1706 +
8.1707 + ValueIterator& operator++() { ++it; return *this; }
8.1708 + ValueIterator operator++(int) {
8.1709 + ValueIterator tmp(*this);
8.1710 + operator++();
8.1711 + return tmp;
8.1712 + }
8.1713 +
8.1714 + const Value& operator*() const { return it->first; }
8.1715 + const Value* operator->() const { return &(it->first); }
8.1716 +
8.1717 + bool operator==(ValueIterator jt) const { return it == jt.it; }
8.1718 + bool operator!=(ValueIterator jt) const { return it != jt.it; }
8.1719 +
8.1720 + private:
8.1721 + typename Container::const_iterator it;
8.1722 + };
8.1723 +
8.1724 + /// \brief Returns an iterator to the first value.
8.1725 + ///
8.1726 + /// Returns an stl compatible iterator to the
8.1727 + /// first value of the map. The values of the
8.1728 + /// map can be accessed in the [beginValue, endValue)
8.1729 + /// range.
8.1730 + ValueIterator beginValue() const {
8.1731 + return ValueIterator(invMap.begin());
8.1732 + }
8.1733 +
8.1734 + /// \brief Returns an iterator after the last value.
8.1735 + ///
8.1736 + /// Returns an stl compatible iterator after the
8.1737 + /// last value of the map. The values of the
8.1738 + /// map can be accessed in the [beginValue, endValue)
8.1739 + /// range.
8.1740 + ValueIterator endValue() const {
8.1741 + return ValueIterator(invMap.end());
8.1742 + }
8.1743 +
8.1744 + /// \brief The setter function of the map.
8.1745 + ///
8.1746 + /// Sets the mapped value.
8.1747 + void set(const Key& key, const Value& val) {
8.1748 + Value oldval = Map::operator[](key);
8.1749 + typename Container::iterator it = invMap.find(oldval);
8.1750 + if (it != invMap.end() && it->second == key) {
8.1751 + invMap.erase(it);
8.1752 + }
8.1753 + invMap.insert(make_pair(val, key));
8.1754 + Map::set(key, val);
8.1755 + }
8.1756 +
8.1757 + /// \brief The getter function of the map.
8.1758 + ///
8.1759 + /// It gives back the value associated with the key.
8.1760 + typename MapTraits<Map>::ConstReturnValue
8.1761 + operator[](const Key& key) const {
8.1762 + return Map::operator[](key);
8.1763 + }
8.1764 +
8.1765 + /// \brief Gives back the item by its value.
8.1766 + ///
8.1767 + /// Gives back the item by its value.
8.1768 + Key operator()(const Value& key) const {
8.1769 + typename Container::const_iterator it = invMap.find(key);
8.1770 + return it != invMap.end() ? it->second : INVALID;
8.1771 + }
8.1772 +
8.1773 + protected:
8.1774 +
8.1775 + /// \brief Erase the key from the map.
8.1776 + ///
8.1777 + /// Erase the key to the map. It is called by the
8.1778 + /// \c AlterationNotifier.
8.1779 + virtual void erase(const Key& key) {
8.1780 + Value val = Map::operator[](key);
8.1781 + typename Container::iterator it = invMap.find(val);
8.1782 + if (it != invMap.end() && it->second == key) {
8.1783 + invMap.erase(it);
8.1784 + }
8.1785 + Map::erase(key);
8.1786 + }
8.1787 +
8.1788 + /// \brief Erase more keys from the map.
8.1789 + ///
8.1790 + /// Erase more keys from the map. It is called by the
8.1791 + /// \c AlterationNotifier.
8.1792 + virtual void erase(const std::vector<Key>& keys) {
8.1793 + for (int i = 0; i < int(keys.size()); ++i) {
8.1794 + Value val = Map::operator[](keys[i]);
8.1795 + typename Container::iterator it = invMap.find(val);
8.1796 + if (it != invMap.end() && it->second == keys[i]) {
8.1797 + invMap.erase(it);
8.1798 + }
8.1799 + }
8.1800 + Map::erase(keys);
8.1801 + }
8.1802 +
8.1803 + /// \brief Clear the keys from the map and inverse map.
8.1804 + ///
8.1805 + /// Clear the keys from the map and inverse map. It is called by the
8.1806 + /// \c AlterationNotifier.
8.1807 + virtual void clear() {
8.1808 + invMap.clear();
8.1809 + Map::clear();
8.1810 + }
8.1811 +
8.1812 + public:
8.1813 +
8.1814 + /// \brief The inverse map type.
8.1815 + ///
8.1816 + /// The inverse of this map. The subscript operator of the map
8.1817 + /// gives back always the item what was last assigned to the value.
8.1818 + class InverseMap {
8.1819 + public:
8.1820 + /// \brief Constructor of the InverseMap.
8.1821 + ///
8.1822 + /// Constructor of the InverseMap.
8.1823 + explicit InverseMap(const InvertableMap& _inverted)
8.1824 + : inverted(_inverted) {}
8.1825 +
8.1826 + /// The value type of the InverseMap.
8.1827 + typedef typename InvertableMap::Key Value;
8.1828 + /// The key type of the InverseMap.
8.1829 + typedef typename InvertableMap::Value Key;
8.1830 +
8.1831 + /// \brief Subscript operator.
8.1832 + ///
8.1833 + /// Subscript operator. It gives back always the item
8.1834 + /// what was last assigned to the value.
8.1835 + Value operator[](const Key& key) const {
8.1836 + return inverted(key);
8.1837 + }
8.1838 +
8.1839 + private:
8.1840 + const InvertableMap& inverted;
8.1841 + };
8.1842 +
8.1843 + /// \brief It gives back the just readable inverse map.
8.1844 + ///
8.1845 + /// It gives back the just readable inverse map.
8.1846 + InverseMap inverse() const {
8.1847 + return InverseMap(*this);
8.1848 + }
8.1849 +
8.1850 +
8.1851 +
8.1852 + };
8.1853 +
8.1854 + /// \brief Provides a mutable, continuous and unique descriptor for each
8.1855 + /// item in the digraph.
8.1856 + ///
8.1857 + /// The DescriptorMap class provides a unique and continuous (but mutable)
8.1858 + /// descriptor (id) for each item of the same type (e.g. node) in the
8.1859 + /// digraph. This id is <ul><li>\b unique: different items (nodes) get
8.1860 + /// different ids <li>\b continuous: the range of the ids is the set of
8.1861 + /// integers between 0 and \c n-1, where \c n is the number of the items of
8.1862 + /// this type (e.g. nodes) (so the id of a node can change if you delete an
8.1863 + /// other node, i.e. this id is mutable). </ul> This map can be inverted
8.1864 + /// with its member class \c InverseMap.
8.1865 + ///
8.1866 + /// \param _Digraph The digraph class the \c DescriptorMap belongs to.
8.1867 + /// \param _Item The Item is the Key of the Map. It may be Node, Arc or
8.1868 + /// Edge.
8.1869 + template <typename _Digraph, typename _Item>
8.1870 + class DescriptorMap : protected DefaultMap<_Digraph, _Item, int> {
8.1871 +
8.1872 + typedef _Item Item;
8.1873 + typedef DefaultMap<_Digraph, _Item, int> Map;
8.1874 +
8.1875 + public:
8.1876 + /// The digraph class of DescriptorMap.
8.1877 + typedef _Digraph Digraph;
8.1878 +
8.1879 + /// The key type of DescriptorMap (Node, Arc, Edge).
8.1880 + typedef typename Map::Key Key;
8.1881 + /// The value type of DescriptorMap.
8.1882 + typedef typename Map::Value Value;
8.1883 +
8.1884 + /// \brief Constructor.
8.1885 + ///
8.1886 + /// Constructor for descriptor map.
8.1887 + explicit DescriptorMap(const Digraph& _digraph) : Map(_digraph) {
8.1888 + Item it;
8.1889 + const typename Map::Notifier* nf = Map::notifier();
8.1890 + for (nf->first(it); it != INVALID; nf->next(it)) {
8.1891 + Map::set(it, invMap.size());
8.1892 + invMap.push_back(it);
8.1893 + }
8.1894 + }
8.1895 +
8.1896 + protected:
8.1897 +
8.1898 + /// \brief Add a new key to the map.
8.1899 + ///
8.1900 + /// Add a new key to the map. It is called by the
8.1901 + /// \c AlterationNotifier.
8.1902 + virtual void add(const Item& item) {
8.1903 + Map::add(item);
8.1904 + Map::set(item, invMap.size());
8.1905 + invMap.push_back(item);
8.1906 + }
8.1907 +
8.1908 + /// \brief Add more new keys to the map.
8.1909 + ///
8.1910 + /// Add more new keys to the map. It is called by the
8.1911 + /// \c AlterationNotifier.
8.1912 + virtual void add(const std::vector<Item>& items) {
8.1913 + Map::add(items);
8.1914 + for (int i = 0; i < int(items.size()); ++i) {
8.1915 + Map::set(items[i], invMap.size());
8.1916 + invMap.push_back(items[i]);
8.1917 + }
8.1918 + }
8.1919 +
8.1920 + /// \brief Erase the key from the map.
8.1921 + ///
8.1922 + /// Erase the key from the map. It is called by the
8.1923 + /// \c AlterationNotifier.
8.1924 + virtual void erase(const Item& item) {
8.1925 + Map::set(invMap.back(), Map::operator[](item));
8.1926 + invMap[Map::operator[](item)] = invMap.back();
8.1927 + invMap.pop_back();
8.1928 + Map::erase(item);
8.1929 + }
8.1930 +
8.1931 + /// \brief Erase more keys from the map.
8.1932 + ///
8.1933 + /// Erase more keys from the map. It is called by the
8.1934 + /// \c AlterationNotifier.
8.1935 + virtual void erase(const std::vector<Item>& items) {
8.1936 + for (int i = 0; i < int(items.size()); ++i) {
8.1937 + Map::set(invMap.back(), Map::operator[](items[i]));
8.1938 + invMap[Map::operator[](items[i])] = invMap.back();
8.1939 + invMap.pop_back();
8.1940 + }
8.1941 + Map::erase(items);
8.1942 + }
8.1943 +
8.1944 + /// \brief Build the unique map.
8.1945 + ///
8.1946 + /// Build the unique map. It is called by the
8.1947 + /// \c AlterationNotifier.
8.1948 + virtual void build() {
8.1949 + Map::build();
8.1950 + Item it;
8.1951 + const typename Map::Notifier* nf = Map::notifier();
8.1952 + for (nf->first(it); it != INVALID; nf->next(it)) {
8.1953 + Map::set(it, invMap.size());
8.1954 + invMap.push_back(it);
8.1955 + }
8.1956 + }
8.1957 +
8.1958 + /// \brief Clear the keys from the map.
8.1959 + ///
8.1960 + /// Clear the keys from the map. It is called by the
8.1961 + /// \c AlterationNotifier.
8.1962 + virtual void clear() {
8.1963 + invMap.clear();
8.1964 + Map::clear();
8.1965 + }
8.1966 +
8.1967 + public:
8.1968 +
8.1969 + /// \brief Returns the maximal value plus one.
8.1970 + ///
8.1971 + /// Returns the maximal value plus one in the map.
8.1972 + unsigned int size() const {
8.1973 + return invMap.size();
8.1974 + }
8.1975 +
8.1976 + /// \brief Swaps the position of the two items in the map.
8.1977 + ///
8.1978 + /// Swaps the position of the two items in the map.
8.1979 + void swap(const Item& p, const Item& q) {
8.1980 + int pi = Map::operator[](p);
8.1981 + int qi = Map::operator[](q);
8.1982 + Map::set(p, qi);
8.1983 + invMap[qi] = p;
8.1984 + Map::set(q, pi);
8.1985 + invMap[pi] = q;
8.1986 + }
8.1987 +
8.1988 + /// \brief Gives back the \e descriptor of the item.
8.1989 + ///
8.1990 + /// Gives back the mutable and unique \e descriptor of the map.
8.1991 + int operator[](const Item& item) const {
8.1992 + return Map::operator[](item);
8.1993 + }
8.1994 +
8.1995 + /// \brief Gives back the item by its descriptor.
8.1996 + ///
8.1997 + /// Gives back th item by its descriptor.
8.1998 + Item operator()(int id) const {
8.1999 + return invMap[id];
8.2000 + }
8.2001 +
8.2002 + private:
8.2003 +
8.2004 + typedef std::vector<Item> Container;
8.2005 + Container invMap;
8.2006 +
8.2007 + public:
8.2008 + /// \brief The inverse map type of DescriptorMap.
8.2009 + ///
8.2010 + /// The inverse map type of DescriptorMap.
8.2011 + class InverseMap {
8.2012 + public:
8.2013 + /// \brief Constructor of the InverseMap.
8.2014 + ///
8.2015 + /// Constructor of the InverseMap.
8.2016 + explicit InverseMap(const DescriptorMap& _inverted)
8.2017 + : inverted(_inverted) {}
8.2018 +
8.2019 +
8.2020 + /// The value type of the InverseMap.
8.2021 + typedef typename DescriptorMap::Key Value;
8.2022 + /// The key type of the InverseMap.
8.2023 + typedef typename DescriptorMap::Value Key;
8.2024 +
8.2025 + /// \brief Subscript operator.
8.2026 + ///
8.2027 + /// Subscript operator. It gives back the item
8.2028 + /// that the descriptor belongs to currently.
8.2029 + Value operator[](const Key& key) const {
8.2030 + return inverted(key);
8.2031 + }
8.2032 +
8.2033 + /// \brief Size of the map.
8.2034 + ///
8.2035 + /// Returns the size of the map.
8.2036 + unsigned int size() const {
8.2037 + return inverted.size();
8.2038 + }
8.2039 +
8.2040 + private:
8.2041 + const DescriptorMap& inverted;
8.2042 + };
8.2043 +
8.2044 + /// \brief Gives back the inverse of the map.
8.2045 + ///
8.2046 + /// Gives back the inverse of the map.
8.2047 + const InverseMap inverse() const {
8.2048 + return InverseMap(*this);
8.2049 + }
8.2050 + };
8.2051 +
8.2052 + /// \brief Returns the source of the given arc.
8.2053 + ///
8.2054 + /// The SourceMap gives back the source Node of the given arc.
8.2055 + /// \see TargetMap
8.2056 + /// \author Balazs Dezso
8.2057 + template <typename Digraph>
8.2058 + class SourceMap {
8.2059 + public:
8.2060 +
8.2061 + typedef typename Digraph::Node Value;
8.2062 + typedef typename Digraph::Arc Key;
8.2063 +
8.2064 + /// \brief Constructor
8.2065 + ///
8.2066 + /// Constructor
8.2067 + /// \param _digraph The digraph that the map belongs to.
8.2068 + explicit SourceMap(const Digraph& _digraph) : digraph(_digraph) {}
8.2069 +
8.2070 + /// \brief The subscript operator.
8.2071 + ///
8.2072 + /// The subscript operator.
8.2073 + /// \param arc The arc
8.2074 + /// \return The source of the arc
8.2075 + Value operator[](const Key& arc) const {
8.2076 + return digraph.source(arc);
8.2077 + }
8.2078 +
8.2079 + private:
8.2080 + const Digraph& digraph;
8.2081 + };
8.2082 +
8.2083 + /// \brief Returns a \ref SourceMap class.
8.2084 + ///
8.2085 + /// This function just returns an \ref SourceMap class.
8.2086 + /// \relates SourceMap
8.2087 + template <typename Digraph>
8.2088 + inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
8.2089 + return SourceMap<Digraph>(digraph);
8.2090 + }
8.2091 +
8.2092 + /// \brief Returns the target of the given arc.
8.2093 + ///
8.2094 + /// The TargetMap gives back the target Node of the given arc.
8.2095 + /// \see SourceMap
8.2096 + /// \author Balazs Dezso
8.2097 + template <typename Digraph>
8.2098 + class TargetMap {
8.2099 + public:
8.2100 +
8.2101 + typedef typename Digraph::Node Value;
8.2102 + typedef typename Digraph::Arc Key;
8.2103 +
8.2104 + /// \brief Constructor
8.2105 + ///
8.2106 + /// Constructor
8.2107 + /// \param _digraph The digraph that the map belongs to.
8.2108 + explicit TargetMap(const Digraph& _digraph) : digraph(_digraph) {}
8.2109 +
8.2110 + /// \brief The subscript operator.
8.2111 + ///
8.2112 + /// The subscript operator.
8.2113 + /// \param e The arc
8.2114 + /// \return The target of the arc
8.2115 + Value operator[](const Key& e) const {
8.2116 + return digraph.target(e);
8.2117 + }
8.2118 +
8.2119 + private:
8.2120 + const Digraph& digraph;
8.2121 + };
8.2122 +
8.2123 + /// \brief Returns a \ref TargetMap class.
8.2124 + ///
8.2125 + /// This function just returns a \ref TargetMap class.
8.2126 + /// \relates TargetMap
8.2127 + template <typename Digraph>
8.2128 + inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
8.2129 + return TargetMap<Digraph>(digraph);
8.2130 + }
8.2131 +
8.2132 + /// \brief Returns the "forward" directed arc view of an edge.
8.2133 + ///
8.2134 + /// Returns the "forward" directed arc view of an edge.
8.2135 + /// \see BackwardMap
8.2136 + /// \author Balazs Dezso
8.2137 + template <typename Digraph>
8.2138 + class ForwardMap {
8.2139 + public:
8.2140 +
8.2141 + typedef typename Digraph::Arc Value;
8.2142 + typedef typename Digraph::Edge Key;
8.2143 +
8.2144 + /// \brief Constructor
8.2145 + ///
8.2146 + /// Constructor
8.2147 + /// \param _digraph The digraph that the map belongs to.
8.2148 + explicit ForwardMap(const Digraph& _digraph) : digraph(_digraph) {}
8.2149 +
8.2150 + /// \brief The subscript operator.
8.2151 + ///
8.2152 + /// The subscript operator.
8.2153 + /// \param key An edge
8.2154 + /// \return The "forward" directed arc view of edge
8.2155 + Value operator[](const Key& key) const {
8.2156 + return digraph.direct(key, true);
8.2157 + }
8.2158 +
8.2159 + private:
8.2160 + const Digraph& digraph;
8.2161 + };
8.2162 +
8.2163 + /// \brief Returns a \ref ForwardMap class.
8.2164 + ///
8.2165 + /// This function just returns an \ref ForwardMap class.
8.2166 + /// \relates ForwardMap
8.2167 + template <typename Digraph>
8.2168 + inline ForwardMap<Digraph> forwardMap(const Digraph& digraph) {
8.2169 + return ForwardMap<Digraph>(digraph);
8.2170 + }
8.2171 +
8.2172 + /// \brief Returns the "backward" directed arc view of an edge.
8.2173 + ///
8.2174 + /// Returns the "backward" directed arc view of an edge.
8.2175 + /// \see ForwardMap
8.2176 + /// \author Balazs Dezso
8.2177 + template <typename Digraph>
8.2178 + class BackwardMap {
8.2179 + public:
8.2180 +
8.2181 + typedef typename Digraph::Arc Value;
8.2182 + typedef typename Digraph::Edge Key;
8.2183 +
8.2184 + /// \brief Constructor
8.2185 + ///
8.2186 + /// Constructor
8.2187 + /// \param _digraph The digraph that the map belongs to.
8.2188 + explicit BackwardMap(const Digraph& _digraph) : digraph(_digraph) {}
8.2189 +
8.2190 + /// \brief The subscript operator.
8.2191 + ///
8.2192 + /// The subscript operator.
8.2193 + /// \param key An edge
8.2194 + /// \return The "backward" directed arc view of edge
8.2195 + Value operator[](const Key& key) const {
8.2196 + return digraph.direct(key, false);
8.2197 + }
8.2198 +
8.2199 + private:
8.2200 + const Digraph& digraph;
8.2201 + };
8.2202 +
8.2203 + /// \brief Returns a \ref BackwardMap class
8.2204 +
8.2205 + /// This function just returns a \ref BackwardMap class.
8.2206 + /// \relates BackwardMap
8.2207 + template <typename Digraph>
8.2208 + inline BackwardMap<Digraph> backwardMap(const Digraph& digraph) {
8.2209 + return BackwardMap<Digraph>(digraph);
8.2210 + }
8.2211 +
8.2212 + /// \brief Potential difference map
8.2213 + ///
8.2214 + /// If there is an potential map on the nodes then we
8.2215 + /// can get an arc map as we get the substraction of the
8.2216 + /// values of the target and source.
8.2217 + template <typename Digraph, typename NodeMap>
8.2218 + class PotentialDifferenceMap {
8.2219 + public:
8.2220 + typedef typename Digraph::Arc Key;
8.2221 + typedef typename NodeMap::Value Value;
8.2222 +
8.2223 + /// \brief Constructor
8.2224 + ///
8.2225 + /// Contructor of the map
8.2226 + explicit PotentialDifferenceMap(const Digraph& _digraph,
8.2227 + const NodeMap& _potential)
8.2228 + : digraph(_digraph), potential(_potential) {}
8.2229 +
8.2230 + /// \brief Const subscription operator
8.2231 + ///
8.2232 + /// Const subscription operator
8.2233 + Value operator[](const Key& arc) const {
8.2234 + return potential[digraph.target(arc)] - potential[digraph.source(arc)];
8.2235 + }
8.2236 +
8.2237 + private:
8.2238 + const Digraph& digraph;
8.2239 + const NodeMap& potential;
8.2240 + };
8.2241 +
8.2242 + /// \brief Returns a PotentialDifferenceMap.
8.2243 + ///
8.2244 + /// This function just returns a PotentialDifferenceMap.
8.2245 + /// \relates PotentialDifferenceMap
8.2246 + template <typename Digraph, typename NodeMap>
8.2247 + PotentialDifferenceMap<Digraph, NodeMap>
8.2248 + potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
8.2249 + return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential);
8.2250 + }
8.2251 +
8.2252 + /// \brief Map of the node in-degrees.
8.2253 + ///
8.2254 + /// This map returns the in-degree of a node. Once it is constructed,
8.2255 + /// the degrees are stored in a standard NodeMap, so each query is done
8.2256 + /// in constant time. On the other hand, the values are updated automatically
8.2257 + /// whenever the digraph changes.
8.2258 + ///
8.2259 + /// \warning Besides addNode() and addArc(), a digraph structure may provide
8.2260 + /// alternative ways to modify the digraph. The correct behavior of InDegMap
8.2261 + /// is not guarantied if these additional features are used. For example
8.2262 + /// the functions \ref ListDigraph::changeSource() "changeSource()",
8.2263 + /// \ref ListDigraph::changeTarget() "changeTarget()" and
8.2264 + /// \ref ListDigraph::reverseArc() "reverseArc()"
8.2265 + /// of \ref ListDigraph will \e not update the degree values correctly.
8.2266 + ///
8.2267 + /// \sa OutDegMap
8.2268 +
8.2269 + template <typename _Digraph>
8.2270 + class InDegMap
8.2271 + : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
8.2272 + ::ItemNotifier::ObserverBase {
8.2273 +
8.2274 + public:
8.2275 +
8.2276 + typedef _Digraph Digraph;
8.2277 + typedef int Value;
8.2278 + typedef typename Digraph::Node Key;
8.2279 +
8.2280 + typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc>
8.2281 + ::ItemNotifier::ObserverBase Parent;
8.2282 +
8.2283 + private:
8.2284 +
8.2285 + class AutoNodeMap : public DefaultMap<_Digraph, Key, int> {
8.2286 + public:
8.2287 +
8.2288 + typedef DefaultMap<_Digraph, Key, int> Parent;
8.2289 + typedef typename Parent::Digraph Digraph;
8.2290 +
8.2291 + AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
8.2292 +
8.2293 + virtual void add(const Key& key) {
8.2294 + Parent::add(key);
8.2295 + Parent::set(key, 0);
8.2296 + }
8.2297 +
8.2298 + virtual void add(const std::vector<Key>& keys) {
8.2299 + Parent::add(keys);
8.2300 + for (int i = 0; i < int(keys.size()); ++i) {
8.2301 + Parent::set(keys[i], 0);
8.2302 + }
8.2303 + }
8.2304 +
8.2305 + virtual void build() {
8.2306 + Parent::build();
8.2307 + Key it;
8.2308 + typename Parent::Notifier* nf = Parent::notifier();
8.2309 + for (nf->first(it); it != INVALID; nf->next(it)) {
8.2310 + Parent::set(it, 0);
8.2311 + }
8.2312 + }
8.2313 + };
8.2314 +
8.2315 + public:
8.2316 +
8.2317 + /// \brief Constructor.
8.2318 + ///
8.2319 + /// Constructor for creating in-degree map.
8.2320 + explicit InDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) {
8.2321 + Parent::attach(digraph.notifier(typename _Digraph::Arc()));
8.2322 +
8.2323 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
8.2324 + deg[it] = countInArcs(digraph, it);
8.2325 + }
8.2326 + }
8.2327 +
8.2328 + /// Gives back the in-degree of a Node.
8.2329 + int operator[](const Key& key) const {
8.2330 + return deg[key];
8.2331 + }
8.2332 +
8.2333 + protected:
8.2334 +
8.2335 + typedef typename Digraph::Arc Arc;
8.2336 +
8.2337 + virtual void add(const Arc& arc) {
8.2338 + ++deg[digraph.target(arc)];
8.2339 + }
8.2340 +
8.2341 + virtual void add(const std::vector<Arc>& arcs) {
8.2342 + for (int i = 0; i < int(arcs.size()); ++i) {
8.2343 + ++deg[digraph.target(arcs[i])];
8.2344 + }
8.2345 + }
8.2346 +
8.2347 + virtual void erase(const Arc& arc) {
8.2348 + --deg[digraph.target(arc)];
8.2349 + }
8.2350 +
8.2351 + virtual void erase(const std::vector<Arc>& arcs) {
8.2352 + for (int i = 0; i < int(arcs.size()); ++i) {
8.2353 + --deg[digraph.target(arcs[i])];
8.2354 + }
8.2355 + }
8.2356 +
8.2357 + virtual void build() {
8.2358 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
8.2359 + deg[it] = countInArcs(digraph, it);
8.2360 + }
8.2361 + }
8.2362 +
8.2363 + virtual void clear() {
8.2364 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
8.2365 + deg[it] = 0;
8.2366 + }
8.2367 + }
8.2368 + private:
8.2369 +
8.2370 + const _Digraph& digraph;
8.2371 + AutoNodeMap deg;
8.2372 + };
8.2373 +
8.2374 + /// \brief Map of the node out-degrees.
8.2375 + ///
8.2376 + /// This map returns the out-degree of a node. Once it is constructed,
8.2377 + /// the degrees are stored in a standard NodeMap, so each query is done
8.2378 + /// in constant time. On the other hand, the values are updated automatically
8.2379 + /// whenever the digraph changes.
8.2380 + ///
8.2381 + /// \warning Besides addNode() and addArc(), a digraph structure may provide
8.2382 + /// alternative ways to modify the digraph. The correct behavior of OutDegMap
8.2383 + /// is not guarantied if these additional features are used. For example
8.2384 + /// the functions \ref ListDigraph::changeSource() "changeSource()",
8.2385 + /// \ref ListDigraph::changeTarget() "changeTarget()" and
8.2386 + /// \ref ListDigraph::reverseArc() "reverseArc()"
8.2387 + /// of \ref ListDigraph will \e not update the degree values correctly.
8.2388 + ///
8.2389 + /// \sa InDegMap
8.2390 +
8.2391 + template <typename _Digraph>
8.2392 + class OutDegMap
8.2393 + : protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
8.2394 + ::ItemNotifier::ObserverBase {
8.2395 +
8.2396 + public:
8.2397 +
8.2398 + typedef typename ItemSetTraits<_Digraph, typename _Digraph::Arc>
8.2399 + ::ItemNotifier::ObserverBase Parent;
8.2400 +
8.2401 + typedef _Digraph Digraph;
8.2402 + typedef int Value;
8.2403 + typedef typename Digraph::Node Key;
8.2404 +
8.2405 + private:
8.2406 +
8.2407 + class AutoNodeMap : public DefaultMap<_Digraph, Key, int> {
8.2408 + public:
8.2409 +
8.2410 + typedef DefaultMap<_Digraph, Key, int> Parent;
8.2411 + typedef typename Parent::Digraph Digraph;
8.2412 +
8.2413 + AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
8.2414 +
8.2415 + virtual void add(const Key& key) {
8.2416 + Parent::add(key);
8.2417 + Parent::set(key, 0);
8.2418 + }
8.2419 + virtual void add(const std::vector<Key>& keys) {
8.2420 + Parent::add(keys);
8.2421 + for (int i = 0; i < int(keys.size()); ++i) {
8.2422 + Parent::set(keys[i], 0);
8.2423 + }
8.2424 + }
8.2425 + virtual void build() {
8.2426 + Parent::build();
8.2427 + Key it;
8.2428 + typename Parent::Notifier* nf = Parent::notifier();
8.2429 + for (nf->first(it); it != INVALID; nf->next(it)) {
8.2430 + Parent::set(it, 0);
8.2431 + }
8.2432 + }
8.2433 + };
8.2434 +
8.2435 + public:
8.2436 +
8.2437 + /// \brief Constructor.
8.2438 + ///
8.2439 + /// Constructor for creating out-degree map.
8.2440 + explicit OutDegMap(const Digraph& _digraph) : digraph(_digraph), deg(_digraph) {
8.2441 + Parent::attach(digraph.notifier(typename _Digraph::Arc()));
8.2442 +
8.2443 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
8.2444 + deg[it] = countOutArcs(digraph, it);
8.2445 + }
8.2446 + }
8.2447 +
8.2448 + /// Gives back the out-degree of a Node.
8.2449 + int operator[](const Key& key) const {
8.2450 + return deg[key];
8.2451 + }
8.2452 +
8.2453 + protected:
8.2454 +
8.2455 + typedef typename Digraph::Arc Arc;
8.2456 +
8.2457 + virtual void add(const Arc& arc) {
8.2458 + ++deg[digraph.source(arc)];
8.2459 + }
8.2460 +
8.2461 + virtual void add(const std::vector<Arc>& arcs) {
8.2462 + for (int i = 0; i < int(arcs.size()); ++i) {
8.2463 + ++deg[digraph.source(arcs[i])];
8.2464 + }
8.2465 + }
8.2466 +
8.2467 + virtual void erase(const Arc& arc) {
8.2468 + --deg[digraph.source(arc)];
8.2469 + }
8.2470 +
8.2471 + virtual void erase(const std::vector<Arc>& arcs) {
8.2472 + for (int i = 0; i < int(arcs.size()); ++i) {
8.2473 + --deg[digraph.source(arcs[i])];
8.2474 + }
8.2475 + }
8.2476 +
8.2477 + virtual void build() {
8.2478 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
8.2479 + deg[it] = countOutArcs(digraph, it);
8.2480 + }
8.2481 + }
8.2482 +
8.2483 + virtual void clear() {
8.2484 + for(typename _Digraph::NodeIt it(digraph); it != INVALID; ++it) {
8.2485 + deg[it] = 0;
8.2486 + }
8.2487 + }
8.2488 + private:
8.2489 +
8.2490 + const _Digraph& digraph;
8.2491 + AutoNodeMap deg;
8.2492 + };
8.2493 +
8.2494 +
8.2495 + ///Dynamic arc look up between given endpoints.
8.2496 +
8.2497 + ///\ingroup gutils
8.2498 + ///Using this class, you can find an arc in a digraph from a given
8.2499 + ///source to a given target in amortized time <em>O(log d)</em>,
8.2500 + ///where <em>d</em> is the out-degree of the source node.
8.2501 + ///
8.2502 + ///It is possible to find \e all parallel arcs between two nodes with
8.2503 + ///the \c findFirst() and \c findNext() members.
8.2504 + ///
8.2505 + ///See the \ref ArcLookUp and \ref AllArcLookUp classes if your
8.2506 + ///digraph do not changed so frequently.
8.2507 + ///
8.2508 + ///This class uses a self-adjusting binary search tree, Sleator's
8.2509 + ///and Tarjan's Splay tree for guarantee the logarithmic amortized
8.2510 + ///time bound for arc lookups. This class also guarantees the
8.2511 + ///optimal time bound in a constant factor for any distribution of
8.2512 + ///queries.
8.2513 + ///
8.2514 + ///\param G The type of the underlying digraph.
8.2515 + ///
8.2516 + ///\sa ArcLookUp
8.2517 + ///\sa AllArcLookUp
8.2518 + template<class G>
8.2519 + class DynArcLookUp
8.2520 + : protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
8.2521 + {
8.2522 + public:
8.2523 + typedef typename ItemSetTraits<G, typename G::Arc>
8.2524 + ::ItemNotifier::ObserverBase Parent;
8.2525 +
8.2526 + GRAPH_TYPEDEFS(typename G);
8.2527 + typedef G Digraph;
8.2528 +
8.2529 + protected:
8.2530 +
8.2531 + class AutoNodeMap : public DefaultMap<G, Node, Arc> {
8.2532 + public:
8.2533 +
8.2534 + typedef DefaultMap<G, Node, Arc> Parent;
8.2535 +
8.2536 + AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
8.2537 +
8.2538 + virtual void add(const Node& node) {
8.2539 + Parent::add(node);
8.2540 + Parent::set(node, INVALID);
8.2541 + }
8.2542 +
8.2543 + virtual void add(const std::vector<Node>& nodes) {
8.2544 + Parent::add(nodes);
8.2545 + for (int i = 0; i < int(nodes.size()); ++i) {
8.2546 + Parent::set(nodes[i], INVALID);
8.2547 + }
8.2548 + }
8.2549 +
8.2550 + virtual void build() {
8.2551 + Parent::build();
8.2552 + Node it;
8.2553 + typename Parent::Notifier* nf = Parent::notifier();
8.2554 + for (nf->first(it); it != INVALID; nf->next(it)) {
8.2555 + Parent::set(it, INVALID);
8.2556 + }
8.2557 + }
8.2558 + };
8.2559 +
8.2560 + const Digraph &_g;
8.2561 + AutoNodeMap _head;
8.2562 + typename Digraph::template ArcMap<Arc> _parent;
8.2563 + typename Digraph::template ArcMap<Arc> _left;
8.2564 + typename Digraph::template ArcMap<Arc> _right;
8.2565 +
8.2566 + class ArcLess {
8.2567 + const Digraph &g;
8.2568 + public:
8.2569 + ArcLess(const Digraph &_g) : g(_g) {}
8.2570 + bool operator()(Arc a,Arc b) const
8.2571 + {
8.2572 + return g.target(a)<g.target(b);
8.2573 + }
8.2574 + };
8.2575 +
8.2576 + public:
8.2577 +
8.2578 + ///Constructor
8.2579 +
8.2580 + ///Constructor.
8.2581 + ///
8.2582 + ///It builds up the search database.
8.2583 + DynArcLookUp(const Digraph &g)
8.2584 + : _g(g),_head(g),_parent(g),_left(g),_right(g)
8.2585 + {
8.2586 + Parent::attach(_g.notifier(typename Digraph::Arc()));
8.2587 + refresh();
8.2588 + }
8.2589 +
8.2590 + protected:
8.2591 +
8.2592 + virtual void add(const Arc& arc) {
8.2593 + insert(arc);
8.2594 + }
8.2595 +
8.2596 + virtual void add(const std::vector<Arc>& arcs) {
8.2597 + for (int i = 0; i < int(arcs.size()); ++i) {
8.2598 + insert(arcs[i]);
8.2599 + }
8.2600 + }
8.2601 +
8.2602 + virtual void erase(const Arc& arc) {
8.2603 + remove(arc);
8.2604 + }
8.2605 +
8.2606 + virtual void erase(const std::vector<Arc>& arcs) {
8.2607 + for (int i = 0; i < int(arcs.size()); ++i) {
8.2608 + remove(arcs[i]);
8.2609 + }
8.2610 + }
8.2611 +
8.2612 + virtual void build() {
8.2613 + refresh();
8.2614 + }
8.2615 +
8.2616 + virtual void clear() {
8.2617 + for(NodeIt n(_g);n!=INVALID;++n) {
8.2618 + _head.set(n, INVALID);
8.2619 + }
8.2620 + }
8.2621 +
8.2622 + void insert(Arc arc) {
8.2623 + Node s = _g.source(arc);
8.2624 + Node t = _g.target(arc);
8.2625 + _left.set(arc, INVALID);
8.2626 + _right.set(arc, INVALID);
8.2627 +
8.2628 + Arc e = _head[s];
8.2629 + if (e == INVALID) {
8.2630 + _head.set(s, arc);
8.2631 + _parent.set(arc, INVALID);
8.2632 + return;
8.2633 + }
8.2634 + while (true) {
8.2635 + if (t < _g.target(e)) {
8.2636 + if (_left[e] == INVALID) {
8.2637 + _left.set(e, arc);
8.2638 + _parent.set(arc, e);
8.2639 + splay(arc);
8.2640 + return;
8.2641 + } else {
8.2642 + e = _left[e];
8.2643 + }
8.2644 + } else {
8.2645 + if (_right[e] == INVALID) {
8.2646 + _right.set(e, arc);
8.2647 + _parent.set(arc, e);
8.2648 + splay(arc);
8.2649 + return;
8.2650 + } else {
8.2651 + e = _right[e];
8.2652 + }
8.2653 + }
8.2654 + }
8.2655 + }
8.2656 +
8.2657 + void remove(Arc arc) {
8.2658 + if (_left[arc] == INVALID) {
8.2659 + if (_right[arc] != INVALID) {
8.2660 + _parent.set(_right[arc], _parent[arc]);
8.2661 + }
8.2662 + if (_parent[arc] != INVALID) {
8.2663 + if (_left[_parent[arc]] == arc) {
8.2664 + _left.set(_parent[arc], _right[arc]);
8.2665 + } else {
8.2666 + _right.set(_parent[arc], _right[arc]);
8.2667 + }
8.2668 + } else {
8.2669 + _head.set(_g.source(arc), _right[arc]);
8.2670 + }
8.2671 + } else if (_right[arc] == INVALID) {
8.2672 + _parent.set(_left[arc], _parent[arc]);
8.2673 + if (_parent[arc] != INVALID) {
8.2674 + if (_left[_parent[arc]] == arc) {
8.2675 + _left.set(_parent[arc], _left[arc]);
8.2676 + } else {
8.2677 + _right.set(_parent[arc], _left[arc]);
8.2678 + }
8.2679 + } else {
8.2680 + _head.set(_g.source(arc), _left[arc]);
8.2681 + }
8.2682 + } else {
8.2683 + Arc e = _left[arc];
8.2684 + if (_right[e] != INVALID) {
8.2685 + e = _right[e];
8.2686 + while (_right[e] != INVALID) {
8.2687 + e = _right[e];
8.2688 + }
8.2689 + Arc s = _parent[e];
8.2690 + _right.set(_parent[e], _left[e]);
8.2691 + if (_left[e] != INVALID) {
8.2692 + _parent.set(_left[e], _parent[e]);
8.2693 + }
8.2694 +
8.2695 + _left.set(e, _left[arc]);
8.2696 + _parent.set(_left[arc], e);
8.2697 + _right.set(e, _right[arc]);
8.2698 + _parent.set(_right[arc], e);
8.2699 +
8.2700 + _parent.set(e, _parent[arc]);
8.2701 + if (_parent[arc] != INVALID) {
8.2702 + if (_left[_parent[arc]] == arc) {
8.2703 + _left.set(_parent[arc], e);
8.2704 + } else {
8.2705 + _right.set(_parent[arc], e);
8.2706 + }
8.2707 + }
8.2708 + splay(s);
8.2709 + } else {
8.2710 + _right.set(e, _right[arc]);
8.2711 + _parent.set(_right[arc], e);
8.2712 +
8.2713 + if (_parent[arc] != INVALID) {
8.2714 + if (_left[_parent[arc]] == arc) {
8.2715 + _left.set(_parent[arc], e);
8.2716 + } else {
8.2717 + _right.set(_parent[arc], e);
8.2718 + }
8.2719 + } else {
8.2720 + _head.set(_g.source(arc), e);
8.2721 + }
8.2722 + }
8.2723 + }
8.2724 + }
8.2725 +
8.2726 + Arc refreshRec(std::vector<Arc> &v,int a,int b)
8.2727 + {
8.2728 + int m=(a+b)/2;
8.2729 + Arc me=v[m];
8.2730 + if (a < m) {
8.2731 + Arc left = refreshRec(v,a,m-1);
8.2732 + _left.set(me, left);
8.2733 + _parent.set(left, me);
8.2734 + } else {
8.2735 + _left.set(me, INVALID);
8.2736 + }
8.2737 + if (m < b) {
8.2738 + Arc right = refreshRec(v,m+1,b);
8.2739 + _right.set(me, right);
8.2740 + _parent.set(right, me);
8.2741 + } else {
8.2742 + _right.set(me, INVALID);
8.2743 + }
8.2744 + return me;
8.2745 + }
8.2746 +
8.2747 + void refresh() {
8.2748 + for(NodeIt n(_g);n!=INVALID;++n) {
8.2749 + std::vector<Arc> v;
8.2750 + for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
8.2751 + if(v.size()) {
8.2752 + std::sort(v.begin(),v.end(),ArcLess(_g));
8.2753 + Arc head = refreshRec(v,0,v.size()-1);
8.2754 + _head.set(n, head);
8.2755 + _parent.set(head, INVALID);
8.2756 + }
8.2757 + else _head.set(n, INVALID);
8.2758 + }
8.2759 + }
8.2760 +
8.2761 + void zig(Arc v) {
8.2762 + Arc w = _parent[v];
8.2763 + _parent.set(v, _parent[w]);
8.2764 + _parent.set(w, v);
8.2765 + _left.set(w, _right[v]);
8.2766 + _right.set(v, w);
8.2767 + if (_parent[v] != INVALID) {
8.2768 + if (_right[_parent[v]] == w) {
8.2769 + _right.set(_parent[v], v);
8.2770 + } else {
8.2771 + _left.set(_parent[v], v);
8.2772 + }
8.2773 + }
8.2774 + if (_left[w] != INVALID){
8.2775 + _parent.set(_left[w], w);
8.2776 + }
8.2777 + }
8.2778 +
8.2779 + void zag(Arc v) {
8.2780 + Arc w = _parent[v];
8.2781 + _parent.set(v, _parent[w]);
8.2782 + _parent.set(w, v);
8.2783 + _right.set(w, _left[v]);
8.2784 + _left.set(v, w);
8.2785 + if (_parent[v] != INVALID){
8.2786 + if (_left[_parent[v]] == w) {
8.2787 + _left.set(_parent[v], v);
8.2788 + } else {
8.2789 + _right.set(_parent[v], v);
8.2790 + }
8.2791 + }
8.2792 + if (_right[w] != INVALID){
8.2793 + _parent.set(_right[w], w);
8.2794 + }
8.2795 + }
8.2796 +
8.2797 + void splay(Arc v) {
8.2798 + while (_parent[v] != INVALID) {
8.2799 + if (v == _left[_parent[v]]) {
8.2800 + if (_parent[_parent[v]] == INVALID) {
8.2801 + zig(v);
8.2802 + } else {
8.2803 + if (_parent[v] == _left[_parent[_parent[v]]]) {
8.2804 + zig(_parent[v]);
8.2805 + zig(v);
8.2806 + } else {
8.2807 + zig(v);
8.2808 + zag(v);
8.2809 + }
8.2810 + }
8.2811 + } else {
8.2812 + if (_parent[_parent[v]] == INVALID) {
8.2813 + zag(v);
8.2814 + } else {
8.2815 + if (_parent[v] == _left[_parent[_parent[v]]]) {
8.2816 + zag(v);
8.2817 + zig(v);
8.2818 + } else {
8.2819 + zag(_parent[v]);
8.2820 + zag(v);
8.2821 + }
8.2822 + }
8.2823 + }
8.2824 + }
8.2825 + _head[_g.source(v)] = v;
8.2826 + }
8.2827 +
8.2828 +
8.2829 + public:
8.2830 +
8.2831 + ///Find an arc between two nodes.
8.2832 +
8.2833 + ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
8.2834 + /// <em>d</em> is the number of outgoing arcs of \c s.
8.2835 + ///\param s The source node
8.2836 + ///\param t The target node
8.2837 + ///\return An arc from \c s to \c t if there exists,
8.2838 + ///\ref INVALID otherwise.
8.2839 + Arc operator()(Node s, Node t) const
8.2840 + {
8.2841 + Arc e = _head[s];
8.2842 + while (true) {
8.2843 + if (_g.target(e) == t) {
8.2844 + const_cast<DynArcLookUp&>(*this).splay(e);
8.2845 + return e;
8.2846 + } else if (t < _g.target(e)) {
8.2847 + if (_left[e] == INVALID) {
8.2848 + const_cast<DynArcLookUp&>(*this).splay(e);
8.2849 + return INVALID;
8.2850 + } else {
8.2851 + e = _left[e];
8.2852 + }
8.2853 + } else {
8.2854 + if (_right[e] == INVALID) {
8.2855 + const_cast<DynArcLookUp&>(*this).splay(e);
8.2856 + return INVALID;
8.2857 + } else {
8.2858 + e = _right[e];
8.2859 + }
8.2860 + }
8.2861 + }
8.2862 + }
8.2863 +
8.2864 + ///Find the first arc between two nodes.
8.2865 +
8.2866 + ///Find the first arc between two nodes in time
8.2867 + /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
8.2868 + /// outgoing arcs of \c s.
8.2869 + ///\param s The source node
8.2870 + ///\param t The target node
8.2871 + ///\return An arc from \c s to \c t if there exists, \ref INVALID
8.2872 + /// otherwise.
8.2873 + Arc findFirst(Node s, Node t) const
8.2874 + {
8.2875 + Arc e = _head[s];
8.2876 + Arc r = INVALID;
8.2877 + while (true) {
8.2878 + if (_g.target(e) < t) {
8.2879 + if (_right[e] == INVALID) {
8.2880 + const_cast<DynArcLookUp&>(*this).splay(e);
8.2881 + return r;
8.2882 + } else {
8.2883 + e = _right[e];
8.2884 + }
8.2885 + } else {
8.2886 + if (_g.target(e) == t) {
8.2887 + r = e;
8.2888 + }
8.2889 + if (_left[e] == INVALID) {
8.2890 + const_cast<DynArcLookUp&>(*this).splay(e);
8.2891 + return r;
8.2892 + } else {
8.2893 + e = _left[e];
8.2894 + }
8.2895 + }
8.2896 + }
8.2897 + }
8.2898 +
8.2899 + ///Find the next arc between two nodes.
8.2900 +
8.2901 + ///Find the next arc between two nodes in time
8.2902 + /// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
8.2903 + /// outgoing arcs of \c s.
8.2904 + ///\param s The source node
8.2905 + ///\param t The target node
8.2906 + ///\return An arc from \c s to \c t if there exists, \ref INVALID
8.2907 + /// otherwise.
8.2908 +
8.2909 + ///\note If \c e is not the result of the previous \c findFirst()
8.2910 + ///operation then the amorized time bound can not be guaranteed.
8.2911 +#ifdef DOXYGEN
8.2912 + Arc findNext(Node s, Node t, Arc e) const
8.2913 +#else
8.2914 + Arc findNext(Node, Node t, Arc e) const
8.2915 +#endif
8.2916 + {
8.2917 + if (_right[e] != INVALID) {
8.2918 + e = _right[e];
8.2919 + while (_left[e] != INVALID) {
8.2920 + e = _left[e];
8.2921 + }
8.2922 + const_cast<DynArcLookUp&>(*this).splay(e);
8.2923 + } else {
8.2924 + while (_parent[e] != INVALID && _right[_parent[e]] == e) {
8.2925 + e = _parent[e];
8.2926 + }
8.2927 + if (_parent[e] == INVALID) {
8.2928 + return INVALID;
8.2929 + } else {
8.2930 + e = _parent[e];
8.2931 + const_cast<DynArcLookUp&>(*this).splay(e);
8.2932 + }
8.2933 + }
8.2934 + if (_g.target(e) == t) return e;
8.2935 + else return INVALID;
8.2936 + }
8.2937 +
8.2938 + };
8.2939 +
8.2940 + ///Fast arc look up between given endpoints.
8.2941 +
8.2942 + ///\ingroup gutils
8.2943 + ///Using this class, you can find an arc in a digraph from a given
8.2944 + ///source to a given target in time <em>O(log d)</em>,
8.2945 + ///where <em>d</em> is the out-degree of the source node.
8.2946 + ///
8.2947 + ///It is not possible to find \e all parallel arcs between two nodes.
8.2948 + ///Use \ref AllArcLookUp for this purpose.
8.2949 + ///
8.2950 + ///\warning This class is static, so you should refresh() (or at least
8.2951 + ///refresh(Node)) this data structure
8.2952 + ///whenever the digraph changes. This is a time consuming (superlinearly
8.2953 + ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
8.2954 + ///
8.2955 + ///\param G The type of the underlying digraph.
8.2956 + ///
8.2957 + ///\sa DynArcLookUp
8.2958 + ///\sa AllArcLookUp
8.2959 + template<class G>
8.2960 + class ArcLookUp
8.2961 + {
8.2962 + public:
8.2963 + GRAPH_TYPEDEFS(typename G);
8.2964 + typedef G Digraph;
8.2965 +
8.2966 + protected:
8.2967 + const Digraph &_g;
8.2968 + typename Digraph::template NodeMap<Arc> _head;
8.2969 + typename Digraph::template ArcMap<Arc> _left;
8.2970 + typename Digraph::template ArcMap<Arc> _right;
8.2971 +
8.2972 + class ArcLess {
8.2973 + const Digraph &g;
8.2974 + public:
8.2975 + ArcLess(const Digraph &_g) : g(_g) {}
8.2976 + bool operator()(Arc a,Arc b) const
8.2977 + {
8.2978 + return g.target(a)<g.target(b);
8.2979 + }
8.2980 + };
8.2981 +
8.2982 + public:
8.2983 +
8.2984 + ///Constructor
8.2985 +
8.2986 + ///Constructor.
8.2987 + ///
8.2988 + ///It builds up the search database, which remains valid until the digraph
8.2989 + ///changes.
8.2990 + ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
8.2991 +
8.2992 + private:
8.2993 + Arc refreshRec(std::vector<Arc> &v,int a,int b)
8.2994 + {
8.2995 + int m=(a+b)/2;
8.2996 + Arc me=v[m];
8.2997 + _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
8.2998 + _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
8.2999 + return me;
8.3000 + }
8.3001 + public:
8.3002 + ///Refresh the data structure at a node.
8.3003 +
8.3004 + ///Build up the search database of node \c n.
8.3005 + ///
8.3006 + ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
8.3007 + ///the number of the outgoing arcs of \c n.
8.3008 + void refresh(Node n)
8.3009 + {
8.3010 + std::vector<Arc> v;
8.3011 + for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
8.3012 + if(v.size()) {
8.3013 + std::sort(v.begin(),v.end(),ArcLess(_g));
8.3014 + _head[n]=refreshRec(v,0,v.size()-1);
8.3015 + }
8.3016 + else _head[n]=INVALID;
8.3017 + }
8.3018 + ///Refresh the full data structure.
8.3019 +
8.3020 + ///Build up the full search database. In fact, it simply calls
8.3021 + ///\ref refresh(Node) "refresh(n)" for each node \c n.
8.3022 + ///
8.3023 + ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
8.3024 + ///the number of the arcs of \c n and <em>D</em> is the maximum
8.3025 + ///out-degree of the digraph.
8.3026 +
8.3027 + void refresh()
8.3028 + {
8.3029 + for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
8.3030 + }
8.3031 +
8.3032 + ///Find an arc between two nodes.
8.3033 +
8.3034 + ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
8.3035 + /// <em>d</em> is the number of outgoing arcs of \c s.
8.3036 + ///\param s The source node
8.3037 + ///\param t The target node
8.3038 + ///\return An arc from \c s to \c t if there exists,
8.3039 + ///\ref INVALID otherwise.
8.3040 + ///
8.3041 + ///\warning If you change the digraph, refresh() must be called before using
8.3042 + ///this operator. If you change the outgoing arcs of
8.3043 + ///a single node \c n, then
8.3044 + ///\ref refresh(Node) "refresh(n)" is enough.
8.3045 + ///
8.3046 + Arc operator()(Node s, Node t) const
8.3047 + {
8.3048 + Arc e;
8.3049 + for(e=_head[s];
8.3050 + e!=INVALID&&_g.target(e)!=t;
8.3051 + e = t < _g.target(e)?_left[e]:_right[e]) ;
8.3052 + return e;
8.3053 + }
8.3054 +
8.3055 + };
8.3056 +
8.3057 + ///Fast look up of all arcs between given endpoints.
8.3058 +
8.3059 + ///\ingroup gutils
8.3060 + ///This class is the same as \ref ArcLookUp, with the addition
8.3061 + ///that it makes it possible to find all arcs between given endpoints.
8.3062 + ///
8.3063 + ///\warning This class is static, so you should refresh() (or at least
8.3064 + ///refresh(Node)) this data structure
8.3065 + ///whenever the digraph changes. This is a time consuming (superlinearly
8.3066 + ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
8.3067 + ///
8.3068 + ///\param G The type of the underlying digraph.
8.3069 + ///
8.3070 + ///\sa DynArcLookUp
8.3071 + ///\sa ArcLookUp
8.3072 + template<class G>
8.3073 + class AllArcLookUp : public ArcLookUp<G>
8.3074 + {
8.3075 + using ArcLookUp<G>::_g;
8.3076 + using ArcLookUp<G>::_right;
8.3077 + using ArcLookUp<G>::_left;
8.3078 + using ArcLookUp<G>::_head;
8.3079 +
8.3080 + GRAPH_TYPEDEFS(typename G);
8.3081 + typedef G Digraph;
8.3082 +
8.3083 + typename Digraph::template ArcMap<Arc> _next;
8.3084 +
8.3085 + Arc refreshNext(Arc head,Arc next=INVALID)
8.3086 + {
8.3087 + if(head==INVALID) return next;
8.3088 + else {
8.3089 + next=refreshNext(_right[head],next);
8.3090 +// _next[head]=next;
8.3091 + _next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
8.3092 + ? next : INVALID;
8.3093 + return refreshNext(_left[head],head);
8.3094 + }
8.3095 + }
8.3096 +
8.3097 + void refreshNext()
8.3098 + {
8.3099 + for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
8.3100 + }
8.3101 +
8.3102 + public:
8.3103 + ///Constructor
8.3104 +
8.3105 + ///Constructor.
8.3106 + ///
8.3107 + ///It builds up the search database, which remains valid until the digraph
8.3108 + ///changes.
8.3109 + AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
8.3110 +
8.3111 + ///Refresh the data structure at a node.
8.3112 +
8.3113 + ///Build up the search database of node \c n.
8.3114 + ///
8.3115 + ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
8.3116 + ///the number of the outgoing arcs of \c n.
8.3117 +
8.3118 + void refresh(Node n)
8.3119 + {
8.3120 + ArcLookUp<G>::refresh(n);
8.3121 + refreshNext(_head[n]);
8.3122 + }
8.3123 +
8.3124 + ///Refresh the full data structure.
8.3125 +
8.3126 + ///Build up the full search database. In fact, it simply calls
8.3127 + ///\ref refresh(Node) "refresh(n)" for each node \c n.
8.3128 + ///
8.3129 + ///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
8.3130 + ///the number of the arcs of \c n and <em>D</em> is the maximum
8.3131 + ///out-degree of the digraph.
8.3132 +
8.3133 + void refresh()
8.3134 + {
8.3135 + for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
8.3136 + }
8.3137 +
8.3138 + ///Find an arc between two nodes.
8.3139 +
8.3140 + ///Find an arc between two nodes.
8.3141 + ///\param s The source node
8.3142 + ///\param t The target node
8.3143 + ///\param prev The previous arc between \c s and \c t. It it is INVALID or
8.3144 + ///not given, the operator finds the first appropriate arc.
8.3145 + ///\return An arc from \c s to \c t after \c prev or
8.3146 + ///\ref INVALID if there is no more.
8.3147 + ///
8.3148 + ///For example, you can count the number of arcs from \c u to \c v in the
8.3149 + ///following way.
8.3150 + ///\code
8.3151 + ///AllArcLookUp<ListDigraph> ae(g);
8.3152 + ///...
8.3153 + ///int n=0;
8.3154 + ///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
8.3155 + ///\endcode
8.3156 + ///
8.3157 + ///Finding the first arc take <em>O(</em>log<em>d)</em> time, where
8.3158 + /// <em>d</em> is the number of outgoing arcs of \c s. Then, the
8.3159 + ///consecutive arcs are found in constant time.
8.3160 + ///
8.3161 + ///\warning If you change the digraph, refresh() must be called before using
8.3162 + ///this operator. If you change the outgoing arcs of
8.3163 + ///a single node \c n, then
8.3164 + ///\ref refresh(Node) "refresh(n)" is enough.
8.3165 + ///
8.3166 +#ifdef DOXYGEN
8.3167 + Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
8.3168 +#else
8.3169 + using ArcLookUp<G>::operator() ;
8.3170 + Arc operator()(Node s, Node t, Arc prev) const
8.3171 + {
8.3172 + return prev==INVALID?(*this)(s,t):_next[prev];
8.3173 + }
8.3174 +#endif
8.3175 +
8.3176 + };
8.3177 +
8.3178 + /// @}
8.3179 +
8.3180 +} //END OF NAMESPACE LEMON
8.3181 +
8.3182 +#endif
9.1 --- a/lemon/path.h Thu Feb 07 21:28:39 2008 +0000
9.2 +++ b/lemon/path.h Thu Feb 07 21:37:07 2008 +0000
9.3 @@ -29,6 +29,7 @@
9.4
9.5 #include <lemon/error.h>
9.6 #include <lemon/bits/invalid.h>
9.7 +#include <lemon/concepts/path.h>
9.8
9.9 namespace lemon {
9.10
10.1 --- a/test/Makefile.am Thu Feb 07 21:28:39 2008 +0000
10.2 +++ b/test/Makefile.am Thu Feb 07 21:37:07 2008 +0000
10.3 @@ -3,10 +3,13 @@
10.4
10.5 noinst_HEADERS += \
10.6 test/digraph_test.h \
10.7 + test/heap_test.h \
10.8 test/map_test.h \
10.9 test/test_tools.h
10.10
10.11 check_PROGRAMS += \
10.12 + test/bfs_test \
10.13 + test/dfs_test \
10.14 test/digraph_test \
10.15 test/dim_test \
10.16 test/graph_test \
10.17 @@ -19,10 +22,13 @@
10.18 TESTS += $(check_PROGRAMS)
10.19 XFAIL_TESTS += test/test_tools_fail$(EXEEXT)
10.20
10.21 +test_bfs_test_SOURCES = test/bfs_test.cc
10.22 +test_dfs_test_SOURCES = test/dfs_test.cc
10.23 test_digraph_test_SOURCES = test/digraph_test.cc
10.24 test_dim_test_SOURCES = test/dim_test.cc
10.25 #test_error_test_SOURCES = test/error_test.cc
10.26 test_graph_test_SOURCES = test/graph_test.cc
10.27 +# test_heap_test_SOURCES = test/heap_test.cc
10.28 test_maps_test_SOURCES = test/maps_test.cc
10.29 test_path_test_SOURCES = test/path_test.cc
10.30 test_random_test_SOURCES = test/random_test.cc
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/test/bfs_test.cc Thu Feb 07 21:37:07 2008 +0000
11.3 @@ -0,0 +1,141 @@
11.4 +/* -*- C++ -*-
11.5 + *
11.6 + * This file is a part of LEMON, a generic C++ optimization library
11.7 + *
11.8 + * Copyright (C) 2003-2008
11.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
11.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
11.11 + *
11.12 + * Permission to use, modify and distribute this software is granted
11.13 + * provided that this copyright notice appears in all copies. For
11.14 + * precise terms see the accompanying LICENSE file.
11.15 + *
11.16 + * This software is provided "AS IS" with no warranty of any kind,
11.17 + * express or implied, and with no claim as to its suitability for any
11.18 + * purpose.
11.19 + *
11.20 + */
11.21 +
11.22 +#include "test_tools.h"
11.23 +//#include <lemon/smart_graph.h>
11.24 +#include <lemon/list_graph.h>
11.25 +#include <lemon/bfs.h>
11.26 +#include <lemon/path.h>
11.27 +#include<lemon/concepts/digraph.h>
11.28 +
11.29 +using namespace lemon;
11.30 +
11.31 +const int PET_SIZE =5;
11.32 +
11.33 +
11.34 +void check_Bfs_Compile()
11.35 +{
11.36 + typedef concepts::Digraph Digraph;
11.37 +
11.38 + typedef Digraph::Arc Arc;
11.39 + typedef Digraph::Node Node;
11.40 + typedef Digraph::ArcIt ArcIt;
11.41 + typedef Digraph::NodeIt NodeIt;
11.42 +
11.43 + typedef Bfs<Digraph> BType;
11.44 +
11.45 + Digraph G;
11.46 + Node n;
11.47 + Arc e;
11.48 + int l;
11.49 + bool b;
11.50 + BType::DistMap d(G);
11.51 + BType::PredMap p(G);
11.52 + // BType::PredNodeMap pn(G);
11.53 +
11.54 + BType bfs_test(G);
11.55 +
11.56 + bfs_test.run(n);
11.57 +
11.58 + l = bfs_test.dist(n);
11.59 + e = bfs_test.predArc(n);
11.60 + n = bfs_test.predNode(n);
11.61 + d = bfs_test.distMap();
11.62 + p = bfs_test.predMap();
11.63 + // pn = bfs_test.predNodeMap();
11.64 + b = bfs_test.reached(n);
11.65 +
11.66 + Path<Digraph> pp = bfs_test.path(n);
11.67 +}
11.68 +
11.69 +void check_Bfs_Function_Compile()
11.70 +{
11.71 + typedef int VType;
11.72 + typedef concepts::Digraph Digraph;
11.73 +
11.74 + typedef Digraph::Arc Arc;
11.75 + typedef Digraph::Node Node;
11.76 + typedef Digraph::ArcIt ArcIt;
11.77 + typedef Digraph::NodeIt NodeIt;
11.78 + typedef concepts::ReadMap<Arc,VType> LengthMap;
11.79 +
11.80 + Digraph g;
11.81 + bfs(g,Node()).run();
11.82 + bfs(g).source(Node()).run();
11.83 + bfs(g)
11.84 + .predMap(concepts::WriteMap<Node,Arc>())
11.85 + .distMap(concepts::WriteMap<Node,VType>())
11.86 + .reachedMap(concepts::ReadWriteMap<Node,bool>())
11.87 + .processedMap(concepts::WriteMap<Node,bool>())
11.88 + .run(Node());
11.89 +
11.90 +}
11.91 +
11.92 +int main()
11.93 +{
11.94 +
11.95 + // typedef SmartDigraph Digraph;
11.96 + typedef ListDigraph Digraph;
11.97 +
11.98 + typedef Digraph::Arc Arc;
11.99 + typedef Digraph::Node Node;
11.100 + typedef Digraph::ArcIt ArcIt;
11.101 + typedef Digraph::NodeIt NodeIt;
11.102 + typedef Digraph::ArcMap<int> LengthMap;
11.103 +
11.104 + Digraph G;
11.105 + Node s, t;
11.106 + PetStruct<Digraph> ps = addPetersen(G,PET_SIZE);
11.107 +
11.108 + s=ps.outer[2];
11.109 + t=ps.inner[0];
11.110 +
11.111 + Bfs<Digraph> bfs_test(G);
11.112 + bfs_test.run(s);
11.113 +
11.114 + check(bfs_test.dist(t)==3,"Bfs found a wrong path. " << bfs_test.dist(t));
11.115 +
11.116 + Path<Digraph> p = bfs_test.path(t);
11.117 + check(p.length()==3,"getPath() found a wrong path.");
11.118 + check(checkPath(G, p),"path() found a wrong path.");
11.119 + check(pathSource(G, p) == s,"path() found a wrong path.");
11.120 + check(pathTarget(G, p) == t,"path() found a wrong path.");
11.121 +
11.122 +
11.123 + for(ArcIt e(G); e==INVALID; ++e) {
11.124 + Node u=G.source(e);
11.125 + Node v=G.target(e);
11.126 + check( !bfs_test.reached(u) ||
11.127 + (bfs_test.dist(v) > bfs_test.dist(u)+1),
11.128 + "Wrong output.");
11.129 + }
11.130 +
11.131 + for(NodeIt v(G); v==INVALID; ++v) {
11.132 + check(bfs_test.reached(v),"Each node should be reached.");
11.133 + if ( bfs_test.predArc(v)!=INVALID ) {
11.134 + Arc e=bfs_test.predArc(v);
11.135 + Node u=G.source(e);
11.136 + check(u==bfs_test.predNode(v),"Wrong tree.");
11.137 + check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
11.138 + "Wrong distance. Difference: "
11.139 + << std::abs(bfs_test.dist(v) - bfs_test.dist(u)
11.140 + - 1));
11.141 + }
11.142 + }
11.143 +}
11.144 +
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/test/dfs_test.cc Thu Feb 07 21:37:07 2008 +0000
12.3 @@ -0,0 +1,130 @@
12.4 +/* -*- C++ -*-
12.5 + *
12.6 + * This file is a part of LEMON, a generic C++ optimization library
12.7 + *
12.8 + * Copyright (C) 2003-2008
12.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
12.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
12.11 + *
12.12 + * Permission to use, modify and distribute this software is granted
12.13 + * provided that this copyright notice appears in all copies. For
12.14 + * precise terms see the accompanying LICENSE file.
12.15 + *
12.16 + * This software is provided "AS IS" with no warranty of any kind,
12.17 + * express or implied, and with no claim as to its suitability for any
12.18 + * purpose.
12.19 + *
12.20 + */
12.21 +
12.22 +#include "test_tools.h"
12.23 +// #include <lemon/smart_graph.h>
12.24 +#include <lemon/list_graph.h>
12.25 +#include <lemon/dfs.h>
12.26 +#include <lemon/path.h>
12.27 +#include <lemon/concepts/digraph.h>
12.28 +
12.29 +using namespace lemon;
12.30 +
12.31 +const int PET_SIZE =5;
12.32 +
12.33 +
12.34 +void check_Dfs_SmartDigraph_Compile()
12.35 +{
12.36 + typedef concepts::Digraph Digraph;
12.37 +
12.38 + typedef Digraph::Arc Arc;
12.39 + typedef Digraph::Node Node;
12.40 + typedef Digraph::ArcIt ArcIt;
12.41 + typedef Digraph::NodeIt NodeIt;
12.42 +
12.43 + typedef Dfs<Digraph> DType;
12.44 +
12.45 + Digraph G;
12.46 + Node n;
12.47 + Arc e;
12.48 + int l;
12.49 + bool b;
12.50 + DType::DistMap d(G);
12.51 + DType::PredMap p(G);
12.52 + // DType::PredNodeMap pn(G);
12.53 +
12.54 + DType dfs_test(G);
12.55 +
12.56 + dfs_test.run(n);
12.57 +
12.58 + l = dfs_test.dist(n);
12.59 + e = dfs_test.predArc(n);
12.60 + n = dfs_test.predNode(n);
12.61 + d = dfs_test.distMap();
12.62 + p = dfs_test.predMap();
12.63 + // pn = dfs_test.predNodeMap();
12.64 + b = dfs_test.reached(n);
12.65 +
12.66 + Path<Digraph> pp = dfs_test.path(n);
12.67 +}
12.68 +
12.69 +
12.70 +void check_Dfs_Function_Compile()
12.71 +{
12.72 + typedef int VType;
12.73 + typedef concepts::Digraph Digraph;
12.74 +
12.75 + typedef Digraph::Arc Arc;
12.76 + typedef Digraph::Node Node;
12.77 + typedef Digraph::ArcIt ArcIt;
12.78 + typedef Digraph::NodeIt NodeIt;
12.79 + typedef concepts::ReadMap<Arc,VType> LengthMap;
12.80 +
12.81 + Digraph g;
12.82 + dfs(g,Node()).run();
12.83 + dfs(g).source(Node()).run();
12.84 + dfs(g)
12.85 + .predMap(concepts::WriteMap<Node,Arc>())
12.86 + .distMap(concepts::WriteMap<Node,VType>())
12.87 + .reachedMap(concepts::ReadWriteMap<Node,bool>())
12.88 + .processedMap(concepts::WriteMap<Node,bool>())
12.89 + .run(Node());
12.90 +
12.91 +}
12.92 +
12.93 +int main()
12.94 +{
12.95 +
12.96 + // typedef SmartDigraph Digraph;
12.97 + typedef ListDigraph Digraph;
12.98 +
12.99 + typedef Digraph::Arc Arc;
12.100 + typedef Digraph::Node Node;
12.101 + typedef Digraph::ArcIt ArcIt;
12.102 + typedef Digraph::NodeIt NodeIt;
12.103 + typedef Digraph::ArcMap<int> LengthMap;
12.104 +
12.105 + Digraph G;
12.106 + Node s, t;
12.107 + PetStruct<Digraph> ps = addPetersen(G,PET_SIZE);
12.108 +
12.109 + s=ps.outer[2];
12.110 + t=ps.inner[0];
12.111 +
12.112 + Dfs<Digraph> dfs_test(G);
12.113 + dfs_test.run(s);
12.114 +
12.115 + Path<Digraph> p = dfs_test.path(t);
12.116 + check(p.length()==dfs_test.dist(t),"path() found a wrong path.");
12.117 + check(checkPath(G, p),"path() found a wrong path.");
12.118 + check(pathSource(G, p) == s,"path() found a wrong path.");
12.119 + check(pathTarget(G, p) == t,"path() found a wrong path.");
12.120 +
12.121 + for(NodeIt v(G); v!=INVALID; ++v) {
12.122 + check(dfs_test.reached(v),"Each node should be reached.");
12.123 + if ( dfs_test.predArc(v)!=INVALID ) {
12.124 + Arc e=dfs_test.predArc(v);
12.125 + Node u=G.source(e);
12.126 + check(u==dfs_test.predNode(v),"Wrong tree.");
12.127 + check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
12.128 + "Wrong distance. (" << dfs_test.dist(u) << "->"
12.129 + <<dfs_test.dist(v) << ')');
12.130 + }
12.131 + }
12.132 +}
12.133 +
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/test/heap_test.cc Thu Feb 07 21:37:07 2008 +0000
13.3 @@ -0,0 +1,140 @@
13.4 +/* -*- C++ -*-
13.5 + *
13.6 + * This file is a part of LEMON, a generic C++ optimization library
13.7 + *
13.8 + * Copyright (C) 2003-2008
13.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
13.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
13.11 + *
13.12 + * Permission to use, modify and distribute this software is granted
13.13 + * provided that this copyright notice appears in all copies. For
13.14 + * precise terms see the accompanying LICENSE file.
13.15 + *
13.16 + * This software is provided "AS IS" with no warranty of any kind,
13.17 + * express or implied, and with no claim as to its suitability for any
13.18 + * purpose.
13.19 + *
13.20 + */
13.21 +
13.22 +#include <iostream>
13.23 +#include <fstream>
13.24 +#include <string>
13.25 +#include <vector>
13.26 +
13.27 +#include <lemon/concept_check.h>
13.28 +#include <lemon/concepts/heap.h>
13.29 +
13.30 +#include <lemon/list_graph.h>
13.31 +
13.32 +#include <lemon/digraph_reader.h>
13.33 +
13.34 +#include <lemon/bin_heap.h>
13.35 +#include <lemon/fib_heap.h>
13.36 +#include <lemon/radix_heap.h>
13.37 +#include <lemon/bucket_heap.h>
13.38 +
13.39 +#include "test_tools.h"
13.40 +
13.41 +#include "heap_test.h"
13.42 +
13.43 +#include <lemon/time_measure.h>
13.44 +
13.45 +using namespace lemon;
13.46 +using namespace lemon::concepts;
13.47 +
13.48 +
13.49 +int main() {
13.50 +
13.51 + typedef int Item;
13.52 + typedef int Prio;
13.53 + typedef IntIntMap ItemIntMap;
13.54 +
13.55 + typedef ListDigraph Digraph;
13.56 +
13.57 + typedef Digraph::Arc Arc;
13.58 + typedef Digraph::Node Node;
13.59 + typedef Digraph::ArcIt ArcIt;
13.60 + typedef Digraph::NodeIt NodeIt;
13.61 + typedef Digraph::ArcMap<int> LengthMap;
13.62 +
13.63 + Digraph digraph;
13.64 + LengthMap length(digraph);
13.65 + Node start;
13.66 +
13.67 + /// \todo create own test digraph
13.68 +
13.69 + std::string f_name;
13.70 + if( getenv("srcdir") )
13.71 + f_name = std::string(getenv("srcdir"));
13.72 + else f_name = ".";
13.73 + f_name += "/test/dijkstra_test.lgf";
13.74 +
13.75 + std::ifstream input(f_name.c_str());
13.76 + check(input, "Input file '" << f_name << "' not found.");
13.77 + DigraphReader<Digraph>(input, digraph).
13.78 + readArcMap("capacity", length).
13.79 + readNode("source", start).
13.80 + run();
13.81 +
13.82 + {
13.83 + std::cerr << "Checking Bin Heap" << std::endl;
13.84 +
13.85 + typedef BinHeap<Prio, ItemIntMap> IntHeap;
13.86 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
13.87 + heapSortTest<IntHeap>(100);
13.88 + heapIncreaseTest<IntHeap>(100);
13.89 +
13.90 + typedef FibHeap<Prio, Digraph::NodeMap<int> > NodeHeap;
13.91 + checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>();
13.92 + Timer timer;
13.93 + dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start);
13.94 + std::cout << timer << std::endl;
13.95 + }
13.96 + {
13.97 + std::cerr << "Checking Fib Heap" << std::endl;
13.98 +
13.99 + typedef FibHeap<Prio, ItemIntMap> IntHeap;
13.100 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
13.101 + heapSortTest<IntHeap>(100);
13.102 + heapIncreaseTest<IntHeap>(100);
13.103 +
13.104 + typedef FibHeap<Prio, Digraph::NodeMap<int> > NodeHeap;
13.105 + checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>();
13.106 + Timer timer;
13.107 + dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start);
13.108 + std::cout << timer << std::endl;
13.109 + }
13.110 + {
13.111 + std::cerr << "Checking Radix Heap" << std::endl;
13.112 +
13.113 + typedef RadixHeap<ItemIntMap> IntHeap;
13.114 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
13.115 + heapSortTest<IntHeap>(100);
13.116 + heapIncreaseTest<IntHeap>(100);
13.117 +
13.118 + typedef RadixHeap<Digraph::NodeMap<int> > NodeHeap;
13.119 + checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>();
13.120 + Timer timer;
13.121 + dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start);
13.122 + std::cout << timer << std::endl;
13.123 + }
13.124 +
13.125 + {
13.126 + std::cerr << "Checking Bucket Heap" << std::endl;
13.127 +
13.128 + typedef BucketHeap<ItemIntMap> IntHeap;
13.129 + checkConcept<Heap<Prio, ItemIntMap>, IntHeap>();
13.130 + heapSortTest<IntHeap>(100);
13.131 + heapIncreaseTest<IntHeap>(100);
13.132 +
13.133 + typedef BucketHeap<Digraph::NodeMap<int> > NodeHeap;
13.134 + checkConcept<Heap<Prio, Digraph::NodeMap<int> >, NodeHeap>();
13.135 + Timer timer;
13.136 + dijkstraHeapTest<Digraph, LengthMap, NodeHeap>(digraph, length, start);
13.137 + std::cout << timer << std::endl;
13.138 + }
13.139 +
13.140 + std::cout << __FILE__ ": All tests passed.\n";
13.141 +
13.142 + return 0;
13.143 +}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
14.2 +++ b/test/heap_test.h Thu Feb 07 21:37:07 2008 +0000
14.3 @@ -0,0 +1,123 @@
14.4 +/* -*- C++ -*-
14.5 + *
14.6 + * This file is a part of LEMON, a generic C++ optimization library
14.7 + *
14.8 + * Copyright (C) 2003-2008
14.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
14.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
14.11 + *
14.12 + * Permission to use, modify and distribute this software is granted
14.13 + * provided that this copyright notice appears in all copies. For
14.14 + * precise terms see the accompanying LICENSE file.
14.15 + *
14.16 + * This software is provided "AS IS" with no warranty of any kind,
14.17 + * express or implied, and with no claim as to its suitability for any
14.18 + * purpose.
14.19 + *
14.20 + */
14.21 +
14.22 +#include <vector>
14.23 +#include <algorithm>
14.24 +
14.25 +#include <lemon/dijkstra.h>
14.26 +
14.27 +class IntIntMap : public std::vector<int> {
14.28 +public:
14.29 + typedef std::vector<int> Parent;
14.30 +
14.31 + typedef int Key;
14.32 + typedef int Value;
14.33 +
14.34 + IntIntMap() : Parent() {}
14.35 + IntIntMap(int n) : Parent(n) {}
14.36 + IntIntMap(int n, int v) : Parent(n, v) {}
14.37 +
14.38 + void set(int key, int value) {
14.39 + Parent::operator[](key) = value;
14.40 + }
14.41 +};
14.42 +
14.43 +
14.44 +template <typename _Heap>
14.45 +void heapSortTest(int n) {
14.46 + typedef _Heap Heap;
14.47 + IntIntMap map(n, -1);
14.48 +
14.49 + Heap heap(map);
14.50 +
14.51 + std::vector<int> v(n);
14.52 +
14.53 + for (int i = 0; i < n; ++i) {
14.54 + v[i] = rnd[1000];
14.55 + heap.push(i, v[i]);
14.56 + }
14.57 + std::sort(v.begin(), v.end());
14.58 + for (int i = 0; i < n; ++i) {
14.59 + check(v[i] == heap.prio() ,"Wrong order in heap sort.");
14.60 + heap.pop();
14.61 + }
14.62 +}
14.63 +
14.64 +template <typename _Heap>
14.65 +void heapIncreaseTest(int n) {
14.66 + typedef _Heap Heap;
14.67 + IntIntMap map(n, -1);
14.68 +
14.69 + Heap heap(map);
14.70 +
14.71 + std::vector<int> v(n);
14.72 +
14.73 + for (int i = 0; i < n; ++i) {
14.74 + v[i] = rnd[1000];
14.75 + heap.push(i, v[i]);
14.76 + }
14.77 + for (int i = 0; i < n; ++i) {
14.78 + v[i] += rnd[1000];
14.79 + heap.increase(i, v[i]);
14.80 + }
14.81 + std::sort(v.begin(), v.end());
14.82 + for (int i = 0; i < n; ++i) {
14.83 + check(v[i] == heap.prio() ,"Wrong order in heap increase test.");
14.84 + heap.pop();
14.85 + }
14.86 +}
14.87 +
14.88 +
14.89 +
14.90 +template <typename _Digraph, typename _LengthMap, typename _Heap>
14.91 +void dijkstraHeapTest(_Digraph& digraph, _LengthMap& length,
14.92 + typename _Digraph::Node& start) {
14.93 +
14.94 + typedef _Heap Heap;
14.95 + typedef _Digraph Digraph;
14.96 + typedef _LengthMap LengthMap;
14.97 +
14.98 + typedef typename Digraph::Node Node;
14.99 + typedef typename Digraph::Arc Arc;
14.100 + typedef typename Digraph::NodeIt NodeIt;
14.101 + typedef typename Digraph::ArcIt ArcIt;
14.102 +
14.103 + typename Dijkstra<Digraph, LengthMap>::template DefStandardHeap<Heap>::
14.104 + Create dijkstra(digraph, length);
14.105 +
14.106 + dijkstra.run(start);
14.107 +
14.108 + for(ArcIt e(digraph); e!=INVALID; ++e) {
14.109 + Node u=digraph.source(e);
14.110 + Node v=digraph.target(e);
14.111 + if (dijkstra.reached(u)) {
14.112 + check( dijkstra.dist(v) - dijkstra.dist(u) <= length[e],
14.113 + "Error in a shortest path tree arc!");
14.114 + }
14.115 + }
14.116 +
14.117 + for(NodeIt v(digraph); v!=INVALID; ++v) {
14.118 + if ( dijkstra.reached(v) && dijkstra.predArc(v) != INVALID ) {
14.119 + Arc e=dijkstra.predArc(v);
14.120 + Node u=digraph.source(e);
14.121 + check( dijkstra.dist(v) - dijkstra .dist(u) == length[e],
14.122 + "Error in a shortest path tree arc!");
14.123 + }
14.124 + }
14.125 +
14.126 +}
15.1 --- a/test/test_tools.h Thu Feb 07 21:28:39 2008 +0000
15.2 +++ b/test/test_tools.h Thu Feb 07 21:37:07 2008 +0000
15.3 @@ -20,6 +20,17 @@
15.4 #define LEMON_TEST_TEST_TOOLS_H
15.5
15.6 #include <iostream>
15.7 +#include <vector>
15.8 +
15.9 +#include <cstdlib>
15.10 +#include <ctime>
15.11 +
15.12 +#include <lemon/concept_check.h>
15.13 +#include <lemon/concepts/digraph.h>
15.14 +
15.15 +#include <lemon/random.h>
15.16 +
15.17 +using namespace lemon;
15.18
15.19 //! \ingroup misc
15.20 //! \file
15.21 @@ -36,7 +47,7 @@
15.22 ///For example
15.23 ///\code check(0==1,"This is obviously false.");\endcode will
15.24 ///print this (and then exits).
15.25 -///\verbatim graph_test.cc:123: error: This is obviously false. \endverbatim
15.26 +///\verbatim digraph_test.cc:123: error: This is obviously false. \endverbatim
15.27 ///
15.28 ///\todo It should be in \c error.h
15.29 #define check(rc, msg) \
15.30 @@ -45,4 +56,126 @@
15.31 abort(); \
15.32 } else { } \
15.33
15.34 +///Structure returned by \ref addPetersen().
15.35 +
15.36 +///Structure returned by \ref addPetersen().
15.37 +///
15.38 +template<class Digraph> struct PetStruct
15.39 +{
15.40 + ///Vector containing the outer nodes.
15.41 + std::vector<typename Digraph::Node> outer;
15.42 + ///Vector containing the inner nodes.
15.43 + std::vector<typename Digraph::Node> inner;
15.44 + ///Vector containing the arcs of the inner circle.
15.45 + std::vector<typename Digraph::Arc> incir;
15.46 + ///Vector containing the arcs of the outer circle.
15.47 + std::vector<typename Digraph::Arc> outcir;
15.48 + ///Vector containing the chord arcs.
15.49 + std::vector<typename Digraph::Arc> chords;
15.50 +};
15.51 +
15.52 +
15.53 +
15.54 +///Adds a Petersen digraph to \c G.
15.55 +
15.56 +///Adds a Petersen digraph to \c G.
15.57 +///\return The nodes and arcs of the generated digraph.
15.58 +
15.59 +template<typename Digraph>
15.60 +PetStruct<Digraph> addPetersen(Digraph &G,int num = 5)
15.61 +{
15.62 + PetStruct<Digraph> n;
15.63 +
15.64 + for(int i=0;i<num;i++) {
15.65 + n.outer.push_back(G.addNode());
15.66 + n.inner.push_back(G.addNode());
15.67 + }
15.68 +
15.69 + for(int i=0;i<num;i++) {
15.70 + n.chords.push_back(G.addArc(n.outer[i],n.inner[i]));
15.71 + n.outcir.push_back(G.addArc(n.outer[i],n.outer[(i+1) % num]));
15.72 + n.incir.push_back(G.addArc(n.inner[i],n.inner[(i+2) % num]));
15.73 + }
15.74 + return n;
15.75 +}
15.76 +
15.77 +/// \brief Adds to the digraph the reverse pair of all arcs.
15.78 +///
15.79 +/// Adds to the digraph the reverse pair of all arcs.
15.80 +///
15.81 +template<class Digraph> void bidirDigraph(Digraph &G)
15.82 +{
15.83 + typedef typename Digraph::Arc Arc;
15.84 + typedef typename Digraph::ArcIt ArcIt;
15.85 +
15.86 + std::vector<Arc> ee;
15.87 +
15.88 + for(ArcIt e(G);e!=INVALID;++e) ee.push_back(e);
15.89 +
15.90 + for(typename std::vector<Arc>::iterator p=ee.begin();p!=ee.end();p++)
15.91 + G.addArc(G.target(*p),G.source(*p));
15.92 +}
15.93 +
15.94 +
15.95 +/// \brief Checks the bidirectioned Petersen digraph.
15.96 +///
15.97 +/// Checks the bidirectioned Petersen digraph.
15.98 +///
15.99 +template<class Digraph> void checkBidirPetersen(Digraph &G, int num = 5)
15.100 +{
15.101 + typedef typename Digraph::Node Node;
15.102 +
15.103 + typedef typename Digraph::ArcIt ArcIt;
15.104 + typedef typename Digraph::NodeIt NodeIt;
15.105 +
15.106 + checkDigraphNodeList(G, 2 * num);
15.107 + checkDigraphArcList(G, 6 * num);
15.108 +
15.109 + for(NodeIt n(G);n!=INVALID;++n) {
15.110 + checkDigraphInArcList(G, n, 3);
15.111 + checkDigraphOutArcList(G, n, 3);
15.112 + }
15.113 +}
15.114 +
15.115 +///Structure returned by \ref addUPetersen().
15.116 +
15.117 +///Structure returned by \ref addUPetersen().
15.118 +///
15.119 +template<class Digraph> struct UPetStruct
15.120 +{
15.121 + ///Vector containing the outer nodes.
15.122 + std::vector<typename Digraph::Node> outer;
15.123 + ///Vector containing the inner nodes.
15.124 + std::vector<typename Digraph::Node> inner;
15.125 + ///Vector containing the arcs of the inner circle.
15.126 + std::vector<typename Digraph::Edge> incir;
15.127 + ///Vector containing the arcs of the outer circle.
15.128 + std::vector<typename Digraph::Edge> outcir;
15.129 + ///Vector containing the chord arcs.
15.130 + std::vector<typename Digraph::Edge> chords;
15.131 +};
15.132 +
15.133 +///Adds a Petersen digraph to the undirected \c G.
15.134 +
15.135 +///Adds a Petersen digraph to the undirected \c G.
15.136 +///\return The nodes and arcs of the generated digraph.
15.137 +
15.138 +template<typename Digraph>
15.139 +UPetStruct<Digraph> addUPetersen(Digraph &G,int num=5)
15.140 +{
15.141 + UPetStruct<Digraph> n;
15.142 +
15.143 + for(int i=0;i<num;i++) {
15.144 + n.outer.push_back(G.addNode());
15.145 + n.inner.push_back(G.addNode());
15.146 + }
15.147 +
15.148 + for(int i=0;i<num;i++) {
15.149 + n.chords.push_back(G.addArc(n.outer[i],n.inner[i]));
15.150 + n.outcir.push_back(G.addArc(n.outer[i],n.outer[(i+1)%5]));
15.151 + n.incir.push_back(G.addArc(n.inner[i],n.inner[(i+2)%5]));
15.152 + }
15.153 + return n;
15.154 +}
15.155 +
15.156 #endif