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