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