lemon/dijkstra.h
author deba
Thu, 20 Mar 2008 16:25:47 +0000
changeset 2596 9c00e972cdfd
parent 2538 7bdd328de87a
child 2618 6aa6fcaeaea5
permissions -rw-r--r--
Back porting commit 81563e019fa4
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@2553
     5
 * Copyright (C) 2003-2008
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
alpar@921
    19
#ifndef LEMON_DIJKSTRA_H
alpar@921
    20
#define LEMON_DIJKSTRA_H
alpar@255
    21
deba@2376
    22
///\ingroup shortest_path
alpar@255
    23
///\file
alpar@255
    24
///\brief Dijkstra algorithm.
alpar@1283
    25
///
alpar@255
    26
alpar@953
    27
#include <lemon/list_graph.h>
alpar@921
    28
#include <lemon/bin_heap.h>
deba@2335
    29
#include <lemon/bits/path_dump.h>
deba@1993
    30
#include <lemon/bits/invalid.h>
alpar@1119
    31
#include <lemon/error.h>
alpar@1119
    32
#include <lemon/maps.h>
alpar@255
    33
deba@2335
    34
alpar@921
    35
namespace lemon {
jacint@385
    36
deba@2537
    37
  /// \brief Default OperationTraits for the Dijkstra algorithm class.
deba@2537
    38
  ///  
deba@2537
    39
  /// It defines all computational operations and constants which are
deba@2537
    40
  /// used in the Dijkstra algorithm.
deba@2537
    41
  template <typename Value>
deba@2537
    42
  struct DijkstraDefaultOperationTraits {
deba@2537
    43
    /// \brief Gives back the zero value of the type.
deba@2537
    44
    static Value zero() {
deba@2537
    45
      return static_cast<Value>(0);
deba@2537
    46
    }
deba@2537
    47
    /// \brief Gives back the sum of the given two elements.
deba@2537
    48
    static Value plus(const Value& left, const Value& right) {
deba@2537
    49
      return left + right;
deba@2537
    50
    }
deba@2537
    51
    /// \brief Gives back true only if the first value less than the second.
deba@2537
    52
    static bool less(const Value& left, const Value& right) {
deba@2537
    53
      return left < right;
deba@2537
    54
    }
deba@2537
    55
  };
deba@2537
    56
deba@2537
    57
  /// \brief Widest path OperationTraits for the Dijkstra algorithm class.
deba@2537
    58
  ///  
deba@2537
    59
  /// It defines all computational operations and constants which are
deba@2537
    60
  /// used in the Dijkstra algorithm for widest path computation.
deba@2537
    61
  template <typename Value>
deba@2537
    62
  struct DijkstraWidestPathOperationTraits {
deba@2537
    63
    /// \brief Gives back the maximum value of the type.
deba@2537
    64
    static Value zero() {
deba@2537
    65
      return std::numeric_limits<Value>::max();
deba@2537
    66
    }
deba@2538
    67
    /// \brief Gives back the minimum of the given two elements.
deba@2537
    68
    static Value plus(const Value& left, const Value& right) {
deba@2537
    69
      return std::min(left, right);
deba@2537
    70
    }
deba@2537
    71
    /// \brief Gives back true only if the first value less than the second.
deba@2537
    72
    static bool less(const Value& left, const Value& right) {
deba@2537
    73
      return left < right;
deba@2537
    74
    }
deba@2537
    75
  };
alpar@1151
    76
  
alpar@954
    77
  ///Default traits class of Dijkstra class.
alpar@954
    78
alpar@954
    79
  ///Default traits class of Dijkstra class.
alpar@954
    80
  ///\param GR Graph type.
alpar@954
    81
  ///\param LM Type of length map.
alpar@953
    82
  template<class GR, class LM>
alpar@953
    83
  struct DijkstraDefaultTraits
alpar@953
    84
  {
alpar@954
    85
    ///The graph type the algorithm runs on. 
alpar@953
    86
    typedef GR Graph;
alpar@953
    87
    ///The type of the map that stores the edge lengths.
alpar@953
    88
hegyi@1124
    89
    ///The type of the map that stores the edge lengths.
alpar@2260
    90
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
alpar@953
    91
    typedef LM LengthMap;
alpar@954
    92
    //The type of the length of the edges.
alpar@987
    93
    typedef typename LM::Value Value;
deba@2537
    94
    /// Operation traits for Dijkstra algorithm.
deba@2537
    95
deba@2537
    96
    /// It defines the used operation by the algorithm.
deba@2537
    97
    /// \see DijkstraDefaultOperationTraits
deba@2537
    98
    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
deba@1721
    99
    /// The cross reference type used by heap.
deba@1721
   100
deba@2537
   101
deba@1721
   102
    /// The cross reference type used by heap.
deba@1721
   103
    /// Usually it is \c Graph::NodeMap<int>.
deba@1721
   104
    typedef typename Graph::template NodeMap<int> HeapCrossRef;
deba@1721
   105
    ///Instantiates a HeapCrossRef.
deba@1721
   106
deba@2537
   107
    ///This function instantiates a \c HeapCrossRef. 
deba@1721
   108
    /// \param G is the graph, to which we would like to define the 
deba@1721
   109
    /// HeapCrossRef.
deba@1721
   110
    static HeapCrossRef *createHeapCrossRef(const GR &G) 
deba@1721
   111
    {
deba@1721
   112
      return new HeapCrossRef(G);
deba@1721
   113
    }
deba@1721
   114
    
alpar@954
   115
    ///The heap type used by Dijkstra algorithm.
alpar@967
   116
alpar@967
   117
    ///The heap type used by Dijkstra algorithm.
alpar@967
   118
    ///
alpar@967
   119
    ///\sa BinHeap
alpar@967
   120
    ///\sa Dijkstra
mqrelly@2263
   121
    typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap;
alpar@953
   122
deba@1721
   123
    static Heap *createHeap(HeapCrossRef& R) 
deba@1721
   124
    {
deba@1721
   125
      return new Heap(R);
deba@1721
   126
    }
deba@1721
   127
alpar@953
   128
    ///\brief The type of the map that stores the last
alpar@953
   129
    ///edges of the shortest paths.
alpar@953
   130
    /// 
hegyi@1124
   131
    ///The type of the map that stores the last
hegyi@1124
   132
    ///edges of the shortest paths.
alpar@2260
   133
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@953
   134
    ///
alpar@954
   135
    typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
alpar@954
   136
    ///Instantiates a PredMap.
alpar@953
   137
 
deba@2537
   138
    ///This function instantiates a \c PredMap. 
hegyi@1123
   139
    ///\param G is the graph, to which we would like to define the PredMap.
alpar@1119
   140
    ///\todo The graph alone may be insufficient for the initialization
alpar@954
   141
    static PredMap *createPredMap(const GR &G) 
alpar@953
   142
    {
alpar@953
   143
      return new PredMap(G);
alpar@953
   144
    }
alpar@1119
   145
alpar@1218
   146
    ///The type of the map that stores whether a nodes is processed.
alpar@1119
   147
 
alpar@1218
   148
    ///The type of the map that stores whether a nodes is processed.
alpar@2260
   149
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1119
   150
    ///By default it is a NullMap.
alpar@1218
   151
    ///\todo If it is set to a real map,
alpar@1218
   152
    ///Dijkstra::processed() should read this.
alpar@1119
   153
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   154
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
   155
    ///Instantiates a ProcessedMap.
alpar@1119
   156
 
deba@2537
   157
    ///This function instantiates a \c ProcessedMap. 
alpar@1536
   158
    ///\param g is the graph, to which
deba@2537
   159
    ///we would like to define the \c ProcessedMap
alpar@1536
   160
#ifdef DOXYGEN
alpar@1536
   161
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
   162
#else
alpar@1366
   163
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
   164
#endif
alpar@1119
   165
    {
alpar@1218
   166
      return new ProcessedMap();
alpar@1119
   167
    }
alpar@953
   168
    ///The type of the map that stores the dists of the nodes.
alpar@953
   169
 
hegyi@1124
   170
    ///The type of the map that stores the dists of the nodes.
alpar@2260
   171
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@953
   172
    ///
alpar@987
   173
    typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
alpar@954
   174
    ///Instantiates a DistMap.
alpar@953
   175
 
hegyi@1123
   176
    ///This function instantiates a \ref DistMap. 
hegyi@1123
   177
    ///\param G is the graph, to which we would like to define the \ref DistMap
alpar@954
   178
    static DistMap *createDistMap(const GR &G)
alpar@953
   179
    {
alpar@953
   180
      return new DistMap(G);
alpar@953
   181
    }
alpar@953
   182
  };
alpar@953
   183
  
alpar@255
   184
  ///%Dijkstra algorithm class.
alpar@1125
   185
  
deba@2376
   186
  /// \ingroup shortest_path
alpar@255
   187
  ///This class provides an efficient implementation of %Dijkstra algorithm.
alpar@255
   188
  ///The edge lengths are passed to the algorithm using a
alpar@2260
   189
  ///\ref concepts::ReadMap "ReadMap",
alpar@255
   190
  ///so it is easy to change it to any kind of length.
alpar@255
   191
  ///
alpar@880
   192
  ///The type of the length is determined by the
alpar@2260
   193
  ///\ref concepts::ReadMap::Value "Value" of the length map.
alpar@255
   194
  ///
alpar@255
   195
  ///It is also possible to change the underlying priority heap.
alpar@255
   196
  ///
alpar@1218
   197
  ///\param GR The graph type the algorithm runs on. The default value
alpar@1218
   198
  ///is \ref ListGraph. The value of GR is not used directly by
alpar@1218
   199
  ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits.
alpar@1218
   200
  ///\param LM This read-only EdgeMap determines the lengths of the
alpar@1218
   201
  ///edges. It is read once for each edge, so the map may involve in
alpar@1218
   202
  ///relatively time consuming process to compute the edge length if
alpar@1218
   203
  ///it is necessary. The default map type is \ref
alpar@2260
   204
  ///concepts::Graph::EdgeMap "Graph::EdgeMap<int>".  The value
alpar@1218
   205
  ///of LM is not used directly by Dijkstra, it is only passed to \ref
alpar@1218
   206
  ///DijkstraDefaultTraits.  \param TR Traits class to set
alpar@1218
   207
  ///various data types used by the algorithm.  The default traits
alpar@1218
   208
  ///class is \ref DijkstraDefaultTraits
alpar@1218
   209
  ///"DijkstraDefaultTraits<GR,LM>".  See \ref
alpar@1218
   210
  ///DijkstraDefaultTraits for the documentation of a Dijkstra traits
alpar@1218
   211
  ///class.
alpar@456
   212
  ///
alpar@689
   213
  ///\author Jacint Szabo and Alpar Juttner
alpar@584
   214
alpar@255
   215
#ifdef DOXYGEN
deba@2537
   216
  template <typename GR, typename LM, typename TR>
alpar@255
   217
#else
alpar@953
   218
  template <typename GR=ListGraph,
alpar@584
   219
	    typename LM=typename GR::template EdgeMap<int>,
alpar@953
   220
	    typename TR=DijkstraDefaultTraits<GR,LM> >
alpar@255
   221
#endif
alpar@1116
   222
  class Dijkstra {
alpar@255
   223
  public:
alpar@1125
   224
    /**
alpar@1125
   225
     * \brief \ref Exception for uninitialized parameters.
alpar@1125
   226
     *
alpar@1125
   227
     * This error represents problems in the initialization
alpar@1125
   228
     * of the parameters of the algorithms.
alpar@1125
   229
     */
alpar@1125
   230
    class UninitializedParameter : public lemon::UninitializedParameter {
alpar@1125
   231
    public:
alpar@2151
   232
      virtual const char* what() const throw() {
alpar@1218
   233
	return "lemon::Dijkstra::UninitializedParameter";
alpar@1125
   234
      }
alpar@1125
   235
    };
alpar@1119
   236
alpar@953
   237
    typedef TR Traits;
alpar@584
   238
    ///The type of the underlying graph.
alpar@954
   239
    typedef typename TR::Graph Graph;
alpar@911
   240
    ///\e
alpar@255
   241
    typedef typename Graph::Node Node;
alpar@911
   242
    ///\e
alpar@255
   243
    typedef typename Graph::NodeIt NodeIt;
alpar@911
   244
    ///\e
alpar@255
   245
    typedef typename Graph::Edge Edge;
alpar@911
   246
    ///\e
alpar@255
   247
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@255
   248
    
alpar@584
   249
    ///The type of the length of the edges.
alpar@987
   250
    typedef typename TR::LengthMap::Value Value;
alpar@693
   251
    ///The type of the map that stores the edge lengths.
alpar@954
   252
    typedef typename TR::LengthMap LengthMap;
alpar@693
   253
    ///\brief The type of the map that stores the last
alpar@584
   254
    ///edges of the shortest paths.
alpar@953
   255
    typedef typename TR::PredMap PredMap;
alpar@1218
   256
    ///The type of the map indicating if a node is processed.
alpar@1218
   257
    typedef typename TR::ProcessedMap ProcessedMap;
alpar@693
   258
    ///The type of the map that stores the dists of the nodes.
alpar@953
   259
    typedef typename TR::DistMap DistMap;
deba@1721
   260
    ///The cross reference type used for the current heap.
deba@1721
   261
    typedef typename TR::HeapCrossRef HeapCrossRef;
alpar@953
   262
    ///The heap type used by the dijkstra algorithm.
alpar@953
   263
    typedef typename TR::Heap Heap;
deba@2537
   264
    ///The operation traits.
deba@2537
   265
    typedef typename TR::OperationTraits OperationTraits;
alpar@255
   266
  private:
alpar@802
   267
    /// Pointer to the underlying graph.
alpar@688
   268
    const Graph *G;
alpar@802
   269
    /// Pointer to the length map
alpar@954
   270
    const LengthMap *length;
alpar@802
   271
    ///Pointer to the map of predecessors edges.
alpar@1119
   272
    PredMap *_pred;
alpar@1119
   273
    ///Indicates if \ref _pred is locally allocated (\c true) or not.
alpar@1119
   274
    bool local_pred;
alpar@802
   275
    ///Pointer to the map of distances.
alpar@1130
   276
    DistMap *_dist;
alpar@1130
   277
    ///Indicates if \ref _dist is locally allocated (\c true) or not.
alpar@1130
   278
    bool local_dist;
alpar@1218
   279
    ///Pointer to the map of processed status of the nodes.
alpar@1218
   280
    ProcessedMap *_processed;
alpar@1218
   281
    ///Indicates if \ref _processed is locally allocated (\c true) or not.
alpar@1218
   282
    bool local_processed;
deba@1721
   283
    ///Pointer to the heap cross references.
deba@1721
   284
    HeapCrossRef *_heap_cross_ref;
deba@1721
   285
    ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
deba@1721
   286
    bool local_heap_cross_ref;
deba@1721
   287
    ///Pointer to the heap.
deba@1721
   288
    Heap *_heap;
deba@1721
   289
    ///Indicates if \ref _heap is locally allocated (\c true) or not.
deba@1721
   290
    bool local_heap;
alpar@688
   291
alpar@1128
   292
    ///Creates the maps if necessary.
alpar@688
   293
    
alpar@688
   294
    ///\todo Better memory allocation (instead of new).
alpar@1128
   295
    void create_maps() 
alpar@688
   296
    {
alpar@1119
   297
      if(!_pred) {
alpar@1119
   298
	local_pred = true;
alpar@1119
   299
	_pred = Traits::createPredMap(*G);
alpar@688
   300
      }
alpar@1130
   301
      if(!_dist) {
alpar@1130
   302
	local_dist = true;
alpar@1130
   303
	_dist = Traits::createDistMap(*G);
alpar@688
   304
      }
alpar@1218
   305
      if(!_processed) {
alpar@1218
   306
	local_processed = true;
alpar@1218
   307
	_processed = Traits::createProcessedMap(*G);
alpar@1119
   308
      }
deba@1721
   309
      if (!_heap_cross_ref) {
deba@1721
   310
	local_heap_cross_ref = true;
deba@1721
   311
	_heap_cross_ref = Traits::createHeapCrossRef(*G);
deba@1721
   312
      }
deba@1721
   313
      if (!_heap) {
deba@1721
   314
	local_heap = true;
deba@1721
   315
	_heap = Traits::createHeap(*_heap_cross_ref);
deba@1721
   316
      }
alpar@688
   317
    }
alpar@255
   318
    
alpar@255
   319
  public :
deba@1710
   320
deba@1710
   321
    typedef Dijkstra Create;
alpar@1116
   322
 
alpar@1128
   323
    ///\name Named template parameters
alpar@1128
   324
alpar@1128
   325
    ///@{
alpar@1128
   326
alpar@953
   327
    template <class T>
alpar@1116
   328
    struct DefPredMapTraits : public Traits {
alpar@953
   329
      typedef T PredMap;
klao@2010
   330
      static PredMap *createPredMap(const Graph &)
alpar@953
   331
      {
alpar@1126
   332
	throw UninitializedParameter();
alpar@953
   333
      }
alpar@953
   334
    };
alpar@954
   335
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@954
   336
alpar@954
   337
    ///\ref named-templ-param "Named parameter" for setting PredMap type
alpar@1043
   338
    ///
alpar@953
   339
    template <class T>
deba@1709
   340
    struct DefPredMap 
deba@1709
   341
      : public Dijkstra< Graph,	LengthMap, DefPredMapTraits<T> > {
deba@1709
   342
      typedef Dijkstra< Graph,	LengthMap, DefPredMapTraits<T> > Create;
deba@1709
   343
    };
alpar@953
   344
    
alpar@953
   345
    template <class T>
alpar@1116
   346
    struct DefDistMapTraits : public Traits {
alpar@953
   347
      typedef T DistMap;
klao@2010
   348
      static DistMap *createDistMap(const Graph &)
alpar@953
   349
      {
alpar@1126
   350
	throw UninitializedParameter();
alpar@953
   351
      }
alpar@953
   352
    };
alpar@954
   353
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@954
   354
alpar@954
   355
    ///\ref named-templ-param "Named parameter" for setting DistMap type
alpar@1043
   356
    ///
alpar@953
   357
    template <class T>
deba@1709
   358
    struct DefDistMap 
deba@1709
   359
      : public Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > { 
deba@1709
   360
      typedef Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > Create;
deba@1709
   361
    };
alpar@953
   362
    
alpar@1128
   363
    template <class T>
alpar@1218
   364
    struct DefProcessedMapTraits : public Traits {
alpar@1218
   365
      typedef T ProcessedMap;
alpar@1218
   366
      static ProcessedMap *createProcessedMap(const Graph &G) 
alpar@1128
   367
      {
alpar@1128
   368
	throw UninitializedParameter();
alpar@1128
   369
      }
alpar@1128
   370
    };
alpar@1218
   371
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
alpar@1128
   372
alpar@1218
   373
    ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
alpar@1128
   374
    ///
alpar@1128
   375
    template <class T>
deba@1709
   376
    struct DefProcessedMap 
deba@1709
   377
      : public Dijkstra< Graph,	LengthMap, DefProcessedMapTraits<T> > { 
deba@1709
   378
      typedef Dijkstra< Graph,	LengthMap, DefProcessedMapTraits<T> > Create;
deba@1709
   379
    };
alpar@1128
   380
    
alpar@1218
   381
    struct DefGraphProcessedMapTraits : public Traits {
alpar@1218
   382
      typedef typename Graph::template NodeMap<bool> ProcessedMap;
alpar@1218
   383
      static ProcessedMap *createProcessedMap(const Graph &G) 
alpar@1128
   384
      {
alpar@1218
   385
	return new ProcessedMap(G);
alpar@1128
   386
      }
alpar@1128
   387
    };
alpar@1128
   388
    ///\brief \ref named-templ-param "Named parameter"
alpar@1218
   389
    ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
alpar@1128
   390
    ///
alpar@1128
   391
    ///\ref named-templ-param "Named parameter"
alpar@1218
   392
    ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
alpar@1128
   393
    ///If you don't set it explicitely, it will be automatically allocated.
alpar@1128
   394
    template <class T>
deba@1709
   395
    struct DefProcessedMapToBeDefaultMap 
deba@1709
   396
      : public Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> {
deba@1709
   397
      typedef Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> Create;
deba@1709
   398
    };
deba@1721
   399
deba@1721
   400
    template <class H, class CR>
deba@1721
   401
    struct DefHeapTraits : public Traits {
deba@1721
   402
      typedef CR HeapCrossRef;
deba@1721
   403
      typedef H Heap;
deba@1741
   404
      static HeapCrossRef *createHeapCrossRef(const Graph &) {
deba@1741
   405
	throw UninitializedParameter();
deba@1721
   406
      }
deba@1741
   407
      static Heap *createHeap(HeapCrossRef &) 
deba@1721
   408
      {
deba@1741
   409
	throw UninitializedParameter();
deba@1721
   410
      }
deba@1721
   411
    };
deba@2230
   412
    ///\brief \ref named-templ-param "Named parameter" for setting
deba@2230
   413
    ///heap and cross reference type
deba@2230
   414
    ///
deba@1721
   415
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1721
   416
    ///reference type
deba@1721
   417
    ///
deba@1721
   418
    template <class H, class CR = typename Graph::template NodeMap<int> >
deba@1721
   419
    struct DefHeap
deba@1721
   420
      : public Dijkstra< Graph,	LengthMap, DefHeapTraits<H, CR> > { 
deba@1721
   421
      typedef Dijkstra< Graph,	LengthMap, DefHeapTraits<H, CR> > Create;
deba@1721
   422
    };
deba@1741
   423
deba@1741
   424
    template <class H, class CR>
deba@1741
   425
    struct DefStandardHeapTraits : public Traits {
deba@1741
   426
      typedef CR HeapCrossRef;
deba@1741
   427
      typedef H Heap;
deba@1741
   428
      static HeapCrossRef *createHeapCrossRef(const Graph &G) {
deba@1741
   429
	return new HeapCrossRef(G);
deba@1741
   430
      }
deba@1741
   431
      static Heap *createHeap(HeapCrossRef &R) 
deba@1741
   432
      {
deba@1741
   433
	return new Heap(R);
deba@1741
   434
      }
deba@1741
   435
    };
deba@2230
   436
    ///\brief \ref named-templ-param "Named parameter" for setting
deba@2230
   437
    ///heap and cross reference type with automatic allocation
deba@2230
   438
    ///
deba@1741
   439
    ///\ref named-templ-param "Named parameter" for setting heap and cross 
deba@1741
   440
    ///reference type. It can allocate the heap and the cross reference 
deba@1741
   441
    ///object if the cross reference's constructor waits for the graph as 
deba@1741
   442
    ///parameter and the heap's constructor waits for the cross reference.
deba@1741
   443
    template <class H, class CR = typename Graph::template NodeMap<int> >
deba@1741
   444
    struct DefStandardHeap
deba@1741
   445
      : public Dijkstra< Graph,	LengthMap, DefStandardHeapTraits<H, CR> > { 
deba@1741
   446
      typedef Dijkstra< Graph,	LengthMap, DefStandardHeapTraits<H, CR> > 
deba@1741
   447
      Create;
deba@1741
   448
    };
deba@2537
   449
deba@2537
   450
    template <class T>
deba@2537
   451
    struct DefOperationTraitsTraits : public Traits {
deba@2537
   452
      typedef T OperationTraits;
deba@2537
   453
    };
deba@2537
   454
    
deba@2537
   455
    /// \brief \ref named-templ-param "Named parameter" for setting 
deba@2537
   456
    /// OperationTraits type
deba@2537
   457
    ///
deba@2537
   458
    /// \ref named-templ-param "Named parameter" for setting OperationTraits
deba@2537
   459
    /// type
deba@2537
   460
    template <class T>
deba@2537
   461
    struct DefOperationTraits
deba@2537
   462
      : public Dijkstra<Graph, LengthMap, DefOperationTraitsTraits<T> > {
deba@2537
   463
      typedef Dijkstra<Graph, LengthMap, DefOperationTraitsTraits<T> >
deba@2537
   464
      Create;
deba@2537
   465
    };
alpar@1128
   466
    
alpar@1128
   467
    ///@}
alpar@1128
   468
alpar@1128
   469
deba@1710
   470
  protected:
deba@1710
   471
deba@1710
   472
    Dijkstra() {}
deba@1710
   473
alpar@1128
   474
  public:      
alpar@1128
   475
    
alpar@802
   476
    ///Constructor.
alpar@255
   477
    
alpar@802
   478
    ///\param _G the graph the algorithm will run on.
alpar@802
   479
    ///\param _length the length map used by the algorithm.
alpar@954
   480
    Dijkstra(const Graph& _G, const LengthMap& _length) :
alpar@688
   481
      G(&_G), length(&_length),
alpar@1119
   482
      _pred(NULL), local_pred(false),
alpar@1130
   483
      _dist(NULL), local_dist(false),
alpar@1218
   484
      _processed(NULL), local_processed(false),
deba@1721
   485
      _heap_cross_ref(NULL), local_heap_cross_ref(false),
deba@1721
   486
      _heap(NULL), local_heap(false)
alpar@688
   487
    { }
alpar@688
   488
    
alpar@802
   489
    ///Destructor.
alpar@688
   490
    ~Dijkstra() 
alpar@688
   491
    {
alpar@1119
   492
      if(local_pred) delete _pred;
alpar@1130
   493
      if(local_dist) delete _dist;
alpar@1218
   494
      if(local_processed) delete _processed;
deba@1721
   495
      if(local_heap_cross_ref) delete _heap_cross_ref;
deba@1721
   496
      if(local_heap) delete _heap;
alpar@688
   497
    }
alpar@688
   498
alpar@688
   499
    ///Sets the length map.
alpar@688
   500
alpar@688
   501
    ///Sets the length map.
alpar@688
   502
    ///\return <tt> (*this) </tt>
alpar@1116
   503
    Dijkstra &lengthMap(const LengthMap &m) 
alpar@688
   504
    {
alpar@688
   505
      length = &m;
alpar@688
   506
      return *this;
alpar@688
   507
    }
alpar@688
   508
alpar@688
   509
    ///Sets the map storing the predecessor edges.
alpar@688
   510
alpar@688
   511
    ///Sets the map storing the predecessor edges.
alpar@688
   512
    ///If you don't use this function before calling \ref run(),
alpar@688
   513
    ///it will allocate one. The destuctor deallocates this
alpar@688
   514
    ///automatically allocated map, of course.
alpar@688
   515
    ///\return <tt> (*this) </tt>
alpar@1116
   516
    Dijkstra &predMap(PredMap &m) 
alpar@688
   517
    {
alpar@1119
   518
      if(local_pred) {
alpar@1119
   519
	delete _pred;
alpar@1119
   520
	local_pred=false;
alpar@688
   521
      }
alpar@1119
   522
      _pred = &m;
alpar@688
   523
      return *this;
alpar@688
   524
    }
alpar@688
   525
alpar@688
   526
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   527
alpar@688
   528
    ///Sets the map storing the distances calculated by the algorithm.
alpar@688
   529
    ///If you don't use this function before calling \ref run(),
alpar@688
   530
    ///it will allocate one. The destuctor deallocates this
alpar@688
   531
    ///automatically allocated map, of course.
alpar@688
   532
    ///\return <tt> (*this) </tt>
alpar@1116
   533
    Dijkstra &distMap(DistMap &m) 
alpar@688
   534
    {
alpar@1130
   535
      if(local_dist) {
alpar@1130
   536
	delete _dist;
alpar@1130
   537
	local_dist=false;
alpar@688
   538
      }
alpar@1130
   539
      _dist = &m;
alpar@688
   540
      return *this;
alpar@688
   541
    }
alpar@694
   542
deba@1741
   543
    ///Sets the heap and the cross reference used by algorithm.
deba@1741
   544
deba@1741
   545
    ///Sets the heap and the cross reference used by algorithm.
deba@1741
   546
    ///If you don't use this function before calling \ref run(),
deba@1741
   547
    ///it will allocate one. The destuctor deallocates this
deba@1981
   548
    ///automatically allocated heap and cross reference, of course.
deba@1741
   549
    ///\return <tt> (*this) </tt>
deba@2386
   550
    Dijkstra &heap(Heap& hp, HeapCrossRef &cr)
deba@1741
   551
    {
deba@1741
   552
      if(local_heap_cross_ref) {
deba@1741
   553
	delete _heap_cross_ref;
deba@1741
   554
	local_heap_cross_ref=false;
deba@1741
   555
      }
deba@2386
   556
      _heap_cross_ref = &cr;
deba@1741
   557
      if(local_heap) {
deba@1741
   558
	delete _heap;
deba@1741
   559
	local_heap=false;
deba@1741
   560
      }
deba@2386
   561
      _heap = &hp;
deba@1741
   562
      return *this;
deba@1741
   563
    }
deba@1741
   564
alpar@1130
   565
  private:
alpar@1130
   566
    void finalizeNodeData(Node v,Value dst)
alpar@1130
   567
    {
alpar@1218
   568
      _processed->set(v,true);
alpar@1130
   569
      _dist->set(v, dst);
alpar@1130
   570
    }
alpar@1130
   571
alpar@1130
   572
  public:
alpar@2354
   573
alpar@2354
   574
    typedef PredMapPath<Graph, PredMap> Path;
alpar@2354
   575
alpar@1218
   576
    ///\name Execution control
alpar@1128
   577
    ///The simplest way to execute the algorithm is to use
alpar@1156
   578
    ///one of the member functions called \c run(...).
alpar@1128
   579
    ///\n
alpar@1218
   580
    ///If you need more control on the execution,
alpar@1128
   581
    ///first you must call \ref init(), then you can add several source nodes
alpar@1218
   582
    ///with \ref addSource().
alpar@1218
   583
    ///Finally \ref start() will perform the actual path
alpar@1128
   584
    ///computation.
alpar@1128
   585
alpar@1128
   586
    ///@{
alpar@1128
   587
alpar@1128
   588
    ///Initializes the internal data structures.
alpar@1128
   589
alpar@1128
   590
    ///Initializes the internal data structures.
alpar@1128
   591
    ///
alpar@1128
   592
    void init()
alpar@1128
   593
    {
alpar@1128
   594
      create_maps();
deba@1721
   595
      _heap->clear();
alpar@774
   596
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@1119
   597
	_pred->set(u,INVALID);
alpar@1218
   598
	_processed->set(u,false);
deba@1721
   599
	_heap_cross_ref->set(u,Heap::PRE_HEAP);
alpar@694
   600
      }
alpar@1128
   601
    }
alpar@1128
   602
    
alpar@1128
   603
    ///Adds a new source node.
alpar@1128
   604
alpar@1155
   605
    ///Adds a new source node to the priority heap.
alpar@1128
   606
    ///
alpar@1128
   607
    ///The optional second parameter is the initial distance of the node.
alpar@1128
   608
    ///
alpar@1155
   609
    ///It checks if the node has already been added to the heap and
deba@1988
   610
    ///it is pushed to the heap only if either it was not in the heap
deba@1988
   611
    ///or the shortest path found till then is shorter than \c dst.
deba@2537
   612
    void addSource(Node s,Value dst=OperationTraits::zero())
alpar@1128
   613
    {
deba@1721
   614
      if(_heap->state(s) != Heap::IN_HEAP) {
deba@1721
   615
	_heap->push(s,dst);
deba@2537
   616
      } else if(OperationTraits::less((*_heap)[s], dst)) {
deba@1988
   617
	_heap->set(s,dst);
alpar@1155
   618
	_pred->set(s,INVALID);
alpar@1155
   619
      }
alpar@1128
   620
    }
alpar@1128
   621
    
alpar@1155
   622
    ///Processes the next node in the priority heap
alpar@1155
   623
alpar@1155
   624
    ///Processes the next node in the priority heap.
alpar@1155
   625
    ///
alpar@1516
   626
    ///\return The processed node.
alpar@1516
   627
    ///
alpar@1155
   628
    ///\warning The priority heap must not be empty!
alpar@1516
   629
    Node processNextNode()
alpar@1128
   630
    {
deba@1721
   631
      Node v=_heap->top(); 
deba@1721
   632
      Value oldvalue=_heap->prio();
deba@1721
   633
      _heap->pop();
alpar@1130
   634
      finalizeNodeData(v,oldvalue);
alpar@694
   635
      
alpar@1128
   636
      for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
alpar@1128
   637
	Node w=G->target(e); 
deba@1721
   638
	switch(_heap->state(w)) {
alpar@1128
   639
	case Heap::PRE_HEAP:
deba@2537
   640
	  _heap->push(w,OperationTraits::plus(oldvalue, (*length)[e])); 
alpar@1128
   641
	  _pred->set(w,e);
alpar@1128
   642
	  break;
alpar@1128
   643
	case Heap::IN_HEAP:
deba@2537
   644
	  {
deba@2537
   645
	    Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]);
deba@2537
   646
	    if ( OperationTraits::less(newvalue, (*_heap)[w]) ) {
deba@2537
   647
	      _heap->decrease(w, newvalue); 
deba@2537
   648
	      _pred->set(w,e);
deba@2537
   649
	    }
alpar@694
   650
	  }
alpar@1128
   651
	  break;
alpar@1128
   652
	case Heap::POST_HEAP:
alpar@1128
   653
	  break;
alpar@694
   654
	}
alpar@694
   655
      }
alpar@1516
   656
      return v;
alpar@694
   657
    }
alpar@1128
   658
alpar@1665
   659
    ///Next node to be processed.
alpar@1665
   660
    
alpar@1665
   661
    ///Next node to be processed.
alpar@1665
   662
    ///
alpar@1665
   663
    ///\return The next node to be processed or INVALID if the priority heap
alpar@1665
   664
    /// is empty.
deba@1694
   665
    Node nextNode()
alpar@1665
   666
    { 
deba@2439
   667
      return !_heap->empty()?_heap->top():INVALID;
alpar@1665
   668
    }
alpar@1665
   669
 
alpar@1218
   670
    ///\brief Returns \c false if there are nodes
alpar@1218
   671
    ///to be processed in the priority heap
alpar@1155
   672
    ///
alpar@1218
   673
    ///Returns \c false if there are nodes
alpar@1218
   674
    ///to be processed in the priority heap
deba@1721
   675
    bool emptyQueue() { return _heap->empty(); }
alpar@1155
   676
    ///Returns the number of the nodes to be processed in the priority heap
alpar@1155
   677
alpar@1155
   678
    ///Returns the number of the nodes to be processed in the priority heap
alpar@1155
   679
    ///
deba@1721
   680
    int queueSize() { return _heap->size(); }
alpar@1155
   681
    
alpar@1130
   682
    ///Executes the algorithm.
alpar@1128
   683
alpar@1130
   684
    ///Executes the algorithm.
alpar@1128
   685
    ///
alpar@1130
   686
    ///\pre init() must be called and at least one node should be added
alpar@1130
   687
    ///with addSource() before using this function.
alpar@1128
   688
    ///
alpar@1128
   689
    ///This method runs the %Dijkstra algorithm from the root node(s)
alpar@1128
   690
    ///in order to
alpar@1128
   691
    ///compute the
alpar@1128
   692
    ///shortest path to each node. The algorithm computes
alpar@1128
   693
    ///- The shortest path tree.
alpar@1128
   694
    ///- The distance of each node from the root(s).
alpar@1128
   695
    ///
alpar@1128
   696
    void start()
alpar@1128
   697
    {
deba@1721
   698
      while ( !_heap->empty() ) processNextNode();
alpar@1128
   699
    }
alpar@255
   700
    
alpar@1130
   701
    ///Executes the algorithm until \c dest is reached.
alpar@1128
   702
alpar@1130
   703
    ///Executes the algorithm until \c dest is reached.
alpar@1128
   704
    ///
alpar@1130
   705
    ///\pre init() must be called and at least one node should be added
alpar@1130
   706
    ///with addSource() before using this function.
alpar@1128
   707
    ///
alpar@1128
   708
    ///This method runs the %Dijkstra algorithm from the root node(s)
alpar@1128
   709
    ///in order to
alpar@1128
   710
    ///compute the
alpar@1128
   711
    ///shortest path to \c dest. The algorithm computes
alpar@1128
   712
    ///- The shortest path to \c  dest.
alpar@1128
   713
    ///- The distance of \c dest from the root(s).
alpar@1128
   714
    ///
alpar@1128
   715
    void start(Node dest)
alpar@1128
   716
    {
deba@1721
   717
      while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
deba@1721
   718
      if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
alpar@1130
   719
    }
alpar@1130
   720
    
alpar@1130
   721
    ///Executes the algorithm until a condition is met.
alpar@1130
   722
alpar@1130
   723
    ///Executes the algorithm until a condition is met.
alpar@1130
   724
    ///
alpar@1130
   725
    ///\pre init() must be called and at least one node should be added
alpar@1130
   726
    ///with addSource() before using this function.
alpar@1130
   727
    ///
alpar@1130
   728
    ///\param nm must be a bool (or convertible) node map. The algorithm
deba@2443
   729
    ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
deba@2439
   730
    ///
kpeter@2476
   731
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
deba@2439
   732
    ///\c INVALID if no such node was found.
deba@1345
   733
    template<class NodeBoolMap>
deba@2439
   734
    Node start(const NodeBoolMap &nm)
alpar@1130
   735
    {
deba@1721
   736
      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
deba@2439
   737
      if ( _heap->empty() ) return INVALID;
deba@2439
   738
      finalizeNodeData(_heap->top(),_heap->prio());
deba@2439
   739
      return _heap->top();
alpar@1128
   740
    }
alpar@1128
   741
    
alpar@1128
   742
    ///Runs %Dijkstra algorithm from node \c s.
alpar@1128
   743
    
alpar@1128
   744
    ///This method runs the %Dijkstra algorithm from a root node \c s
alpar@1128
   745
    ///in order to
alpar@1128
   746
    ///compute the
alpar@1128
   747
    ///shortest path to each node. The algorithm computes
alpar@1128
   748
    ///- The shortest path tree.
alpar@1128
   749
    ///- The distance of each node from the root.
alpar@1128
   750
    ///
alpar@1128
   751
    ///\note d.run(s) is just a shortcut of the following code.
alpar@1128
   752
    ///\code
alpar@1128
   753
    ///  d.init();
alpar@1128
   754
    ///  d.addSource(s);
alpar@1128
   755
    ///  d.start();
alpar@1128
   756
    ///\endcode
alpar@1128
   757
    void run(Node s) {
alpar@1128
   758
      init();
alpar@1128
   759
      addSource(s);
alpar@1128
   760
      start();
alpar@1128
   761
    }
alpar@1128
   762
    
alpar@1130
   763
    ///Finds the shortest path between \c s and \c t.
alpar@1130
   764
    
alpar@1130
   765
    ///Finds the shortest path between \c s and \c t.
alpar@1130
   766
    ///
alpar@1130
   767
    ///\return The length of the shortest s---t path if there exists one,
alpar@1130
   768
    ///0 otherwise.
alpar@1130
   769
    ///\note Apart from the return value, d.run(s) is
alpar@1130
   770
    ///just a shortcut of the following code.
alpar@1130
   771
    ///\code
alpar@1130
   772
    ///  d.init();
alpar@1130
   773
    ///  d.addSource(s);
alpar@1130
   774
    ///  d.start(t);
alpar@1130
   775
    ///\endcode
alpar@1130
   776
    Value run(Node s,Node t) {
alpar@1130
   777
      init();
alpar@1130
   778
      addSource(s);
alpar@1130
   779
      start(t);
deba@2537
   780
      return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t];
alpar@1130
   781
    }
alpar@1130
   782
    
alpar@1128
   783
    ///@}
alpar@1128
   784
alpar@1128
   785
    ///\name Query Functions
alpar@1128
   786
    ///The result of the %Dijkstra algorithm can be obtained using these
alpar@1128
   787
    ///functions.\n
alpar@1128
   788
    ///Before the use of these functions,
alpar@1128
   789
    ///either run() or start() must be called.
alpar@1128
   790
    
alpar@1128
   791
    ///@{
alpar@1128
   792
deba@2335
   793
    ///Gives back the shortest path.
alpar@1283
   794
    
deba@2335
   795
    ///Gives back the shortest path.
deba@2335
   796
    ///\pre The \c t should be reachable from the source.
deba@2335
   797
    Path path(Node t) 
alpar@1283
   798
    {
deba@2335
   799
      return Path(*G, *_pred, t);
alpar@1283
   800
    }
deba@2335
   801
jacint@385
   802
    ///The distance of a node from the root.
alpar@255
   803
jacint@385
   804
    ///Returns the distance of a node from the root.
alpar@255
   805
    ///\pre \ref run() must be called before using this function.
jacint@385
   806
    ///\warning If node \c v in unreachable from the root the return value
alpar@255
   807
    ///of this funcion is undefined.
alpar@1130
   808
    Value dist(Node v) const { return (*_dist)[v]; }
jacint@373
   809
deba@2358
   810
    ///The current distance of a node from the root.
deba@2358
   811
deba@2358
   812
    ///Returns the current distance of a node from the root.
deba@2358
   813
    ///It may be decreased in the following processes.
deba@2358
   814
    ///\pre \c node should be reached but not processed
deba@2358
   815
    Value currentDist(Node v) const { return (*_heap)[v]; }
deba@2358
   816
alpar@584
   817
    ///Returns the 'previous edge' of the shortest path tree.
alpar@255
   818
alpar@584
   819
    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
alpar@785
   820
    ///i.e. it returns the last edge of a shortest path from the root to \c
alpar@688
   821
    ///v. It is \ref INVALID
alpar@688
   822
    ///if \c v is unreachable from the root or if \c v=s. The
jacint@385
   823
    ///shortest path tree used here is equal to the shortest path tree used in
alpar@1631
   824
    ///\ref predNode().  \pre \ref run() must be called before using
jacint@385
   825
    ///this function.
deba@1763
   826
    Edge predEdge(Node v) const { return (*_pred)[v]; }
jacint@373
   827
alpar@584
   828
    ///Returns the 'previous node' of the shortest path tree.
alpar@255
   829
alpar@584
   830
    ///For a node \c v it returns the 'previous node' of the shortest path tree,
jacint@385
   831
    ///i.e. it returns the last but one node from a shortest path from the
jacint@385
   832
    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
jacint@385
   833
    ///\c v=s. The shortest path tree used here is equal to the shortest path
deba@1763
   834
    ///tree used in \ref predEdge().  \pre \ref run() must be called before
jacint@385
   835
    ///using this function.
alpar@1130
   836
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
alpar@1130
   837
				  G->source((*_pred)[v]); }
alpar@255
   838
    
alpar@255
   839
    ///Returns a reference to the NodeMap of distances.
alpar@255
   840
jacint@385
   841
    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
jacint@385
   842
    ///be called before using this function.
alpar@1130
   843
    const DistMap &distMap() const { return *_dist;}
jacint@385
   844
 
alpar@255
   845
    ///Returns a reference to the shortest path tree map.
alpar@255
   846
alpar@255
   847
    ///Returns a reference to the NodeMap of the edges of the
alpar@255
   848
    ///shortest path tree.
alpar@255
   849
    ///\pre \ref run() must be called before using this function.
alpar@1119
   850
    const PredMap &predMap() const { return *_pred;}
jacint@385
   851
 
jacint@385
   852
    ///Checks if a node is reachable from the root.
alpar@255
   853
jacint@385
   854
    ///Returns \c true if \c v is reachable from the root.
alpar@1218
   855
    ///\warning The source nodes are inditated as unreached.
alpar@255
   856
    ///\pre \ref run() must be called before using this function.
jacint@385
   857
    ///
deba@1721
   858
    bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
alpar@1734
   859
alpar@1734
   860
    ///Checks if a node is processed.
alpar@1734
   861
alpar@1734
   862
    ///Returns \c true if \c v is processed, i.e. the shortest
alpar@1734
   863
    ///path to \c v has already found.
alpar@1734
   864
    ///\pre \ref run() must be called before using this function.
alpar@1734
   865
    ///
alpar@1734
   866
    bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
alpar@255
   867
    
alpar@1128
   868
    ///@}
alpar@255
   869
  };
alpar@953
   870
alpar@1218
   871
alpar@1218
   872
alpar@1218
   873
alpar@1218
   874
 
alpar@1218
   875
  ///Default traits class of Dijkstra function.
alpar@1218
   876
alpar@1218
   877
  ///Default traits class of Dijkstra function.
alpar@1218
   878
  ///\param GR Graph type.
alpar@1218
   879
  ///\param LM Type of length map.
alpar@1218
   880
  template<class GR, class LM>
alpar@1218
   881
  struct DijkstraWizardDefaultTraits
alpar@1218
   882
  {
alpar@1218
   883
    ///The graph type the algorithm runs on. 
alpar@1218
   884
    typedef GR Graph;
alpar@1218
   885
    ///The type of the map that stores the edge lengths.
alpar@1218
   886
alpar@1218
   887
    ///The type of the map that stores the edge lengths.
alpar@2260
   888
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
alpar@1218
   889
    typedef LM LengthMap;
alpar@1218
   890
    //The type of the length of the edges.
alpar@1218
   891
    typedef typename LM::Value Value;
deba@2537
   892
    /// Operation traits for Dijkstra algorithm.
deba@2537
   893
deba@2537
   894
    /// It defines the used operation by the algorithm.
deba@2537
   895
    /// \see DijkstraDefaultOperationTraits
deba@2537
   896
    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
alpar@1218
   897
    ///The heap type used by Dijkstra algorithm.
alpar@1218
   898
deba@1721
   899
    /// The cross reference type used by heap.
deba@1721
   900
deba@1721
   901
    /// The cross reference type used by heap.
deba@1721
   902
    /// Usually it is \c Graph::NodeMap<int>.
deba@1721
   903
    typedef typename Graph::template NodeMap<int> HeapCrossRef;
deba@1721
   904
    ///Instantiates a HeapCrossRef.
deba@1721
   905
deba@1721
   906
    ///This function instantiates a \ref HeapCrossRef. 
deba@1721
   907
    /// \param G is the graph, to which we would like to define the 
deba@1721
   908
    /// HeapCrossRef.
deba@1721
   909
    /// \todo The graph alone may be insufficient for the initialization
deba@1721
   910
    static HeapCrossRef *createHeapCrossRef(const GR &G) 
deba@1721
   911
    {
deba@1721
   912
      return new HeapCrossRef(G);
deba@1721
   913
    }
deba@1721
   914
    
deba@1721
   915
    ///The heap type used by Dijkstra algorithm.
deba@1721
   916
alpar@1218
   917
    ///The heap type used by Dijkstra algorithm.
alpar@1218
   918
    ///
alpar@1218
   919
    ///\sa BinHeap
alpar@1218
   920
    ///\sa Dijkstra
mqrelly@2263
   921
    typedef BinHeap<typename LM::Value, typename GR::template NodeMap<int>,
alpar@1218
   922
		    std::less<Value> > Heap;
alpar@1218
   923
deba@1721
   924
    static Heap *createHeap(HeapCrossRef& R) 
deba@1721
   925
    {
deba@1721
   926
      return new Heap(R);
deba@1721
   927
    }
deba@1721
   928
alpar@1218
   929
    ///\brief The type of the map that stores the last
alpar@1218
   930
    ///edges of the shortest paths.
alpar@1218
   931
    /// 
alpar@1218
   932
    ///The type of the map that stores the last
alpar@1218
   933
    ///edges of the shortest paths.
alpar@2260
   934
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   935
    ///
alpar@1218
   936
    typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
alpar@1218
   937
    ///Instantiates a PredMap.
alpar@1218
   938
 
alpar@1218
   939
    ///This function instantiates a \ref PredMap. 
alpar@1536
   940
    ///\param g is the graph, to which we would like to define the PredMap.
alpar@1218
   941
    ///\todo The graph alone may be insufficient for the initialization
alpar@1536
   942
#ifdef DOXYGEN
alpar@1536
   943
    static PredMap *createPredMap(const GR &g) 
alpar@1536
   944
#else
alpar@1367
   945
    static PredMap *createPredMap(const GR &) 
alpar@1536
   946
#endif
alpar@1218
   947
    {
alpar@1218
   948
      return new PredMap();
alpar@1218
   949
    }
alpar@1218
   950
    ///The type of the map that stores whether a nodes is processed.
alpar@1218
   951
 
alpar@1218
   952
    ///The type of the map that stores whether a nodes is processed.
alpar@2260
   953
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   954
    ///By default it is a NullMap.
alpar@1218
   955
    ///\todo If it is set to a real map,
alpar@1218
   956
    ///Dijkstra::processed() should read this.
alpar@1218
   957
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   958
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
   959
    ///Instantiates a ProcessedMap.
alpar@1218
   960
 
alpar@1218
   961
    ///This function instantiates a \ref ProcessedMap. 
alpar@1536
   962
    ///\param g is the graph, to which
alpar@1218
   963
    ///we would like to define the \ref ProcessedMap
alpar@1536
   964
#ifdef DOXYGEN
alpar@1536
   965
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
   966
#else
alpar@1367
   967
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
   968
#endif
alpar@1218
   969
    {
alpar@1218
   970
      return new ProcessedMap();
alpar@1218
   971
    }
alpar@1218
   972
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   973
 
alpar@1218
   974
    ///The type of the map that stores the dists of the nodes.
alpar@2260
   975
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   976
    ///
alpar@1218
   977
    typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
alpar@1218
   978
    ///Instantiates a DistMap.
alpar@1218
   979
 
alpar@1218
   980
    ///This function instantiates a \ref DistMap. 
alpar@1536
   981
    ///\param g is the graph, to which we would like to define the \ref DistMap
alpar@1536
   982
#ifdef DOXYGEN
alpar@1536
   983
    static DistMap *createDistMap(const GR &g)
alpar@1536
   984
#else
alpar@1367
   985
    static DistMap *createDistMap(const GR &)
alpar@1536
   986
#endif
alpar@1218
   987
    {
alpar@1218
   988
      return new DistMap();
alpar@1218
   989
    }
alpar@1218
   990
  };
alpar@1218
   991
  
hegyi@1123
   992
  /// Default traits used by \ref DijkstraWizard
hegyi@1123
   993
alpar@1151
   994
  /// To make it easier to use Dijkstra algorithm
alpar@1151
   995
  ///we have created a wizard class.
alpar@1151
   996
  /// This \ref DijkstraWizard class needs default traits,
alpar@1151
   997
  ///as well as the \ref Dijkstra class.
hegyi@1123
   998
  /// The \ref DijkstraWizardBase is a class to be the default traits of the
hegyi@1123
   999
  /// \ref DijkstraWizard class.
alpar@1220
  1000
  /// \todo More named parameters are required...
alpar@1116
  1001
  template<class GR,class LM>
alpar@1218
  1002
  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
alpar@1116
  1003
  {
alpar@1116
  1004
alpar@1218
  1005
    typedef DijkstraWizardDefaultTraits<GR,LM> Base;
alpar@1116
  1006
  protected:
alpar@1201
  1007
    /// Type of the nodes in the graph.
alpar@1201
  1008
    typedef typename Base::Graph::Node Node;
alpar@1201
  1009
alpar@1116
  1010
    /// Pointer to the underlying graph.
alpar@1116
  1011
    void *_g;
alpar@1116
  1012
    /// Pointer to the length map
alpar@1116
  1013
    void *_length;
alpar@1116
  1014
    ///Pointer to the map of predecessors edges.
alpar@1116
  1015
    void *_pred;
alpar@1116
  1016
    ///Pointer to the map of distances.
alpar@1116
  1017
    void *_dist;
alpar@1116
  1018
    ///Pointer to the source node.
alpar@1201
  1019
    Node _source;
alpar@1116
  1020
alpar@1116
  1021
    public:
hegyi@1123
  1022
    /// Constructor.
hegyi@1123
  1023
    
hegyi@1123
  1024
    /// This constructor does not require parameters, therefore it initiates
hegyi@1123
  1025
    /// all of the attributes to default values (0, INVALID).
alpar@1218
  1026
    DijkstraWizardBase() : _g(0), _length(0), _pred(0),
alpar@1218
  1027
			   _dist(0), _source(INVALID) {}
alpar@1116
  1028
hegyi@1123
  1029
    /// Constructor.
hegyi@1123
  1030
    
alpar@1156
  1031
    /// This constructor requires some parameters,
alpar@1156
  1032
    /// listed in the parameters list.
hegyi@1123
  1033
    /// Others are initiated to 0.
hegyi@1123
  1034
    /// \param g is the initial value of  \ref _g
hegyi@1123
  1035
    /// \param l is the initial value of  \ref _length
hegyi@1123
  1036
    /// \param s is the initial value of  \ref _source
alpar@1116
  1037
    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
deba@2386
  1038
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 
deba@2386
  1039
      _length(reinterpret_cast<void*>(const_cast<LM*>(&l))), 
deba@2386
  1040
      _pred(0), _dist(0), _source(s) {}
alpar@1116
  1041
alpar@1116
  1042
  };
alpar@1116
  1043
  
alpar@1229
  1044
  /// A class to make the usage of Dijkstra algorithm easier
alpar@953
  1045
hegyi@1123
  1046
  /// This class is created to make it easier to use Dijkstra algorithm.
hegyi@1123
  1047
  /// It uses the functions and features of the plain \ref Dijkstra,
alpar@1151
  1048
  /// but it is much simpler to use it.
alpar@953
  1049
  ///
hegyi@1123
  1050
  /// Simplicity means that the way to change the types defined
hegyi@1123
  1051
  /// in the traits class is based on functions that returns the new class
alpar@1151
  1052
  /// and not on templatable built-in classes.
alpar@1151
  1053
  /// When using the plain \ref Dijkstra
alpar@1151
  1054
  /// the new class with the modified type comes from
alpar@1151
  1055
  /// the original class by using the ::
alpar@1151
  1056
  /// operator. In the case of \ref DijkstraWizard only
alpar@1151
  1057
  /// a function have to be called and it will
hegyi@1123
  1058
  /// return the needed class.
hegyi@1123
  1059
  ///
hegyi@1123
  1060
  /// It does not have own \ref run method. When its \ref run method is called
deba@1721
  1061
  /// it initiates a plain \ref Dijkstra class, and calls the \ref 
deba@1721
  1062
  /// Dijkstra::run method of it.
alpar@953
  1063
  template<class TR>
alpar@1116
  1064
  class DijkstraWizard : public TR
alpar@953
  1065
  {
alpar@1116
  1066
    typedef TR Base;
alpar@953
  1067
hegyi@1123
  1068
    ///The type of the underlying graph.
alpar@953
  1069
    typedef typename TR::Graph Graph;
alpar@1119
  1070
    //\e
alpar@953
  1071
    typedef typename Graph::Node Node;
alpar@1119
  1072
    //\e
alpar@953
  1073
    typedef typename Graph::NodeIt NodeIt;
alpar@1119
  1074
    //\e
alpar@953
  1075
    typedef typename Graph::Edge Edge;
alpar@1119
  1076
    //\e
alpar@953
  1077
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@953
  1078
    
hegyi@1123
  1079
    ///The type of the map that stores the edge lengths.
alpar@953
  1080
    typedef typename TR::LengthMap LengthMap;
hegyi@1123
  1081
    ///The type of the length of the edges.
alpar@987
  1082
    typedef typename LengthMap::Value Value;
hegyi@1123
  1083
    ///\brief The type of the map that stores the last
hegyi@1123
  1084
    ///edges of the shortest paths.
alpar@953
  1085
    typedef typename TR::PredMap PredMap;
hegyi@1123
  1086
    ///The type of the map that stores the dists of the nodes.
alpar@953
  1087
    typedef typename TR::DistMap DistMap;
hegyi@1123
  1088
    ///The heap type used by the dijkstra algorithm.
alpar@953
  1089
    typedef typename TR::Heap Heap;
deba@2269
  1090
  public:
hegyi@1123
  1091
    /// Constructor.
alpar@1116
  1092
    DijkstraWizard() : TR() {}
alpar@953
  1093
hegyi@1123
  1094
    /// Constructor that requires parameters.
hegyi@1124
  1095
hegyi@1124
  1096
    /// Constructor that requires parameters.
hegyi@1123
  1097
    /// These parameters will be the default values for the traits class.
alpar@1116
  1098
    DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
alpar@1116
  1099
      TR(g,l,s) {}
alpar@953
  1100
hegyi@1123
  1101
    ///Copy constructor
alpar@1116
  1102
    DijkstraWizard(const TR &b) : TR(b) {}
alpar@953
  1103
alpar@1116
  1104
    ~DijkstraWizard() {}
alpar@1116
  1105
hegyi@1123
  1106
    ///Runs Dijkstra algorithm from a given node.
hegyi@1123
  1107
    
hegyi@1123
  1108
    ///Runs Dijkstra algorithm from a given node.
hegyi@1123
  1109
    ///The node can be given by the \ref source function.
alpar@1116
  1110
    void run()
alpar@953
  1111
    {
alpar@1201
  1112
      if(Base::_source==INVALID) throw UninitializedParameter();
deba@1193
  1113
      Dijkstra<Graph,LengthMap,TR> 
deba@2386
  1114
	dij(*reinterpret_cast<const Graph*>(Base::_g),
deba@2386
  1115
            *reinterpret_cast<const LengthMap*>(Base::_length));
deba@2386
  1116
      if(Base::_pred) dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
deba@2386
  1117
      if(Base::_dist) dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
deba@1345
  1118
      dij.run(Base::_source);
alpar@1116
  1119
    }
alpar@1116
  1120
hegyi@1124
  1121
    ///Runs Dijkstra algorithm from the given node.
hegyi@1123
  1122
hegyi@1124
  1123
    ///Runs Dijkstra algorithm from the given node.
hegyi@1123
  1124
    ///\param s is the given source.
alpar@1116
  1125
    void run(Node s)
alpar@1116
  1126
    {
alpar@1201
  1127
      Base::_source=s;
alpar@1116
  1128
      run();
alpar@953
  1129
    }
alpar@953
  1130
alpar@953
  1131
    template<class T>
alpar@1116
  1132
    struct DefPredMapBase : public Base {
alpar@1116
  1133
      typedef T PredMap;
alpar@1367
  1134
      static PredMap *createPredMap(const Graph &) { return 0; };
alpar@1236
  1135
      DefPredMapBase(const TR &b) : TR(b) {}
alpar@1116
  1136
    };
alpar@953
  1137
    
alpar@1156
  1138
    ///\brief \ref named-templ-param "Named parameter"
alpar@1156
  1139
    ///function for setting PredMap type
alpar@1156
  1140
    ///
alpar@1156
  1141
    /// \ref named-templ-param "Named parameter"
alpar@1156
  1142
    ///function for setting PredMap type
hegyi@1124
  1143
    ///
alpar@953
  1144
    template<class T>
alpar@1116
  1145
    DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@953
  1146
    {
deba@2386
  1147
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1116
  1148
      return DijkstraWizard<DefPredMapBase<T> >(*this);
alpar@953
  1149
    }
alpar@953
  1150
    
alpar@1116
  1151
    template<class T>
alpar@1116
  1152
    struct DefDistMapBase : public Base {
alpar@1116
  1153
      typedef T DistMap;
alpar@1367
  1154
      static DistMap *createDistMap(const Graph &) { return 0; };
alpar@1236
  1155
      DefDistMapBase(const TR &b) : TR(b) {}
alpar@1116
  1156
    };
alpar@953
  1157
    
alpar@1156
  1158
    ///\brief \ref named-templ-param "Named parameter"
alpar@1156
  1159
    ///function for setting DistMap type
alpar@1156
  1160
    ///
alpar@1156
  1161
    /// \ref named-templ-param "Named parameter"
alpar@1156
  1162
    ///function for setting DistMap type
hegyi@1124
  1163
    ///
alpar@953
  1164
    template<class T>
alpar@1116
  1165
    DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@953
  1166
    {
deba@2386
  1167
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1116
  1168
      return DijkstraWizard<DefDistMapBase<T> >(*this);
alpar@953
  1169
    }
alpar@1117
  1170
    
hegyi@1123
  1171
    /// Sets the source node, from which the Dijkstra algorithm runs.
hegyi@1123
  1172
hegyi@1123
  1173
    /// Sets the source node, from which the Dijkstra algorithm runs.
hegyi@1123
  1174
    /// \param s is the source node.
alpar@1117
  1175
    DijkstraWizard<TR> &source(Node s) 
alpar@953
  1176
    {
alpar@1201
  1177
      Base::_source=s;
alpar@953
  1178
      return *this;
alpar@953
  1179
    }
alpar@953
  1180
    
alpar@953
  1181
  };
alpar@255
  1182
  
alpar@1218
  1183
  ///Function type interface for Dijkstra algorithm.
alpar@953
  1184
deba@2376
  1185
  /// \ingroup shortest_path
alpar@1218
  1186
  ///Function type interface for Dijkstra algorithm.
alpar@953
  1187
  ///
alpar@1218
  1188
  ///This function also has several
alpar@1218
  1189
  ///\ref named-templ-func-param "named parameters",
alpar@1218
  1190
  ///they are declared as the members of class \ref DijkstraWizard.
alpar@1218
  1191
  ///The following
alpar@1218
  1192
  ///example shows how to use these parameters.
alpar@1218
  1193
  ///\code
alpar@1218
  1194
  ///  dijkstra(g,length,source).predMap(preds).run();
alpar@1218
  1195
  ///\endcode
alpar@1218
  1196
  ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
alpar@1218
  1197
  ///to the end of the parameter list.
alpar@1218
  1198
  ///\sa DijkstraWizard
alpar@1218
  1199
  ///\sa Dijkstra
alpar@953
  1200
  template<class GR, class LM>
alpar@1116
  1201
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
alpar@1116
  1202
  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
alpar@953
  1203
  {
alpar@1116
  1204
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
alpar@953
  1205
  }
alpar@953
  1206
alpar@921
  1207
} //END OF NAMESPACE LEMON
alpar@255
  1208
alpar@255
  1209
#endif