lemon/dijkstra.h
author deba
Tue, 17 Oct 2006 10:50:57 +0000
changeset 2247 269a0dcee70b
parent 2151 38ec4a930c05
child 2260 4274224f8a7d
permissions -rw-r--r--
Update the Path concept
Concept check for paths

DirPath renamed to Path
The interface updated to the new lemon interface
Make difference between the empty path and the path from one node
Builder interface have not been changed
// I wanted but there was not accordance about it

UPath is removed
It was a buggy implementation, it could not iterate on the
nodes in the right order
Right way to use undirected paths => path of edges in undirected graphs

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