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