lemon/dijkstra.h
author deba
Mon, 05 Mar 2007 17:10:58 +0000
changeset 2392 4bbeaf115cdb
parent 2386 81b47fc5c444
child 2439 3f1c7a6c33cd
permissions -rw-r--r--
Bug whit Parent typedef in SplitGraphAdaptor::CombinedEdgeMap
Fixing graph to eps parameters for demo
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@2391
     5
 * Copyright (C) 2003-2007
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
deba@2376
    22
///\ingroup shortest_path
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
  
deba@2376
   143
  /// \ingroup shortest_path
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@2386
   490
    Dijkstra &heap(Heap& hp, HeapCrossRef &cr)
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@2386
   496
      _heap_cross_ref = &cr;
deba@1741
   497
      if(local_heap) {
deba@1741
   498
	delete _heap;
deba@1741
   499
	local_heap=false;
deba@1741
   500
      }
deba@2386
   501
      _heap = &hp;
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@2354
   513
alpar@2354
   514
    typedef PredMapPath<Graph, PredMap> Path;
alpar@2354
   515
alpar@1218
   516
    ///\name Execution control
alpar@1128
   517
    ///The simplest way to execute the algorithm is to use
alpar@1156
   518
    ///one of the member functions called \c run(...).
alpar@1128
   519
    ///\n
alpar@1218
   520
    ///If you need more control on the execution,
alpar@1128
   521
    ///first you must call \ref init(), then you can add several source nodes
alpar@1218
   522
    ///with \ref addSource().
alpar@1218
   523
    ///Finally \ref start() will perform the actual path
alpar@1128
   524
    ///computation.
alpar@1128
   525
alpar@1128
   526
    ///@{
alpar@1128
   527
alpar@1128
   528
    ///Initializes the internal data structures.
alpar@1128
   529
alpar@1128
   530
    ///Initializes the internal data structures.
alpar@1128
   531
    ///
alpar@1128
   532
    void init()
alpar@1128
   533
    {
alpar@1128
   534
      create_maps();
deba@1721
   535
      _heap->clear();
alpar@774
   536
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
alpar@1119
   537
	_pred->set(u,INVALID);
alpar@1218
   538
	_processed->set(u,false);
deba@1721
   539
	_heap_cross_ref->set(u,Heap::PRE_HEAP);
alpar@694
   540
      }
alpar@1128
   541
    }
alpar@1128
   542
    
alpar@1128
   543
    ///Adds a new source node.
alpar@1128
   544
alpar@1155
   545
    ///Adds a new source node to the priority heap.
alpar@1128
   546
    ///
alpar@1128
   547
    ///The optional second parameter is the initial distance of the node.
alpar@1128
   548
    ///
alpar@1155
   549
    ///It checks if the node has already been added to the heap and
deba@1988
   550
    ///it is pushed to the heap only if either it was not in the heap
deba@1988
   551
    ///or the shortest path found till then is shorter than \c dst.
alpar@1734
   552
    void addSource(Node s,Value dst=dijkstraZero<Value>())
alpar@1128
   553
    {
deba@1721
   554
      if(_heap->state(s) != Heap::IN_HEAP) {
deba@1721
   555
	_heap->push(s,dst);
deba@1721
   556
      } else if((*_heap)[s]<dst) {
deba@1988
   557
	_heap->set(s,dst);
alpar@1155
   558
	_pred->set(s,INVALID);
alpar@1155
   559
      }
alpar@1128
   560
    }
alpar@1128
   561
    
alpar@1155
   562
    ///Processes the next node in the priority heap
alpar@1155
   563
alpar@1155
   564
    ///Processes the next node in the priority heap.
alpar@1155
   565
    ///
alpar@1516
   566
    ///\return The processed node.
alpar@1516
   567
    ///
alpar@1155
   568
    ///\warning The priority heap must not be empty!
alpar@1516
   569
    Node processNextNode()
alpar@1128
   570
    {
deba@1721
   571
      Node v=_heap->top(); 
deba@1721
   572
      Value oldvalue=_heap->prio();
deba@1721
   573
      _heap->pop();
alpar@1130
   574
      finalizeNodeData(v,oldvalue);
alpar@694
   575
      
alpar@1128
   576
      for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
alpar@1128
   577
	Node w=G->target(e); 
deba@1721
   578
	switch(_heap->state(w)) {
alpar@1128
   579
	case Heap::PRE_HEAP:
deba@1721
   580
	  _heap->push(w,oldvalue+(*length)[e]); 
alpar@1128
   581
	  _pred->set(w,e);
alpar@1128
   582
	  break;
alpar@1128
   583
	case Heap::IN_HEAP:
deba@1721
   584
	  if ( oldvalue+(*length)[e] < (*_heap)[w] ) {
deba@1721
   585
	    _heap->decrease(w, oldvalue+(*length)[e]); 
alpar@1119
   586
	    _pred->set(w,e);
alpar@694
   587
	  }
alpar@1128
   588
	  break;
alpar@1128
   589
	case Heap::POST_HEAP:
alpar@1128
   590
	  break;
alpar@694
   591
	}
alpar@694
   592
      }
alpar@1516
   593
      return v;
alpar@694
   594
    }
alpar@1128
   595
alpar@1665
   596
    ///Next node to be processed.
alpar@1665
   597
    
alpar@1665
   598
    ///Next node to be processed.
alpar@1665
   599
    ///
alpar@1665
   600
    ///\return The next node to be processed or INVALID if the priority heap
alpar@1665
   601
    /// is empty.
deba@1694
   602
    Node nextNode()
alpar@1665
   603
    { 
deba@1721
   604
      return _heap->empty()?_heap->top():INVALID;
alpar@1665
   605
    }
alpar@1665
   606
 
alpar@1218
   607
    ///\brief Returns \c false if there are nodes
alpar@1218
   608
    ///to be processed in the priority heap
alpar@1155
   609
    ///
alpar@1218
   610
    ///Returns \c false if there are nodes
alpar@1218
   611
    ///to be processed in the priority heap
deba@1721
   612
    bool emptyQueue() { return _heap->empty(); }
alpar@1155
   613
    ///Returns the number of the nodes to be processed in the priority heap
alpar@1155
   614
alpar@1155
   615
    ///Returns the number of the nodes to be processed in the priority heap
alpar@1155
   616
    ///
deba@1721
   617
    int queueSize() { return _heap->size(); }
alpar@1155
   618
    
alpar@1130
   619
    ///Executes the algorithm.
alpar@1128
   620
alpar@1130
   621
    ///Executes the algorithm.
alpar@1128
   622
    ///
alpar@1130
   623
    ///\pre init() must be called and at least one node should be added
alpar@1130
   624
    ///with addSource() before using this function.
alpar@1128
   625
    ///
alpar@1128
   626
    ///This method runs the %Dijkstra algorithm from the root node(s)
alpar@1128
   627
    ///in order to
alpar@1128
   628
    ///compute the
alpar@1128
   629
    ///shortest path to each node. The algorithm computes
alpar@1128
   630
    ///- The shortest path tree.
alpar@1128
   631
    ///- The distance of each node from the root(s).
alpar@1128
   632
    ///
alpar@1128
   633
    void start()
alpar@1128
   634
    {
deba@1721
   635
      while ( !_heap->empty() ) processNextNode();
alpar@1128
   636
    }
alpar@255
   637
    
alpar@1130
   638
    ///Executes the algorithm until \c dest is reached.
alpar@1128
   639
alpar@1130
   640
    ///Executes the algorithm until \c dest is reached.
alpar@1128
   641
    ///
alpar@1130
   642
    ///\pre init() must be called and at least one node should be added
alpar@1130
   643
    ///with addSource() before using this function.
alpar@1128
   644
    ///
alpar@1128
   645
    ///This method runs the %Dijkstra algorithm from the root node(s)
alpar@1128
   646
    ///in order to
alpar@1128
   647
    ///compute the
alpar@1128
   648
    ///shortest path to \c dest. The algorithm computes
alpar@1128
   649
    ///- The shortest path to \c  dest.
alpar@1128
   650
    ///- The distance of \c dest from the root(s).
alpar@1128
   651
    ///
alpar@1128
   652
    void start(Node dest)
alpar@1128
   653
    {
deba@1721
   654
      while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
deba@1721
   655
      if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
alpar@1130
   656
    }
alpar@1130
   657
    
alpar@1130
   658
    ///Executes the algorithm until a condition is met.
alpar@1130
   659
alpar@1130
   660
    ///Executes the algorithm until a condition is met.
alpar@1130
   661
    ///
alpar@1130
   662
    ///\pre init() must be called and at least one node should be added
alpar@1130
   663
    ///with addSource() before using this function.
alpar@1130
   664
    ///
alpar@1130
   665
    ///\param nm must be a bool (or convertible) node map. The algorithm
alpar@1130
   666
    ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
deba@1345
   667
    template<class NodeBoolMap>
deba@1345
   668
    void start(const NodeBoolMap &nm)
alpar@1130
   669
    {
deba@1721
   670
      while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
deba@1721
   671
      if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
alpar@1128
   672
    }
alpar@1128
   673
    
alpar@1128
   674
    ///Runs %Dijkstra algorithm from node \c s.
alpar@1128
   675
    
alpar@1128
   676
    ///This method runs the %Dijkstra algorithm from a root node \c s
alpar@1128
   677
    ///in order to
alpar@1128
   678
    ///compute the
alpar@1128
   679
    ///shortest path to each node. The algorithm computes
alpar@1128
   680
    ///- The shortest path tree.
alpar@1128
   681
    ///- The distance of each node from the root.
alpar@1128
   682
    ///
alpar@1128
   683
    ///\note d.run(s) is just a shortcut of the following code.
alpar@1128
   684
    ///\code
alpar@1128
   685
    ///  d.init();
alpar@1128
   686
    ///  d.addSource(s);
alpar@1128
   687
    ///  d.start();
alpar@1128
   688
    ///\endcode
alpar@1128
   689
    void run(Node s) {
alpar@1128
   690
      init();
alpar@1128
   691
      addSource(s);
alpar@1128
   692
      start();
alpar@1128
   693
    }
alpar@1128
   694
    
alpar@1130
   695
    ///Finds the shortest path between \c s and \c t.
alpar@1130
   696
    
alpar@1130
   697
    ///Finds the shortest path between \c s and \c t.
alpar@1130
   698
    ///
alpar@1130
   699
    ///\return The length of the shortest s---t path if there exists one,
alpar@1130
   700
    ///0 otherwise.
alpar@1130
   701
    ///\note Apart from the return value, d.run(s) is
alpar@1130
   702
    ///just a shortcut of the following code.
alpar@1130
   703
    ///\code
alpar@1130
   704
    ///  d.init();
alpar@1130
   705
    ///  d.addSource(s);
alpar@1130
   706
    ///  d.start(t);
alpar@1130
   707
    ///\endcode
alpar@1130
   708
    Value run(Node s,Node t) {
alpar@1130
   709
      init();
alpar@1130
   710
      addSource(s);
alpar@1130
   711
      start(t);
alpar@1734
   712
      return (*_pred)[t]==INVALID?dijkstraZero<Value>():(*_dist)[t];
alpar@1130
   713
    }
alpar@1130
   714
    
alpar@1128
   715
    ///@}
alpar@1128
   716
alpar@1128
   717
    ///\name Query Functions
alpar@1128
   718
    ///The result of the %Dijkstra algorithm can be obtained using these
alpar@1128
   719
    ///functions.\n
alpar@1128
   720
    ///Before the use of these functions,
alpar@1128
   721
    ///either run() or start() must be called.
alpar@1128
   722
    
alpar@1128
   723
    ///@{
alpar@1128
   724
deba@2335
   725
    ///Gives back the shortest path.
alpar@1283
   726
    
deba@2335
   727
    ///Gives back the shortest path.
deba@2335
   728
    ///\pre The \c t should be reachable from the source.
deba@2335
   729
    Path path(Node t) 
alpar@1283
   730
    {
deba@2335
   731
      return Path(*G, *_pred, t);
alpar@1283
   732
    }
deba@2335
   733
jacint@385
   734
    ///The distance of a node from the root.
alpar@255
   735
jacint@385
   736
    ///Returns the distance of a node from the root.
alpar@255
   737
    ///\pre \ref run() must be called before using this function.
jacint@385
   738
    ///\warning If node \c v in unreachable from the root the return value
alpar@255
   739
    ///of this funcion is undefined.
alpar@1130
   740
    Value dist(Node v) const { return (*_dist)[v]; }
jacint@373
   741
deba@2358
   742
    ///The current distance of a node from the root.
deba@2358
   743
deba@2358
   744
    ///Returns the current distance of a node from the root.
deba@2358
   745
    ///It may be decreased in the following processes.
deba@2358
   746
    ///\pre \c node should be reached but not processed
deba@2358
   747
    Value currentDist(Node v) const { return (*_heap)[v]; }
deba@2358
   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@2260
   820
    ///It must meet the \ref concepts::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
mqrelly@2263
   848
    typedef BinHeap<typename LM::Value, typename GR::template NodeMap<int>,
alpar@1218
   849
		    std::less<Value> > Heap;
alpar@1218
   850
deba@1721
   851
    static Heap *createHeap(HeapCrossRef& R) 
deba@1721
   852
    {
deba@1721
   853
      return new Heap(R);
deba@1721
   854
    }
deba@1721
   855
alpar@1218
   856
    ///\brief The type of the map that stores the last
alpar@1218
   857
    ///edges of the shortest paths.
alpar@1218
   858
    /// 
alpar@1218
   859
    ///The type of the map that stores the last
alpar@1218
   860
    ///edges of the shortest paths.
alpar@2260
   861
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   862
    ///
alpar@1218
   863
    typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
alpar@1218
   864
    ///Instantiates a PredMap.
alpar@1218
   865
 
alpar@1218
   866
    ///This function instantiates a \ref PredMap. 
alpar@1536
   867
    ///\param g is the graph, to which we would like to define the PredMap.
alpar@1218
   868
    ///\todo The graph alone may be insufficient for the initialization
alpar@1536
   869
#ifdef DOXYGEN
alpar@1536
   870
    static PredMap *createPredMap(const GR &g) 
alpar@1536
   871
#else
alpar@1367
   872
    static PredMap *createPredMap(const GR &) 
alpar@1536
   873
#endif
alpar@1218
   874
    {
alpar@1218
   875
      return new PredMap();
alpar@1218
   876
    }
alpar@1218
   877
    ///The type of the map that stores whether a nodes is processed.
alpar@1218
   878
 
alpar@1218
   879
    ///The type of the map that stores whether a nodes is processed.
alpar@2260
   880
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   881
    ///By default it is a NullMap.
alpar@1218
   882
    ///\todo If it is set to a real map,
alpar@1218
   883
    ///Dijkstra::processed() should read this.
alpar@1218
   884
    ///\todo named parameter to set this type, function to read and write.
alpar@1218
   885
    typedef NullMap<typename Graph::Node,bool> ProcessedMap;
alpar@1218
   886
    ///Instantiates a ProcessedMap.
alpar@1218
   887
 
alpar@1218
   888
    ///This function instantiates a \ref ProcessedMap. 
alpar@1536
   889
    ///\param g is the graph, to which
alpar@1218
   890
    ///we would like to define the \ref ProcessedMap
alpar@1536
   891
#ifdef DOXYGEN
alpar@1536
   892
    static ProcessedMap *createProcessedMap(const GR &g)
alpar@1536
   893
#else
alpar@1367
   894
    static ProcessedMap *createProcessedMap(const GR &)
alpar@1536
   895
#endif
alpar@1218
   896
    {
alpar@1218
   897
      return new ProcessedMap();
alpar@1218
   898
    }
alpar@1218
   899
    ///The type of the map that stores the dists of the nodes.
alpar@1218
   900
 
alpar@1218
   901
    ///The type of the map that stores the dists of the nodes.
alpar@2260
   902
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
alpar@1218
   903
    ///
alpar@1218
   904
    typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
alpar@1218
   905
    ///Instantiates a DistMap.
alpar@1218
   906
 
alpar@1218
   907
    ///This function instantiates a \ref DistMap. 
alpar@1536
   908
    ///\param g is the graph, to which we would like to define the \ref DistMap
alpar@1536
   909
#ifdef DOXYGEN
alpar@1536
   910
    static DistMap *createDistMap(const GR &g)
alpar@1536
   911
#else
alpar@1367
   912
    static DistMap *createDistMap(const GR &)
alpar@1536
   913
#endif
alpar@1218
   914
    {
alpar@1218
   915
      return new DistMap();
alpar@1218
   916
    }
alpar@1218
   917
  };
alpar@1218
   918
  
hegyi@1123
   919
  /// Default traits used by \ref DijkstraWizard
hegyi@1123
   920
alpar@1151
   921
  /// To make it easier to use Dijkstra algorithm
alpar@1151
   922
  ///we have created a wizard class.
alpar@1151
   923
  /// This \ref DijkstraWizard class needs default traits,
alpar@1151
   924
  ///as well as the \ref Dijkstra class.
hegyi@1123
   925
  /// The \ref DijkstraWizardBase is a class to be the default traits of the
hegyi@1123
   926
  /// \ref DijkstraWizard class.
alpar@1220
   927
  /// \todo More named parameters are required...
alpar@1116
   928
  template<class GR,class LM>
alpar@1218
   929
  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
alpar@1116
   930
  {
alpar@1116
   931
alpar@1218
   932
    typedef DijkstraWizardDefaultTraits<GR,LM> Base;
alpar@1116
   933
  protected:
alpar@1201
   934
    /// Type of the nodes in the graph.
alpar@1201
   935
    typedef typename Base::Graph::Node Node;
alpar@1201
   936
alpar@1116
   937
    /// Pointer to the underlying graph.
alpar@1116
   938
    void *_g;
alpar@1116
   939
    /// Pointer to the length map
alpar@1116
   940
    void *_length;
alpar@1116
   941
    ///Pointer to the map of predecessors edges.
alpar@1116
   942
    void *_pred;
alpar@1116
   943
    ///Pointer to the map of distances.
alpar@1116
   944
    void *_dist;
alpar@1116
   945
    ///Pointer to the source node.
alpar@1201
   946
    Node _source;
alpar@1116
   947
alpar@1116
   948
    public:
hegyi@1123
   949
    /// Constructor.
hegyi@1123
   950
    
hegyi@1123
   951
    /// This constructor does not require parameters, therefore it initiates
hegyi@1123
   952
    /// all of the attributes to default values (0, INVALID).
alpar@1218
   953
    DijkstraWizardBase() : _g(0), _length(0), _pred(0),
alpar@1218
   954
			   _dist(0), _source(INVALID) {}
alpar@1116
   955
hegyi@1123
   956
    /// Constructor.
hegyi@1123
   957
    
alpar@1156
   958
    /// This constructor requires some parameters,
alpar@1156
   959
    /// listed in the parameters list.
hegyi@1123
   960
    /// Others are initiated to 0.
hegyi@1123
   961
    /// \param g is the initial value of  \ref _g
hegyi@1123
   962
    /// \param l is the initial value of  \ref _length
hegyi@1123
   963
    /// \param s is the initial value of  \ref _source
alpar@1116
   964
    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
deba@2386
   965
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), 
deba@2386
   966
      _length(reinterpret_cast<void*>(const_cast<LM*>(&l))), 
deba@2386
   967
      _pred(0), _dist(0), _source(s) {}
alpar@1116
   968
alpar@1116
   969
  };
alpar@1116
   970
  
alpar@1229
   971
  /// A class to make the usage of Dijkstra algorithm easier
alpar@953
   972
hegyi@1123
   973
  /// This class is created to make it easier to use Dijkstra algorithm.
hegyi@1123
   974
  /// It uses the functions and features of the plain \ref Dijkstra,
alpar@1151
   975
  /// but it is much simpler to use it.
alpar@953
   976
  ///
hegyi@1123
   977
  /// Simplicity means that the way to change the types defined
hegyi@1123
   978
  /// in the traits class is based on functions that returns the new class
alpar@1151
   979
  /// and not on templatable built-in classes.
alpar@1151
   980
  /// When using the plain \ref Dijkstra
alpar@1151
   981
  /// the new class with the modified type comes from
alpar@1151
   982
  /// the original class by using the ::
alpar@1151
   983
  /// operator. In the case of \ref DijkstraWizard only
alpar@1151
   984
  /// a function have to be called and it will
hegyi@1123
   985
  /// return the needed class.
hegyi@1123
   986
  ///
hegyi@1123
   987
  /// It does not have own \ref run method. When its \ref run method is called
deba@1721
   988
  /// it initiates a plain \ref Dijkstra class, and calls the \ref 
deba@1721
   989
  /// Dijkstra::run method of it.
alpar@953
   990
  template<class TR>
alpar@1116
   991
  class DijkstraWizard : public TR
alpar@953
   992
  {
alpar@1116
   993
    typedef TR Base;
alpar@953
   994
hegyi@1123
   995
    ///The type of the underlying graph.
alpar@953
   996
    typedef typename TR::Graph Graph;
alpar@1119
   997
    //\e
alpar@953
   998
    typedef typename Graph::Node Node;
alpar@1119
   999
    //\e
alpar@953
  1000
    typedef typename Graph::NodeIt NodeIt;
alpar@1119
  1001
    //\e
alpar@953
  1002
    typedef typename Graph::Edge Edge;
alpar@1119
  1003
    //\e
alpar@953
  1004
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@953
  1005
    
hegyi@1123
  1006
    ///The type of the map that stores the edge lengths.
alpar@953
  1007
    typedef typename TR::LengthMap LengthMap;
hegyi@1123
  1008
    ///The type of the length of the edges.
alpar@987
  1009
    typedef typename LengthMap::Value Value;
hegyi@1123
  1010
    ///\brief The type of the map that stores the last
hegyi@1123
  1011
    ///edges of the shortest paths.
alpar@953
  1012
    typedef typename TR::PredMap PredMap;
hegyi@1123
  1013
    ///The type of the map that stores the dists of the nodes.
alpar@953
  1014
    typedef typename TR::DistMap DistMap;
hegyi@1123
  1015
    ///The heap type used by the dijkstra algorithm.
alpar@953
  1016
    typedef typename TR::Heap Heap;
deba@2269
  1017
  public:
hegyi@1123
  1018
    /// Constructor.
alpar@1116
  1019
    DijkstraWizard() : TR() {}
alpar@953
  1020
hegyi@1123
  1021
    /// Constructor that requires parameters.
hegyi@1124
  1022
hegyi@1124
  1023
    /// Constructor that requires parameters.
hegyi@1123
  1024
    /// These parameters will be the default values for the traits class.
alpar@1116
  1025
    DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
alpar@1116
  1026
      TR(g,l,s) {}
alpar@953
  1027
hegyi@1123
  1028
    ///Copy constructor
alpar@1116
  1029
    DijkstraWizard(const TR &b) : TR(b) {}
alpar@953
  1030
alpar@1116
  1031
    ~DijkstraWizard() {}
alpar@1116
  1032
hegyi@1123
  1033
    ///Runs Dijkstra algorithm from a given node.
hegyi@1123
  1034
    
hegyi@1123
  1035
    ///Runs Dijkstra algorithm from a given node.
hegyi@1123
  1036
    ///The node can be given by the \ref source function.
alpar@1116
  1037
    void run()
alpar@953
  1038
    {
alpar@1201
  1039
      if(Base::_source==INVALID) throw UninitializedParameter();
deba@1193
  1040
      Dijkstra<Graph,LengthMap,TR> 
deba@2386
  1041
	dij(*reinterpret_cast<const Graph*>(Base::_g),
deba@2386
  1042
            *reinterpret_cast<const LengthMap*>(Base::_length));
deba@2386
  1043
      if(Base::_pred) dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
deba@2386
  1044
      if(Base::_dist) dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
deba@1345
  1045
      dij.run(Base::_source);
alpar@1116
  1046
    }
alpar@1116
  1047
hegyi@1124
  1048
    ///Runs Dijkstra algorithm from the given node.
hegyi@1123
  1049
hegyi@1124
  1050
    ///Runs Dijkstra algorithm from the given node.
hegyi@1123
  1051
    ///\param s is the given source.
alpar@1116
  1052
    void run(Node s)
alpar@1116
  1053
    {
alpar@1201
  1054
      Base::_source=s;
alpar@1116
  1055
      run();
alpar@953
  1056
    }
alpar@953
  1057
alpar@953
  1058
    template<class T>
alpar@1116
  1059
    struct DefPredMapBase : public Base {
alpar@1116
  1060
      typedef T PredMap;
alpar@1367
  1061
      static PredMap *createPredMap(const Graph &) { return 0; };
alpar@1236
  1062
      DefPredMapBase(const TR &b) : TR(b) {}
alpar@1116
  1063
    };
alpar@953
  1064
    
alpar@1156
  1065
    ///\brief \ref named-templ-param "Named parameter"
alpar@1156
  1066
    ///function for setting PredMap type
alpar@1156
  1067
    ///
alpar@1156
  1068
    /// \ref named-templ-param "Named parameter"
alpar@1156
  1069
    ///function for setting PredMap type
hegyi@1124
  1070
    ///
alpar@953
  1071
    template<class T>
alpar@1116
  1072
    DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) 
alpar@953
  1073
    {
deba@2386
  1074
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1116
  1075
      return DijkstraWizard<DefPredMapBase<T> >(*this);
alpar@953
  1076
    }
alpar@953
  1077
    
alpar@1116
  1078
    template<class T>
alpar@1116
  1079
    struct DefDistMapBase : public Base {
alpar@1116
  1080
      typedef T DistMap;
alpar@1367
  1081
      static DistMap *createDistMap(const Graph &) { return 0; };
alpar@1236
  1082
      DefDistMapBase(const TR &b) : TR(b) {}
alpar@1116
  1083
    };
alpar@953
  1084
    
alpar@1156
  1085
    ///\brief \ref named-templ-param "Named parameter"
alpar@1156
  1086
    ///function for setting DistMap type
alpar@1156
  1087
    ///
alpar@1156
  1088
    /// \ref named-templ-param "Named parameter"
alpar@1156
  1089
    ///function for setting DistMap type
hegyi@1124
  1090
    ///
alpar@953
  1091
    template<class T>
alpar@1116
  1092
    DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) 
alpar@953
  1093
    {
deba@2386
  1094
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
alpar@1116
  1095
      return DijkstraWizard<DefDistMapBase<T> >(*this);
alpar@953
  1096
    }
alpar@1117
  1097
    
hegyi@1123
  1098
    /// Sets the source node, from which the Dijkstra algorithm runs.
hegyi@1123
  1099
hegyi@1123
  1100
    /// Sets the source node, from which the Dijkstra algorithm runs.
hegyi@1123
  1101
    /// \param s is the source node.
alpar@1117
  1102
    DijkstraWizard<TR> &source(Node s) 
alpar@953
  1103
    {
alpar@1201
  1104
      Base::_source=s;
alpar@953
  1105
      return *this;
alpar@953
  1106
    }
alpar@953
  1107
    
alpar@953
  1108
  };
alpar@255
  1109
  
alpar@1218
  1110
  ///Function type interface for Dijkstra algorithm.
alpar@953
  1111
deba@2376
  1112
  /// \ingroup shortest_path
alpar@1218
  1113
  ///Function type interface for Dijkstra algorithm.
alpar@953
  1114
  ///
alpar@1218
  1115
  ///This function also has several
alpar@1218
  1116
  ///\ref named-templ-func-param "named parameters",
alpar@1218
  1117
  ///they are declared as the members of class \ref DijkstraWizard.
alpar@1218
  1118
  ///The following
alpar@1218
  1119
  ///example shows how to use these parameters.
alpar@1218
  1120
  ///\code
alpar@1218
  1121
  ///  dijkstra(g,length,source).predMap(preds).run();
alpar@1218
  1122
  ///\endcode
alpar@1218
  1123
  ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
alpar@1218
  1124
  ///to the end of the parameter list.
alpar@1218
  1125
  ///\sa DijkstraWizard
alpar@1218
  1126
  ///\sa Dijkstra
alpar@953
  1127
  template<class GR, class LM>
alpar@1116
  1128
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
alpar@1116
  1129
  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
alpar@953
  1130
  {
alpar@1116
  1131
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
alpar@953
  1132
  }
alpar@953
  1133
alpar@921
  1134
} //END OF NAMESPACE LEMON
alpar@255
  1135
alpar@255
  1136
#endif