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