src/work/alpar/dijkstra.h
changeset 954 5b1ffef43d4c
parent 953 d9c115e2eeaf
child 955 0a066f80e05f
equal deleted inserted replaced
1:d6746a9cc1b4 2:26db6da01207
    28 namespace lemon {
    28 namespace lemon {
    29 
    29 
    30 /// \addtogroup flowalgs
    30 /// \addtogroup flowalgs
    31 /// @{
    31 /// @{
    32 
    32 
       
    33   ///Default traits class of Dijkstra class.
       
    34 
       
    35   ///Default traits class of Dijkstra class.
       
    36   ///\param GR Graph type.
       
    37   ///\param LM Type of length map.
    33   template<class GR, class LM>
    38   template<class GR, class LM>
    34   struct DijkstraDefaultTraits
    39   struct DijkstraDefaultTraits
    35   {
    40   {
    36     ///\e 
    41     ///The graph type the algorithm runs on. 
    37     typedef GR Graph;
    42     typedef GR Graph;
    38     ///\e
       
    39     typedef typename Graph::Node Node;
       
    40     ///\e
       
    41     typedef typename Graph::Edge Edge;
       
    42     ///The type of the map that stores the edge lengths.
    43     ///The type of the map that stores the edge lengths.
    43 
    44 
    44     ///It must meet the \ref ReadMap concept.
    45     ///It must meet the \ref ReadMap concept.
    45     ///
    46     ///
    46     typedef LM LengthMap;
    47     typedef LM LengthMap;
    47     ///The type of the length of the edges.
    48     //The type of the length of the edges.
    48     typedef typename LM::ValueType ValueType;
    49     typedef typename LM::ValueType ValueType;
    49     ///The heap type used by the dijkstra algorithm.
    50     ///The heap type used by Dijkstra algorithm.
    50     typedef BinHeap<typename Graph::Node,
    51     typedef BinHeap<typename Graph::Node,
    51 		    typename LM::ValueType,
    52 		    typename LM::ValueType,
    52 		    typename GR::template NodeMap<int>,
    53 		    typename GR::template NodeMap<int>,
    53 		    std::less<ValueType> > Heap;
    54 		    std::less<ValueType> > Heap;
    54 
    55 
    55     ///\brief The type of the map that stores the last
    56     ///\brief The type of the map that stores the last
    56     ///edges of the shortest paths.
    57     ///edges of the shortest paths.
    57     /// 
    58     /// 
    58     ///It must meet the \ref WriteMap concept.
    59     ///It must meet the \ref WriteMap concept.
    59     ///
    60     ///
    60     typedef typename Graph::template NodeMap<Edge> PredMap;
    61     typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
    61     ///
    62     ///Instantiates a PredMap.
    62  
    63  
    63     ///\todo Please document...
    64     ///\todo Please document...
    64     ///
    65     ///
    65     static PredMap *createPredMap(const Graph &G) 
    66     static PredMap *createPredMap(const GR &G) 
    66     {
    67     {
    67       return new PredMap(G);
    68       return new PredMap(G);
    68     }
    69     }
    69     ///\brief The type of the map that stores the last but one
    70     ///\brief The type of the map that stores the last but one
    70     ///nodes of the shortest paths.
    71     ///nodes of the shortest paths.
    71     ///
    72     ///
    72     ///It must meet the \ref WriteMap concept.
    73     ///It must meet the \ref WriteMap concept.
    73     ///
    74     ///
    74     typedef typename Graph::template NodeMap<Node> PredNodeMap;
    75     typedef typename Graph::template NodeMap<typename GR::Node> PredNodeMap;
    75     ///
    76     ///Instantiates a PredNodeMap.
    76  
    77  
    77     ///\todo Please document...
    78     ///\todo Please document...
    78     /// 
    79     /// 
    79     static PredNodeMap *createPredNodeMap(const Graph &G)
    80     static PredNodeMap *createPredNodeMap(const GR &G)
    80     {
    81     {
    81       return new PredNodeMap(G);
    82       return new PredNodeMap(G);
    82     }
    83     }
    83     ///The type of the map that stores the dists of the nodes.
    84     ///The type of the map that stores the dists of the nodes.
    84  
    85  
    85     ///It must meet the \ref WriteMap concept.
    86     ///It must meet the \ref WriteMap concept.
    86     ///
    87     ///
    87     typedef typename Graph::template NodeMap<ValueType> DistMap;
    88     typedef typename Graph::template NodeMap<typename LM::ValueType> DistMap;
    88     ///
    89     ///Instantiates a DistMap.
    89  
    90  
    90     ///\todo Please document...
    91     ///\todo Please document...
    91     ///
    92     ///
    92     static DistMap *createDistMap(const Graph &G)
    93     static DistMap *createDistMap(const GR &G)
    93     {
    94     {
    94       return new DistMap(G);
    95       return new DistMap(G);
    95     }
    96     }
    96   };
    97   };
    97   
    98   
   106   ///\ref skeleton::ReadMap::ValueType "ValueType" of the length map.
   107   ///\ref skeleton::ReadMap::ValueType "ValueType" of the length map.
   107   ///
   108   ///
   108   ///It is also possible to change the underlying priority heap.
   109   ///It is also possible to change the underlying priority heap.
   109   ///
   110   ///
   110   ///\param GR The graph type the algorithm runs on. The default value is
   111   ///\param GR The graph type the algorithm runs on. The default value is
   111   ///\ref ListGraph
   112   ///\ref ListGraph. The value of GR is not used directly by %Dijsktra, it
       
   113   ///is only passed to \ref DijkstraDefaultTraits.
   112   ///\param LM This read-only
   114   ///\param LM This read-only
   113   ///EdgeMap
   115   ///EdgeMap
   114   ///determines the
   116   ///determines the
   115   ///lengths of the edges. It is read once for each edge, so the map
   117   ///lengths of the edges. It is read once for each edge, so the map
   116   ///may involve in relatively time consuming process to compute the edge
   118   ///may involve in relatively time consuming process to compute the edge
   117   ///length if it is necessary. The default map type is
   119   ///length if it is necessary. The default map type is
   118   ///\ref skeleton::StaticGraph::EdgeMap "Graph::EdgeMap<int>"
   120   ///\ref skeleton::StaticGraph::EdgeMap "Graph::EdgeMap<int>".
   119   ///\param Heap The heap type used by the %Dijkstra
   121   ///The value of LM is not used directly by %Dijsktra, it
   120   ///algorithm. The default
   122   ///is only passed to \ref DijkstraDefaultTraits.
   121   ///is using \ref BinHeap "binary heap".
   123   ///\param TR Traits class to set various data types used by the algorithm.
       
   124   ///The default traits class is
       
   125   ///\ref DijkstraDefaultTraits<GR,LM> "DijkstraDefaultTraits<GR,LM>".
       
   126   ///See \ref DijkstraDefaultTraits for the documentation of
       
   127   ///a Dijkstra traits class.
   122   ///
   128   ///
   123   ///\author Jacint Szabo and Alpar Juttner
   129   ///\author Jacint Szabo and Alpar Juttner
   124   ///\todo We need a typedef-names should be standardized. (-:
   130   ///\todo We need a typedef-names should be standardized. (-:
   125   ///\todo Type of \c PredMap, \c PredNodeMap and \c DistMap
       
   126   ///should not be fixed. (Problematic to solve).
       
   127 
   131 
   128 #ifdef DOXYGEN
   132 #ifdef DOXYGEN
   129   template <typename GR,
   133   template <typename GR,
   130 	    typename LM,
   134 	    typename LM,
   131 	    typename TR>
   135 	    typename TR>
   136 #endif
   140 #endif
   137   class Dijkstra{
   141   class Dijkstra{
   138   public:
   142   public:
   139     typedef TR Traits;
   143     typedef TR Traits;
   140     ///The type of the underlying graph.
   144     ///The type of the underlying graph.
   141     typedef GR Graph;
   145     typedef typename TR::Graph Graph;
   142     ///\e
   146     ///\e
   143     typedef typename Graph::Node Node;
   147     typedef typename Graph::Node Node;
   144     ///\e
   148     ///\e
   145     typedef typename Graph::NodeIt NodeIt;
   149     typedef typename Graph::NodeIt NodeIt;
   146     ///\e
   150     ///\e
   147     typedef typename Graph::Edge Edge;
   151     typedef typename Graph::Edge Edge;
   148     ///\e
   152     ///\e
   149     typedef typename Graph::OutEdgeIt OutEdgeIt;
   153     typedef typename Graph::OutEdgeIt OutEdgeIt;
   150     
   154     
   151     ///The type of the length of the edges.
   155     ///The type of the length of the edges.
   152     typedef typename LM::ValueType ValueType;
   156     typedef typename TR::LengthMap::ValueType ValueType;
   153     ///The type of the map that stores the edge lengths.
   157     ///The type of the map that stores the edge lengths.
   154     typedef LM LengthMap;
   158     typedef typename TR::LengthMap LengthMap;
   155     ///\brief The type of the map that stores the last
   159     ///\brief The type of the map that stores the last
   156     ///edges of the shortest paths.
   160     ///edges of the shortest paths.
   157     typedef typename TR::PredMap PredMap;
   161     typedef typename TR::PredMap PredMap;
   158     ///\brief The type of the map that stores the last but one
   162     ///\brief The type of the map that stores the last but one
   159     ///nodes of the shortest paths.
   163     ///nodes of the shortest paths.
   160     typedef typename TR::PredNodeMap PredNodeMap;
   164     typedef typename TR::PredNodeMap PredNodeMap;
   161     ///The type of the map that stores the dists of the nodes.
   165     ///The type of the map that stores the dists of the nodes.
   162     typedef typename TR::DistMap DistMap;
   166     typedef typename TR::DistMap DistMap;
   163 
       
   164     ///The heap type used by the dijkstra algorithm.
   167     ///The heap type used by the dijkstra algorithm.
   165     typedef typename TR::Heap Heap;
   168     typedef typename TR::Heap Heap;
   166 
   169 
   167   private:
   170   private:
   168     /// Pointer to the underlying graph.
   171     /// Pointer to the underlying graph.
   169     const Graph *G;
   172     const Graph *G;
   170     /// Pointer to the length map
   173     /// Pointer to the length map
   171     const LM *length;
   174     const LengthMap *length;
   172     ///Pointer to the map of predecessors edges.
   175     ///Pointer to the map of predecessors edges.
   173     PredMap *predecessor;
   176     PredMap *predecessor;
   174     ///Indicates if \ref predecessor is locally allocated (\c true) or not.
   177     ///Indicates if \ref predecessor is locally allocated (\c true) or not.
   175     bool local_predecessor;
   178     bool local_predecessor;
   176     ///Pointer to the map of predecessors nodes.
   179     ///Pointer to the map of predecessors nodes.
   217 	std::cerr << __FILE__ ":" << __LINE__ <<
   220 	std::cerr << __FILE__ ":" << __LINE__ <<
   218 	  ": error: Special maps should be manually created" << std::endl;
   221 	  ": error: Special maps should be manually created" << std::endl;
   219 	exit(1);
   222 	exit(1);
   220       }
   223       }
   221     };
   224     };
   222     ///\ref named-templ-param "Named parameter" for setting PredMap's type
   225     ///\ref named-templ-param "Named parameter" for setting PredMap type
       
   226 
       
   227     ///\ingroup flowalgs 
       
   228     ///\ref named-templ-param "Named parameter" for setting PredMap type
   223     template <class T>
   229     template <class T>
   224     class SetPredMap : public Dijkstra< Graph,
   230     class SetPredMap : public Dijkstra< Graph,
   225 					LengthMap,
   231 					LengthMap,
   226 					SetPredMapTraits<T> > { };
   232 					SetPredMapTraits<T> > { };
   227     
   233     
   235 	std::cerr << __FILE__ ":" << __LINE__ <<
   241 	std::cerr << __FILE__ ":" << __LINE__ <<
   236 	  ": error: Special maps should be manually created" << std::endl;
   242 	  ": error: Special maps should be manually created" << std::endl;
   237 	exit(1);
   243 	exit(1);
   238       }
   244       }
   239     };
   245     };
   240     ///\ref named-templ-param "Named parameter" for setting PredNodeMap's type
   246     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
       
   247 
       
   248     ///\ingroup flowalgs 
       
   249     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
   241     template <class T>
   250     template <class T>
   242     class SetPredNodeMap : public Dijkstra< Graph,
   251     class SetPredNodeMap : public Dijkstra< Graph,
   243 					    LengthMap,
   252 					    LengthMap,
   244 					    SetPredNodeMapTraits<T> > { };
   253 					    SetPredNodeMapTraits<T> > { };
   245     
   254     
   253 	std::cerr << __FILE__ ":" << __LINE__ <<
   262 	std::cerr << __FILE__ ":" << __LINE__ <<
   254 	  ": error: Special maps should be manually created" << std::endl;
   263 	  ": error: Special maps should be manually created" << std::endl;
   255 	exit(1);
   264 	exit(1);
   256       }
   265       }
   257     };
   266     };
   258     ///\ref named-templ-param "Named parameter" for setting DistMap's type
   267     ///\ref named-templ-param "Named parameter" for setting DistMap type
       
   268 
       
   269     ///\ingroup flowalgs 
       
   270     ///\ref named-templ-param "Named parameter" for setting DistMap type
   259     template <class T>
   271     template <class T>
   260     class SetDistMap : public Dijkstra< Graph,
   272     class SetDistMap : public Dijkstra< Graph,
   261 					LengthMap,
   273 					LengthMap,
   262 					SetDistMapTraits<T> > { };
   274 					SetDistMapTraits<T> > { };
   263     
   275     
   264     ///Constructor.
   276     ///Constructor.
   265     
   277     
   266     ///\param _G the graph the algorithm will run on.
   278     ///\param _G the graph the algorithm will run on.
   267     ///\param _length the length map used by the algorithm.
   279     ///\param _length the length map used by the algorithm.
   268     Dijkstra(const Graph& _G, const LM& _length) :
   280     Dijkstra(const Graph& _G, const LengthMap& _length) :
   269       G(&_G), length(&_length),
   281       G(&_G), length(&_length),
   270       predecessor(NULL), local_predecessor(false),
   282       predecessor(NULL), local_predecessor(false),
   271       pred_node(NULL), local_pred_node(false),
   283       pred_node(NULL), local_pred_node(false),
   272       distance(NULL), local_distance(false)
   284       distance(NULL), local_distance(false)
   273     { }
   285     { }
   282 
   294 
   283     ///Sets the length map.
   295     ///Sets the length map.
   284 
   296 
   285     ///Sets the length map.
   297     ///Sets the length map.
   286     ///\return <tt> (*this) </tt>
   298     ///\return <tt> (*this) </tt>
   287     Dijkstra &setLengthMap(const LM &m) 
   299     Dijkstra &setLengthMap(const LengthMap &m) 
   288     {
   300     {
   289       length = &m;
   301       length = &m;
   290       return *this;
   302       return *this;
   291     }
   303     }
   292 
   304 
   347   ///in order to
   359   ///in order to
   348   ///compute the
   360   ///compute the
   349   ///shortest path to each node. The algorithm computes
   361   ///shortest path to each node. The algorithm computes
   350   ///- The shortest path tree.
   362   ///- The shortest path tree.
   351   ///- The distance of each node from the root.
   363   ///- The distance of each node from the root.
   352     
   364   ///\todo heap_map's type could also be in the traits class.
   353     void run(Node s) {
   365     void run(Node s) {
   354       
   366       
   355       init_maps();
   367       init_maps();
   356       
   368       
   357       source = s;
   369       source = s;
   359       for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
   371       for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
   360 	predecessor->set(u,INVALID);
   372 	predecessor->set(u,INVALID);
   361 	pred_node->set(u,INVALID);
   373 	pred_node->set(u,INVALID);
   362       }
   374       }
   363       
   375       
   364       typename GR::template NodeMap<int> heap_map(*G,-1);
   376       typename Graph::template NodeMap<int> heap_map(*G,-1);
   365       
   377       
   366       Heap heap(heap_map);
   378       Heap heap(heap_map);
   367       
   379       
   368       heap.push(s,0); 
   380       heap.push(s,0); 
   369       
   381       
   581     
   593     
   582   };
   594   };
   583   
   595   
   584   ///\e
   596   ///\e
   585 
   597 
   586   ///\e
   598   ///\todo Please document...
   587   ///
   599   ///
   588   template<class GR, class LM>
   600   template<class GR, class LM>
   589   _Dijkstra<DijkstraDefaultTraits<GR,LM> >
   601   _Dijkstra<DijkstraDefaultTraits<GR,LM> >
   590   dijkstra(const GR &g,const LM &l,typename GR::Node s)
   602   dijkstra(const GR &g,const LM &l,typename GR::Node s)
   591   {
   603   {