| 
alpar@255
 | 
     1  | 
// -*- C++ -*-
  | 
| 
alpar@255
 | 
     2  | 
#ifndef HUGO_DIJKSTRA_H
  | 
| 
alpar@255
 | 
     3  | 
#define HUGO_DIJKSTRA_H
  | 
| 
alpar@255
 | 
     4  | 
  | 
| 
klao@491
 | 
     5  | 
///\ingroup galgs
  | 
| 
alpar@255
 | 
     6  | 
///\file
  | 
| 
alpar@255
 | 
     7  | 
///\brief Dijkstra algorithm.
  | 
| 
alpar@255
 | 
     8  | 
  | 
| 
klao@258
 | 
     9  | 
#include <bin_heap.h>
  | 
| 
alpar@257
 | 
    10  | 
#include <invalid.h>
  | 
| 
alpar@255
 | 
    11  | 
  | 
| 
alpar@255
 | 
    12  | 
namespace hugo {
 | 
| 
jacint@385
 | 
    13  | 
  | 
| 
alpar@430
 | 
    14  | 
/// \addtogroup galgs
  | 
| 
alpar@430
 | 
    15  | 
/// @{
 | 
| 
alpar@430
 | 
    16  | 
  | 
| 
alpar@255
 | 
    17  | 
  ///%Dijkstra algorithm class.
  | 
| 
alpar@255
 | 
    18  | 
  | 
| 
alpar@255
 | 
    19  | 
  ///This class provides an efficient implementation of %Dijkstra algorithm.
  | 
| 
alpar@255
 | 
    20  | 
  ///The edge lengths are passed to the algorithm using a
  | 
| 
alpar@255
 | 
    21  | 
  ///\ref ReadMapSkeleton "readable map",
  | 
| 
alpar@255
 | 
    22  | 
  ///so it is easy to change it to any kind of length.
  | 
| 
alpar@255
 | 
    23  | 
  ///
  | 
| 
alpar@255
 | 
    24  | 
  ///The type of the length is determined by the \c ValueType of the length map.
  | 
| 
alpar@255
 | 
    25  | 
  ///
  | 
| 
alpar@255
 | 
    26  | 
  ///It is also possible to change the underlying priority heap.
  | 
| 
alpar@255
 | 
    27  | 
  ///
  | 
| 
jacint@385
 | 
    28  | 
  ///\param Graph The graph type the algorithm runs on.
  | 
| 
jacint@385
 | 
    29  | 
  ///\param LengthMap This read-only
  | 
| 
jacint@385
 | 
    30  | 
  ///EdgeMap
  | 
| 
jacint@385
 | 
    31  | 
  ///determines the
  | 
| 
jacint@385
 | 
    32  | 
  ///lengths of the edges. It is read once for each edge, so the map
  | 
| 
jacint@385
 | 
    33  | 
  ///may involve in relatively time consuming process to compute the edge
  | 
| 
jacint@385
 | 
    34  | 
  ///length if it is necessary. The default map type is
  | 
| 
jacint@385
 | 
    35  | 
  ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
  | 
| 
jacint@385
 | 
    36  | 
  ///\param Heap The heap type used by the %Dijkstra
  | 
| 
jacint@385
 | 
    37  | 
  ///algorithm. The default
  | 
| 
jacint@385
 | 
    38  | 
  ///is using \ref BinHeap "binary heap".
  | 
| 
alpar@456
 | 
    39  | 
  ///
  | 
| 
alpar@456
 | 
    40  | 
  ///\author Jacint Szabo
  | 
| 
alpar@255
 | 
    41  | 
#ifdef DOXYGEN
  | 
| 
alpar@255
 | 
    42  | 
  template <typename Graph,
  | 
| 
alpar@255
 | 
    43  | 
	    typename LengthMap,
  | 
| 
alpar@255
 | 
    44  | 
	    typename Heap>
  | 
| 
alpar@255
 | 
    45  | 
#else
  | 
| 
alpar@255
 | 
    46  | 
  template <typename Graph,
  | 
| 
marci@433
 | 
    47  | 
	    typename LengthMap=typename Graph::template EdgeMap<int>,
  | 
| 
alpar@255
 | 
    48  | 
	    template <class,class,class> class Heap = BinHeap >
  | 
| 
alpar@255
 | 
    49  | 
#endif
  | 
| 
alpar@255
 | 
    50  | 
  class Dijkstra{
 | 
| 
alpar@255
 | 
    51  | 
  public:
  | 
| 
alpar@255
 | 
    52  | 
    typedef typename Graph::Node Node;
  | 
| 
alpar@255
 | 
    53  | 
    typedef typename Graph::NodeIt NodeIt;
  | 
| 
alpar@255
 | 
    54  | 
    typedef typename Graph::Edge Edge;
  | 
| 
alpar@255
 | 
    55  | 
    typedef typename Graph::OutEdgeIt OutEdgeIt;
  | 
| 
alpar@255
 | 
    56  | 
    
  | 
| 
alpar@255
 | 
    57  | 
    typedef typename LengthMap::ValueType ValueType;
  | 
| 
marci@433
 | 
    58  | 
    typedef typename Graph::template NodeMap<Edge> PredMap;
  | 
| 
marci@433
 | 
    59  | 
    typedef typename Graph::template NodeMap<Node> PredNodeMap;
  | 
| 
marci@433
 | 
    60  | 
    typedef typename Graph::template NodeMap<ValueType> DistMap;
  | 
| 
alpar@255
 | 
    61  | 
  | 
| 
alpar@255
 | 
    62  | 
  private:
  | 
| 
alpar@255
 | 
    63  | 
    const Graph& G;
  | 
| 
alpar@255
 | 
    64  | 
    const LengthMap& length;
  | 
| 
alpar@255
 | 
    65  | 
    PredMap predecessor;
  | 
| 
alpar@255
 | 
    66  | 
    PredNodeMap pred_node;
  | 
| 
alpar@255
 | 
    67  | 
    DistMap distance;
  | 
| 
alpar@255
 | 
    68  | 
    
  | 
| 
alpar@255
 | 
    69  | 
  public :
  | 
| 
alpar@255
 | 
    70  | 
    
  | 
| 
marci@459
 | 
    71  | 
    Dijkstra(const Graph& _G, const LengthMap& _length) :
  | 
| 
alpar@255
 | 
    72  | 
      G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
 | 
| 
alpar@255
 | 
    73  | 
    
  | 
| 
alpar@255
 | 
    74  | 
    void run(Node s);
  | 
| 
alpar@255
 | 
    75  | 
    
  | 
| 
jacint@385
 | 
    76  | 
    ///The distance of a node from the root.
  | 
| 
alpar@255
 | 
    77  | 
  | 
| 
jacint@385
 | 
    78  | 
    ///Returns the distance of a node from the root.
  | 
| 
alpar@255
 | 
    79  | 
    ///\pre \ref run() must be called before using this function.
  | 
| 
jacint@385
 | 
    80  | 
    ///\warning If node \c v in unreachable from the root the return value
  | 
| 
alpar@255
 | 
    81  | 
    ///of this funcion is undefined.
  | 
| 
alpar@255
 | 
    82  | 
    ValueType dist(Node v) const { return distance[v]; }
 | 
| 
jacint@373
 | 
    83  | 
  | 
| 
jacint@385
 | 
    84  | 
    ///Returns the previous edge of the shortest path tree.
  | 
| 
alpar@255
 | 
    85  | 
  | 
| 
jacint@385
 | 
    86  | 
    ///For a node \c v it returns the previous edge of the shortest path tree,
  | 
| 
jacint@385
 | 
    87  | 
    ///i.e. it returns the last edge from a shortest path from the root to \c
  | 
| 
jacint@385
 | 
    88  | 
    ///v. It is INVALID if \c v is unreachable from the root or if \c v=s. The
  | 
| 
jacint@385
 | 
    89  | 
    ///shortest path tree used here is equal to the shortest path tree used in
  | 
| 
jacint@385
 | 
    90  | 
    ///\ref predNode(Node v).  \pre \ref run() must be called before using
  | 
| 
jacint@385
 | 
    91  | 
    ///this function.
  | 
| 
alpar@255
 | 
    92  | 
    Edge pred(Node v) const { return predecessor[v]; }
 | 
| 
jacint@373
 | 
    93  | 
  | 
| 
jacint@385
 | 
    94  | 
    ///Returns the previous node of the shortest path tree.
  | 
| 
alpar@255
 | 
    95  | 
  | 
| 
jacint@385
 | 
    96  | 
    ///For a node \c v it returns the previous node of the shortest path tree,
  | 
| 
jacint@385
 | 
    97  | 
    ///i.e. it returns the last but one node from a shortest path from the
  | 
| 
jacint@385
 | 
    98  | 
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
  | 
| 
jacint@385
 | 
    99  | 
    ///\c v=s. The shortest path tree used here is equal to the shortest path
  | 
| 
jacint@385
 | 
   100  | 
    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
  | 
| 
jacint@385
 | 
   101  | 
    ///using this function.
  | 
| 
alpar@255
 | 
   102  | 
    Node predNode(Node v) const { return pred_node[v]; }
 | 
| 
alpar@255
 | 
   103  | 
    
  | 
| 
alpar@255
 | 
   104  | 
    ///Returns a reference to the NodeMap of distances.
  | 
| 
alpar@255
 | 
   105  | 
  | 
| 
jacint@385
 | 
   106  | 
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
  | 
| 
jacint@385
 | 
   107  | 
    ///be called before using this function.
  | 
| 
alpar@255
 | 
   108  | 
    const DistMap &distMap() const { return distance;}
 | 
| 
jacint@385
 | 
   109  | 
 
  | 
| 
alpar@255
 | 
   110  | 
    ///Returns a reference to the shortest path tree map.
  | 
| 
alpar@255
 | 
   111  | 
  | 
| 
alpar@255
 | 
   112  | 
    ///Returns a reference to the NodeMap of the edges of the
  | 
| 
alpar@255
 | 
   113  | 
    ///shortest path tree.
  | 
| 
alpar@255
 | 
   114  | 
    ///\pre \ref run() must be called before using this function.
  | 
| 
alpar@255
 | 
   115  | 
    const PredMap &predMap() const { return predecessor;}
 | 
| 
jacint@385
 | 
   116  | 
 
  | 
| 
jacint@385
 | 
   117  | 
    ///Returns a reference to the map of nodes of shortest paths.
  | 
| 
alpar@255
 | 
   118  | 
  | 
| 
alpar@255
 | 
   119  | 
    ///Returns a reference to the NodeMap of the last but one nodes of the
  | 
| 
jacint@385
 | 
   120  | 
    ///shortest path tree.
  | 
| 
alpar@255
 | 
   121  | 
    ///\pre \ref run() must be called before using this function.
  | 
| 
alpar@255
 | 
   122  | 
    const PredNodeMap &predNodeMap() const { return pred_node;}
 | 
| 
alpar@255
 | 
   123  | 
  | 
| 
jacint@385
 | 
   124  | 
    ///Checks if a node is reachable from the root.
  | 
| 
alpar@255
 | 
   125  | 
  | 
| 
jacint@385
 | 
   126  | 
    ///Returns \c true if \c v is reachable from the root.
  | 
| 
jacint@385
 | 
   127  | 
    ///\warning the root node is reported to be unreached!
  | 
| 
alpar@255
 | 
   128  | 
    ///\todo Is this what we want?
  | 
| 
alpar@255
 | 
   129  | 
    ///\pre \ref run() must be called before using this function.
  | 
| 
jacint@385
 | 
   130  | 
    ///
  | 
| 
alpar@255
 | 
   131  | 
    bool reached(Node v) { return G.valid(predecessor[v]); }
 | 
| 
alpar@255
 | 
   132  | 
    
  | 
| 
alpar@255
 | 
   133  | 
  };
  | 
| 
alpar@255
 | 
   134  | 
  
  | 
| 
alpar@255
 | 
   135  | 
  | 
| 
alpar@255
 | 
   136  | 
  // **********************************************************************
  | 
| 
alpar@255
 | 
   137  | 
  //  IMPLEMENTATIONS
  | 
| 
alpar@255
 | 
   138  | 
  // **********************************************************************
  | 
| 
alpar@255
 | 
   139  | 
  | 
| 
jacint@385
 | 
   140  | 
  ///Runs %Dijkstra algorithm from node the root.
  | 
| 
alpar@255
 | 
   141  | 
  | 
| 
jacint@385
 | 
   142  | 
  ///This method runs the %Dijkstra algorithm from a root node \c s
  | 
| 
jacint@385
 | 
   143  | 
  ///in order to
  | 
| 
jacint@385
 | 
   144  | 
  ///compute the
  | 
| 
jacint@385
 | 
   145  | 
  ///shortest path to each node. The algorithm computes
  | 
| 
jacint@385
 | 
   146  | 
  ///- The shortest path tree.
  | 
| 
jacint@385
 | 
   147  | 
  ///- The distance of each node from the root.
  | 
| 
alpar@255
 | 
   148  | 
  template <typename Graph, typename LengthMap,
  | 
| 
alpar@255
 | 
   149  | 
	    template<class,class,class> class Heap >
  | 
| 
alpar@255
 | 
   150  | 
  void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
 | 
| 
alpar@255
 | 
   151  | 
    
  | 
| 
alpar@255
 | 
   152  | 
    NodeIt u;
  | 
| 
alpar@255
 | 
   153  | 
    for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
 | 
| 
alpar@255
 | 
   154  | 
      predecessor.set(u,INVALID);
  | 
| 
alpar@255
 | 
   155  | 
      pred_node.set(u,INVALID);
  | 
| 
alpar@255
 | 
   156  | 
    }
  | 
| 
alpar@255
 | 
   157  | 
    
  | 
| 
marci@433
 | 
   158  | 
    typename Graph::template NodeMap<int> heap_map(G,-1);
  | 
| 
alpar@255
 | 
   159  | 
    
  | 
| 
marci@433
 | 
   160  | 
    Heap<Node, ValueType, typename Graph::template NodeMap<int> > 
  | 
| 
marci@433
 | 
   161  | 
      heap(heap_map);
  | 
| 
jacint@385
 | 
   162  | 
    
  | 
| 
alpar@255
 | 
   163  | 
    heap.push(s,0); 
  | 
| 
alpar@255
 | 
   164  | 
    
  | 
| 
jacint@385
 | 
   165  | 
      while ( !heap.empty() ) {
 | 
| 
alpar@255
 | 
   166  | 
	
  | 
| 
jacint@385
 | 
   167  | 
	Node v=heap.top(); 
  | 
| 
jacint@385
 | 
   168  | 
	ValueType oldvalue=heap[v];
  | 
| 
jacint@385
 | 
   169  | 
	heap.pop();
  | 
| 
jacint@385
 | 
   170  | 
	distance.set(v, oldvalue);
  | 
| 
jacint@385
 | 
   171  | 
	
  | 
| 
jacint@385
 | 
   172  | 
	{ //FIXME this bracket is for e to be local
 | 
| 
jacint@385
 | 
   173  | 
	  OutEdgeIt e;
  | 
| 
jacint@385
 | 
   174  | 
	for(G.first(e, v);
  | 
| 
jacint@385
 | 
   175  | 
	    G.valid(e); G.next(e)) {
 | 
| 
marci@421
 | 
   176  | 
	  Node w=G.bNode(e); 
  | 
| 
alpar@255
 | 
   177  | 
	  
  | 
| 
alpar@255
 | 
   178  | 
	  switch(heap.state(w)) {
 | 
| 
alpar@255
 | 
   179  | 
	  case heap.PRE_HEAP:
  | 
| 
alpar@255
 | 
   180  | 
	    heap.push(w,oldvalue+length[e]); 
  | 
| 
alpar@255
 | 
   181  | 
	    predecessor.set(w,e);
  | 
| 
alpar@255
 | 
   182  | 
	    pred_node.set(w,v);
  | 
| 
alpar@255
 | 
   183  | 
	    break;
  | 
| 
alpar@255
 | 
   184  | 
	  case heap.IN_HEAP:
  | 
| 
alpar@255
 | 
   185  | 
	    if ( oldvalue+length[e] < heap[w] ) {
 | 
| 
alpar@255
 | 
   186  | 
	      heap.decrease(w, oldvalue+length[e]); 
  | 
| 
alpar@255
 | 
   187  | 
	      predecessor.set(w,e);
  | 
| 
alpar@255
 | 
   188  | 
	      pred_node.set(w,v);
  | 
| 
alpar@255
 | 
   189  | 
	    }
  | 
| 
alpar@255
 | 
   190  | 
	    break;
  | 
| 
alpar@255
 | 
   191  | 
	  case heap.POST_HEAP:
  | 
| 
alpar@255
 | 
   192  | 
	    break;
  | 
| 
alpar@255
 | 
   193  | 
	  }
  | 
| 
alpar@255
 | 
   194  | 
	}
  | 
| 
jacint@385
 | 
   195  | 
      } //FIXME tis bracket
  | 
| 
jacint@385
 | 
   196  | 
      }
  | 
| 
alpar@255
 | 
   197  | 
  }
  | 
| 
alpar@430
 | 
   198  | 
  | 
| 
alpar@430
 | 
   199  | 
/// @}
  | 
| 
alpar@255
 | 
   200  | 
  
  | 
| 
alpar@255
 | 
   201  | 
} //END OF NAMESPACE HUGO
  | 
| 
alpar@255
 | 
   202  | 
  | 
| 
alpar@255
 | 
   203  | 
#endif
  | 
| 
alpar@255
 | 
   204  | 
  | 
| 
alpar@255
 | 
   205  | 
  |