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