alpar@906: /* -*- C++ -*- ladanyi@1435: * lemon/bfs.h - Part of LEMON, a generic C++ optimization library alpar@906: * alpar@1164: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1359: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@906: * alpar@906: * Permission to use, modify and distribute this software is granted alpar@906: * provided that this copyright notice appears in all copies. For alpar@906: * precise terms see the accompanying LICENSE file. alpar@906: * alpar@906: * This software is provided "AS IS" with no warranty of any kind, alpar@906: * express or implied, and with no claim as to its suitability for any alpar@906: * purpose. alpar@906: * alpar@906: */ alpar@906: alpar@921: #ifndef LEMON_BFS_H alpar@921: #define LEMON_BFS_H alpar@774: alpar@774: ///\ingroup flowalgs alpar@774: ///\file alpar@774: ///\brief Bfs algorithm. alpar@774: alpar@1218: #include alpar@1218: #include alpar@921: #include alpar@1218: #include alpar@1218: #include alpar@774: alpar@921: namespace lemon { alpar@774: alpar@774: alpar@1218: alpar@1218: ///Default traits class of Bfs class. alpar@1218: alpar@1218: ///Default traits class of Bfs class. alpar@1218: ///\param GR Graph type. alpar@1218: template alpar@1218: struct BfsDefaultTraits alpar@1218: { alpar@1218: ///The graph type the algorithm runs on. alpar@1218: typedef GR Graph; alpar@1218: ///\brief The type of the map that stores the last alpar@1218: ///edges of the shortest paths. alpar@1218: /// alpar@1218: ///The type of the map that stores the last alpar@1218: ///edges of the shortest paths. alpar@1218: ///It must meet the \ref concept::WriteMap "WriteMap" concept. alpar@1218: /// alpar@1218: typedef typename Graph::template NodeMap PredMap; alpar@1218: ///Instantiates a PredMap. alpar@1218: alpar@1218: ///This function instantiates a \ref PredMap. alpar@1218: ///\param G is the graph, to which we would like to define the PredMap. alpar@1218: ///\todo The graph alone may be insufficient to initialize alpar@1218: static PredMap *createPredMap(const GR &G) alpar@1218: { alpar@1218: return new PredMap(G); alpar@1218: } alpar@1218: ///The type of the map that indicates which nodes are processed. alpar@1218: alpar@1218: ///The type of the map that indicates which nodes are processed. alpar@1218: ///It must meet the \ref concept::WriteMap "WriteMap" concept. alpar@1218: ///\todo named parameter to set this type, function to read and write. alpar@1218: typedef NullMap ProcessedMap; alpar@1218: ///Instantiates a ProcessedMap. alpar@1218: alpar@1218: ///This function instantiates a \ref ProcessedMap. alpar@1536: ///\param g is the graph, to which alpar@1218: ///we would like to define the \ref ProcessedMap alpar@1536: #ifdef DOXYGEN alpar@1536: static ProcessedMap *createProcessedMap(const GR &g) alpar@1536: #else alpar@1367: static ProcessedMap *createProcessedMap(const GR &) alpar@1536: #endif alpar@1218: { alpar@1218: return new ProcessedMap(); alpar@1218: } alpar@1218: ///The type of the map that indicates which nodes are reached. alpar@1218: alpar@1218: ///The type of the map that indicates which nodes are reached. alpar@1218: ///It must meet the \ref concept::WriteMap "WriteMap" concept. alpar@1218: ///\todo named parameter to set this type, function to read and write. alpar@1218: typedef typename Graph::template NodeMap ReachedMap; alpar@1218: ///Instantiates a ReachedMap. alpar@1218: alpar@1218: ///This function instantiates a \ref ReachedMap. alpar@1218: ///\param G is the graph, to which alpar@1218: ///we would like to define the \ref ReachedMap. alpar@1218: static ReachedMap *createReachedMap(const GR &G) alpar@1218: { alpar@1218: return new ReachedMap(G); alpar@1218: } alpar@1218: ///The type of the map that stores the dists of the nodes. alpar@1218: alpar@1218: ///The type of the map that stores the dists of the nodes. alpar@1218: ///It must meet the \ref concept::WriteMap "WriteMap" concept. alpar@1218: /// alpar@1218: typedef typename Graph::template NodeMap DistMap; alpar@1218: ///Instantiates a DistMap. alpar@1218: alpar@1218: ///This function instantiates a \ref DistMap. alpar@1218: ///\param G is the graph, to which we would like to define the \ref DistMap alpar@1218: static DistMap *createDistMap(const GR &G) alpar@1218: { alpar@1218: return new DistMap(G); alpar@1218: } alpar@1218: }; alpar@1218: alpar@781: ///%BFS algorithm class. alpar@1218: alpar@1218: ///\ingroup flowalgs alpar@1218: ///This class provides an efficient implementation of the %BFS algorithm. alpar@774: /// alpar@1218: ///\param GR The graph type the algorithm runs on. The default value is alpar@1218: ///\ref ListGraph. The value of GR is not used directly by Bfs, it alpar@1218: ///is only passed to \ref BfsDefaultTraits. alpar@1218: ///\param TR Traits class to set various data types used by the algorithm. alpar@1218: ///The default traits class is alpar@1218: ///\ref BfsDefaultTraits "BfsDefaultTraits". alpar@1218: ///See \ref BfsDefaultTraits for the documentation of alpar@1218: ///a Bfs traits class. alpar@1218: /// jacint@1270: ///\author Alpar Juttner alpar@774: alpar@774: #ifdef DOXYGEN alpar@1218: template alpar@774: #else alpar@1218: template > alpar@774: #endif alpar@1218: class Bfs { alpar@774: public: alpar@1218: /** alpar@1218: * \brief \ref Exception for uninitialized parameters. alpar@1218: * alpar@1218: * This error represents problems in the initialization alpar@1218: * of the parameters of the algorithms. alpar@1218: */ alpar@1218: class UninitializedParameter : public lemon::UninitializedParameter { alpar@1218: public: alpar@1218: virtual const char* exceptionName() const { alpar@1218: return "lemon::Bfs::UninitializedParameter"; alpar@1218: } alpar@1218: }; alpar@1218: alpar@1218: typedef TR Traits; alpar@774: ///The type of the underlying graph. alpar@1218: typedef typename TR::Graph Graph; alpar@911: ///\e alpar@774: typedef typename Graph::Node Node; alpar@911: ///\e alpar@774: typedef typename Graph::NodeIt NodeIt; alpar@911: ///\e alpar@774: typedef typename Graph::Edge Edge; alpar@911: ///\e alpar@774: typedef typename Graph::OutEdgeIt OutEdgeIt; alpar@774: alpar@774: ///\brief The type of the map that stores the last alpar@774: ///edges of the shortest paths. alpar@1218: typedef typename TR::PredMap PredMap; alpar@1218: ///The type of the map indicating which nodes are reached. alpar@1218: typedef typename TR::ReachedMap ReachedMap; alpar@1218: ///The type of the map indicating which nodes are processed. alpar@1218: typedef typename TR::ProcessedMap ProcessedMap; alpar@774: ///The type of the map that stores the dists of the nodes. alpar@1218: typedef typename TR::DistMap DistMap; alpar@774: private: alpar@802: /// Pointer to the underlying graph. alpar@774: const Graph *G; alpar@802: ///Pointer to the map of predecessors edges. alpar@1218: PredMap *_pred; alpar@1218: ///Indicates if \ref _pred is locally allocated (\c true) or not. alpar@1218: bool local_pred; alpar@802: ///Pointer to the map of distances. alpar@1218: DistMap *_dist; alpar@1218: ///Indicates if \ref _dist is locally allocated (\c true) or not. alpar@1218: bool local_dist; alpar@1218: ///Pointer to the map of reached status of the nodes. alpar@1218: ReachedMap *_reached; alpar@1218: ///Indicates if \ref _reached is locally allocated (\c true) or not. alpar@1218: bool local_reached; alpar@1218: ///Pointer to the map of processed status of the nodes. alpar@1218: ProcessedMap *_processed; alpar@1218: ///Indicates if \ref _processed is locally allocated (\c true) or not. alpar@1218: bool local_processed; alpar@774: alpar@1218: std::vector _queue; alpar@1218: int _queue_head,_queue_tail,_queue_next_dist; alpar@1218: int _curr_dist; alpar@774: alpar@1218: ///Creates the maps if necessary. alpar@1218: alpar@1218: ///\todo Better memory allocation (instead of new). alpar@1218: void create_maps() alpar@774: { alpar@1218: if(!_pred) { alpar@1218: local_pred = true; alpar@1218: _pred = Traits::createPredMap(*G); alpar@774: } alpar@1218: if(!_dist) { alpar@1218: local_dist = true; alpar@1218: _dist = Traits::createDistMap(*G); alpar@774: } alpar@1218: if(!_reached) { alpar@1218: local_reached = true; alpar@1218: _reached = Traits::createReachedMap(*G); alpar@1218: } alpar@1218: if(!_processed) { alpar@1218: local_processed = true; alpar@1218: _processed = Traits::createProcessedMap(*G); alpar@774: } alpar@774: } deba@1710: deba@1710: protected: alpar@774: deba@1710: Bfs() {} deba@1710: deba@1710: public: alpar@1218: deba@1710: typedef Bfs Create; deba@1710: alpar@1218: ///\name Named template parameters alpar@1218: alpar@1218: ///@{ alpar@1218: alpar@1218: template alpar@1218: struct DefPredMapTraits : public Traits { alpar@1218: typedef T PredMap; alpar@1218: static PredMap *createPredMap(const Graph &G) alpar@1218: { alpar@1218: throw UninitializedParameter(); alpar@1218: } alpar@1218: }; alpar@1218: ///\ref named-templ-param "Named parameter" for setting PredMap type alpar@1218: alpar@1218: ///\ref named-templ-param "Named parameter" for setting PredMap type alpar@1218: /// alpar@1218: template deba@1709: struct DefPredMap : public Bfs< Graph, DefPredMapTraits > { deba@1709: typedef Bfs< Graph, DefPredMapTraits > Create; deba@1709: }; alpar@1218: alpar@1218: template alpar@1218: struct DefDistMapTraits : public Traits { alpar@1218: typedef T DistMap; alpar@1218: static DistMap *createDistMap(const Graph &G) alpar@1218: { alpar@1218: throw UninitializedParameter(); alpar@1218: } alpar@1218: }; alpar@1218: ///\ref named-templ-param "Named parameter" for setting DistMap type alpar@1218: alpar@1218: ///\ref named-templ-param "Named parameter" for setting DistMap type alpar@1218: /// alpar@1218: template deba@1709: struct DefDistMap : public Bfs< Graph, DefDistMapTraits > { deba@1709: typedef Bfs< Graph, DefDistMapTraits > Create; deba@1709: }; alpar@1218: alpar@1218: template alpar@1218: struct DefReachedMapTraits : public Traits { alpar@1218: typedef T ReachedMap; alpar@1218: static ReachedMap *createReachedMap(const Graph &G) alpar@1218: { alpar@1218: throw UninitializedParameter(); alpar@1218: } alpar@1218: }; alpar@1218: ///\ref named-templ-param "Named parameter" for setting ReachedMap type alpar@1218: alpar@1218: ///\ref named-templ-param "Named parameter" for setting ReachedMap type alpar@1218: /// alpar@1218: template deba@1709: struct DefReachedMap : public Bfs< Graph, DefReachedMapTraits > { deba@1709: typedef Bfs< Graph, DefReachedMapTraits > Create; deba@1709: }; alpar@1218: alpar@1218: template alpar@1218: struct DefProcessedMapTraits : public Traits { alpar@1218: typedef T ProcessedMap; alpar@1218: static ProcessedMap *createProcessedMap(const Graph &G) alpar@1218: { alpar@1218: throw UninitializedParameter(); alpar@1218: } alpar@1218: }; alpar@1218: ///\ref named-templ-param "Named parameter" for setting ProcessedMap type alpar@1218: alpar@1218: ///\ref named-templ-param "Named parameter" for setting ProcessedMap type alpar@1218: /// alpar@1218: template deba@1709: struct DefProcessedMap : public Bfs< Graph, DefProcessedMapTraits > { deba@1709: typedef Bfs< Graph, DefProcessedMapTraits > Create; deba@1709: }; alpar@1218: alpar@1218: struct DefGraphProcessedMapTraits : public Traits { alpar@1218: typedef typename Graph::template NodeMap ProcessedMap; alpar@1218: static ProcessedMap *createProcessedMap(const Graph &G) alpar@1218: { alpar@1218: return new ProcessedMap(G); alpar@1218: } alpar@1218: }; alpar@1218: ///\brief \ref named-templ-param "Named parameter" alpar@1218: ///for setting the ProcessedMap type to be Graph::NodeMap. alpar@1218: /// alpar@1218: ///\ref named-templ-param "Named parameter" alpar@1218: ///for setting the ProcessedMap type to be Graph::NodeMap. jacint@1270: ///If you don't set it explicitly, it will be automatically allocated. alpar@1218: template deba@1709: struct DefProcessedMapToBeDefaultMap : deba@1709: public Bfs< Graph, DefGraphProcessedMapTraits> { deba@1709: typedef Bfs< Graph, DefGraphProcessedMapTraits> Create; deba@1709: }; alpar@1218: alpar@1218: ///@} alpar@1218: alpar@1218: public: alpar@1218: alpar@802: ///Constructor. alpar@802: alpar@802: ///\param _G the graph the algorithm will run on. alpar@911: /// alpar@774: Bfs(const Graph& _G) : alpar@774: G(&_G), alpar@1218: _pred(NULL), local_pred(false), alpar@1218: _dist(NULL), local_dist(false), alpar@1218: _reached(NULL), local_reached(false), alpar@1218: _processed(NULL), local_processed(false) alpar@774: { } alpar@774: alpar@802: ///Destructor. alpar@774: ~Bfs() alpar@774: { alpar@1218: if(local_pred) delete _pred; alpar@1218: if(local_dist) delete _dist; alpar@1218: if(local_reached) delete _reached; alpar@1218: if(local_processed) delete _processed; alpar@774: } alpar@774: alpar@774: ///Sets the map storing the predecessor edges. alpar@774: alpar@774: ///Sets the map storing the predecessor edges. alpar@774: ///If you don't use this function before calling \ref run(), jacint@1270: ///it will allocate one. The destructor deallocates this alpar@774: ///automatically allocated map, of course. alpar@774: ///\return (*this) alpar@1218: Bfs &predMap(PredMap &m) alpar@774: { alpar@1218: if(local_pred) { alpar@1218: delete _pred; alpar@1218: local_pred=false; alpar@774: } alpar@1218: _pred = &m; alpar@774: return *this; alpar@774: } alpar@774: alpar@1218: ///Sets the map indicating the reached nodes. alpar@774: alpar@1218: ///Sets the map indicating the reached nodes. alpar@774: ///If you don't use this function before calling \ref run(), jacint@1270: ///it will allocate one. The destructor deallocates this alpar@774: ///automatically allocated map, of course. alpar@774: ///\return (*this) alpar@1218: Bfs &reachedMap(ReachedMap &m) alpar@774: { alpar@1218: if(local_reached) { alpar@1218: delete _reached; alpar@1218: local_reached=false; alpar@774: } alpar@1218: _reached = &m; alpar@774: return *this; alpar@774: } alpar@774: alpar@1218: ///Sets the map indicating the processed nodes. alpar@1218: alpar@1218: ///Sets the map indicating the processed nodes. alpar@1218: ///If you don't use this function before calling \ref run(), jacint@1270: ///it will allocate one. The destructor deallocates this alpar@1218: ///automatically allocated map, of course. alpar@1218: ///\return (*this) alpar@1218: Bfs &processedMap(ProcessedMap &m) alpar@1218: { alpar@1218: if(local_processed) { alpar@1218: delete _processed; alpar@1218: local_processed=false; alpar@1218: } alpar@1218: _processed = &m; alpar@1218: return *this; alpar@1218: } alpar@1218: alpar@774: ///Sets the map storing the distances calculated by the algorithm. alpar@774: alpar@774: ///Sets the map storing the distances calculated by the algorithm. alpar@774: ///If you don't use this function before calling \ref run(), jacint@1270: ///it will allocate one. The destructor deallocates this alpar@774: ///automatically allocated map, of course. alpar@774: ///\return (*this) alpar@1218: Bfs &distMap(DistMap &m) alpar@774: { alpar@1218: if(local_dist) { alpar@1218: delete _dist; alpar@1218: local_dist=false; alpar@774: } alpar@1218: _dist = &m; alpar@774: return *this; alpar@774: } alpar@774: alpar@1218: public: alpar@1218: ///\name Execution control alpar@1218: ///The simplest way to execute the algorithm is to use alpar@1218: ///one of the member functions called \c run(...). alpar@1218: ///\n alpar@1218: ///If you need more control on the execution, alpar@1218: ///first you must call \ref init(), then you can add several source nodes alpar@1218: ///with \ref addSource(). alpar@1218: ///Finally \ref start() will perform the actual path alpar@1218: ///computation. alpar@1218: alpar@1218: ///@{ alpar@1218: alpar@1218: ///Initializes the internal data structures. alpar@1218: alpar@1218: ///Initializes the internal data structures. alpar@1218: /// alpar@1218: void init() alpar@1218: { alpar@1218: create_maps(); alpar@1218: _queue.resize(countNodes(*G)); alpar@1218: _queue_head=_queue_tail=0; alpar@1218: _curr_dist=1; alpar@774: for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { alpar@1218: _pred->set(u,INVALID); alpar@1218: _reached->set(u,false); alpar@1218: _processed->set(u,false); alpar@774: } alpar@774: } alpar@774: alpar@1218: ///Adds a new source node. alpar@774: alpar@1218: ///Adds a new source node to the set of nodes to be processed. alpar@1218: /// alpar@1218: void addSource(Node s) alpar@1218: { alpar@1218: if(!(*_reached)[s]) alpar@1218: { alpar@1218: _reached->set(s,true); alpar@1218: _pred->set(s,INVALID); alpar@1218: _dist->set(s,0); alpar@1218: _queue[_queue_head++]=s; alpar@1218: _queue_next_dist=_queue_head; alpar@1218: } alpar@1218: } alpar@1218: alpar@1218: ///Processes the next node. alpar@1218: alpar@1218: ///Processes the next node. alpar@1218: /// alpar@1516: ///\return The processed node. alpar@1516: /// alpar@1218: ///\warning The queue must not be empty! alpar@1516: Node processNextNode() alpar@1218: { alpar@1218: if(_queue_tail==_queue_next_dist) { alpar@1218: _curr_dist++; alpar@1218: _queue_next_dist=_queue_head; alpar@1218: } alpar@1218: Node n=_queue[_queue_tail++]; alpar@1218: _processed->set(n,true); alpar@1218: Node m; alpar@1218: for(OutEdgeIt e(*G,n);e!=INVALID;++e) alpar@1218: if(!(*_reached)[m=G->target(e)]) { alpar@1218: _queue[_queue_head++]=m; alpar@1218: _reached->set(m,true); alpar@1218: _pred->set(m,e); alpar@1218: _dist->set(m,_curr_dist); alpar@1218: } alpar@1516: return n; alpar@1218: } alpar@1218: alpar@1665: ///Next node to be processed. alpar@1665: alpar@1665: ///Next node to be processed. alpar@1665: /// alpar@1665: ///\return The next node to be processed or INVALID if the queue is alpar@1665: /// empty. deba@1694: Node nextNode() alpar@1665: { alpar@1665: return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; alpar@1665: } alpar@1665: alpar@1218: ///\brief Returns \c false if there are nodes alpar@1218: ///to be processed in the queue alpar@1218: /// alpar@1218: ///Returns \c false if there are nodes alpar@1218: ///to be processed in the queue alpar@1218: bool emptyQueue() { return _queue_tail==_queue_head; } alpar@1218: ///Returns the number of the nodes to be processed. alpar@1218: alpar@1218: ///Returns the number of the nodes to be processed in the queue. alpar@1218: /// alpar@1218: int queueSize() { return _queue_head-_queue_tail; } alpar@1218: alpar@1218: ///Executes the algorithm. alpar@1218: alpar@1218: ///Executes the algorithm. alpar@1218: /// alpar@1218: ///\pre init() must be called and at least one node should be added alpar@1218: ///with addSource() before using this function. alpar@1218: /// alpar@1218: ///This method runs the %BFS algorithm from the root node(s) alpar@1218: ///in order to alpar@1218: ///compute the alpar@1218: ///shortest path to each node. The algorithm computes alpar@1218: ///- The shortest path tree. alpar@1218: ///- The distance of each node from the root(s). alpar@1218: /// alpar@1218: void start() alpar@1218: { alpar@1218: while ( !emptyQueue() ) processNextNode(); alpar@1218: } alpar@1218: alpar@1218: ///Executes the algorithm until \c dest is reached. alpar@1218: alpar@1218: ///Executes the algorithm until \c dest is reached. alpar@1218: /// alpar@1218: ///\pre init() must be called and at least one node should be added alpar@1218: ///with addSource() before using this function. alpar@1218: /// alpar@1218: ///This method runs the %BFS algorithm from the root node(s) alpar@1218: ///in order to alpar@1218: ///compute the alpar@1218: ///shortest path to \c dest. The algorithm computes alpar@1218: ///- The shortest path to \c dest. alpar@1218: ///- The distance of \c dest from the root(s). alpar@1218: /// alpar@1218: void start(Node dest) alpar@1218: { alpar@1218: while ( !emptyQueue() && _queue[_queue_tail]!=dest ) processNextNode(); alpar@1218: } alpar@1218: alpar@1218: ///Executes the algorithm until a condition is met. alpar@1218: alpar@1218: ///Executes the algorithm until a condition is met. alpar@1218: /// alpar@1218: ///\pre init() must be called and at least one node should be added alpar@1218: ///with addSource() before using this function. alpar@1218: /// alpar@1218: ///\param nm must be a bool (or convertible) node map. The algorithm alpar@1218: ///will stop when it reaches a node \c v with nm[v]==true. alpar@1218: template deba@1755: void start(const NM &nm) deba@1755: { deba@1755: while ( !emptyQueue() && !nm[_queue[_queue_tail]] ) processNextNode(); deba@1755: } alpar@1218: alpar@1218: ///Runs %BFS algorithm from node \c s. alpar@1218: alpar@1218: ///This method runs the %BFS algorithm from a root node \c s alpar@1218: ///in order to alpar@1218: ///compute the alpar@1218: ///shortest path to each node. The algorithm computes alpar@1218: ///- The shortest path tree. alpar@1218: ///- The distance of each node from the root. alpar@1218: /// alpar@1218: ///\note d.run(s) is just a shortcut of the following code. alpar@1218: ///\code alpar@1218: /// d.init(); alpar@1218: /// d.addSource(s); alpar@1218: /// d.start(); alpar@1218: ///\endcode alpar@1218: void run(Node s) { alpar@1218: init(); alpar@1218: addSource(s); alpar@1218: start(); alpar@1218: } alpar@1218: alpar@1218: ///Finds the shortest path between \c s and \c t. alpar@1218: alpar@1218: ///Finds the shortest path between \c s and \c t. alpar@1218: /// alpar@1218: ///\return The length of the shortest s---t path if there exists one, alpar@1218: ///0 otherwise. alpar@1218: ///\note Apart from the return value, d.run(s) is alpar@1218: ///just a shortcut of the following code. alpar@1218: ///\code alpar@1218: /// d.init(); alpar@1218: /// d.addSource(s); alpar@1218: /// d.start(t); alpar@1218: ///\endcode alpar@1218: int run(Node s,Node t) { alpar@1218: init(); alpar@1218: addSource(s); alpar@1218: start(t); alpar@1218: return reached(t)?_curr_dist-1+(_queue_tail==_queue_next_dist):0; alpar@1218: } alpar@1218: alpar@1218: ///@} alpar@1218: alpar@1218: ///\name Query Functions alpar@1218: ///The result of the %BFS algorithm can be obtained using these alpar@1218: ///functions.\n alpar@1218: ///Before the use of these functions, alpar@1218: ///either run() or start() must be called. alpar@1218: alpar@1218: ///@{ alpar@1218: alpar@1283: ///Copies the shortest path to \c t into \c p alpar@1283: alpar@1283: ///This function copies the shortest path to \c t into \c p. alpar@1536: ///If \c t is a source itself or unreachable, then it does not alpar@1283: ///alter \c p. alpar@1283: ///\return Returns \c true if a path to \c t was actually copied to \c p, alpar@1283: ///\c false otherwise. alpar@1283: ///\sa DirPath alpar@1283: template alpar@1283: bool getPath(P &p,Node t) alpar@1283: { alpar@1283: if(reached(t)) { alpar@1283: p.clear(); alpar@1283: typename P::Builder b(p); deba@1763: for(b.setStartNode(t);predEdge(t)!=INVALID;t=predNode(t)) deba@1763: b.pushFront(predEdge(t)); alpar@1283: b.commit(); alpar@1283: return true; alpar@1283: } alpar@1283: return false; alpar@1283: } alpar@1283: alpar@1218: ///The distance of a node from the root(s). alpar@1218: alpar@1218: ///Returns the distance of a node from the root(s). alpar@774: ///\pre \ref run() must be called before using this function. alpar@1218: ///\warning If node \c v in unreachable from the root(s) the return value jacint@1270: ///of this function is undefined. alpar@1218: int dist(Node v) const { return (*_dist)[v]; } alpar@774: alpar@1218: ///Returns the 'previous edge' of the shortest path tree. alpar@774: alpar@1218: ///For a node \c v it returns the 'previous edge' alpar@1218: ///of the shortest path tree, alpar@1218: ///i.e. it returns the last edge of a shortest path from the root(s) to \c alpar@774: ///v. It is \ref INVALID alpar@1218: ///if \c v is unreachable from the root(s) or \c v is a root. The alpar@1218: ///shortest path tree used here is equal to the shortest path tree used in alpar@1631: ///\ref predNode(). alpar@1218: ///\pre Either \ref run() or \ref start() must be called before using alpar@774: ///this function. deba@1763: Edge predEdge(Node v) const { return (*_pred)[v];} alpar@774: alpar@1218: ///Returns the 'previous node' of the shortest path tree. alpar@774: alpar@1218: ///For a node \c v it returns the 'previous node' alpar@1218: ///of the shortest path tree, alpar@774: ///i.e. it returns the last but one node from a shortest path from the alpar@1218: ///root(a) to \c /v. alpar@1218: ///It is INVALID if \c v is unreachable from the root(s) or alpar@1218: ///if \c v itself a root. alpar@1218: ///The shortest path tree used here is equal to the shortest path deba@1763: ///tree used in \ref predEdge(). alpar@1218: ///\pre Either \ref run() or \ref start() must be called before alpar@774: ///using this function. alpar@1218: Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: alpar@1218: G->source((*_pred)[v]); } alpar@774: alpar@774: ///Returns a reference to the NodeMap of distances. alpar@1218: alpar@1218: ///Returns a reference to the NodeMap of distances. alpar@1218: ///\pre Either \ref run() or \ref init() must alpar@774: ///be called before using this function. alpar@1218: const DistMap &distMap() const { return *_dist;} alpar@774: alpar@1218: ///Returns a reference to the shortest path tree map. alpar@774: alpar@774: ///Returns a reference to the NodeMap of the edges of the alpar@1218: ///shortest path tree. alpar@1218: ///\pre Either \ref run() or \ref init() alpar@1218: ///must be called before using this function. alpar@1218: const PredMap &predMap() const { return *_pred;} alpar@774: alpar@774: ///Checks if a node is reachable from the root. alpar@774: alpar@774: ///Returns \c true if \c v is reachable from the root. jacint@1270: ///\warning The source nodes are indicated as unreached. alpar@1218: ///\pre Either \ref run() or \ref start() alpar@1218: ///must be called before using this function. alpar@774: /// alpar@1218: bool reached(Node v) { return (*_reached)[v]; } alpar@1218: alpar@1218: ///@} alpar@1218: }; alpar@1218: alpar@1218: ///Default traits class of Bfs function. alpar@1218: alpar@1218: ///Default traits class of Bfs function. alpar@1218: ///\param GR Graph type. alpar@1218: template alpar@1218: struct BfsWizardDefaultTraits alpar@1218: { alpar@1218: ///The graph type the algorithm runs on. alpar@1218: typedef GR Graph; alpar@1218: ///\brief The type of the map that stores the last alpar@1218: ///edges of the shortest paths. alpar@1218: /// alpar@1218: ///The type of the map that stores the last alpar@1218: ///edges of the shortest paths. alpar@1218: ///It must meet the \ref concept::WriteMap "WriteMap" concept. alpar@774: /// alpar@1218: typedef NullMap PredMap; alpar@1218: ///Instantiates a PredMap. alpar@1218: alpar@1218: ///This function instantiates a \ref PredMap. alpar@1536: ///\param g is the graph, to which we would like to define the PredMap. alpar@1218: ///\todo The graph alone may be insufficient to initialize alpar@1536: #ifdef DOXYGEN alpar@1536: static PredMap *createPredMap(const GR &g) alpar@1536: #else alpar@1367: static PredMap *createPredMap(const GR &) alpar@1536: #endif alpar@1218: { alpar@1218: return new PredMap(); alpar@1218: } alpar@1218: alpar@1218: ///The type of the map that indicates which nodes are processed. alpar@1218: alpar@1218: ///The type of the map that indicates which nodes are processed. alpar@1218: ///It must meet the \ref concept::WriteMap "WriteMap" concept. alpar@1218: ///\todo named parameter to set this type, function to read and write. alpar@1218: typedef NullMap ProcessedMap; alpar@1218: ///Instantiates a ProcessedMap. alpar@1218: alpar@1218: ///This function instantiates a \ref ProcessedMap. alpar@1536: ///\param g is the graph, to which alpar@1218: ///we would like to define the \ref ProcessedMap alpar@1536: #ifdef DOXYGEN alpar@1536: static ProcessedMap *createProcessedMap(const GR &g) alpar@1536: #else alpar@1367: static ProcessedMap *createProcessedMap(const GR &) alpar@1536: #endif alpar@1218: { alpar@1218: return new ProcessedMap(); alpar@1218: } alpar@1218: ///The type of the map that indicates which nodes are reached. alpar@1218: alpar@1218: ///The type of the map that indicates which nodes are reached. alpar@1218: ///It must meet the \ref concept::WriteMap "WriteMap" concept. alpar@1218: ///\todo named parameter to set this type, function to read and write. alpar@1218: typedef typename Graph::template NodeMap ReachedMap; alpar@1218: ///Instantiates a ReachedMap. alpar@1218: alpar@1218: ///This function instantiates a \ref ReachedMap. alpar@1218: ///\param G is the graph, to which alpar@1218: ///we would like to define the \ref ReachedMap. alpar@1218: static ReachedMap *createReachedMap(const GR &G) alpar@1218: { alpar@1218: return new ReachedMap(G); alpar@1218: } alpar@1218: ///The type of the map that stores the dists of the nodes. alpar@1218: alpar@1218: ///The type of the map that stores the dists of the nodes. alpar@1218: ///It must meet the \ref concept::WriteMap "WriteMap" concept. alpar@1218: /// alpar@1218: typedef NullMap DistMap; alpar@1218: ///Instantiates a DistMap. alpar@1218: alpar@1218: ///This function instantiates a \ref DistMap. alpar@1536: ///\param g is the graph, to which we would like to define the \ref DistMap alpar@1536: #ifdef DOXYGEN alpar@1536: static DistMap *createDistMap(const GR &g) alpar@1536: #else alpar@1367: static DistMap *createDistMap(const GR &) alpar@1536: #endif alpar@1218: { alpar@1218: return new DistMap(); alpar@1218: } alpar@1218: }; alpar@1218: alpar@1218: /// Default traits used by \ref BfsWizard alpar@1218: alpar@1218: /// To make it easier to use Bfs algorithm alpar@1218: ///we have created a wizard class. alpar@1218: /// This \ref BfsWizard class needs default traits, alpar@1218: ///as well as the \ref Bfs class. alpar@1218: /// The \ref BfsWizardBase is a class to be the default traits of the alpar@1218: /// \ref BfsWizard class. alpar@1218: template alpar@1218: class BfsWizardBase : public BfsWizardDefaultTraits alpar@1218: { alpar@1218: alpar@1218: typedef BfsWizardDefaultTraits Base; alpar@1218: protected: alpar@1218: /// Type of the nodes in the graph. alpar@1218: typedef typename Base::Graph::Node Node; alpar@1218: alpar@1218: /// Pointer to the underlying graph. alpar@1218: void *_g; alpar@1218: ///Pointer to the map of reached nodes. alpar@1218: void *_reached; alpar@1218: ///Pointer to the map of processed nodes. alpar@1218: void *_processed; alpar@1218: ///Pointer to the map of predecessors edges. alpar@1218: void *_pred; alpar@1218: ///Pointer to the map of distances. alpar@1218: void *_dist; alpar@1218: ///Pointer to the source node. alpar@1218: Node _source; alpar@1218: alpar@1218: public: alpar@1218: /// Constructor. alpar@1218: alpar@1218: /// This constructor does not require parameters, therefore it initiates alpar@1218: /// all of the attributes to default values (0, INVALID). alpar@1218: BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), alpar@1218: _dist(0), _source(INVALID) {} alpar@1218: alpar@1218: /// Constructor. alpar@1218: alpar@1218: /// This constructor requires some parameters, alpar@1218: /// listed in the parameters list. alpar@1218: /// Others are initiated to 0. alpar@1218: /// \param g is the initial value of \ref _g alpar@1218: /// \param s is the initial value of \ref _source alpar@1218: BfsWizardBase(const GR &g, Node s=INVALID) : alpar@1218: _g((void *)&g), _reached(0), _processed(0), _pred(0), alpar@1218: _dist(0), _source(s) {} alpar@1218: alpar@1218: }; alpar@1218: alpar@1218: /// A class to make the usage of Bfs algorithm easier alpar@1218: alpar@1218: /// This class is created to make it easier to use Bfs algorithm. alpar@1218: /// It uses the functions and features of the plain \ref Bfs, alpar@1218: /// but it is much simpler to use it. alpar@1218: /// alpar@1218: /// Simplicity means that the way to change the types defined alpar@1218: /// in the traits class is based on functions that returns the new class alpar@1218: /// and not on templatable built-in classes. alpar@1218: /// When using the plain \ref Bfs alpar@1218: /// the new class with the modified type comes from alpar@1218: /// the original class by using the :: alpar@1218: /// operator. In the case of \ref BfsWizard only alpar@1218: /// a function have to be called and it will alpar@1218: /// return the needed class. alpar@1218: /// alpar@1218: /// It does not have own \ref run method. When its \ref run method is called alpar@1218: /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run alpar@1218: /// method of it. alpar@1218: template alpar@1218: class BfsWizard : public TR alpar@1218: { alpar@1218: typedef TR Base; alpar@1218: alpar@1218: ///The type of the underlying graph. alpar@1218: typedef typename TR::Graph Graph; alpar@1218: //\e alpar@1218: typedef typename Graph::Node Node; alpar@1218: //\e alpar@1218: typedef typename Graph::NodeIt NodeIt; alpar@1218: //\e alpar@1218: typedef typename Graph::Edge Edge; alpar@1218: //\e alpar@1218: typedef typename Graph::OutEdgeIt OutEdgeIt; alpar@1218: alpar@1218: ///\brief The type of the map that stores alpar@1218: ///the reached nodes alpar@1218: typedef typename TR::ReachedMap ReachedMap; alpar@1218: ///\brief The type of the map that stores alpar@1218: ///the processed nodes alpar@1218: typedef typename TR::ProcessedMap ProcessedMap; alpar@1218: ///\brief The type of the map that stores the last alpar@1218: ///edges of the shortest paths. alpar@1218: typedef typename TR::PredMap PredMap; alpar@1218: ///The type of the map that stores the dists of the nodes. alpar@1218: typedef typename TR::DistMap DistMap; alpar@1218: alpar@1218: public: alpar@1218: /// Constructor. alpar@1218: BfsWizard() : TR() {} alpar@1218: alpar@1218: /// Constructor that requires parameters. alpar@1218: alpar@1218: /// Constructor that requires parameters. alpar@1218: /// These parameters will be the default values for the traits class. alpar@1218: BfsWizard(const Graph &g, Node s=INVALID) : alpar@1218: TR(g,s) {} alpar@1218: alpar@1218: ///Copy constructor alpar@1218: BfsWizard(const TR &b) : TR(b) {} alpar@1218: alpar@1218: ~BfsWizard() {} alpar@1218: alpar@1218: ///Runs Bfs algorithm from a given node. alpar@1218: alpar@1218: ///Runs Bfs algorithm from a given node. alpar@1218: ///The node can be given by the \ref source function. alpar@1218: void run() alpar@1218: { alpar@1218: if(Base::_source==INVALID) throw UninitializedParameter(); alpar@1218: Bfs alg(*(Graph*)Base::_g); alpar@1218: if(Base::_reached) alpar@1218: alg.reachedMap(*(ReachedMap*)Base::_reached); alpar@1218: if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed); alpar@1218: if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred); alpar@1218: if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist); alpar@1218: alg.run(Base::_source); alpar@1218: } alpar@1218: alpar@1218: ///Runs Bfs algorithm from the given node. alpar@1218: alpar@1218: ///Runs Bfs algorithm from the given node. alpar@1218: ///\param s is the given source. alpar@1218: void run(Node s) alpar@1218: { alpar@1218: Base::_source=s; alpar@1218: run(); alpar@1218: } alpar@1218: alpar@1218: template alpar@1218: struct DefPredMapBase : public Base { alpar@1218: typedef T PredMap; alpar@1367: static PredMap *createPredMap(const Graph &) { return 0; }; alpar@1236: DefPredMapBase(const TR &b) : TR(b) {} alpar@1218: }; alpar@1218: alpar@1218: ///\brief \ref named-templ-param "Named parameter" alpar@1218: ///function for setting PredMap alpar@1218: /// alpar@1218: /// \ref named-templ-param "Named parameter" alpar@1218: ///function for setting PredMap alpar@1218: /// alpar@1218: template alpar@1218: BfsWizard > predMap(const T &t) alpar@1218: { alpar@1218: Base::_pred=(void *)&t; alpar@1218: return BfsWizard >(*this); alpar@1218: } alpar@1218: alpar@1218: alpar@1218: template alpar@1218: struct DefReachedMapBase : public Base { alpar@1218: typedef T ReachedMap; alpar@1367: static ReachedMap *createReachedMap(const Graph &) { return 0; }; alpar@1236: DefReachedMapBase(const TR &b) : TR(b) {} alpar@1218: }; alpar@1218: alpar@1218: ///\brief \ref named-templ-param "Named parameter" alpar@1218: ///function for setting ReachedMap alpar@1218: /// alpar@1218: /// \ref named-templ-param "Named parameter" alpar@1218: ///function for setting ReachedMap alpar@1218: /// alpar@1218: template alpar@1218: BfsWizard > reachedMap(const T &t) alpar@1218: { alpar@1218: Base::_pred=(void *)&t; alpar@1218: return BfsWizard >(*this); alpar@1218: } alpar@1218: alpar@1218: alpar@1218: template alpar@1218: struct DefProcessedMapBase : public Base { alpar@1218: typedef T ProcessedMap; alpar@1367: static ProcessedMap *createProcessedMap(const Graph &) { return 0; }; alpar@1236: DefProcessedMapBase(const TR &b) : TR(b) {} alpar@1218: }; alpar@1218: alpar@1218: ///\brief \ref named-templ-param "Named parameter" alpar@1218: ///function for setting ProcessedMap alpar@1218: /// alpar@1218: /// \ref named-templ-param "Named parameter" alpar@1218: ///function for setting ProcessedMap alpar@1218: /// alpar@1218: template alpar@1218: BfsWizard > processedMap(const T &t) alpar@1218: { alpar@1218: Base::_pred=(void *)&t; alpar@1218: return BfsWizard >(*this); alpar@1218: } alpar@1218: alpar@1218: alpar@1218: template alpar@1218: struct DefDistMapBase : public Base { alpar@1218: typedef T DistMap; alpar@1367: static DistMap *createDistMap(const Graph &) { return 0; }; alpar@1236: DefDistMapBase(const TR &b) : TR(b) {} alpar@1218: }; alpar@1218: alpar@1218: ///\brief \ref named-templ-param "Named parameter" alpar@1218: ///function for setting DistMap type alpar@1218: /// alpar@1218: /// \ref named-templ-param "Named parameter" alpar@1218: ///function for setting DistMap type alpar@1218: /// alpar@1218: template alpar@1218: BfsWizard > distMap(const T &t) alpar@1218: { alpar@1218: Base::_dist=(void *)&t; alpar@1218: return BfsWizard >(*this); alpar@1218: } alpar@1218: alpar@1218: /// Sets the source node, from which the Bfs algorithm runs. alpar@1218: alpar@1218: /// Sets the source node, from which the Bfs algorithm runs. alpar@1218: /// \param s is the source node. alpar@1218: BfsWizard &source(Node s) alpar@1218: { alpar@1218: Base::_source=s; alpar@1218: return *this; alpar@1218: } alpar@774: alpar@774: }; alpar@774: alpar@1218: ///Function type interface for Bfs algorithm. alpar@1218: alpar@1218: /// \ingroup flowalgs alpar@1218: ///Function type interface for Bfs algorithm. alpar@1218: /// alpar@1218: ///This function also has several alpar@1218: ///\ref named-templ-func-param "named parameters", alpar@1218: ///they are declared as the members of class \ref BfsWizard. alpar@1218: ///The following alpar@1218: ///example shows how to use these parameters. alpar@1218: ///\code alpar@1218: /// bfs(g,source).predMap(preds).run(); alpar@1218: ///\endcode alpar@1218: ///\warning Don't forget to put the \ref BfsWizard::run() "run()" alpar@1218: ///to the end of the parameter list. alpar@1218: ///\sa BfsWizard alpar@1218: ///\sa Bfs alpar@1218: template alpar@1218: BfsWizard > alpar@1218: bfs(const GR &g,typename GR::Node s=INVALID) alpar@1218: { alpar@1218: return BfsWizard >(g,s); alpar@1218: } alpar@1218: alpar@921: } //END OF NAMESPACE LEMON alpar@774: alpar@774: #endif alpar@774: