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