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