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