gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge
0 7 0
merge default
4 files changed with 488 insertions and 338 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_BFS_H
20 20
#define LEMON_BFS_H
21 21

	
22 22
///\ingroup search
23 23
///\file
24 24
///\brief BFS algorithm.
25 25

	
26 26
#include <lemon/list_graph.h>
27 27
#include <lemon/bits/path_dump.h>
28 28
#include <lemon/core.h>
29 29
#include <lemon/error.h>
30 30
#include <lemon/maps.h>
31
#include <lemon/path.h>
31 32

	
32 33
namespace lemon {
33 34

	
34 35
  ///Default traits class of Bfs class.
35 36

	
36 37
  ///Default traits class of Bfs class.
37 38
  ///\tparam GR Digraph type.
38 39
  template<class GR>
39 40
  struct BfsDefaultTraits
40 41
  {
41 42
    ///The type of the digraph the algorithm runs on.
42 43
    typedef GR Digraph;
43 44

	
44 45
    ///\brief The type of the map that stores the predecessor
45 46
    ///arcs of the shortest paths.
46 47
    ///
47 48
    ///The type of the map that stores the predecessor
48 49
    ///arcs of the shortest paths.
49 50
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
50 51
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
51 52
    ///Instantiates a \ref PredMap.
52 53

	
53 54
    ///This function instantiates a \ref PredMap.
54 55
    ///\param g is the digraph, to which we would like to define the
55 56
    ///\ref PredMap.
56 57
    static PredMap *createPredMap(const Digraph &g)
57 58
    {
58 59
      return new PredMap(g);
59 60
    }
60 61

	
61 62
    ///The type of the map that indicates which nodes are processed.
62 63

	
63 64
    ///The type of the map that indicates which nodes are processed.
64 65
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
65 66
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
66 67
    ///Instantiates a \ref ProcessedMap.
67 68

	
68 69
    ///This function instantiates a \ref ProcessedMap.
69 70
    ///\param g is the digraph, to which
70 71
    ///we would like to define the \ref ProcessedMap
71 72
#ifdef DOXYGEN
72 73
    static ProcessedMap *createProcessedMap(const Digraph &g)
73 74
#else
74 75
    static ProcessedMap *createProcessedMap(const Digraph &)
75 76
#endif
76 77
    {
77 78
      return new ProcessedMap();
78 79
    }
79 80

	
80 81
    ///The type of the map that indicates which nodes are reached.
81 82

	
82 83
    ///The type of the map that indicates which nodes are reached.
83 84
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
84 85
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
85 86
    ///Instantiates a \ref ReachedMap.
86 87

	
87 88
    ///This function instantiates a \ref ReachedMap.
88 89
    ///\param g is the digraph, to which
89 90
    ///we would like to define the \ref ReachedMap.
90 91
    static ReachedMap *createReachedMap(const Digraph &g)
91 92
    {
92 93
      return new ReachedMap(g);
93 94
    }
94 95

	
95 96
    ///The type of the map that stores the distances of the nodes.
96 97

	
97 98
    ///The type of the map that stores the distances of the nodes.
98 99
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
99 100
    typedef typename Digraph::template NodeMap<int> DistMap;
100 101
    ///Instantiates a \ref DistMap.
101 102

	
102 103
    ///This function instantiates a \ref DistMap.
103 104
    ///\param g is the digraph, to which we would like to define the
104 105
    ///\ref DistMap.
105 106
    static DistMap *createDistMap(const Digraph &g)
106 107
    {
107 108
      return new DistMap(g);
108 109
    }
109 110
  };
110 111

	
111 112
  ///%BFS algorithm class.
112 113

	
113 114
  ///\ingroup search
114 115
  ///This class provides an efficient implementation of the %BFS algorithm.
115 116
  ///
116
  ///There is also a \ref bfs() "function type interface" for the BFS
117
  ///There is also a \ref bfs() "function-type interface" for the BFS
117 118
  ///algorithm, which is convenient in the simplier cases and it can be
118 119
  ///used easier.
119 120
  ///
120 121
  ///\tparam GR The type of the digraph the algorithm runs on.
121 122
  ///The default value is \ref ListDigraph. The value of GR is not used
122 123
  ///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits.
123 124
  ///\tparam TR Traits class to set various data types used by the algorithm.
124 125
  ///The default traits class is
125 126
  ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
126 127
  ///See \ref BfsDefaultTraits for the documentation of
127 128
  ///a Bfs traits class.
128 129
#ifdef DOXYGEN
129 130
  template <typename GR,
130 131
            typename TR>
131 132
#else
132 133
  template <typename GR=ListDigraph,
133 134
            typename TR=BfsDefaultTraits<GR> >
134 135
#endif
135 136
  class Bfs {
136 137
  public:
137 138
    ///\ref Exception for uninitialized parameters.
138 139

	
139 140
    ///This error represents problems in the initialization of the
140 141
    ///parameters of the algorithm.
141 142
    class UninitializedParameter : public lemon::UninitializedParameter {
142 143
    public:
143 144
      virtual const char* what() const throw() {
144 145
        return "lemon::Bfs::UninitializedParameter";
145 146
      }
146 147
    };
147 148

	
148 149
    ///The type of the digraph the algorithm runs on.
149 150
    typedef typename TR::Digraph Digraph;
150 151

	
151 152
    ///\brief The type of the map that stores the predecessor arcs of the
152 153
    ///shortest paths.
153 154
    typedef typename TR::PredMap PredMap;
154 155
    ///The type of the map that stores the distances of the nodes.
155 156
    typedef typename TR::DistMap DistMap;
156 157
    ///The type of the map that indicates which nodes are reached.
157 158
    typedef typename TR::ReachedMap ReachedMap;
158 159
    ///The type of the map that indicates which nodes are processed.
159 160
    typedef typename TR::ProcessedMap ProcessedMap;
160 161
    ///The type of the paths.
161 162
    typedef PredMapPath<Digraph, PredMap> Path;
162 163

	
163 164
    ///The traits class.
164 165
    typedef TR Traits;
165 166

	
166 167
  private:
167 168

	
168 169
    typedef typename Digraph::Node Node;
169 170
    typedef typename Digraph::NodeIt NodeIt;
170 171
    typedef typename Digraph::Arc Arc;
171 172
    typedef typename Digraph::OutArcIt OutArcIt;
172 173

	
173 174
    //Pointer to the underlying digraph.
174 175
    const Digraph *G;
175 176
    //Pointer to the map of predecessor arcs.
176 177
    PredMap *_pred;
177 178
    //Indicates if _pred is locally allocated (true) or not.
178 179
    bool local_pred;
179 180
    //Pointer to the map of distances.
180 181
    DistMap *_dist;
181 182
    //Indicates if _dist is locally allocated (true) or not.
182 183
    bool local_dist;
183 184
    //Pointer to the map of reached status of the nodes.
184 185
    ReachedMap *_reached;
185 186
    //Indicates if _reached is locally allocated (true) or not.
186 187
    bool local_reached;
187 188
    //Pointer to the map of processed status of the nodes.
188 189
    ProcessedMap *_processed;
189 190
    //Indicates if _processed is locally allocated (true) or not.
190 191
    bool local_processed;
191 192

	
192 193
    std::vector<typename Digraph::Node> _queue;
193 194
    int _queue_head,_queue_tail,_queue_next_dist;
194 195
    int _curr_dist;
195 196

	
196 197
    //Creates the maps if necessary.
197 198
    void create_maps()
198 199
    {
199 200
      if(!_pred) {
200 201
        local_pred = true;
201 202
        _pred = Traits::createPredMap(*G);
202 203
      }
203 204
      if(!_dist) {
204 205
        local_dist = true;
205 206
        _dist = Traits::createDistMap(*G);
206 207
      }
207 208
      if(!_reached) {
208 209
        local_reached = true;
209 210
        _reached = Traits::createReachedMap(*G);
210 211
      }
211 212
      if(!_processed) {
212 213
        local_processed = true;
213 214
        _processed = Traits::createProcessedMap(*G);
214 215
      }
215 216
    }
216 217

	
217 218
  protected:
218 219

	
219 220
    Bfs() {}
220 221

	
221 222
  public:
222 223

	
223 224
    typedef Bfs Create;
224 225

	
225 226
    ///\name Named template parameters
226 227

	
227 228
    ///@{
228 229

	
229 230
    template <class T>
230 231
    struct SetPredMapTraits : public Traits {
231 232
      typedef T PredMap;
232 233
      static PredMap *createPredMap(const Digraph &)
233 234
      {
234 235
        throw UninitializedParameter();
235 236
      }
236 237
    };
237 238
    ///\brief \ref named-templ-param "Named parameter" for setting
238 239
    ///\ref PredMap type.
239 240
    ///
240 241
    ///\ref named-templ-param "Named parameter" for setting
241 242
    ///\ref PredMap type.
242 243
    template <class T>
243 244
    struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > {
244 245
      typedef Bfs< Digraph, SetPredMapTraits<T> > Create;
245 246
    };
246 247

	
247 248
    template <class T>
248 249
    struct SetDistMapTraits : public Traits {
249 250
      typedef T DistMap;
250 251
      static DistMap *createDistMap(const Digraph &)
251 252
      {
252 253
        throw UninitializedParameter();
253 254
      }
254 255
    };
255 256
    ///\brief \ref named-templ-param "Named parameter" for setting
256 257
    ///\ref DistMap type.
257 258
    ///
258 259
    ///\ref named-templ-param "Named parameter" for setting
259 260
    ///\ref DistMap type.
260 261
    template <class T>
261 262
    struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > {
262 263
      typedef Bfs< Digraph, SetDistMapTraits<T> > Create;
263 264
    };
264 265

	
265 266
    template <class T>
266 267
    struct SetReachedMapTraits : public Traits {
267 268
      typedef T ReachedMap;
268 269
      static ReachedMap *createReachedMap(const Digraph &)
269 270
      {
270 271
        throw UninitializedParameter();
271 272
      }
272 273
    };
273 274
    ///\brief \ref named-templ-param "Named parameter" for setting
274 275
    ///\ref ReachedMap type.
275 276
    ///
276 277
    ///\ref named-templ-param "Named parameter" for setting
277 278
    ///\ref ReachedMap type.
278 279
    template <class T>
279 280
    struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
280 281
      typedef Bfs< Digraph, SetReachedMapTraits<T> > Create;
281 282
    };
282 283

	
283 284
    template <class T>
284 285
    struct SetProcessedMapTraits : public Traits {
285 286
      typedef T ProcessedMap;
286 287
      static ProcessedMap *createProcessedMap(const Digraph &)
287 288
      {
288 289
        throw UninitializedParameter();
289 290
      }
290 291
    };
291 292
    ///\brief \ref named-templ-param "Named parameter" for setting
292 293
    ///\ref ProcessedMap type.
293 294
    ///
294 295
    ///\ref named-templ-param "Named parameter" for setting
295 296
    ///\ref ProcessedMap type.
296 297
    template <class T>
297 298
    struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
298 299
      typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create;
299 300
    };
300 301

	
301 302
    struct SetStandardProcessedMapTraits : public Traits {
302 303
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
303 304
      static ProcessedMap *createProcessedMap(const Digraph &g)
304 305
      {
305 306
        return new ProcessedMap(g);
306 307
      }
307 308
    };
308 309
    ///\brief \ref named-templ-param "Named parameter" for setting
... ...
@@ -649,701 +650,738 @@
649 650
    ///    b.processNextNode(nm, rnode);
650 651
    ///  }
651 652
    ///  return rnode;
652 653
    ///\endcode
653 654
    template<class NodeBoolMap>
654 655
    Node start(const NodeBoolMap &nm)
655 656
    {
656 657
      Node rnode = INVALID;
657 658
      while ( !emptyQueue() && rnode == INVALID ) {
658 659
        processNextNode(nm, rnode);
659 660
      }
660 661
      return rnode;
661 662
    }
662 663

	
663 664
    ///Runs the algorithm from the given node.
664 665

	
665 666
    ///This method runs the %BFS algorithm from node \c s
666 667
    ///in order to compute the shortest path to each node.
667 668
    ///
668 669
    ///The algorithm computes
669 670
    ///- the shortest path tree,
670 671
    ///- the distance of each node from the root.
671 672
    ///
672 673
    ///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
673 674
    ///\code
674 675
    ///  b.init();
675 676
    ///  b.addSource(s);
676 677
    ///  b.start();
677 678
    ///\endcode
678 679
    void run(Node s) {
679 680
      init();
680 681
      addSource(s);
681 682
      start();
682 683
    }
683 684

	
684 685
    ///Finds the shortest path between \c s and \c t.
685 686

	
686 687
    ///This method runs the %BFS algorithm from node \c s
687 688
    ///in order to compute the shortest path to \c t.
688 689
    ///
689 690
    ///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path,
690 691
    ///if \c t is reachable form \c s, \c 0 otherwise.
691 692
    ///
692 693
    ///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a
693 694
    ///shortcut of the following code.
694 695
    ///\code
695 696
    ///  b.init();
696 697
    ///  b.addSource(s);
697 698
    ///  b.start(t);
698 699
    ///\endcode
699 700
    int run(Node s,Node t) {
700 701
      init();
701 702
      addSource(s);
702 703
      start(t);
703 704
      return reached(t) ? _curr_dist : 0;
704 705
    }
705 706

	
706 707
    ///Runs the algorithm to visit all nodes in the digraph.
707 708

	
708 709
    ///This method runs the %BFS algorithm in order to
709 710
    ///compute the shortest path to each node.
710 711
    ///
711 712
    ///The algorithm computes
712 713
    ///- the shortest path tree (forest),
713 714
    ///- the distance of each node from the root(s).
714 715
    ///
715 716
    ///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
716 717
    ///\code
717 718
    ///  b.init();
718 719
    ///  for (NodeIt n(gr); n != INVALID; ++n) {
719 720
    ///    if (!b.reached(n)) {
720 721
    ///      b.addSource(n);
721 722
    ///      b.start();
722 723
    ///    }
723 724
    ///  }
724 725
    ///\endcode
725 726
    void run() {
726 727
      init();
727 728
      for (NodeIt n(*G); n != INVALID; ++n) {
728 729
        if (!reached(n)) {
729 730
          addSource(n);
730 731
          start();
731 732
        }
732 733
      }
733 734
    }
734 735

	
735 736
    ///@}
736 737

	
737 738
    ///\name Query Functions
738 739
    ///The result of the %BFS algorithm can be obtained using these
739 740
    ///functions.\n
740 741
    ///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start()
741 742
    ///"start()" must be called before using them.
742 743

	
743 744
    ///@{
744 745

	
745 746
    ///The shortest path to a node.
746 747

	
747 748
    ///Returns the shortest path to a node.
748 749
    ///
749 750
    ///\warning \c t should be reachable from the root(s).
750 751
    ///
751 752
    ///\pre Either \ref run() or \ref start() must be called before
752 753
    ///using this function.
753 754
    Path path(Node t) const { return Path(*G, *_pred, t); }
754 755

	
755 756
    ///The distance of a node from the root(s).
756 757

	
757 758
    ///Returns the distance of a node from the root(s).
758 759
    ///
759 760
    ///\warning If node \c v is not reachable from the root(s), then
760 761
    ///the return value of this function is undefined.
761 762
    ///
762 763
    ///\pre Either \ref run() or \ref start() must be called before
763 764
    ///using this function.
764 765
    int dist(Node v) const { return (*_dist)[v]; }
765 766

	
766 767
    ///Returns the 'previous arc' of the shortest path tree for a node.
767 768

	
768 769
    ///This function returns the 'previous arc' of the shortest path
769 770
    ///tree for the node \c v, i.e. it returns the last arc of a
770 771
    ///shortest path from the root(s) to \c v. It is \c INVALID if \c v
771 772
    ///is not reachable from the root(s) or if \c v is a root.
772 773
    ///
773 774
    ///The shortest path tree used here is equal to the shortest path
774 775
    ///tree used in \ref predNode().
775 776
    ///
776 777
    ///\pre Either \ref run() or \ref start() must be called before
777 778
    ///using this function.
778 779
    Arc predArc(Node v) const { return (*_pred)[v];}
779 780

	
780 781
    ///Returns the 'previous node' of the shortest path tree for a node.
781 782

	
782 783
    ///This function returns the 'previous node' of the shortest path
783 784
    ///tree for the node \c v, i.e. it returns the last but one node
784 785
    ///from a shortest path from the root(s) to \c v. It is \c INVALID
785 786
    ///if \c v is not reachable from the root(s) or if \c v is a root.
786 787
    ///
787 788
    ///The shortest path tree used here is equal to the shortest path
788 789
    ///tree used in \ref predArc().
789 790
    ///
790 791
    ///\pre Either \ref run() or \ref start() must be called before
791 792
    ///using this function.
792 793
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
793 794
                                  G->source((*_pred)[v]); }
794 795

	
795 796
    ///\brief Returns a const reference to the node map that stores the
796 797
    /// distances of the nodes.
797 798
    ///
798 799
    ///Returns a const reference to the node map that stores the distances
799 800
    ///of the nodes calculated by the algorithm.
800 801
    ///
801 802
    ///\pre Either \ref run() or \ref init()
802 803
    ///must be called before using this function.
803 804
    const DistMap &distMap() const { return *_dist;}
804 805

	
805 806
    ///\brief Returns a const reference to the node map that stores the
806 807
    ///predecessor arcs.
807 808
    ///
808 809
    ///Returns a const reference to the node map that stores the predecessor
809 810
    ///arcs, which form the shortest path tree.
810 811
    ///
811 812
    ///\pre Either \ref run() or \ref init()
812 813
    ///must be called before using this function.
813 814
    const PredMap &predMap() const { return *_pred;}
814 815

	
815 816
    ///Checks if a node is reachable from the root(s).
816 817

	
817 818
    ///Returns \c true if \c v is reachable from the root(s).
818 819
    ///\pre Either \ref run() or \ref start()
819 820
    ///must be called before using this function.
820 821
    bool reached(Node v) const { return (*_reached)[v]; }
821 822

	
822 823
    ///@}
823 824
  };
824 825

	
825 826
  ///Default traits class of bfs() function.
826 827

	
827 828
  ///Default traits class of bfs() function.
828 829
  ///\tparam GR Digraph type.
829 830
  template<class GR>
830 831
  struct BfsWizardDefaultTraits
831 832
  {
832 833
    ///The type of the digraph the algorithm runs on.
833 834
    typedef GR Digraph;
834 835

	
835 836
    ///\brief The type of the map that stores the predecessor
836 837
    ///arcs of the shortest paths.
837 838
    ///
838 839
    ///The type of the map that stores the predecessor
839 840
    ///arcs of the shortest paths.
840 841
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
841
    typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap;
842
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
842 843
    ///Instantiates a \ref PredMap.
843 844

	
844 845
    ///This function instantiates a \ref PredMap.
845 846
    ///\param g is the digraph, to which we would like to define the
846 847
    ///\ref PredMap.
847
#ifdef DOXYGEN
848 848
    static PredMap *createPredMap(const Digraph &g)
849
#else
850
    static PredMap *createPredMap(const Digraph &)
851
#endif
852 849
    {
853
      return new PredMap();
850
      return new PredMap(g);
854 851
    }
855 852

	
856 853
    ///The type of the map that indicates which nodes are processed.
857 854

	
858 855
    ///The type of the map that indicates which nodes are processed.
859 856
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
857
    ///By default it is a NullMap.
860 858
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
861 859
    ///Instantiates a \ref ProcessedMap.
862 860

	
863 861
    ///This function instantiates a \ref ProcessedMap.
864 862
    ///\param g is the digraph, to which
865 863
    ///we would like to define the \ref ProcessedMap.
866 864
#ifdef DOXYGEN
867 865
    static ProcessedMap *createProcessedMap(const Digraph &g)
868 866
#else
869 867
    static ProcessedMap *createProcessedMap(const Digraph &)
870 868
#endif
871 869
    {
872 870
      return new ProcessedMap();
873 871
    }
874 872

	
875 873
    ///The type of the map that indicates which nodes are reached.
876 874

	
877 875
    ///The type of the map that indicates which nodes are reached.
878 876
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
879 877
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
880 878
    ///Instantiates a \ref ReachedMap.
881 879

	
882 880
    ///This function instantiates a \ref ReachedMap.
883 881
    ///\param g is the digraph, to which
884 882
    ///we would like to define the \ref ReachedMap.
885 883
    static ReachedMap *createReachedMap(const Digraph &g)
886 884
    {
887 885
      return new ReachedMap(g);
888 886
    }
889 887

	
890 888
    ///The type of the map that stores the distances of the nodes.
891 889

	
892 890
    ///The type of the map that stores the distances of the nodes.
893 891
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
894
    ///
895
    typedef NullMap<typename Digraph::Node,int> DistMap;
892
    typedef typename Digraph::template NodeMap<int> DistMap;
896 893
    ///Instantiates a \ref DistMap.
897 894

	
898 895
    ///This function instantiates a \ref DistMap.
899 896
    ///\param g is the digraph, to which we would like to define
900 897
    ///the \ref DistMap
901
#ifdef DOXYGEN
902 898
    static DistMap *createDistMap(const Digraph &g)
903
#else
904
    static DistMap *createDistMap(const Digraph &)
905
#endif
906 899
    {
907
      return new DistMap();
900
      return new DistMap(g);
908 901
    }
902

	
903
    ///The type of the shortest paths.
904

	
905
    ///The type of the shortest paths.
906
    ///It must meet the \ref concepts::Path "Path" concept.
907
    typedef lemon::Path<Digraph> Path;
909 908
  };
910 909

	
911 910
  /// Default traits class used by \ref BfsWizard
912 911

	
913 912
  /// To make it easier to use Bfs algorithm
914 913
  /// we have created a wizard class.
915 914
  /// This \ref BfsWizard class needs default traits,
916 915
  /// as well as the \ref Bfs class.
917 916
  /// The \ref BfsWizardBase is a class to be the default traits of the
918 917
  /// \ref BfsWizard class.
919 918
  template<class GR>
920 919
  class BfsWizardBase : public BfsWizardDefaultTraits<GR>
921 920
  {
922 921

	
923 922
    typedef BfsWizardDefaultTraits<GR> Base;
924 923
  protected:
925 924
    //The type of the nodes in the digraph.
926 925
    typedef typename Base::Digraph::Node Node;
927 926

	
928 927
    //Pointer to the digraph the algorithm runs on.
929 928
    void *_g;
930 929
    //Pointer to the map of reached nodes.
931 930
    void *_reached;
932 931
    //Pointer to the map of processed nodes.
933 932
    void *_processed;
934 933
    //Pointer to the map of predecessors arcs.
935 934
    void *_pred;
936 935
    //Pointer to the map of distances.
937 936
    void *_dist;
938
    //Pointer to the source node.
939
    Node _source;
937
    //Pointer to the shortest path to the target node.
938
    void *_path;
939
    //Pointer to the distance of the target node.
940
    int *_di;
940 941

	
941 942
    public:
942 943
    /// Constructor.
943 944

	
944 945
    /// This constructor does not require parameters, therefore it initiates
945
    /// all of the attributes to default values (0, INVALID).
946
    /// all of the attributes to \c 0.
946 947
    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
947
                      _dist(0), _source(INVALID) {}
948
                      _dist(0), _path(0), _di(0) {}
948 949

	
949 950
    /// Constructor.
950 951

	
951
    /// This constructor requires some parameters,
952
    /// listed in the parameters list.
953
    /// Others are initiated to 0.
952
    /// This constructor requires one parameter,
953
    /// others are initiated to \c 0.
954 954
    /// \param g The digraph the algorithm runs on.
955
    /// \param s The source node.
956
    BfsWizardBase(const GR &g, Node s=INVALID) :
955
    BfsWizardBase(const GR &g) :
957 956
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
958
      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
957
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
959 958

	
960 959
  };
961 960

	
962
  /// Auxiliary class for the function type interface of BFS algorithm.
961
  /// Auxiliary class for the function-type interface of BFS algorithm.
963 962

	
964
  /// This auxiliary class is created to implement the function type
965
  /// interface of \ref Bfs algorithm. It uses the functions and features
966
  /// of the plain \ref Bfs, but it is much simpler to use it.
967
  /// It should only be used through the \ref bfs() function, which makes
968
  /// it easier to use the algorithm.
963
  /// This auxiliary class is created to implement the
964
  /// \ref bfs() "function-type interface" of \ref Bfs algorithm.
965
  /// It does not have own \ref run() method, it uses the functions
966
  /// and features of the plain \ref Bfs.
969 967
  ///
970
  /// Simplicity means that the way to change the types defined
971
  /// in the traits class is based on functions that returns the new class
972
  /// and not on templatable built-in classes.
973
  /// When using the plain \ref Bfs
974
  /// the new class with the modified type comes from
975
  /// the original class by using the ::
976
  /// operator. In the case of \ref BfsWizard only
977
  /// a function have to be called, and it will
978
  /// return the needed class.
979
  ///
980
  /// It does not have own \ref run() method. When its \ref run() method
981
  /// is called, it initiates a plain \ref Bfs object, and calls the
982
  /// \ref Bfs::run() method of it.
968
  /// This class should only be used through the \ref bfs() function,
969
  /// which makes it easier to use the algorithm.
983 970
  template<class TR>
984 971
  class BfsWizard : public TR
985 972
  {
986 973
    typedef TR Base;
987 974

	
988 975
    ///The type of the digraph the algorithm runs on.
989 976
    typedef typename TR::Digraph Digraph;
990 977

	
991 978
    typedef typename Digraph::Node Node;
992 979
    typedef typename Digraph::NodeIt NodeIt;
993 980
    typedef typename Digraph::Arc Arc;
994 981
    typedef typename Digraph::OutArcIt OutArcIt;
995 982

	
996 983
    ///\brief The type of the map that stores the predecessor
997 984
    ///arcs of the shortest paths.
998 985
    typedef typename TR::PredMap PredMap;
999 986
    ///\brief The type of the map that stores the distances of the nodes.
1000 987
    typedef typename TR::DistMap DistMap;
1001 988
    ///\brief The type of the map that indicates which nodes are reached.
1002 989
    typedef typename TR::ReachedMap ReachedMap;
1003 990
    ///\brief The type of the map that indicates which nodes are processed.
1004 991
    typedef typename TR::ProcessedMap ProcessedMap;
992
    ///The type of the shortest paths
993
    typedef typename TR::Path Path;
1005 994

	
1006 995
  public:
1007 996

	
1008 997
    /// Constructor.
1009 998
    BfsWizard() : TR() {}
1010 999

	
1011 1000
    /// Constructor that requires parameters.
1012 1001

	
1013 1002
    /// Constructor that requires parameters.
1014 1003
    /// These parameters will be the default values for the traits class.
1015
    BfsWizard(const Digraph &g, Node s=INVALID) :
1016
      TR(g,s) {}
1004
    /// \param g The digraph the algorithm runs on.
1005
    BfsWizard(const Digraph &g) :
1006
      TR(g) {}
1017 1007

	
1018 1008
    ///Copy constructor
1019 1009
    BfsWizard(const TR &b) : TR(b) {}
1020 1010

	
1021 1011
    ~BfsWizard() {}
1022 1012

	
1023
    ///Runs BFS algorithm from a source node.
1013
    ///Runs BFS algorithm from the given source node.
1024 1014

	
1025
    ///Runs BFS algorithm from a source node.
1026
    ///The node can be given with the \ref source() function.
1015
    ///This method runs BFS algorithm from node \c s
1016
    ///in order to compute the shortest path to each node.
1017
    void run(Node s)
1018
    {
1019
      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
1020
      if (Base::_pred)
1021
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1022
      if (Base::_dist)
1023
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1024
      if (Base::_reached)
1025
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
1026
      if (Base::_processed)
1027
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1028
      if (s!=INVALID)
1029
        alg.run(s);
1030
      else
1031
        alg.run();
1032
    }
1033

	
1034
    ///Finds the shortest path between \c s and \c t.
1035

	
1036
    ///This method runs BFS algorithm from node \c s
1037
    ///in order to compute the shortest path to node \c t
1038
    ///(it stops searching when \c t is processed).
1039
    ///
1040
    ///\return \c true if \c t is reachable form \c s.
1041
    bool run(Node s, Node t)
1042
    {
1043
      if (s==INVALID || t==INVALID) throw UninitializedParameter();
1044
      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
1045
      if (Base::_pred)
1046
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1047
      if (Base::_dist)
1048
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1049
      if (Base::_reached)
1050
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
1051
      if (Base::_processed)
1052
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1053
      alg.run(s,t);
1054
      if (Base::_path)
1055
        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
1056
      if (Base::_di)
1057
        *Base::_di = alg.dist(t);
1058
      return alg.reached(t);
1059
    }
1060

	
1061
    ///Runs BFS algorithm to visit all nodes in the digraph.
1062

	
1063
    ///This method runs BFS algorithm in order to compute
1064
    ///the shortest path to each node.
1027 1065
    void run()
1028 1066
    {
1029
      if(Base::_source==INVALID) throw UninitializedParameter();
1030
      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
1031
      if(Base::_reached)
1032
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
1033
      if(Base::_processed)
1034
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1035
      if(Base::_pred)
1036
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1037
      if(Base::_dist)
1038
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1039
      alg.run(Base::_source);
1040
    }
1041

	
1042
    ///Runs BFS algorithm from the given node.
1043

	
1044
    ///Runs BFS algorithm from the given node.
1045
    ///\param s is the given source.
1046
    void run(Node s)
1047
    {
1048
      Base::_source=s;
1049
      run();
1050
    }
1051

	
1052
    /// Sets the source node, from which the Bfs algorithm runs.
1053

	
1054
    /// Sets the source node, from which the Bfs algorithm runs.
1055
    /// \param s is the source node.
1056
    BfsWizard<TR> &source(Node s)
1057
    {
1058
      Base::_source=s;
1059
      return *this;
1067
      run(INVALID);
1060 1068
    }
1061 1069

	
1062 1070
    template<class T>
1063 1071
    struct SetPredMapBase : public Base {
1064 1072
      typedef T PredMap;
1065 1073
      static PredMap *createPredMap(const Digraph &) { return 0; };
1066 1074
      SetPredMapBase(const TR &b) : TR(b) {}
1067 1075
    };
1068
    ///\brief \ref named-templ-param "Named parameter"
1076
    ///\brief \ref named-func-param "Named parameter"
1069 1077
    ///for setting \ref PredMap object.
1070 1078
    ///
1071
    /// \ref named-templ-param "Named parameter"
1079
    ///\ref named-func-param "Named parameter"
1072 1080
    ///for setting \ref PredMap object.
1073 1081
    template<class T>
1074 1082
    BfsWizard<SetPredMapBase<T> > predMap(const T &t)
1075 1083
    {
1076 1084
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1077 1085
      return BfsWizard<SetPredMapBase<T> >(*this);
1078 1086
    }
1079 1087

	
1080 1088
    template<class T>
1081 1089
    struct SetReachedMapBase : public Base {
1082 1090
      typedef T ReachedMap;
1083 1091
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1084 1092
      SetReachedMapBase(const TR &b) : TR(b) {}
1085 1093
    };
1086
    ///\brief \ref named-templ-param "Named parameter"
1094
    ///\brief \ref named-func-param "Named parameter"
1087 1095
    ///for setting \ref ReachedMap object.
1088 1096
    ///
1089
    /// \ref named-templ-param "Named parameter"
1097
    /// \ref named-func-param "Named parameter"
1090 1098
    ///for setting \ref ReachedMap object.
1091 1099
    template<class T>
1092 1100
    BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1093 1101
    {
1094 1102
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1095 1103
      return BfsWizard<SetReachedMapBase<T> >(*this);
1096 1104
    }
1097 1105

	
1098 1106
    template<class T>
1107
    struct SetDistMapBase : public Base {
1108
      typedef T DistMap;
1109
      static DistMap *createDistMap(const Digraph &) { return 0; };
1110
      SetDistMapBase(const TR &b) : TR(b) {}
1111
    };
1112
    ///\brief \ref named-func-param "Named parameter"
1113
    ///for setting \ref DistMap object.
1114
    ///
1115
    /// \ref named-func-param "Named parameter"
1116
    ///for setting \ref DistMap object.
1117
    template<class T>
1118
    BfsWizard<SetDistMapBase<T> > distMap(const T &t)
1119
    {
1120
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1121
      return BfsWizard<SetDistMapBase<T> >(*this);
1122
    }
1123

	
1124
    template<class T>
1099 1125
    struct SetProcessedMapBase : public Base {
1100 1126
      typedef T ProcessedMap;
1101 1127
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1102 1128
      SetProcessedMapBase(const TR &b) : TR(b) {}
1103 1129
    };
1104
    ///\brief \ref named-templ-param "Named parameter"
1130
    ///\brief \ref named-func-param "Named parameter"
1105 1131
    ///for setting \ref ProcessedMap object.
1106 1132
    ///
1107
    /// \ref named-templ-param "Named parameter"
1133
    /// \ref named-func-param "Named parameter"
1108 1134
    ///for setting \ref ProcessedMap object.
1109 1135
    template<class T>
1110 1136
    BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1111 1137
    {
1112 1138
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1113 1139
      return BfsWizard<SetProcessedMapBase<T> >(*this);
1114 1140
    }
1115 1141

	
1116 1142
    template<class T>
1117
    struct SetDistMapBase : public Base {
1118
      typedef T DistMap;
1119
      static DistMap *createDistMap(const Digraph &) { return 0; };
1120
      SetDistMapBase(const TR &b) : TR(b) {}
1143
    struct SetPathBase : public Base {
1144
      typedef T Path;
1145
      SetPathBase(const TR &b) : TR(b) {}
1121 1146
    };
1122
    ///\brief \ref named-templ-param "Named parameter"
1123
    ///for setting \ref DistMap object.
1147
    ///\brief \ref named-func-param "Named parameter"
1148
    ///for getting the shortest path to the target node.
1124 1149
    ///
1125
    /// \ref named-templ-param "Named parameter"
1126
    ///for setting \ref DistMap object.
1150
    ///\ref named-func-param "Named parameter"
1151
    ///for getting the shortest path to the target node.
1127 1152
    template<class T>
1128
    BfsWizard<SetDistMapBase<T> > distMap(const T &t)
1153
    BfsWizard<SetPathBase<T> > path(const T &t)
1129 1154
    {
1130
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1131
      return BfsWizard<SetDistMapBase<T> >(*this);
1155
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1156
      return BfsWizard<SetPathBase<T> >(*this);
1157
    }
1158

	
1159
    ///\brief \ref named-func-param "Named parameter"
1160
    ///for getting the distance of the target node.
1161
    ///
1162
    ///\ref named-func-param "Named parameter"
1163
    ///for getting the distance of the target node.
1164
    BfsWizard dist(const int &d)
1165
    {
1166
      Base::_di=const_cast<int*>(&d);
1167
      return *this;
1132 1168
    }
1133 1169

	
1134 1170
  };
1135 1171

	
1136
  ///Function type interface for Bfs algorithm.
1172
  ///Function-type interface for BFS algorithm.
1137 1173

	
1138 1174
  /// \ingroup search
1139
  ///Function type interface for Bfs algorithm.
1175
  ///Function-type interface for BFS algorithm.
1140 1176
  ///
1141
  ///This function also has several
1142
  ///\ref named-templ-func-param "named parameters",
1177
  ///This function also has several \ref named-func-param "named parameters",
1143 1178
  ///they are declared as the members of class \ref BfsWizard.
1144
  ///The following
1145
  ///example shows how to use these parameters.
1179
  ///The following examples show how to use these parameters.
1146 1180
  ///\code
1147
  ///  bfs(g,source).predMap(preds).run();
1181
  ///  // Compute shortest path from node s to each node
1182
  ///  bfs(g).predMap(preds).distMap(dists).run(s);
1183
  ///
1184
  ///  // Compute shortest path from s to t
1185
  ///  bool reached = bfs(g).path(p).dist(d).run(s,t);
1148 1186
  ///\endcode
1149 1187
  ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
1150 1188
  ///to the end of the parameter list.
1151 1189
  ///\sa BfsWizard
1152 1190
  ///\sa Bfs
1153 1191
  template<class GR>
1154 1192
  BfsWizard<BfsWizardBase<GR> >
1155
  bfs(const GR &g,typename GR::Node s=INVALID)
1193
  bfs(const GR &digraph)
1156 1194
  {
1157
    return BfsWizard<BfsWizardBase<GR> >(g,s);
1195
    return BfsWizard<BfsWizardBase<GR> >(digraph);
1158 1196
  }
1159 1197

	
1160 1198
#ifdef DOXYGEN
1161 1199
  /// \brief Visitor class for BFS.
1162 1200
  ///
1163 1201
  /// This class defines the interface of the BfsVisit events, and
1164 1202
  /// it could be the base of a real visitor class.
1165 1203
  template <typename _Digraph>
1166 1204
  struct BfsVisitor {
1167 1205
    typedef _Digraph Digraph;
1168 1206
    typedef typename Digraph::Arc Arc;
1169 1207
    typedef typename Digraph::Node Node;
1170 1208
    /// \brief Called for the source node(s) of the BFS.
1171 1209
    ///
1172 1210
    /// This function is called for the source node(s) of the BFS.
1173 1211
    void start(const Node& node) {}
1174 1212
    /// \brief Called when a node is reached first time.
1175 1213
    ///
1176 1214
    /// This function is called when a node is reached first time.
1177 1215
    void reach(const Node& node) {}
1178 1216
    /// \brief Called when a node is processed.
1179 1217
    ///
1180 1218
    /// This function is called when a node is processed.
1181 1219
    void process(const Node& node) {}
1182 1220
    /// \brief Called when an arc reaches a new node.
1183 1221
    ///
1184 1222
    /// This function is called when the BFS finds an arc whose target node
1185 1223
    /// is not reached yet.
1186 1224
    void discover(const Arc& arc) {}
1187 1225
    /// \brief Called when an arc is examined but its target node is
1188 1226
    /// already discovered.
1189 1227
    ///
1190 1228
    /// This function is called when an arc is examined but its target node is
1191 1229
    /// already discovered.
1192 1230
    void examine(const Arc& arc) {}
1193 1231
  };
1194 1232
#else
1195 1233
  template <typename _Digraph>
1196 1234
  struct BfsVisitor {
1197 1235
    typedef _Digraph Digraph;
1198 1236
    typedef typename Digraph::Arc Arc;
1199 1237
    typedef typename Digraph::Node Node;
1200 1238
    void start(const Node&) {}
1201 1239
    void reach(const Node&) {}
1202 1240
    void process(const Node&) {}
1203 1241
    void discover(const Arc&) {}
1204 1242
    void examine(const Arc&) {}
1205 1243

	
1206 1244
    template <typename _Visitor>
1207 1245
    struct Constraints {
1208 1246
      void constraints() {
1209 1247
        Arc arc;
1210 1248
        Node node;
1211 1249
        visitor.start(node);
1212 1250
        visitor.reach(node);
1213 1251
        visitor.process(node);
1214 1252
        visitor.discover(arc);
1215 1253
        visitor.examine(arc);
1216 1254
      }
1217 1255
      _Visitor& visitor;
1218 1256
    };
1219 1257
  };
1220 1258
#endif
1221 1259

	
1222 1260
  /// \brief Default traits class of BfsVisit class.
1223 1261
  ///
1224 1262
  /// Default traits class of BfsVisit class.
1225 1263
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1226 1264
  template<class _Digraph>
1227 1265
  struct BfsVisitDefaultTraits {
1228 1266

	
1229 1267
    /// \brief The type of the digraph the algorithm runs on.
1230 1268
    typedef _Digraph Digraph;
1231 1269

	
1232 1270
    /// \brief The type of the map that indicates which nodes are reached.
1233 1271
    ///
1234 1272
    /// The type of the map that indicates which nodes are reached.
1235 1273
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1236 1274
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1237 1275

	
1238 1276
    /// \brief Instantiates a \ref ReachedMap.
1239 1277
    ///
1240 1278
    /// This function instantiates a \ref ReachedMap.
1241 1279
    /// \param digraph is the digraph, to which
1242 1280
    /// we would like to define the \ref ReachedMap.
1243 1281
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1244 1282
      return new ReachedMap(digraph);
1245 1283
    }
1246 1284

	
1247 1285
  };
1248 1286

	
1249 1287
  /// \ingroup search
1250 1288
  ///
1251 1289
  /// \brief %BFS algorithm class with visitor interface.
1252 1290
  ///
1253 1291
  /// This class provides an efficient implementation of the %BFS algorithm
1254 1292
  /// with visitor interface.
1255 1293
  ///
1256 1294
  /// The %BfsVisit class provides an alternative interface to the Bfs
1257 1295
  /// class. It works with callback mechanism, the BfsVisit object calls
1258 1296
  /// the member functions of the \c Visitor class on every BFS event.
1259 1297
  ///
1260 1298
  /// This interface of the BFS algorithm should be used in special cases
1261 1299
  /// when extra actions have to be performed in connection with certain
1262 1300
  /// events of the BFS algorithm. Otherwise consider to use Bfs or bfs()
1263 1301
  /// instead.
1264 1302
  ///
1265 1303
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1266 1304
  /// The default value is
1267 1305
  /// \ref ListDigraph. The value of _Digraph is not used directly by
1268 1306
  /// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits.
1269 1307
  /// \tparam _Visitor The Visitor type that is used by the algorithm.
1270 1308
  /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which
1271 1309
  /// does not observe the BFS events. If you want to observe the BFS
1272 1310
  /// events, you should implement your own visitor class.
1273 1311
  /// \tparam _Traits Traits class to set various data types used by the
1274 1312
  /// algorithm. The default traits class is
1275 1313
  /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
1276 1314
  /// See \ref BfsVisitDefaultTraits for the documentation of
1277 1315
  /// a BFS visit traits class.
1278 1316
#ifdef DOXYGEN
1279 1317
  template <typename _Digraph, typename _Visitor, typename _Traits>
1280 1318
#else
1281 1319
  template <typename _Digraph = ListDigraph,
1282 1320
            typename _Visitor = BfsVisitor<_Digraph>,
1283 1321
            typename _Traits = BfsDefaultTraits<_Digraph> >
1284 1322
#endif
1285 1323
  class BfsVisit {
1286 1324
  public:
1287 1325

	
1288 1326
    /// \brief \ref Exception for uninitialized parameters.
1289 1327
    ///
1290 1328
    /// This error represents problems in the initialization
1291 1329
    /// of the parameters of the algorithm.
1292 1330
    class UninitializedParameter : public lemon::UninitializedParameter {
1293 1331
    public:
1294 1332
      virtual const char* what() const throw()
1295 1333
      {
1296 1334
        return "lemon::BfsVisit::UninitializedParameter";
1297 1335
      }
1298 1336
    };
1299 1337

	
1300 1338
    ///The traits class.
1301 1339
    typedef _Traits Traits;
1302 1340

	
1303 1341
    ///The type of the digraph the algorithm runs on.
1304 1342
    typedef typename Traits::Digraph Digraph;
1305 1343

	
1306 1344
    ///The visitor type used by the algorithm.
1307 1345
    typedef _Visitor Visitor;
1308 1346

	
1309 1347
    ///The type of the map that indicates which nodes are reached.
1310 1348
    typedef typename Traits::ReachedMap ReachedMap;
1311 1349

	
1312 1350
  private:
1313 1351

	
1314 1352
    typedef typename Digraph::Node Node;
1315 1353
    typedef typename Digraph::NodeIt NodeIt;
1316 1354
    typedef typename Digraph::Arc Arc;
1317 1355
    typedef typename Digraph::OutArcIt OutArcIt;
1318 1356

	
1319 1357
    //Pointer to the underlying digraph.
1320 1358
    const Digraph *_digraph;
1321 1359
    //Pointer to the visitor object.
1322 1360
    Visitor *_visitor;
1323 1361
    //Pointer to the map of reached status of the nodes.
1324 1362
    ReachedMap *_reached;
1325 1363
    //Indicates if _reached is locally allocated (true) or not.
1326 1364
    bool local_reached;
1327 1365

	
1328 1366
    std::vector<typename Digraph::Node> _list;
1329 1367
    int _list_front, _list_back;
1330 1368

	
1331 1369
    //Creates the maps if necessary.
1332 1370
    void create_maps() {
1333 1371
      if(!_reached) {
1334 1372
        local_reached = true;
1335 1373
        _reached = Traits::createReachedMap(*_digraph);
1336 1374
      }
1337 1375
    }
1338 1376

	
1339 1377
  protected:
1340 1378

	
1341 1379
    BfsVisit() {}
1342 1380

	
1343 1381
  public:
1344 1382

	
1345 1383
    typedef BfsVisit Create;
1346 1384

	
1347 1385
    /// \name Named template parameters
1348 1386

	
1349 1387
    ///@{
Ignore white space 384 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\ingroup concept
20 20
///\file
21 21
///\brief Classes for representing paths in digraphs.
22 22
///
23 23

	
24 24
#ifndef LEMON_CONCEPT_PATH_H
25 25
#define LEMON_CONCEPT_PATH_H
26 26

	
27 27
#include <lemon/core.h>
28 28
#include <lemon/concept_check.h>
29 29

	
30 30
namespace lemon {
31 31
  namespace concepts {
32 32

	
33 33
    /// \addtogroup concept
34 34
    /// @{
35 35

	
36 36
    /// \brief A skeleton structure for representing directed paths in
37 37
    /// a digraph.
38 38
    ///
39 39
    /// A skeleton structure for representing directed paths in a
40 40
    /// digraph.
41 41
    /// \tparam _Digraph The digraph type in which the path is.
42 42
    ///
43 43
    /// In a sense, the path can be treated as a list of arcs. The
44 44
    /// lemon path type stores just this list. As a consequence it
45 45
    /// cannot enumerate the nodes in the path and the zero length
46 46
    /// paths cannot store the source.
47 47
    ///
48 48
    template <typename _Digraph>
49 49
    class Path {
50 50
    public:
51 51

	
52 52
      /// Type of the underlying digraph.
53 53
      typedef _Digraph Digraph;
54 54
      /// Arc type of the underlying digraph.
55 55
      typedef typename Digraph::Arc Arc;
56 56

	
57 57
      class ArcIt;
58 58

	
59 59
      /// \brief Default constructor
60 60
      Path() {}
61 61

	
62 62
      /// \brief Template constructor
63 63
      template <typename CPath>
64 64
      Path(const CPath& cpath) {}
65 65

	
66 66
      /// \brief Template assigment
67 67
      template <typename CPath>
68
      Path& operator=(const CPath& cpath) {}
68
      Path& operator=(const CPath& cpath) {
69
        ignore_unused_variable_warning(cpath);
70
        return *this;
71
      }
69 72

	
70 73
      /// Length of the path ie. the number of arcs in the path.
71 74
      int length() const { return 0;}
72 75

	
73 76
      /// Returns whether the path is empty.
74 77
      bool empty() const { return true;}
75 78

	
76 79
      /// Resets the path to an empty path.
77 80
      void clear() {}
78 81

	
79 82
      /// \brief LEMON style iterator for path arcs
80 83
      ///
81 84
      /// This class is used to iterate on the arcs of the paths.
82 85
      class ArcIt {
83 86
      public:
84 87
        /// Default constructor
85 88
        ArcIt() {}
86 89
        /// Invalid constructor
87 90
        ArcIt(Invalid) {}
88 91
        /// Constructor for first arc
89 92
        ArcIt(const Path &) {}
90 93

	
91 94
        /// Conversion to Arc
92 95
        operator Arc() const { return INVALID; }
93 96

	
94 97
        /// Next arc
95 98
        ArcIt& operator++() {return *this;}
96 99

	
97 100
        /// Comparison operator
98 101
        bool operator==(const ArcIt&) const {return true;}
99 102
        /// Comparison operator
100 103
        bool operator!=(const ArcIt&) const {return true;}
101 104
        /// Comparison operator
102 105
        bool operator<(const ArcIt&) const {return false;}
103 106

	
104 107
      };
105 108

	
106 109
      template <typename _Path>
107 110
      struct Constraints {
108 111
        void constraints() {
109 112
          Path<Digraph> pc;
110 113
          _Path p, pp(pc);
111 114
          int l = p.length();
112 115
          int e = p.empty();
113 116
          p.clear();
114 117

	
115 118
          p = pc;
116 119

	
117 120
          typename _Path::ArcIt id, ii(INVALID), i(p);
118 121

	
119 122
          ++i;
120 123
          typename Digraph::Arc ed = i;
121 124

	
122 125
          e = (i == ii);
123 126
          e = (i != ii);
124 127
          e = (i < ii);
125 128

	
126 129
          ignore_unused_variable_warning(l);
127 130
          ignore_unused_variable_warning(pp);
128 131
          ignore_unused_variable_warning(e);
129 132
          ignore_unused_variable_warning(id);
130 133
          ignore_unused_variable_warning(ii);
131 134
          ignore_unused_variable_warning(ed);
132 135
        }
133 136
      };
134 137

	
135 138
    };
136 139

	
137 140
    namespace _path_bits {
138 141

	
139 142
      template <typename _Digraph, typename _Path, typename RevPathTag = void>
140 143
      struct PathDumperConstraints {
141 144
        void constraints() {
142 145
          int l = p.length();
143 146
          int e = p.empty();
144 147

	
145 148
          typename _Path::ArcIt id, i(p);
146 149

	
147 150
          ++i;
148 151
          typename _Digraph::Arc ed = i;
149 152

	
150 153
          e = (i == INVALID);
151 154
          e = (i != INVALID);
152 155

	
153 156
          ignore_unused_variable_warning(l);
154 157
          ignore_unused_variable_warning(e);
155 158
          ignore_unused_variable_warning(id);
156 159
          ignore_unused_variable_warning(ed);
157 160
        }
158 161
        _Path& p;
159 162
      };
160 163

	
161 164
      template <typename _Digraph, typename _Path>
162 165
      struct PathDumperConstraints<
163 166
        _Digraph, _Path,
164 167
        typename enable_if<typename _Path::RevPathTag, void>::type
165 168
      > {
166 169
        void constraints() {
167 170
          int l = p.length();
168 171
          int e = p.empty();
169 172

	
170 173
          typename _Path::RevArcIt id, i(p);
171 174

	
172 175
          ++i;
173 176
          typename _Digraph::Arc ed = i;
174 177

	
175 178
          e = (i == INVALID);
176 179
          e = (i != INVALID);
177 180

	
178 181
          ignore_unused_variable_warning(l);
179 182
          ignore_unused_variable_warning(e);
180 183
          ignore_unused_variable_warning(id);
181 184
          ignore_unused_variable_warning(ed);
182 185
        }
183 186
        _Path& p;
184 187
      };
185 188

	
186 189
    }
187 190

	
188 191

	
189 192
    /// \brief A skeleton structure for path dumpers.
190 193
    ///
191 194
    /// A skeleton structure for path dumpers. The path dumpers are
192 195
    /// the generalization of the paths. The path dumpers can
193 196
    /// enumerate the arcs of the path wheter in forward or in
194 197
    /// backward order.  In most time these classes are not used
195 198
    /// directly rather it used to assign a dumped class to a real
196 199
    /// path type.
197 200
    ///
198 201
    /// The main purpose of this concept is that the shortest path
199 202
    /// algorithms can enumerate easily the arcs in reverse order.
200 203
    /// If we would like to give back a real path from these
201 204
    /// algorithms then we should create a temporarly path object. In
202 205
    /// LEMON such algorithms gives back a path dumper what can
203 206
    /// assigned to a real path and the dumpers can be implemented as
204 207
    /// an adaptor class to the predecessor map.
205 208

	
206 209
    /// \tparam _Digraph  The digraph type in which the path is.
207 210
    ///
208 211
    /// The paths can be constructed from any path type by a
209 212
    /// template constructor or a template assignment operator.
210 213
    ///
211 214
    template <typename _Digraph>
212 215
    class PathDumper {
213 216
    public:
214 217

	
215 218
      /// Type of the underlying digraph.
216 219
      typedef _Digraph Digraph;
217 220
      /// Arc type of the underlying digraph.
218 221
      typedef typename Digraph::Arc Arc;
219 222

	
220 223
      /// Length of the path ie. the number of arcs in the path.
221 224
      int length() const { return 0;}
222 225

	
223 226
      /// Returns whether the path is empty.
224 227
      bool empty() const { return true;}
225 228

	
226 229
      /// \brief Forward or reverse dumping
227 230
      ///
228 231
      /// If the RevPathTag is defined and true then reverse dumping
229 232
      /// is provided in the path dumper. In this case instead of the
230 233
      /// ArcIt the RevArcIt iterator should be implemented in the
231 234
      /// dumper.
232 235
      typedef False RevPathTag;
233 236

	
234 237
      /// \brief LEMON style iterator for path arcs
235 238
      ///
236 239
      /// This class is used to iterate on the arcs of the paths.
237 240
      class ArcIt {
238 241
      public:
239 242
        /// Default constructor
240 243
        ArcIt() {}
241 244
        /// Invalid constructor
242 245
        ArcIt(Invalid) {}
243 246
        /// Constructor for first arc
244 247
        ArcIt(const PathDumper&) {}
245 248

	
246 249
        /// Conversion to Arc
247 250
        operator Arc() const { return INVALID; }
248 251

	
249 252
        /// Next arc
250 253
        ArcIt& operator++() {return *this;}
251 254

	
252 255
        /// Comparison operator
253 256
        bool operator==(const ArcIt&) const {return true;}
254 257
        /// Comparison operator
255 258
        bool operator!=(const ArcIt&) const {return true;}
256 259
        /// Comparison operator
257 260
        bool operator<(const ArcIt&) const {return false;}
258 261

	
259 262
      };
260 263

	
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_DFS_H
20 20
#define LEMON_DFS_H
21 21

	
22 22
///\ingroup search
23 23
///\file
24 24
///\brief DFS algorithm.
25 25

	
26 26
#include <lemon/list_graph.h>
27 27
#include <lemon/bits/path_dump.h>
28 28
#include <lemon/core.h>
29 29
#include <lemon/error.h>
30 30
#include <lemon/assert.h>
31 31
#include <lemon/maps.h>
32
#include <lemon/path.h>
32 33

	
33 34
namespace lemon {
34 35

	
35 36
  ///Default traits class of Dfs class.
36 37

	
37 38
  ///Default traits class of Dfs class.
38 39
  ///\tparam GR Digraph type.
39 40
  template<class GR>
40 41
  struct DfsDefaultTraits
41 42
  {
42 43
    ///The type of the digraph the algorithm runs on.
43 44
    typedef GR Digraph;
44 45

	
45 46
    ///\brief The type of the map that stores the predecessor
46 47
    ///arcs of the %DFS paths.
47 48
    ///
48 49
    ///The type of the map that stores the predecessor
49 50
    ///arcs of the %DFS paths.
50 51
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
51 52
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
52 53
    ///Instantiates a \ref PredMap.
53 54

	
54 55
    ///This function instantiates a \ref PredMap.
55 56
    ///\param g is the digraph, to which we would like to define the
56 57
    ///\ref PredMap.
57 58
    static PredMap *createPredMap(const Digraph &g)
58 59
    {
59 60
      return new PredMap(g);
60 61
    }
61 62

	
62 63
    ///The type of the map that indicates which nodes are processed.
63 64

	
64 65
    ///The type of the map that indicates which nodes are processed.
65 66
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
66 67
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
67 68
    ///Instantiates a \ref ProcessedMap.
68 69

	
69 70
    ///This function instantiates a \ref ProcessedMap.
70 71
    ///\param g is the digraph, to which
71 72
    ///we would like to define the \ref ProcessedMap
72 73
#ifdef DOXYGEN
73 74
    static ProcessedMap *createProcessedMap(const Digraph &g)
74 75
#else
75 76
    static ProcessedMap *createProcessedMap(const Digraph &)
76 77
#endif
77 78
    {
78 79
      return new ProcessedMap();
79 80
    }
80 81

	
81 82
    ///The type of the map that indicates which nodes are reached.
82 83

	
83 84
    ///The type of the map that indicates which nodes are reached.
84 85
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
85 86
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
86 87
    ///Instantiates a \ref ReachedMap.
87 88

	
88 89
    ///This function instantiates a \ref ReachedMap.
89 90
    ///\param g is the digraph, to which
90 91
    ///we would like to define the \ref ReachedMap.
91 92
    static ReachedMap *createReachedMap(const Digraph &g)
92 93
    {
93 94
      return new ReachedMap(g);
94 95
    }
95 96

	
96 97
    ///The type of the map that stores the distances of the nodes.
97 98

	
98 99
    ///The type of the map that stores the distances of the nodes.
99 100
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
100 101
    typedef typename Digraph::template NodeMap<int> DistMap;
101 102
    ///Instantiates a \ref DistMap.
102 103

	
103 104
    ///This function instantiates a \ref DistMap.
104 105
    ///\param g is the digraph, to which we would like to define the
105 106
    ///\ref DistMap.
106 107
    static DistMap *createDistMap(const Digraph &g)
107 108
    {
108 109
      return new DistMap(g);
109 110
    }
110 111
  };
111 112

	
112 113
  ///%DFS algorithm class.
113 114

	
114 115
  ///\ingroup search
115 116
  ///This class provides an efficient implementation of the %DFS algorithm.
116 117
  ///
117
  ///There is also a \ref dfs() "function type interface" for the DFS
118
  ///There is also a \ref dfs() "function-type interface" for the DFS
118 119
  ///algorithm, which is convenient in the simplier cases and it can be
119 120
  ///used easier.
120 121
  ///
121 122
  ///\tparam GR The type of the digraph the algorithm runs on.
122 123
  ///The default value is \ref ListDigraph. The value of GR is not used
123 124
  ///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits.
124 125
  ///\tparam TR Traits class to set various data types used by the algorithm.
125 126
  ///The default traits class is
126 127
  ///\ref DfsDefaultTraits "DfsDefaultTraits<GR>".
127 128
  ///See \ref DfsDefaultTraits for the documentation of
128 129
  ///a Dfs traits class.
129 130
#ifdef DOXYGEN
130 131
  template <typename GR,
131 132
            typename TR>
132 133
#else
133 134
  template <typename GR=ListDigraph,
134 135
            typename TR=DfsDefaultTraits<GR> >
135 136
#endif
136 137
  class Dfs {
137 138
  public:
138 139
    ///\ref Exception for uninitialized parameters.
139 140

	
140 141
    ///This error represents problems in the initialization of the
141 142
    ///parameters of the algorithm.
142 143
    class UninitializedParameter : public lemon::UninitializedParameter {
143 144
    public:
144 145
      virtual const char* what() const throw() {
145 146
        return "lemon::Dfs::UninitializedParameter";
146 147
      }
147 148
    };
148 149

	
149 150
    ///The type of the digraph the algorithm runs on.
150 151
    typedef typename TR::Digraph Digraph;
151 152

	
152 153
    ///\brief The type of the map that stores the predecessor arcs of the
153 154
    ///DFS paths.
154 155
    typedef typename TR::PredMap PredMap;
155 156
    ///The type of the map that stores the distances of the nodes.
156 157
    typedef typename TR::DistMap DistMap;
157 158
    ///The type of the map that indicates which nodes are reached.
158 159
    typedef typename TR::ReachedMap ReachedMap;
159 160
    ///The type of the map that indicates which nodes are processed.
160 161
    typedef typename TR::ProcessedMap ProcessedMap;
161 162
    ///The type of the paths.
162 163
    typedef PredMapPath<Digraph, PredMap> Path;
163 164

	
164 165
    ///The traits class.
165 166
    typedef TR Traits;
166 167

	
167 168
  private:
168 169

	
169 170
    typedef typename Digraph::Node Node;
170 171
    typedef typename Digraph::NodeIt NodeIt;
171 172
    typedef typename Digraph::Arc Arc;
172 173
    typedef typename Digraph::OutArcIt OutArcIt;
173 174

	
174 175
    //Pointer to the underlying digraph.
175 176
    const Digraph *G;
176 177
    //Pointer to the map of predecessor arcs.
177 178
    PredMap *_pred;
178 179
    //Indicates if _pred is locally allocated (true) or not.
179 180
    bool local_pred;
180 181
    //Pointer to the map of distances.
181 182
    DistMap *_dist;
182 183
    //Indicates if _dist is locally allocated (true) or not.
183 184
    bool local_dist;
184 185
    //Pointer to the map of reached status of the nodes.
185 186
    ReachedMap *_reached;
186 187
    //Indicates if _reached is locally allocated (true) or not.
187 188
    bool local_reached;
188 189
    //Pointer to the map of processed status of the nodes.
189 190
    ProcessedMap *_processed;
190 191
    //Indicates if _processed is locally allocated (true) or not.
191 192
    bool local_processed;
192 193

	
193 194
    std::vector<typename Digraph::OutArcIt> _stack;
194 195
    int _stack_head;
195 196

	
196 197
    //Creates the maps if necessary.
197 198
    void create_maps()
198 199
    {
199 200
      if(!_pred) {
200 201
        local_pred = true;
201 202
        _pred = Traits::createPredMap(*G);
202 203
      }
203 204
      if(!_dist) {
204 205
        local_dist = true;
205 206
        _dist = Traits::createDistMap(*G);
206 207
      }
207 208
      if(!_reached) {
208 209
        local_reached = true;
209 210
        _reached = Traits::createReachedMap(*G);
210 211
      }
211 212
      if(!_processed) {
212 213
        local_processed = true;
213 214
        _processed = Traits::createProcessedMap(*G);
214 215
      }
215 216
    }
216 217

	
217 218
  protected:
218 219

	
219 220
    Dfs() {}
220 221

	
221 222
  public:
222 223

	
223 224
    typedef Dfs Create;
224 225

	
225 226
    ///\name Named template parameters
226 227

	
227 228
    ///@{
228 229

	
229 230
    template <class T>
230 231
    struct SetPredMapTraits : public Traits {
231 232
      typedef T PredMap;
232 233
      static PredMap *createPredMap(const Digraph &)
233 234
      {
234 235
        throw UninitializedParameter();
235 236
      }
236 237
    };
237 238
    ///\brief \ref named-templ-param "Named parameter" for setting
238 239
    ///\ref PredMap type.
239 240
    ///
240 241
    ///\ref named-templ-param "Named parameter" for setting
241 242
    ///\ref PredMap type.
242 243
    template <class T>
243 244
    struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
244 245
      typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
245 246
    };
246 247

	
247 248
    template <class T>
248 249
    struct SetDistMapTraits : public Traits {
249 250
      typedef T DistMap;
250 251
      static DistMap *createDistMap(const Digraph &)
251 252
      {
252 253
        throw UninitializedParameter();
253 254
      }
254 255
    };
255 256
    ///\brief \ref named-templ-param "Named parameter" for setting
256 257
    ///\ref DistMap type.
257 258
    ///
258 259
    ///\ref named-templ-param "Named parameter" for setting
259 260
    ///\ref DistMap type.
260 261
    template <class T>
261 262
    struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
262 263
      typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
263 264
    };
264 265

	
265 266
    template <class T>
266 267
    struct SetReachedMapTraits : public Traits {
267 268
      typedef T ReachedMap;
268 269
      static ReachedMap *createReachedMap(const Digraph &)
269 270
      {
270 271
        throw UninitializedParameter();
271 272
      }
272 273
    };
273 274
    ///\brief \ref named-templ-param "Named parameter" for setting
274 275
    ///\ref ReachedMap type.
275 276
    ///
276 277
    ///\ref named-templ-param "Named parameter" for setting
277 278
    ///\ref ReachedMap type.
278 279
    template <class T>
279 280
    struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
280 281
      typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
281 282
    };
282 283

	
283 284
    template <class T>
284 285
    struct SetProcessedMapTraits : public Traits {
285 286
      typedef T ProcessedMap;
286 287
      static ProcessedMap *createProcessedMap(const Digraph &)
287 288
      {
288 289
        throw UninitializedParameter();
289 290
      }
290 291
    };
291 292
    ///\brief \ref named-templ-param "Named parameter" for setting
292 293
    ///\ref ProcessedMap type.
293 294
    ///
294 295
    ///\ref named-templ-param "Named parameter" for setting
295 296
    ///\ref ProcessedMap type.
296 297
    template <class T>
297 298
    struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
298 299
      typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
299 300
    };
300 301

	
301 302
    struct SetStandardProcessedMapTraits : public Traits {
302 303
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
303 304
      static ProcessedMap *createProcessedMap(const Digraph &g)
304 305
      {
305 306
        return new ProcessedMap(g);
306 307
      }
307 308
    };
308 309
    ///\brief \ref named-templ-param "Named parameter" for setting
309 310
    ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
... ...
@@ -583,702 +584,739 @@
583 584
    ///
584 585
    ///\pre init() must be called and a root node should be
585 586
    ///added with addSource() before using this function.
586 587
    ///
587 588
    ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
588 589
    ///not a node map.
589 590
    template<class ArcBoolMap>
590 591
    Arc start(const ArcBoolMap &am)
591 592
    {
592 593
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
593 594
        processNextArc();
594 595
      return emptyQueue() ? INVALID : _stack[_stack_head];
595 596
    }
596 597

	
597 598
    ///Runs the algorithm from the given node.
598 599

	
599 600
    ///This method runs the %DFS algorithm from node \c s
600 601
    ///in order to compute the DFS path to each node.
601 602
    ///
602 603
    ///The algorithm computes
603 604
    ///- the %DFS tree,
604 605
    ///- the distance of each node from the root in the %DFS tree.
605 606
    ///
606 607
    ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
607 608
    ///\code
608 609
    ///  d.init();
609 610
    ///  d.addSource(s);
610 611
    ///  d.start();
611 612
    ///\endcode
612 613
    void run(Node s) {
613 614
      init();
614 615
      addSource(s);
615 616
      start();
616 617
    }
617 618

	
618 619
    ///Finds the %DFS path between \c s and \c t.
619 620

	
620 621
    ///This method runs the %DFS algorithm from node \c s
621 622
    ///in order to compute the DFS path to \c t.
622 623
    ///
623 624
    ///\return The length of the <tt>s</tt>--<tt>t</tt> DFS path,
624 625
    ///if \c t is reachable form \c s, \c 0 otherwise.
625 626
    ///
626 627
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is
627 628
    ///just a shortcut of the following code.
628 629
    ///\code
629 630
    ///  d.init();
630 631
    ///  d.addSource(s);
631 632
    ///  d.start(t);
632 633
    ///\endcode
633 634
    int run(Node s,Node t) {
634 635
      init();
635 636
      addSource(s);
636 637
      start(t);
637 638
      return reached(t)?_stack_head+1:0;
638 639
    }
639 640

	
640 641
    ///Runs the algorithm to visit all nodes in the digraph.
641 642

	
642 643
    ///This method runs the %DFS algorithm in order to compute the
643 644
    ///%DFS path to each node.
644 645
    ///
645 646
    ///The algorithm computes
646 647
    ///- the %DFS tree,
647 648
    ///- the distance of each node from the root in the %DFS tree.
648 649
    ///
649 650
    ///\note <tt>d.run()</tt> is just a shortcut of the following code.
650 651
    ///\code
651 652
    ///  d.init();
652 653
    ///  for (NodeIt n(digraph); n != INVALID; ++n) {
653 654
    ///    if (!d.reached(n)) {
654 655
    ///      d.addSource(n);
655 656
    ///      d.start();
656 657
    ///    }
657 658
    ///  }
658 659
    ///\endcode
659 660
    void run() {
660 661
      init();
661 662
      for (NodeIt it(*G); it != INVALID; ++it) {
662 663
        if (!reached(it)) {
663 664
          addSource(it);
664 665
          start();
665 666
        }
666 667
      }
667 668
    }
668 669

	
669 670
    ///@}
670 671

	
671 672
    ///\name Query Functions
672 673
    ///The result of the %DFS algorithm can be obtained using these
673 674
    ///functions.\n
674 675
    ///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start()
675 676
    ///"start()" must be called before using them.
676 677

	
677 678
    ///@{
678 679

	
679 680
    ///The DFS path to a node.
680 681

	
681 682
    ///Returns the DFS path to a node.
682 683
    ///
683 684
    ///\warning \c t should be reachable from the root.
684 685
    ///
685 686
    ///\pre Either \ref run() or \ref start() must be called before
686 687
    ///using this function.
687 688
    Path path(Node t) const { return Path(*G, *_pred, t); }
688 689

	
689 690
    ///The distance of a node from the root.
690 691

	
691 692
    ///Returns the distance of a node from the root.
692 693
    ///
693 694
    ///\warning If node \c v is not reachable from the root, then
694 695
    ///the return value of this function is undefined.
695 696
    ///
696 697
    ///\pre Either \ref run() or \ref start() must be called before
697 698
    ///using this function.
698 699
    int dist(Node v) const { return (*_dist)[v]; }
699 700

	
700 701
    ///Returns the 'previous arc' of the %DFS tree for a node.
701 702

	
702 703
    ///This function returns the 'previous arc' of the %DFS tree for the
703 704
    ///node \c v, i.e. it returns the last arc of a %DFS path from the
704 705
    ///root to \c v. It is \c INVALID
705 706
    ///if \c v is not reachable from the root(s) or if \c v is a root.
706 707
    ///
707 708
    ///The %DFS tree used here is equal to the %DFS tree used in
708 709
    ///\ref predNode().
709 710
    ///
710 711
    ///\pre Either \ref run() or \ref start() must be called before using
711 712
    ///this function.
712 713
    Arc predArc(Node v) const { return (*_pred)[v];}
713 714

	
714 715
    ///Returns the 'previous node' of the %DFS tree.
715 716

	
716 717
    ///This function returns the 'previous node' of the %DFS
717 718
    ///tree for the node \c v, i.e. it returns the last but one node
718 719
    ///from a %DFS path from the root to \c v. It is \c INVALID
719 720
    ///if \c v is not reachable from the root(s) or if \c v is a root.
720 721
    ///
721 722
    ///The %DFS tree used here is equal to the %DFS tree used in
722 723
    ///\ref predArc().
723 724
    ///
724 725
    ///\pre Either \ref run() or \ref start() must be called before
725 726
    ///using this function.
726 727
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
727 728
                                  G->source((*_pred)[v]); }
728 729

	
729 730
    ///\brief Returns a const reference to the node map that stores the
730 731
    ///distances of the nodes.
731 732
    ///
732 733
    ///Returns a const reference to the node map that stores the
733 734
    ///distances of the nodes calculated by the algorithm.
734 735
    ///
735 736
    ///\pre Either \ref run() or \ref init()
736 737
    ///must be called before using this function.
737 738
    const DistMap &distMap() const { return *_dist;}
738 739

	
739 740
    ///\brief Returns a const reference to the node map that stores the
740 741
    ///predecessor arcs.
741 742
    ///
742 743
    ///Returns a const reference to the node map that stores the predecessor
743 744
    ///arcs, which form the DFS tree.
744 745
    ///
745 746
    ///\pre Either \ref run() or \ref init()
746 747
    ///must be called before using this function.
747 748
    const PredMap &predMap() const { return *_pred;}
748 749

	
749 750
    ///Checks if a node is reachable from the root(s).
750 751

	
751 752
    ///Returns \c true if \c v is reachable from the root(s).
752 753
    ///\pre Either \ref run() or \ref start()
753 754
    ///must be called before using this function.
754 755
    bool reached(Node v) const { return (*_reached)[v]; }
755 756

	
756 757
    ///@}
757 758
  };
758 759

	
759 760
  ///Default traits class of dfs() function.
760 761

	
761 762
  ///Default traits class of dfs() function.
762 763
  ///\tparam GR Digraph type.
763 764
  template<class GR>
764 765
  struct DfsWizardDefaultTraits
765 766
  {
766 767
    ///The type of the digraph the algorithm runs on.
767 768
    typedef GR Digraph;
768 769

	
769 770
    ///\brief The type of the map that stores the predecessor
770 771
    ///arcs of the %DFS paths.
771 772
    ///
772 773
    ///The type of the map that stores the predecessor
773 774
    ///arcs of the %DFS paths.
774 775
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
775
    ///
776
    typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap;
776
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
777 777
    ///Instantiates a \ref PredMap.
778 778

	
779 779
    ///This function instantiates a \ref PredMap.
780 780
    ///\param g is the digraph, to which we would like to define the
781 781
    ///\ref PredMap.
782
#ifdef DOXYGEN
783 782
    static PredMap *createPredMap(const Digraph &g)
784
#else
785
    static PredMap *createPredMap(const Digraph &)
786
#endif
787 783
    {
788
      return new PredMap();
784
      return new PredMap(g);
789 785
    }
790 786

	
791 787
    ///The type of the map that indicates which nodes are processed.
792 788

	
793 789
    ///The type of the map that indicates which nodes are processed.
794 790
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
791
    ///By default it is a NullMap.
795 792
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
796 793
    ///Instantiates a \ref ProcessedMap.
797 794

	
798 795
    ///This function instantiates a \ref ProcessedMap.
799 796
    ///\param g is the digraph, to which
800 797
    ///we would like to define the \ref ProcessedMap.
801 798
#ifdef DOXYGEN
802 799
    static ProcessedMap *createProcessedMap(const Digraph &g)
803 800
#else
804 801
    static ProcessedMap *createProcessedMap(const Digraph &)
805 802
#endif
806 803
    {
807 804
      return new ProcessedMap();
808 805
    }
809 806

	
810 807
    ///The type of the map that indicates which nodes are reached.
811 808

	
812 809
    ///The type of the map that indicates which nodes are reached.
813 810
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
814 811
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
815 812
    ///Instantiates a \ref ReachedMap.
816 813

	
817 814
    ///This function instantiates a \ref ReachedMap.
818 815
    ///\param g is the digraph, to which
819 816
    ///we would like to define the \ref ReachedMap.
820 817
    static ReachedMap *createReachedMap(const Digraph &g)
821 818
    {
822 819
      return new ReachedMap(g);
823 820
    }
824 821

	
825 822
    ///The type of the map that stores the distances of the nodes.
826 823

	
827 824
    ///The type of the map that stores the distances of the nodes.
828 825
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
829
    ///
830
    typedef NullMap<typename Digraph::Node,int> DistMap;
826
    typedef typename Digraph::template NodeMap<int> DistMap;
831 827
    ///Instantiates a \ref DistMap.
832 828

	
833 829
    ///This function instantiates a \ref DistMap.
834 830
    ///\param g is the digraph, to which we would like to define
835 831
    ///the \ref DistMap
836
#ifdef DOXYGEN
837 832
    static DistMap *createDistMap(const Digraph &g)
838
#else
839
    static DistMap *createDistMap(const Digraph &)
840
#endif
841 833
    {
842
      return new DistMap();
834
      return new DistMap(g);
843 835
    }
836

	
837
    ///The type of the DFS paths.
838

	
839
    ///The type of the DFS paths.
840
    ///It must meet the \ref concepts::Path "Path" concept.
841
    typedef lemon::Path<Digraph> Path;
844 842
  };
845 843

	
846 844
  /// Default traits class used by \ref DfsWizard
847 845

	
848 846
  /// To make it easier to use Dfs algorithm
849 847
  /// we have created a wizard class.
850 848
  /// This \ref DfsWizard class needs default traits,
851 849
  /// as well as the \ref Dfs class.
852 850
  /// The \ref DfsWizardBase is a class to be the default traits of the
853 851
  /// \ref DfsWizard class.
854 852
  template<class GR>
855 853
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
856 854
  {
857 855

	
858 856
    typedef DfsWizardDefaultTraits<GR> Base;
859 857
  protected:
860 858
    //The type of the nodes in the digraph.
861 859
    typedef typename Base::Digraph::Node Node;
862 860

	
863 861
    //Pointer to the digraph the algorithm runs on.
864 862
    void *_g;
865 863
    //Pointer to the map of reached nodes.
866 864
    void *_reached;
867 865
    //Pointer to the map of processed nodes.
868 866
    void *_processed;
869 867
    //Pointer to the map of predecessors arcs.
870 868
    void *_pred;
871 869
    //Pointer to the map of distances.
872 870
    void *_dist;
873
    //Pointer to the source node.
874
    Node _source;
871
    //Pointer to the DFS path to the target node.
872
    void *_path;
873
    //Pointer to the distance of the target node.
874
    int *_di;
875 875

	
876 876
    public:
877 877
    /// Constructor.
878 878

	
879 879
    /// This constructor does not require parameters, therefore it initiates
880
    /// all of the attributes to default values (0, INVALID).
880
    /// all of the attributes to \c 0.
881 881
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
882
                      _dist(0), _source(INVALID) {}
882
                      _dist(0), _path(0), _di(0) {}
883 883

	
884 884
    /// Constructor.
885 885

	
886
    /// This constructor requires some parameters,
887
    /// listed in the parameters list.
888
    /// Others are initiated to 0.
886
    /// This constructor requires one parameter,
887
    /// others are initiated to \c 0.
889 888
    /// \param g The digraph the algorithm runs on.
890
    /// \param s The source node.
891
    DfsWizardBase(const GR &g, Node s=INVALID) :
889
    DfsWizardBase(const GR &g) :
892 890
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
893
      _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
891
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
894 892

	
895 893
  };
896 894

	
897
  /// Auxiliary class for the function type interface of DFS algorithm.
895
  /// Auxiliary class for the function-type interface of DFS algorithm.
898 896

	
899
  /// This auxiliary class is created to implement the function type
900
  /// interface of \ref Dfs algorithm. It uses the functions and features
901
  /// of the plain \ref Dfs, but it is much simpler to use it.
902
  /// It should only be used through the \ref dfs() function, which makes
903
  /// it easier to use the algorithm.
897
  /// This auxiliary class is created to implement the
898
  /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
899
  /// It does not have own \ref run() method, it uses the functions
900
  /// and features of the plain \ref Dfs.
904 901
  ///
905
  /// Simplicity means that the way to change the types defined
906
  /// in the traits class is based on functions that returns the new class
907
  /// and not on templatable built-in classes.
908
  /// When using the plain \ref Dfs
909
  /// the new class with the modified type comes from
910
  /// the original class by using the ::
911
  /// operator. In the case of \ref DfsWizard only
912
  /// a function have to be called, and it will
913
  /// return the needed class.
914
  ///
915
  /// It does not have own \ref run() method. When its \ref run() method
916
  /// is called, it initiates a plain \ref Dfs object, and calls the
917
  /// \ref Dfs::run() method of it.
902
  /// This class should only be used through the \ref dfs() function,
903
  /// which makes it easier to use the algorithm.
918 904
  template<class TR>
919 905
  class DfsWizard : public TR
920 906
  {
921 907
    typedef TR Base;
922 908

	
923 909
    ///The type of the digraph the algorithm runs on.
924 910
    typedef typename TR::Digraph Digraph;
925 911

	
926 912
    typedef typename Digraph::Node Node;
927 913
    typedef typename Digraph::NodeIt NodeIt;
928 914
    typedef typename Digraph::Arc Arc;
929 915
    typedef typename Digraph::OutArcIt OutArcIt;
930 916

	
931 917
    ///\brief The type of the map that stores the predecessor
932
    ///arcs of the shortest paths.
918
    ///arcs of the DFS paths.
933 919
    typedef typename TR::PredMap PredMap;
934 920
    ///\brief The type of the map that stores the distances of the nodes.
935 921
    typedef typename TR::DistMap DistMap;
936 922
    ///\brief The type of the map that indicates which nodes are reached.
937 923
    typedef typename TR::ReachedMap ReachedMap;
938 924
    ///\brief The type of the map that indicates which nodes are processed.
939 925
    typedef typename TR::ProcessedMap ProcessedMap;
926
    ///The type of the DFS paths
927
    typedef typename TR::Path Path;
940 928

	
941 929
  public:
942 930

	
943 931
    /// Constructor.
944 932
    DfsWizard() : TR() {}
945 933

	
946 934
    /// Constructor that requires parameters.
947 935

	
948 936
    /// Constructor that requires parameters.
949 937
    /// These parameters will be the default values for the traits class.
950
    DfsWizard(const Digraph &g, Node s=INVALID) :
951
      TR(g,s) {}
938
    /// \param g The digraph the algorithm runs on.
939
    DfsWizard(const Digraph &g) :
940
      TR(g) {}
952 941

	
953 942
    ///Copy constructor
954 943
    DfsWizard(const TR &b) : TR(b) {}
955 944

	
956 945
    ~DfsWizard() {}
957 946

	
958
    ///Runs DFS algorithm from a source node.
947
    ///Runs DFS algorithm from the given source node.
959 948

	
960
    ///Runs DFS algorithm from a source node.
961
    ///The node can be given with the \ref source() function.
949
    ///This method runs DFS algorithm from node \c s
950
    ///in order to compute the DFS path to each node.
951
    void run(Node s)
952
    {
953
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
954
      if (Base::_pred)
955
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
956
      if (Base::_dist)
957
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
958
      if (Base::_reached)
959
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
960
      if (Base::_processed)
961
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
962
      if (s!=INVALID)
963
        alg.run(s);
964
      else
965
        alg.run();
966
    }
967

	
968
    ///Finds the DFS path between \c s and \c t.
969

	
970
    ///This method runs DFS algorithm from node \c s
971
    ///in order to compute the DFS path to node \c t
972
    ///(it stops searching when \c t is processed).
973
    ///
974
    ///\return \c true if \c t is reachable form \c s.
975
    bool run(Node s, Node t)
976
    {
977
      if (s==INVALID || t==INVALID) throw UninitializedParameter();
978
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
979
      if (Base::_pred)
980
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
981
      if (Base::_dist)
982
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
983
      if (Base::_reached)
984
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
985
      if (Base::_processed)
986
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
987
      alg.run(s,t);
988
      if (Base::_path)
989
        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
990
      if (Base::_di)
991
        *Base::_di = alg.dist(t);
992
      return alg.reached(t);
993
      }
994

	
995
    ///Runs DFS algorithm to visit all nodes in the digraph.
996

	
997
    ///This method runs DFS algorithm in order to compute
998
    ///the DFS path to each node.
962 999
    void run()
963 1000
    {
964
      if(Base::_source==INVALID) throw UninitializedParameter();
965
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
966
      if(Base::_reached)
967
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
968
      if(Base::_processed)
969
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
970
      if(Base::_pred)
971
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
972
      if(Base::_dist)
973
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
974
      alg.run(Base::_source);
975
    }
976

	
977
    ///Runs DFS algorithm from the given node.
978

	
979
    ///Runs DFS algorithm from the given node.
980
    ///\param s is the given source.
981
    void run(Node s)
982
    {
983
      Base::_source=s;
984
      run();
985
    }
986

	
987
    /// Sets the source node, from which the Dfs algorithm runs.
988

	
989
    /// Sets the source node, from which the Dfs algorithm runs.
990
    /// \param s is the source node.
991
    DfsWizard<TR> &source(Node s)
992
    {
993
      Base::_source=s;
994
      return *this;
1001
      run(INVALID);
995 1002
    }
996 1003

	
997 1004
    template<class T>
998 1005
    struct SetPredMapBase : public Base {
999 1006
      typedef T PredMap;
1000 1007
      static PredMap *createPredMap(const Digraph &) { return 0; };
1001 1008
      SetPredMapBase(const TR &b) : TR(b) {}
1002 1009
    };
1003
    ///\brief \ref named-templ-param "Named parameter"
1010
    ///\brief \ref named-func-param "Named parameter"
1004 1011
    ///for setting \ref PredMap object.
1005 1012
    ///
1006
    ///\ref named-templ-param "Named parameter"
1013
    ///\ref named-func-param "Named parameter"
1007 1014
    ///for setting \ref PredMap object.
1008 1015
    template<class T>
1009 1016
    DfsWizard<SetPredMapBase<T> > predMap(const T &t)
1010 1017
    {
1011 1018
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1012 1019
      return DfsWizard<SetPredMapBase<T> >(*this);
1013 1020
    }
1014 1021

	
1015 1022
    template<class T>
1016 1023
    struct SetReachedMapBase : public Base {
1017 1024
      typedef T ReachedMap;
1018 1025
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1019 1026
      SetReachedMapBase(const TR &b) : TR(b) {}
1020 1027
    };
1021
    ///\brief \ref named-templ-param "Named parameter"
1028
    ///\brief \ref named-func-param "Named parameter"
1022 1029
    ///for setting \ref ReachedMap object.
1023 1030
    ///
1024
    /// \ref named-templ-param "Named parameter"
1031
    /// \ref named-func-param "Named parameter"
1025 1032
    ///for setting \ref ReachedMap object.
1026 1033
    template<class T>
1027 1034
    DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1028 1035
    {
1029 1036
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1030 1037
      return DfsWizard<SetReachedMapBase<T> >(*this);
1031 1038
    }
1032 1039

	
1033 1040
    template<class T>
1041
    struct SetDistMapBase : public Base {
1042
      typedef T DistMap;
1043
      static DistMap *createDistMap(const Digraph &) { return 0; };
1044
      SetDistMapBase(const TR &b) : TR(b) {}
1045
    };
1046
    ///\brief \ref named-func-param "Named parameter"
1047
    ///for setting \ref DistMap object.
1048
    ///
1049
    /// \ref named-func-param "Named parameter"
1050
    ///for setting \ref DistMap object.
1051
    template<class T>
1052
    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
1053
    {
1054
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1055
      return DfsWizard<SetDistMapBase<T> >(*this);
1056
    }
1057

	
1058
    template<class T>
1034 1059
    struct SetProcessedMapBase : public Base {
1035 1060
      typedef T ProcessedMap;
1036 1061
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1037 1062
      SetProcessedMapBase(const TR &b) : TR(b) {}
1038 1063
    };
1039
    ///\brief \ref named-templ-param "Named parameter"
1064
    ///\brief \ref named-func-param "Named parameter"
1040 1065
    ///for setting \ref ProcessedMap object.
1041 1066
    ///
1042
    /// \ref named-templ-param "Named parameter"
1067
    /// \ref named-func-param "Named parameter"
1043 1068
    ///for setting \ref ProcessedMap object.
1044 1069
    template<class T>
1045 1070
    DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1046 1071
    {
1047 1072
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1048 1073
      return DfsWizard<SetProcessedMapBase<T> >(*this);
1049 1074
    }
1050 1075

	
1051 1076
    template<class T>
1052
    struct SetDistMapBase : public Base {
1053
      typedef T DistMap;
1054
      static DistMap *createDistMap(const Digraph &) { return 0; };
1055
      SetDistMapBase(const TR &b) : TR(b) {}
1077
    struct SetPathBase : public Base {
1078
      typedef T Path;
1079
      SetPathBase(const TR &b) : TR(b) {}
1056 1080
    };
1057
    ///\brief \ref named-templ-param "Named parameter"
1058
    ///for setting \ref DistMap object.
1081
    ///\brief \ref named-func-param "Named parameter"
1082
    ///for getting the DFS path to the target node.
1059 1083
    ///
1060
    ///\ref named-templ-param "Named parameter"
1061
    ///for setting \ref DistMap object.
1084
    ///\ref named-func-param "Named parameter"
1085
    ///for getting the DFS path to the target node.
1062 1086
    template<class T>
1063
    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
1087
    DfsWizard<SetPathBase<T> > path(const T &t)
1064 1088
    {
1065
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1066
      return DfsWizard<SetDistMapBase<T> >(*this);
1089
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1090
      return DfsWizard<SetPathBase<T> >(*this);
1091
    }
1092

	
1093
    ///\brief \ref named-func-param "Named parameter"
1094
    ///for getting the distance of the target node.
1095
    ///
1096
    ///\ref named-func-param "Named parameter"
1097
    ///for getting the distance of the target node.
1098
    DfsWizard dist(const int &d)
1099
    {
1100
      Base::_di=const_cast<int*>(&d);
1101
      return *this;
1067 1102
    }
1068 1103

	
1069 1104
  };
1070 1105

	
1071
  ///Function type interface for Dfs algorithm.
1106
  ///Function-type interface for DFS algorithm.
1072 1107

	
1073 1108
  ///\ingroup search
1074
  ///Function type interface for Dfs algorithm.
1109
  ///Function-type interface for DFS algorithm.
1075 1110
  ///
1076
  ///This function also has several
1077
  ///\ref named-templ-func-param "named parameters",
1111
  ///This function also has several \ref named-func-param "named parameters",
1078 1112
  ///they are declared as the members of class \ref DfsWizard.
1079
  ///The following
1080
  ///example shows how to use these parameters.
1113
  ///The following examples show how to use these parameters.
1081 1114
  ///\code
1082
  ///  dfs(g,source).predMap(preds).run();
1115
  ///  // Compute the DFS tree
1116
  ///  dfs(g).predMap(preds).distMap(dists).run(s);
1117
  ///
1118
  ///  // Compute the DFS path from s to t
1119
  ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
1083 1120
  ///\endcode
1121

	
1084 1122
  ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
1085 1123
  ///to the end of the parameter list.
1086 1124
  ///\sa DfsWizard
1087 1125
  ///\sa Dfs
1088 1126
  template<class GR>
1089 1127
  DfsWizard<DfsWizardBase<GR> >
1090
  dfs(const GR &g,typename GR::Node s=INVALID)
1128
  dfs(const GR &digraph)
1091 1129
  {
1092
    return DfsWizard<DfsWizardBase<GR> >(g,s);
1130
    return DfsWizard<DfsWizardBase<GR> >(digraph);
1093 1131
  }
1094 1132

	
1095 1133
#ifdef DOXYGEN
1096 1134
  /// \brief Visitor class for DFS.
1097 1135
  ///
1098 1136
  /// This class defines the interface of the DfsVisit events, and
1099 1137
  /// it could be the base of a real visitor class.
1100 1138
  template <typename _Digraph>
1101 1139
  struct DfsVisitor {
1102 1140
    typedef _Digraph Digraph;
1103 1141
    typedef typename Digraph::Arc Arc;
1104 1142
    typedef typename Digraph::Node Node;
1105 1143
    /// \brief Called for the source node of the DFS.
1106 1144
    ///
1107 1145
    /// This function is called for the source node of the DFS.
1108 1146
    void start(const Node& node) {}
1109 1147
    /// \brief Called when the source node is leaved.
1110 1148
    ///
1111 1149
    /// This function is called when the source node is leaved.
1112 1150
    void stop(const Node& node) {}
1113 1151
    /// \brief Called when a node is reached first time.
1114 1152
    ///
1115 1153
    /// This function is called when a node is reached first time.
1116 1154
    void reach(const Node& node) {}
1117 1155
    /// \brief Called when an arc reaches a new node.
1118 1156
    ///
1119 1157
    /// This function is called when the DFS finds an arc whose target node
1120 1158
    /// is not reached yet.
1121 1159
    void discover(const Arc& arc) {}
1122 1160
    /// \brief Called when an arc is examined but its target node is
1123 1161
    /// already discovered.
1124 1162
    ///
1125 1163
    /// This function is called when an arc is examined but its target node is
1126 1164
    /// already discovered.
1127 1165
    void examine(const Arc& arc) {}
1128 1166
    /// \brief Called when the DFS steps back from a node.
1129 1167
    ///
1130 1168
    /// This function is called when the DFS steps back from a node.
1131 1169
    void leave(const Node& node) {}
1132 1170
    /// \brief Called when the DFS steps back on an arc.
1133 1171
    ///
1134 1172
    /// This function is called when the DFS steps back on an arc.
1135 1173
    void backtrack(const Arc& arc) {}
1136 1174
  };
1137 1175
#else
1138 1176
  template <typename _Digraph>
1139 1177
  struct DfsVisitor {
1140 1178
    typedef _Digraph Digraph;
1141 1179
    typedef typename Digraph::Arc Arc;
1142 1180
    typedef typename Digraph::Node Node;
1143 1181
    void start(const Node&) {}
1144 1182
    void stop(const Node&) {}
1145 1183
    void reach(const Node&) {}
1146 1184
    void discover(const Arc&) {}
1147 1185
    void examine(const Arc&) {}
1148 1186
    void leave(const Node&) {}
1149 1187
    void backtrack(const Arc&) {}
1150 1188

	
1151 1189
    template <typename _Visitor>
1152 1190
    struct Constraints {
1153 1191
      void constraints() {
1154 1192
        Arc arc;
1155 1193
        Node node;
1156 1194
        visitor.start(node);
1157 1195
        visitor.stop(arc);
1158 1196
        visitor.reach(node);
1159 1197
        visitor.discover(arc);
1160 1198
        visitor.examine(arc);
1161 1199
        visitor.leave(node);
1162 1200
        visitor.backtrack(arc);
1163 1201
      }
1164 1202
      _Visitor& visitor;
1165 1203
    };
1166 1204
  };
1167 1205
#endif
1168 1206

	
1169 1207
  /// \brief Default traits class of DfsVisit class.
1170 1208
  ///
1171 1209
  /// Default traits class of DfsVisit class.
1172 1210
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1173 1211
  template<class _Digraph>
1174 1212
  struct DfsVisitDefaultTraits {
1175 1213

	
1176 1214
    /// \brief The type of the digraph the algorithm runs on.
1177 1215
    typedef _Digraph Digraph;
1178 1216

	
1179 1217
    /// \brief The type of the map that indicates which nodes are reached.
1180 1218
    ///
1181 1219
    /// The type of the map that indicates which nodes are reached.
1182 1220
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1183 1221
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1184 1222

	
1185 1223
    /// \brief Instantiates a \ref ReachedMap.
1186 1224
    ///
1187 1225
    /// This function instantiates a \ref ReachedMap.
1188 1226
    /// \param digraph is the digraph, to which
1189 1227
    /// we would like to define the \ref ReachedMap.
1190 1228
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1191 1229
      return new ReachedMap(digraph);
1192 1230
    }
1193 1231

	
1194 1232
  };
1195 1233

	
1196 1234
  /// \ingroup search
1197 1235
  ///
1198 1236
  /// \brief %DFS algorithm class with visitor interface.
1199 1237
  ///
1200 1238
  /// This class provides an efficient implementation of the %DFS algorithm
1201 1239
  /// with visitor interface.
1202 1240
  ///
1203 1241
  /// The %DfsVisit class provides an alternative interface to the Dfs
1204 1242
  /// class. It works with callback mechanism, the DfsVisit object calls
1205 1243
  /// the member functions of the \c Visitor class on every DFS event.
1206 1244
  ///
1207 1245
  /// This interface of the DFS algorithm should be used in special cases
1208 1246
  /// when extra actions have to be performed in connection with certain
1209 1247
  /// events of the DFS algorithm. Otherwise consider to use Dfs or dfs()
1210 1248
  /// instead.
1211 1249
  ///
1212 1250
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1213 1251
  /// The default value is
1214 1252
  /// \ref ListDigraph. The value of _Digraph is not used directly by
1215 1253
  /// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits.
1216 1254
  /// \tparam _Visitor The Visitor type that is used by the algorithm.
1217 1255
  /// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which
1218 1256
  /// does not observe the DFS events. If you want to observe the DFS
1219 1257
  /// events, you should implement your own visitor class.
1220 1258
  /// \tparam _Traits Traits class to set various data types used by the
1221 1259
  /// algorithm. The default traits class is
1222 1260
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>".
1223 1261
  /// See \ref DfsVisitDefaultTraits for the documentation of
1224 1262
  /// a DFS visit traits class.
1225 1263
#ifdef DOXYGEN
1226 1264
  template <typename _Digraph, typename _Visitor, typename _Traits>
1227 1265
#else
1228 1266
  template <typename _Digraph = ListDigraph,
1229 1267
            typename _Visitor = DfsVisitor<_Digraph>,
1230 1268
            typename _Traits = DfsDefaultTraits<_Digraph> >
1231 1269
#endif
1232 1270
  class DfsVisit {
1233 1271
  public:
1234 1272

	
1235 1273
    /// \brief \ref Exception for uninitialized parameters.
1236 1274
    ///
1237 1275
    /// This error represents problems in the initialization
1238 1276
    /// of the parameters of the algorithm.
1239 1277
    class UninitializedParameter : public lemon::UninitializedParameter {
1240 1278
    public:
1241 1279
      virtual const char* what() const throw()
1242 1280
      {
1243 1281
        return "lemon::DfsVisit::UninitializedParameter";
1244 1282
      }
1245 1283
    };
1246 1284

	
1247 1285
    ///The traits class.
1248 1286
    typedef _Traits Traits;
1249 1287

	
1250 1288
    ///The type of the digraph the algorithm runs on.
1251 1289
    typedef typename Traits::Digraph Digraph;
1252 1290

	
1253 1291
    ///The visitor type used by the algorithm.
1254 1292
    typedef _Visitor Visitor;
1255 1293

	
1256 1294
    ///The type of the map that indicates which nodes are reached.
1257 1295
    typedef typename Traits::ReachedMap ReachedMap;
1258 1296

	
1259 1297
  private:
1260 1298

	
1261 1299
    typedef typename Digraph::Node Node;
1262 1300
    typedef typename Digraph::NodeIt NodeIt;
1263 1301
    typedef typename Digraph::Arc Arc;
1264 1302
    typedef typename Digraph::OutArcIt OutArcIt;
1265 1303

	
1266 1304
    //Pointer to the underlying digraph.
1267 1305
    const Digraph *_digraph;
1268 1306
    //Pointer to the visitor object.
1269 1307
    Visitor *_visitor;
1270 1308
    //Pointer to the map of reached status of the nodes.
1271 1309
    ReachedMap *_reached;
1272 1310
    //Indicates if _reached is locally allocated (true) or not.
1273 1311
    bool local_reached;
1274 1312

	
1275 1313
    std::vector<typename Digraph::Arc> _stack;
1276 1314
    int _stack_head;
1277 1315

	
1278 1316
    //Creates the maps if necessary.
1279 1317
    void create_maps() {
1280 1318
      if(!_reached) {
1281 1319
        local_reached = true;
1282 1320
        _reached = Traits::createReachedMap(*_digraph);
1283 1321
      }
1284 1322
    }
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_DIJKSTRA_H
20 20
#define LEMON_DIJKSTRA_H
21 21

	
22 22
///\ingroup shortest_path
23 23
///\file
24 24
///\brief Dijkstra algorithm.
25 25

	
26 26
#include <limits>
27 27
#include <lemon/list_graph.h>
28 28
#include <lemon/bin_heap.h>
29 29
#include <lemon/bits/path_dump.h>
30 30
#include <lemon/core.h>
31 31
#include <lemon/error.h>
32 32
#include <lemon/maps.h>
33
#include <lemon/path.h>
33 34

	
34 35
namespace lemon {
35 36

	
36 37
  /// \brief Default operation traits for the Dijkstra algorithm class.
37 38
  ///
38 39
  /// This operation traits class defines all computational operations and
39 40
  /// constants which are used in the Dijkstra algorithm.
40 41
  template <typename Value>
41 42
  struct DijkstraDefaultOperationTraits {
42 43
    /// \brief Gives back the zero value of the type.
43 44
    static Value zero() {
44 45
      return static_cast<Value>(0);
45 46
    }
46 47
    /// \brief Gives back the sum of the given two elements.
47 48
    static Value plus(const Value& left, const Value& right) {
48 49
      return left + right;
49 50
    }
50 51
    /// \brief Gives back true only if the first value is less than the second.
51 52
    static bool less(const Value& left, const Value& right) {
52 53
      return left < right;
53 54
    }
54 55
  };
55 56

	
56 57
  /// \brief Widest path operation traits for the Dijkstra algorithm class.
57 58
  ///
58 59
  /// This operation traits class defines all computational operations and
59 60
  /// constants which are used in the Dijkstra algorithm for widest path
60 61
  /// computation.
61 62
  ///
62 63
  /// \see DijkstraDefaultOperationTraits
63 64
  template <typename Value>
64 65
  struct DijkstraWidestPathOperationTraits {
65 66
    /// \brief Gives back the maximum value of the type.
66 67
    static Value zero() {
67 68
      return std::numeric_limits<Value>::max();
68 69
    }
69 70
    /// \brief Gives back the minimum of the given two elements.
70 71
    static Value plus(const Value& left, const Value& right) {
71 72
      return std::min(left, right);
72 73
    }
73 74
    /// \brief Gives back true only if the first value is less than the second.
74 75
    static bool less(const Value& left, const Value& right) {
75 76
      return left < right;
76 77
    }
77 78
  };
78 79

	
79 80
  ///Default traits class of Dijkstra class.
80 81

	
81 82
  ///Default traits class of Dijkstra class.
82 83
  ///\tparam GR The type of the digraph.
83 84
  ///\tparam LM The type of the length map.
84 85
  template<class GR, class LM>
85 86
  struct DijkstraDefaultTraits
86 87
  {
87 88
    ///The type of the digraph the algorithm runs on.
88 89
    typedef GR Digraph;
89 90

	
90 91
    ///The type of the map that stores the arc lengths.
91 92

	
92 93
    ///The type of the map that stores the arc lengths.
93 94
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
94 95
    typedef LM LengthMap;
95 96
    ///The type of the length of the arcs.
96 97
    typedef typename LM::Value Value;
97 98

	
98 99
    /// Operation traits for Dijkstra algorithm.
99 100

	
100 101
    /// This class defines the operations that are used in the algorithm.
101 102
    /// \see DijkstraDefaultOperationTraits
102 103
    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
103 104

	
104 105
    /// The cross reference type used by the heap.
105 106

	
106 107
    /// The cross reference type used by the heap.
107 108
    /// Usually it is \c Digraph::NodeMap<int>.
108 109
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
109 110
    ///Instantiates a \ref HeapCrossRef.
110 111

	
111 112
    ///This function instantiates a \ref HeapCrossRef.
112 113
    /// \param g is the digraph, to which we would like to define the
113 114
    /// \ref HeapCrossRef.
114 115
    static HeapCrossRef *createHeapCrossRef(const Digraph &g)
115 116
    {
116 117
      return new HeapCrossRef(g);
117 118
    }
118 119

	
119 120
    ///The heap type used by the Dijkstra algorithm.
120 121

	
121 122
    ///The heap type used by the Dijkstra algorithm.
122 123
    ///
123 124
    ///\sa BinHeap
124 125
    ///\sa Dijkstra
125 126
    typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap;
126 127
    ///Instantiates a \ref Heap.
127 128

	
128 129
    ///This function instantiates a \ref Heap.
129 130
    static Heap *createHeap(HeapCrossRef& r)
130 131
    {
131 132
      return new Heap(r);
132 133
    }
133 134

	
134 135
    ///\brief The type of the map that stores the predecessor
135 136
    ///arcs of the shortest paths.
136 137
    ///
137 138
    ///The type of the map that stores the predecessor
138 139
    ///arcs of the shortest paths.
139 140
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
140 141
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
141 142
    ///Instantiates a \ref PredMap.
142 143

	
143 144
    ///This function instantiates a \ref PredMap.
144 145
    ///\param g is the digraph, to which we would like to define the
145 146
    ///\ref PredMap.
146 147
    static PredMap *createPredMap(const Digraph &g)
147 148
    {
148 149
      return new PredMap(g);
149 150
    }
150 151

	
151 152
    ///The type of the map that indicates which nodes are processed.
152 153

	
153 154
    ///The type of the map that indicates which nodes are processed.
154 155
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
155 156
    ///By default it is a NullMap.
156 157
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
157 158
    ///Instantiates a \ref ProcessedMap.
158 159

	
159 160
    ///This function instantiates a \ref ProcessedMap.
160 161
    ///\param g is the digraph, to which
161 162
    ///we would like to define the \ref ProcessedMap
162 163
#ifdef DOXYGEN
163 164
    static ProcessedMap *createProcessedMap(const Digraph &g)
164 165
#else
165 166
    static ProcessedMap *createProcessedMap(const Digraph &)
166 167
#endif
167 168
    {
168 169
      return new ProcessedMap();
169 170
    }
170 171

	
171 172
    ///The type of the map that stores the distances of the nodes.
172 173

	
173 174
    ///The type of the map that stores the distances of the nodes.
174 175
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
175 176
    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
176 177
    ///Instantiates a \ref DistMap.
177 178

	
178 179
    ///This function instantiates a \ref DistMap.
179 180
    ///\param g is the digraph, to which we would like to define
180 181
    ///the \ref DistMap
181 182
    static DistMap *createDistMap(const Digraph &g)
182 183
    {
183 184
      return new DistMap(g);
184 185
    }
185 186
  };
186 187

	
187 188
  ///%Dijkstra algorithm class.
188 189

	
189 190
  /// \ingroup shortest_path
190 191
  ///This class provides an efficient implementation of the %Dijkstra algorithm.
191 192
  ///
192 193
  ///The arc lengths are passed to the algorithm using a
193 194
  ///\ref concepts::ReadMap "ReadMap",
194 195
  ///so it is easy to change it to any kind of length.
195 196
  ///The type of the length is determined by the
196 197
  ///\ref concepts::ReadMap::Value "Value" of the length map.
197 198
  ///It is also possible to change the underlying priority heap.
198 199
  ///
199
  ///There is also a \ref dijkstra() "function type interface" for the
200
  ///There is also a \ref dijkstra() "function-type interface" for the
200 201
  ///%Dijkstra algorithm, which is convenient in the simplier cases and
201 202
  ///it can be used easier.
202 203
  ///
203 204
  ///\tparam GR The type of the digraph the algorithm runs on.
204 205
  ///The default value is \ref ListDigraph.
205 206
  ///The value of GR is not used directly by \ref Dijkstra, it is only
206 207
  ///passed to \ref DijkstraDefaultTraits.
207 208
  ///\tparam LM A readable arc map that determines the lengths of the
208 209
  ///arcs. It is read once for each arc, so the map may involve in
209 210
  ///relatively time consuming process to compute the arc lengths if
210 211
  ///it is necessary. The default map type is \ref
211 212
  ///concepts::Digraph::ArcMap "Digraph::ArcMap<int>".
212 213
  ///The value of LM is not used directly by \ref Dijkstra, it is only
213 214
  ///passed to \ref DijkstraDefaultTraits.
214 215
  ///\tparam TR Traits class to set various data types used by the algorithm.
215 216
  ///The default traits class is \ref DijkstraDefaultTraits
216 217
  ///"DijkstraDefaultTraits<GR,LM>". See \ref DijkstraDefaultTraits
217 218
  ///for the documentation of a Dijkstra traits class.
218 219
#ifdef DOXYGEN
219 220
  template <typename GR, typename LM, typename TR>
220 221
#else
221 222
  template <typename GR=ListDigraph,
222 223
            typename LM=typename GR::template ArcMap<int>,
223 224
            typename TR=DijkstraDefaultTraits<GR,LM> >
224 225
#endif
225 226
  class Dijkstra {
226 227
  public:
227 228
    ///\ref Exception for uninitialized parameters.
228 229

	
229 230
    ///This error represents problems in the initialization of the
230 231
    ///parameters of the algorithm.
231 232
    class UninitializedParameter : public lemon::UninitializedParameter {
232 233
    public:
233 234
      virtual const char* what() const throw() {
234 235
        return "lemon::Dijkstra::UninitializedParameter";
235 236
      }
236 237
    };
237 238

	
238 239
    ///The type of the digraph the algorithm runs on.
239 240
    typedef typename TR::Digraph Digraph;
240 241

	
241 242
    ///The type of the length of the arcs.
242 243
    typedef typename TR::LengthMap::Value Value;
243 244
    ///The type of the map that stores the arc lengths.
244 245
    typedef typename TR::LengthMap LengthMap;
245 246
    ///\brief The type of the map that stores the predecessor arcs of the
246 247
    ///shortest paths.
247 248
    typedef typename TR::PredMap PredMap;
248 249
    ///The type of the map that stores the distances of the nodes.
249 250
    typedef typename TR::DistMap DistMap;
250 251
    ///The type of the map that indicates which nodes are processed.
251 252
    typedef typename TR::ProcessedMap ProcessedMap;
252 253
    ///The type of the paths.
253 254
    typedef PredMapPath<Digraph, PredMap> Path;
254 255
    ///The cross reference type used for the current heap.
255 256
    typedef typename TR::HeapCrossRef HeapCrossRef;
256 257
    ///The heap type used by the algorithm.
257 258
    typedef typename TR::Heap Heap;
258 259
    ///The operation traits class.
259 260
    typedef typename TR::OperationTraits OperationTraits;
260 261

	
261 262
    ///The traits class.
262 263
    typedef TR Traits;
263 264

	
264 265
  private:
265 266

	
266 267
    typedef typename Digraph::Node Node;
267 268
    typedef typename Digraph::NodeIt NodeIt;
268 269
    typedef typename Digraph::Arc Arc;
269 270
    typedef typename Digraph::OutArcIt OutArcIt;
270 271

	
271 272
    //Pointer to the underlying digraph.
272 273
    const Digraph *G;
273 274
    //Pointer to the length map.
274 275
    const LengthMap *length;
275 276
    //Pointer to the map of predecessors arcs.
276 277
    PredMap *_pred;
277 278
    //Indicates if _pred is locally allocated (true) or not.
278 279
    bool local_pred;
279 280
    //Pointer to the map of distances.
280 281
    DistMap *_dist;
281 282
    //Indicates if _dist is locally allocated (true) or not.
282 283
    bool local_dist;
283 284
    //Pointer to the map of processed status of the nodes.
284 285
    ProcessedMap *_processed;
285 286
    //Indicates if _processed is locally allocated (true) or not.
286 287
    bool local_processed;
287 288
    //Pointer to the heap cross references.
288 289
    HeapCrossRef *_heap_cross_ref;
289 290
    //Indicates if _heap_cross_ref is locally allocated (true) or not.
290 291
    bool local_heap_cross_ref;
291 292
    //Pointer to the heap.
292 293
    Heap *_heap;
293 294
    //Indicates if _heap is locally allocated (true) or not.
294 295
    bool local_heap;
295 296

	
296 297
    //Creates the maps if necessary.
297 298
    void create_maps()
298 299
    {
299 300
      if(!_pred) {
300 301
        local_pred = true;
301 302
        _pred = Traits::createPredMap(*G);
302 303
      }
303 304
      if(!_dist) {
304 305
        local_dist = true;
305 306
        _dist = Traits::createDistMap(*G);
306 307
      }
307 308
      if(!_processed) {
308 309
        local_processed = true;
309 310
        _processed = Traits::createProcessedMap(*G);
310 311
      }
311 312
      if (!_heap_cross_ref) {
312 313
        local_heap_cross_ref = true;
313 314
        _heap_cross_ref = Traits::createHeapCrossRef(*G);
314 315
      }
315 316
      if (!_heap) {
316 317
        local_heap = true;
317 318
        _heap = Traits::createHeap(*_heap_cross_ref);
318 319
      }
319 320
    }
320 321

	
321 322
  public:
322 323

	
323 324
    typedef Dijkstra Create;
324 325

	
325 326
    ///\name Named template parameters
326 327

	
327 328
    ///@{
328 329

	
329 330
    template <class T>
330 331
    struct SetPredMapTraits : public Traits {
331 332
      typedef T PredMap;
332 333
      static PredMap *createPredMap(const Digraph &)
333 334
      {
334 335
        throw UninitializedParameter();
335 336
      }
336 337
    };
337 338
    ///\brief \ref named-templ-param "Named parameter" for setting
338 339
    ///\ref PredMap type.
339 340
    ///
340 341
    ///\ref named-templ-param "Named parameter" for setting
341 342
    ///\ref PredMap type.
342 343
    template <class T>
343 344
    struct SetPredMap
344 345
      : public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > {
345 346
      typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create;
346 347
    };
347 348

	
348 349
    template <class T>
349 350
    struct SetDistMapTraits : public Traits {
350 351
      typedef T DistMap;
351 352
      static DistMap *createDistMap(const Digraph &)
352 353
      {
353 354
        throw UninitializedParameter();
354 355
      }
355 356
    };
356 357
    ///\brief \ref named-templ-param "Named parameter" for setting
357 358
    ///\ref DistMap type.
358 359
    ///
359 360
    ///\ref named-templ-param "Named parameter" for setting
360 361
    ///\ref DistMap type.
361 362
    template <class T>
362 363
    struct SetDistMap
363 364
      : public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > {
364 365
      typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create;
365 366
    };
366 367

	
367 368
    template <class T>
368 369
    struct SetProcessedMapTraits : public Traits {
369 370
      typedef T ProcessedMap;
370 371
      static ProcessedMap *createProcessedMap(const Digraph &)
371 372
      {
372 373
        throw UninitializedParameter();
373 374
      }
374 375
    };
375 376
    ///\brief \ref named-templ-param "Named parameter" for setting
376 377
    ///\ref ProcessedMap type.
377 378
    ///
378 379
    ///\ref named-templ-param "Named parameter" for setting
379 380
    ///\ref ProcessedMap type.
380 381
    template <class T>
381 382
    struct SetProcessedMap
382 383
      : public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > {
383 384
      typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create;
384 385
    };
385 386

	
386 387
    struct SetStandardProcessedMapTraits : public Traits {
387 388
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
388 389
      static ProcessedMap *createProcessedMap(const Digraph &g)
389 390
      {
390 391
        return new ProcessedMap(g);
391 392
      }
... ...
@@ -793,486 +794,513 @@
793 794
    ///This method runs the %Dijkstra algorithm from node \c s
794 795
    ///in order to compute the shortest path to \c t.
795 796
    ///
796 797
    ///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path,
797 798
    ///if \c t is reachable form \c s, \c 0 otherwise.
798 799
    ///
799 800
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a
800 801
    ///shortcut of the following code.
801 802
    ///\code
802 803
    ///  d.init();
803 804
    ///  d.addSource(s);
804 805
    ///  d.start(t);
805 806
    ///\endcode
806 807
    Value run(Node s,Node t) {
807 808
      init();
808 809
      addSource(s);
809 810
      start(t);
810 811
      return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t];
811 812
    }
812 813

	
813 814
    ///@}
814 815

	
815 816
    ///\name Query Functions
816 817
    ///The result of the %Dijkstra algorithm can be obtained using these
817 818
    ///functions.\n
818 819
    ///Either \ref lemon::Dijkstra::run() "run()" or
819 820
    ///\ref lemon::Dijkstra::start() "start()" must be called before
820 821
    ///using them.
821 822

	
822 823
    ///@{
823 824

	
824 825
    ///The shortest path to a node.
825 826

	
826 827
    ///Returns the shortest path to a node.
827 828
    ///
828 829
    ///\warning \c t should be reachable from the root(s).
829 830
    ///
830 831
    ///\pre Either \ref run() or \ref start() must be called before
831 832
    ///using this function.
832 833
    Path path(Node t) const { return Path(*G, *_pred, t); }
833 834

	
834 835
    ///The distance of a node from the root(s).
835 836

	
836 837
    ///Returns the distance of a node from the root(s).
837 838
    ///
838 839
    ///\warning If node \c v is not reachable from the root(s), then
839 840
    ///the return value of this function is undefined.
840 841
    ///
841 842
    ///\pre Either \ref run() or \ref start() must be called before
842 843
    ///using this function.
843 844
    Value dist(Node v) const { return (*_dist)[v]; }
844 845

	
845 846
    ///Returns the 'previous arc' of the shortest path tree for a node.
846 847

	
847 848
    ///This function returns the 'previous arc' of the shortest path
848 849
    ///tree for the node \c v, i.e. it returns the last arc of a
849 850
    ///shortest path from the root(s) to \c v. It is \c INVALID if \c v
850 851
    ///is not reachable from the root(s) or if \c v is a root.
851 852
    ///
852 853
    ///The shortest path tree used here is equal to the shortest path
853 854
    ///tree used in \ref predNode().
854 855
    ///
855 856
    ///\pre Either \ref run() or \ref start() must be called before
856 857
    ///using this function.
857 858
    Arc predArc(Node v) const { return (*_pred)[v]; }
858 859

	
859 860
    ///Returns the 'previous node' of the shortest path tree for a node.
860 861

	
861 862
    ///This function returns the 'previous node' of the shortest path
862 863
    ///tree for the node \c v, i.e. it returns the last but one node
863 864
    ///from a shortest path from the root(s) to \c v. It is \c INVALID
864 865
    ///if \c v is not reachable from the root(s) or if \c v is a root.
865 866
    ///
866 867
    ///The shortest path tree used here is equal to the shortest path
867 868
    ///tree used in \ref predArc().
868 869
    ///
869 870
    ///\pre Either \ref run() or \ref start() must be called before
870 871
    ///using this function.
871 872
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
872 873
                                  G->source((*_pred)[v]); }
873 874

	
874 875
    ///\brief Returns a const reference to the node map that stores the
875 876
    ///distances of the nodes.
876 877
    ///
877 878
    ///Returns a const reference to the node map that stores the distances
878 879
    ///of the nodes calculated by the algorithm.
879 880
    ///
880 881
    ///\pre Either \ref run() or \ref init()
881 882
    ///must be called before using this function.
882 883
    const DistMap &distMap() const { return *_dist;}
883 884

	
884 885
    ///\brief Returns a const reference to the node map that stores the
885 886
    ///predecessor arcs.
886 887
    ///
887 888
    ///Returns a const reference to the node map that stores the predecessor
888 889
    ///arcs, which form the shortest path tree.
889 890
    ///
890 891
    ///\pre Either \ref run() or \ref init()
891 892
    ///must be called before using this function.
892 893
    const PredMap &predMap() const { return *_pred;}
893 894

	
894 895
    ///Checks if a node is reachable from the root(s).
895 896

	
896 897
    ///Returns \c true if \c v is reachable from the root(s).
897 898
    ///\pre Either \ref run() or \ref start()
898 899
    ///must be called before using this function.
899 900
    bool reached(Node v) const { return (*_heap_cross_ref)[v] !=
900 901
                                        Heap::PRE_HEAP; }
901 902

	
902 903
    ///Checks if a node is processed.
903 904

	
904 905
    ///Returns \c true if \c v is processed, i.e. the shortest
905 906
    ///path to \c v has already found.
906 907
    ///\pre Either \ref run() or \ref start()
907 908
    ///must be called before using this function.
908 909
    bool processed(Node v) const { return (*_heap_cross_ref)[v] ==
909 910
                                          Heap::POST_HEAP; }
910 911

	
911 912
    ///The current distance of a node from the root(s).
912 913

	
913 914
    ///Returns the current distance of a node from the root(s).
914 915
    ///It may be decreased in the following processes.
915 916
    ///\pre \c v should be reached but not processed.
916 917
    Value currentDist(Node v) const { return (*_heap)[v]; }
917 918

	
918 919
    ///@}
919 920
  };
920 921

	
921 922

	
922 923
  ///Default traits class of dijkstra() function.
923 924

	
924 925
  ///Default traits class of dijkstra() function.
925 926
  ///\tparam GR The type of the digraph.
926 927
  ///\tparam LM The type of the length map.
927 928
  template<class GR, class LM>
928 929
  struct DijkstraWizardDefaultTraits
929 930
  {
930 931
    ///The type of the digraph the algorithm runs on.
931 932
    typedef GR Digraph;
932 933
    ///The type of the map that stores the arc lengths.
933 934

	
934 935
    ///The type of the map that stores the arc lengths.
935 936
    ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
936 937
    typedef LM LengthMap;
937 938
    ///The type of the length of the arcs.
938 939
    typedef typename LM::Value Value;
939 940

	
940 941
    /// Operation traits for Dijkstra algorithm.
941 942

	
942 943
    /// This class defines the operations that are used in the algorithm.
943 944
    /// \see DijkstraDefaultOperationTraits
944 945
    typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
945 946

	
946 947
    /// The cross reference type used by the heap.
947 948

	
948 949
    /// The cross reference type used by the heap.
949 950
    /// Usually it is \c Digraph::NodeMap<int>.
950 951
    typedef typename Digraph::template NodeMap<int> HeapCrossRef;
951 952
    ///Instantiates a \ref HeapCrossRef.
952 953

	
953 954
    ///This function instantiates a \ref HeapCrossRef.
954 955
    /// \param g is the digraph, to which we would like to define the
955 956
    /// HeapCrossRef.
956 957
    static HeapCrossRef *createHeapCrossRef(const Digraph &g)
957 958
    {
958 959
      return new HeapCrossRef(g);
959 960
    }
960 961

	
961 962
    ///The heap type used by the Dijkstra algorithm.
962 963

	
963 964
    ///The heap type used by the Dijkstra algorithm.
964 965
    ///
965 966
    ///\sa BinHeap
966 967
    ///\sa Dijkstra
967 968
    typedef BinHeap<Value, typename Digraph::template NodeMap<int>,
968 969
                    std::less<Value> > Heap;
969 970

	
970 971
    ///Instantiates a \ref Heap.
971 972

	
972 973
    ///This function instantiates a \ref Heap.
973 974
    /// \param r is the HeapCrossRef which is used.
974 975
    static Heap *createHeap(HeapCrossRef& r)
975 976
    {
976 977
      return new Heap(r);
977 978
    }
978 979

	
979 980
    ///\brief The type of the map that stores the predecessor
980 981
    ///arcs of the shortest paths.
981 982
    ///
982 983
    ///The type of the map that stores the predecessor
983 984
    ///arcs of the shortest paths.
984 985
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
985
    typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap;
986
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
986 987
    ///Instantiates a \ref PredMap.
987 988

	
988 989
    ///This function instantiates a \ref PredMap.
989 990
    ///\param g is the digraph, to which we would like to define the
990 991
    ///\ref PredMap.
991
#ifdef DOXYGEN
992 992
    static PredMap *createPredMap(const Digraph &g)
993
#else
994
    static PredMap *createPredMap(const Digraph &)
995
#endif
996 993
    {
997
      return new PredMap();
994
      return new PredMap(g);
998 995
    }
999 996

	
1000 997
    ///The type of the map that indicates which nodes are processed.
1001 998

	
1002 999
    ///The type of the map that indicates which nodes are processed.
1003 1000
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
1004 1001
    ///By default it is a NullMap.
1005 1002
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
1006 1003
    ///Instantiates a \ref ProcessedMap.
1007 1004

	
1008 1005
    ///This function instantiates a \ref ProcessedMap.
1009 1006
    ///\param g is the digraph, to which
1010 1007
    ///we would like to define the \ref ProcessedMap.
1011 1008
#ifdef DOXYGEN
1012 1009
    static ProcessedMap *createProcessedMap(const Digraph &g)
1013 1010
#else
1014 1011
    static ProcessedMap *createProcessedMap(const Digraph &)
1015 1012
#endif
1016 1013
    {
1017 1014
      return new ProcessedMap();
1018 1015
    }
1019 1016

	
1020 1017
    ///The type of the map that stores the distances of the nodes.
1021 1018

	
1022 1019
    ///The type of the map that stores the distances of the nodes.
1023 1020
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
1024
    typedef NullMap<typename Digraph::Node,Value> DistMap;
1021
    typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
1025 1022
    ///Instantiates a \ref DistMap.
1026 1023

	
1027 1024
    ///This function instantiates a \ref DistMap.
1028 1025
    ///\param g is the digraph, to which we would like to define
1029 1026
    ///the \ref DistMap
1030
#ifdef DOXYGEN
1031 1027
    static DistMap *createDistMap(const Digraph &g)
1032
#else
1033
    static DistMap *createDistMap(const Digraph &)
1034
#endif
1035 1028
    {
1036
      return new DistMap();
1029
      return new DistMap(g);
1037 1030
    }
1031

	
1032
    ///The type of the shortest paths.
1033

	
1034
    ///The type of the shortest paths.
1035
    ///It must meet the \ref concepts::Path "Path" concept.
1036
    typedef lemon::Path<Digraph> Path;
1038 1037
  };
1039 1038

	
1040 1039
  /// Default traits class used by \ref DijkstraWizard
1041 1040

	
1042 1041
  /// To make it easier to use Dijkstra algorithm
1043 1042
  /// we have created a wizard class.
1044 1043
  /// This \ref DijkstraWizard class needs default traits,
1045 1044
  /// as well as the \ref Dijkstra class.
1046 1045
  /// The \ref DijkstraWizardBase is a class to be the default traits of the
1047 1046
  /// \ref DijkstraWizard class.
1048 1047
  template<class GR,class LM>
1049 1048
  class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
1050 1049
  {
1051 1050
    typedef DijkstraWizardDefaultTraits<GR,LM> Base;
1052 1051
  protected:
1053 1052
    //The type of the nodes in the digraph.
1054 1053
    typedef typename Base::Digraph::Node Node;
1055 1054

	
1056 1055
    //Pointer to the digraph the algorithm runs on.
1057 1056
    void *_g;
1058
    //Pointer to the length map
1057
    //Pointer to the length map.
1059 1058
    void *_length;
1060 1059
    //Pointer to the map of processed nodes.
1061 1060
    void *_processed;
1062 1061
    //Pointer to the map of predecessors arcs.
1063 1062
    void *_pred;
1064 1063
    //Pointer to the map of distances.
1065 1064
    void *_dist;
1066
    //Pointer to the source node.
1067
    Node _source;
1065
    //Pointer to the shortest path to the target node.
1066
    void *_path;
1067
    //Pointer to the distance of the target node.
1068
    void *_di;
1068 1069

	
1069 1070
  public:
1070 1071
    /// Constructor.
1071 1072

	
1072 1073
    /// This constructor does not require parameters, therefore it initiates
1073
    /// all of the attributes to default values (0, INVALID).
1074
    /// all of the attributes to \c 0.
1074 1075
    DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0),
1075
                           _dist(0), _source(INVALID) {}
1076
                           _dist(0), _path(0), _di(0) {}
1076 1077

	
1077 1078
    /// Constructor.
1078 1079

	
1079
    /// This constructor requires some parameters,
1080
    /// listed in the parameters list.
1081
    /// Others are initiated to 0.
1080
    /// This constructor requires two parameters,
1081
    /// others are initiated to \c 0.
1082 1082
    /// \param g The digraph the algorithm runs on.
1083 1083
    /// \param l The length map.
1084
    /// \param s The source node.
1085
    DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
1084
    DijkstraWizardBase(const GR &g,const LM &l) :
1086 1085
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
1087 1086
      _length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
1088
      _processed(0), _pred(0), _dist(0), _source(s) {}
1087
      _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
1089 1088

	
1090 1089
  };
1091 1090

	
1092
  /// Auxiliary class for the function type interface of Dijkstra algorithm.
1091
  /// Auxiliary class for the function-type interface of Dijkstra algorithm.
1093 1092

	
1094
  /// This auxiliary class is created to implement the function type
1095
  /// interface of \ref Dijkstra algorithm. It uses the functions and features
1096
  /// of the plain \ref Dijkstra, but it is much simpler to use it.
1097
  /// It should only be used through the \ref dijkstra() function, which makes
1098
  /// it easier to use the algorithm.
1093
  /// This auxiliary class is created to implement the
1094
  /// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm.
1095
  /// It does not have own \ref run() method, it uses the functions
1096
  /// and features of the plain \ref Dijkstra.
1099 1097
  ///
1100
  /// Simplicity means that the way to change the types defined
1101
  /// in the traits class is based on functions that returns the new class
1102
  /// and not on templatable built-in classes.
1103
  /// When using the plain \ref Dijkstra
1104
  /// the new class with the modified type comes from
1105
  /// the original class by using the ::
1106
  /// operator. In the case of \ref DijkstraWizard only
1107
  /// a function have to be called, and it will
1108
  /// return the needed class.
1109
  ///
1110
  /// It does not have own \ref run() method. When its \ref run() method
1111
  /// is called, it initiates a plain \ref Dijkstra object, and calls the
1112
  /// \ref Dijkstra::run() method of it.
1098
  /// This class should only be used through the \ref dijkstra() function,
1099
  /// which makes it easier to use the algorithm.
1113 1100
  template<class TR>
1114 1101
  class DijkstraWizard : public TR
1115 1102
  {
1116 1103
    typedef TR Base;
1117 1104

	
1118 1105
    ///The type of the digraph the algorithm runs on.
1119 1106
    typedef typename TR::Digraph Digraph;
1120 1107

	
1121 1108
    typedef typename Digraph::Node Node;
1122 1109
    typedef typename Digraph::NodeIt NodeIt;
1123 1110
    typedef typename Digraph::Arc Arc;
1124 1111
    typedef typename Digraph::OutArcIt OutArcIt;
1125 1112

	
1126 1113
    ///The type of the map that stores the arc lengths.
1127 1114
    typedef typename TR::LengthMap LengthMap;
1128 1115
    ///The type of the length of the arcs.
1129 1116
    typedef typename LengthMap::Value Value;
1130 1117
    ///\brief The type of the map that stores the predecessor
1131 1118
    ///arcs of the shortest paths.
1132 1119
    typedef typename TR::PredMap PredMap;
1133 1120
    ///The type of the map that stores the distances of the nodes.
1134 1121
    typedef typename TR::DistMap DistMap;
1135 1122
    ///The type of the map that indicates which nodes are processed.
1136 1123
    typedef typename TR::ProcessedMap ProcessedMap;
1124
    ///The type of the shortest paths
1125
    typedef typename TR::Path Path;
1137 1126
    ///The heap type used by the dijkstra algorithm.
1138 1127
    typedef typename TR::Heap Heap;
1139 1128

	
1140 1129
  public:
1141 1130

	
1142 1131
    /// Constructor.
1143 1132
    DijkstraWizard() : TR() {}
1144 1133

	
1145 1134
    /// Constructor that requires parameters.
1146 1135

	
1147 1136
    /// Constructor that requires parameters.
1148 1137
    /// These parameters will be the default values for the traits class.
1149
    DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) :
1150
      TR(g,l,s) {}
1138
    /// \param g The digraph the algorithm runs on.
1139
    /// \param l The length map.
1140
    DijkstraWizard(const Digraph &g, const LengthMap &l) :
1141
      TR(g,l) {}
1151 1142

	
1152 1143
    ///Copy constructor
1153 1144
    DijkstraWizard(const TR &b) : TR(b) {}
1154 1145

	
1155 1146
    ~DijkstraWizard() {}
1156 1147

	
1157
    ///Runs Dijkstra algorithm from a source node.
1148
    ///Runs Dijkstra algorithm from the given source node.
1158 1149

	
1159
    ///Runs Dijkstra algorithm from a source node.
1160
    ///The node can be given with the \ref source() function.
1161
    void run()
1150
    ///This method runs %Dijkstra algorithm from the given source node
1151
    ///in order to compute the shortest path to each node.
1152
    void run(Node s)
1162 1153
    {
1163
      if(Base::_source==INVALID) throw UninitializedParameter();
1154
      if (s==INVALID) throw UninitializedParameter();
1164 1155
      Dijkstra<Digraph,LengthMap,TR>
1165
        dij(*reinterpret_cast<const Digraph*>(Base::_g),
1166
            *reinterpret_cast<const LengthMap*>(Base::_length));
1167
      if(Base::_processed)
1168
        dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1169
      if(Base::_pred)
1170
        dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1171
      if(Base::_dist)
1172
        dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1173
      dij.run(Base::_source);
1156
        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
1157
             *reinterpret_cast<const LengthMap*>(Base::_length));
1158
      if (Base::_pred)
1159
        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1160
      if (Base::_dist)
1161
        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1162
      if (Base::_processed)
1163
        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1164
      dijk.run(s);
1174 1165
    }
1175 1166

	
1176
    ///Runs Dijkstra algorithm from the given node.
1167
    ///Finds the shortest path between \c s and \c t.
1177 1168

	
1178
    ///Runs Dijkstra algorithm from the given node.
1179
    ///\param s is the given source.
1180
    void run(Node s)
1169
    ///This method runs the %Dijkstra algorithm from node \c s
1170
    ///in order to compute the shortest path to node \c t
1171
    ///(it stops searching when \c t is processed).
1172
    ///
1173
    ///\return \c true if \c t is reachable form \c s.
1174
    bool run(Node s, Node t)
1181 1175
    {
1182
      Base::_source=s;
1183
      run();
1184
    }
1185

	
1186
    /// Sets the source node, from which the Dijkstra algorithm runs.
1187

	
1188
    /// Sets the source node, from which the Dijkstra algorithm runs.
1189
    /// \param s is the source node.
1190
    DijkstraWizard<TR> &source(Node s)
1191
    {
1192
      Base::_source=s;
1193
      return *this;
1176
      if (s==INVALID || t==INVALID) throw UninitializedParameter();
1177
      Dijkstra<Digraph,LengthMap,TR>
1178
        dijk(*reinterpret_cast<const Digraph*>(Base::_g),
1179
             *reinterpret_cast<const LengthMap*>(Base::_length));
1180
      if (Base::_pred)
1181
        dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1182
      if (Base::_dist)
1183
        dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1184
      if (Base::_processed)
1185
        dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1186
      dijk.run(s,t);
1187
      if (Base::_path)
1188
        *reinterpret_cast<Path*>(Base::_path) = dijk.path(t);
1189
      if (Base::_di)
1190
        *reinterpret_cast<Value*>(Base::_di) = dijk.dist(t);
1191
      return dijk.reached(t);
1194 1192
    }
1195 1193

	
1196 1194
    template<class T>
1197 1195
    struct SetPredMapBase : public Base {
1198 1196
      typedef T PredMap;
1199 1197
      static PredMap *createPredMap(const Digraph &) { return 0; };
1200 1198
      SetPredMapBase(const TR &b) : TR(b) {}
1201 1199
    };
1202
    ///\brief \ref named-templ-param "Named parameter"
1200
    ///\brief \ref named-func-param "Named parameter"
1203 1201
    ///for setting \ref PredMap object.
1204 1202
    ///
1205
    ///\ref named-templ-param "Named parameter"
1203
    ///\ref named-func-param "Named parameter"
1206 1204
    ///for setting \ref PredMap object.
1207 1205
    template<class T>
1208 1206
    DijkstraWizard<SetPredMapBase<T> > predMap(const T &t)
1209 1207
    {
1210 1208
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1211 1209
      return DijkstraWizard<SetPredMapBase<T> >(*this);
1212 1210
    }
1213 1211

	
1214 1212
    template<class T>
1213
    struct SetDistMapBase : public Base {
1214
      typedef T DistMap;
1215
      static DistMap *createDistMap(const Digraph &) { return 0; };
1216
      SetDistMapBase(const TR &b) : TR(b) {}
1217
    };
1218
    ///\brief \ref named-func-param "Named parameter"
1219
    ///for setting \ref DistMap object.
1220
    ///
1221
    ///\ref named-func-param "Named parameter"
1222
    ///for setting \ref DistMap object.
1223
    template<class T>
1224
    DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
1225
    {
1226
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1227
      return DijkstraWizard<SetDistMapBase<T> >(*this);
1228
    }
1229

	
1230
    template<class T>
1215 1231
    struct SetProcessedMapBase : public Base {
1216 1232
      typedef T ProcessedMap;
1217 1233
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1218 1234
      SetProcessedMapBase(const TR &b) : TR(b) {}
1219 1235
    };
1220
    ///\brief \ref named-templ-param "Named parameter"
1236
    ///\brief \ref named-func-param "Named parameter"
1221 1237
    ///for setting \ref ProcessedMap object.
1222 1238
    ///
1223
    /// \ref named-templ-param "Named parameter"
1239
    /// \ref named-func-param "Named parameter"
1224 1240
    ///for setting \ref ProcessedMap object.
1225 1241
    template<class T>
1226 1242
    DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1227 1243
    {
1228 1244
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1229 1245
      return DijkstraWizard<SetProcessedMapBase<T> >(*this);
1230 1246
    }
1231 1247

	
1232 1248
    template<class T>
1233
    struct SetDistMapBase : public Base {
1234
      typedef T DistMap;
1235
      static DistMap *createDistMap(const Digraph &) { return 0; };
1236
      SetDistMapBase(const TR &b) : TR(b) {}
1249
    struct SetPathBase : public Base {
1250
      typedef T Path;
1251
      SetPathBase(const TR &b) : TR(b) {}
1237 1252
    };
1238
    ///\brief \ref named-templ-param "Named parameter"
1239
    ///for setting \ref DistMap object.
1253
    ///\brief \ref named-func-param "Named parameter"
1254
    ///for getting the shortest path to the target node.
1240 1255
    ///
1241
    ///\ref named-templ-param "Named parameter"
1242
    ///for setting \ref DistMap object.
1256
    ///\ref named-func-param "Named parameter"
1257
    ///for getting the shortest path to the target node.
1243 1258
    template<class T>
1244
    DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
1259
    DijkstraWizard<SetPathBase<T> > path(const T &t)
1245 1260
    {
1246
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1247
      return DijkstraWizard<SetDistMapBase<T> >(*this);
1261
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1262
      return DijkstraWizard<SetPathBase<T> >(*this);
1263
    }
1264

	
1265
    ///\brief \ref named-func-param "Named parameter"
1266
    ///for getting the distance of the target node.
1267
    ///
1268
    ///\ref named-func-param "Named parameter"
1269
    ///for getting the distance of the target node.
1270
    DijkstraWizard dist(const Value &d)
1271
    {
1272
      Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d));
1273
      return *this;
1248 1274
    }
1249 1275

	
1250 1276
  };
1251 1277

	
1252
  ///Function type interface for Dijkstra algorithm.
1278
  ///Function-type interface for Dijkstra algorithm.
1253 1279

	
1254 1280
  /// \ingroup shortest_path
1255
  ///Function type interface for Dijkstra algorithm.
1281
  ///Function-type interface for Dijkstra algorithm.
1256 1282
  ///
1257
  ///This function also has several
1258
  ///\ref named-templ-func-param "named parameters",
1283
  ///This function also has several \ref named-func-param "named parameters",
1259 1284
  ///they are declared as the members of class \ref DijkstraWizard.
1260
  ///The following
1261
  ///example shows how to use these parameters.
1285
  ///The following examples show how to use these parameters.
1262 1286
  ///\code
1263
  ///  dijkstra(g,length,source).predMap(preds).run();
1287
  ///  // Compute shortest path from node s to each node
1288
  ///  dijkstra(g,length).predMap(preds).distMap(dists).run(s);
1289
  ///
1290
  ///  // Compute shortest path from s to t
1291
  ///  bool reached = dijkstra(g,length).path(p).dist(d).run(s,t);
1264 1292
  ///\endcode
1265 1293
  ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1266 1294
  ///to the end of the parameter list.
1267 1295
  ///\sa DijkstraWizard
1268 1296
  ///\sa Dijkstra
1269 1297
  template<class GR, class LM>
1270 1298
  DijkstraWizard<DijkstraWizardBase<GR,LM> >
1271
  dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
1299
  dijkstra(const GR &digraph, const LM &length)
1272 1300
  {
1273
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
1301
    return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length);
1274 1302
  }
1275 1303

	
1276 1304
} //END OF NAMESPACE LEMON
1277 1305

	
1278 1306
#endif
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/concepts/digraph.h>
20 20
#include <lemon/smart_graph.h>
21 21
#include <lemon/list_graph.h>
22 22
#include <lemon/lgf_reader.h>
23 23
#include <lemon/bfs.h>
24 24
#include <lemon/path.h>
25 25

	
26 26
#include "graph_test.h"
27 27
#include "test_tools.h"
28 28

	
29 29
using namespace lemon;
30 30

	
31 31
char test_lgf[] =
32 32
  "@nodes\n"
33 33
  "label\n"
34 34
  "0\n"
35 35
  "1\n"
36 36
  "2\n"
37 37
  "3\n"
38 38
  "4\n"
39 39
  "5\n"
40 40
  "@arcs\n"
41 41
  "     label\n"
42 42
  "0 1  0\n"
43 43
  "1 2  1\n"
44 44
  "2 3  2\n"
45 45
  "3 4  3\n"
46 46
  "0 3  4\n"
47 47
  "0 3  5\n"
48 48
  "5 2  6\n"
49 49
  "@attributes\n"
50 50
  "source 0\n"
51 51
  "target 4\n";
52 52

	
53 53
void checkBfsCompile()
54 54
{
55 55
  typedef concepts::Digraph Digraph;
56 56
  typedef Bfs<Digraph> BType;
57 57

	
58 58
  Digraph G;
59 59
  Digraph::Node n;
60 60
  Digraph::Arc e;
61 61
  int l;
62 62
  bool b;
63 63
  BType::DistMap d(G);
64 64
  BType::PredMap p(G);
65
  //  BType::PredNodeMap pn(G);
66 65

	
67 66
  BType bfs_test(G);
68 67

	
69 68
  bfs_test.run(n);
70 69

	
71 70
  l  = bfs_test.dist(n);
72 71
  e  = bfs_test.predArc(n);
73 72
  n  = bfs_test.predNode(n);
74 73
  d  = bfs_test.distMap();
75

	
76 74
  p  = bfs_test.predMap();
77
  //  pn = bfs_test.predNodeMap();
78 75
  b  = bfs_test.reached(n);
79 76

	
80 77
  Path<Digraph> pp = bfs_test.path(n);
81 78
}
82 79

	
83 80
void checkBfsFunctionCompile()
84 81
{
85 82
  typedef int VType;
86 83
  typedef concepts::Digraph Digraph;
87 84
  typedef Digraph::Arc Arc;
88 85
  typedef Digraph::Node Node;
89 86

	
90 87
  Digraph g;
91
  bfs(g,Node()).run();
92
  bfs(g).source(Node()).run();
88
  bool b;
89
  bfs(g).run(Node());
90
  b=bfs(g).run(Node(),Node());
91
  bfs(g).run();
93 92
  bfs(g)
94
    .predMap(concepts::WriteMap<Node,Arc>())
95
    .distMap(concepts::WriteMap<Node,VType>())
93
    .predMap(concepts::ReadWriteMap<Node,Arc>())
94
    .distMap(concepts::ReadWriteMap<Node,VType>())
96 95
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
97 96
    .processedMap(concepts::WriteMap<Node,bool>())
98 97
    .run(Node());
98
  b=bfs(g)
99
    .predMap(concepts::ReadWriteMap<Node,Arc>())
100
    .distMap(concepts::ReadWriteMap<Node,VType>())
101
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
102
    .processedMap(concepts::WriteMap<Node,bool>())
103
    .path(concepts::Path<Digraph>())
104
    .dist(VType())
105
    .run(Node(),Node());
106
  bfs(g)
107
    .predMap(concepts::ReadWriteMap<Node,Arc>())
108
    .distMap(concepts::ReadWriteMap<Node,VType>())
109
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
110
    .processedMap(concepts::WriteMap<Node,bool>())
111
    .run();
99 112
}
100 113

	
101 114
template <class Digraph>
102 115
void checkBfs() {
103 116
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
104 117

	
105 118
  Digraph G;
106 119
  Node s, t;
107 120

	
108 121
  std::istringstream input(test_lgf);
109 122
  digraphReader(input, G).
110 123
    node("source", s).
111 124
    node("target", t).
112 125
    run();
113 126

	
114 127
  Bfs<Digraph> bfs_test(G);
115 128
  bfs_test.run(s);
116 129

	
117
  check(bfs_test.dist(t)==2,"Bfs found a wrong path." << bfs_test.dist(t));
130
  check(bfs_test.dist(t)==2,"Bfs found a wrong path.");
118 131

	
119 132
  Path<Digraph> p = bfs_test.path(t);
120 133
  check(p.length()==2,"path() found a wrong path.");
121 134
  check(checkPath(G, p),"path() found a wrong path.");
122 135
  check(pathSource(G, p) == s,"path() found a wrong path.");
123 136
  check(pathTarget(G, p) == t,"path() found a wrong path.");
124 137

	
125 138

	
126 139
  for(ArcIt a(G); a!=INVALID; ++a) {
127 140
    Node u=G.source(a);
128 141
    Node v=G.target(a);
129 142
    check( !bfs_test.reached(u) ||
130 143
           (bfs_test.dist(v) <= bfs_test.dist(u)+1),
131
           "Wrong output." << G.id(v) << ' ' << G.id(u));
144
           "Wrong output. " << G.id(u) << "->" << G.id(v));
132 145
  }
133 146

	
134 147
  for(NodeIt v(G); v!=INVALID; ++v) {
135 148
    if (bfs_test.reached(v)) {
136 149
      check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree.");
137 150
      if (bfs_test.predArc(v)!=INVALID ) {
138 151
        Arc a=bfs_test.predArc(v);
139 152
        Node u=G.source(a);
140 153
        check(u==bfs_test.predNode(v),"Wrong tree.");
141 154
        check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
142 155
              "Wrong distance. Difference: "
143
              << std::abs(bfs_test.dist(v) - bfs_test.dist(u)
144
                          - 1));
156
              << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1));
145 157
      }
146 158
    }
147 159
  }
160

	
161
  {
162
    NullMap<Node,Arc> myPredMap;
163
    bfs(G).predMap(myPredMap).run(s);
164
  }
148 165
}
149 166

	
150 167
int main()
151 168
{
152 169
  checkBfs<ListDigraph>();
153 170
  checkBfs<SmartDigraph>();
154 171
  return 0;
155 172
}
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/concepts/digraph.h>
20 20
#include <lemon/smart_graph.h>
21 21
#include <lemon/list_graph.h>
22 22
#include <lemon/lgf_reader.h>
23

	
24 23
#include <lemon/dfs.h>
25 24
#include <lemon/path.h>
26 25

	
27 26
#include "graph_test.h"
28 27
#include "test_tools.h"
29 28

	
30 29
using namespace lemon;
31 30

	
32 31
char test_lgf[] =
33 32
  "@nodes\n"
34 33
  "label\n"
35 34
  "0\n"
36 35
  "1\n"
37 36
  "2\n"
38 37
  "3\n"
39 38
  "4\n"
40 39
  "5\n"
41 40
  "6\n"
42 41
  "@arcs\n"
43 42
  "     label\n"
44 43
  "0 1  0\n"
45 44
  "1 2  1\n"
46 45
  "2 3  2\n"
47 46
  "1 4  3\n"
48 47
  "4 2  4\n"
49 48
  "4 5  5\n"
50 49
  "5 0  6\n"
51 50
  "6 3  7\n"
52 51
  "@attributes\n"
53 52
  "source 0\n"
54 53
  "target 5\n";
55 54

	
56 55
void checkDfsCompile()
57 56
{
58 57
  typedef concepts::Digraph Digraph;
59 58
  typedef Dfs<Digraph> DType;
60 59

	
61 60
  Digraph G;
62 61
  Digraph::Node n;
63 62
  Digraph::Arc e;
64 63
  int l;
65 64
  bool b;
66 65
  DType::DistMap d(G);
67 66
  DType::PredMap p(G);
68 67

	
69 68
  DType dfs_test(G);
70 69

	
71 70
  dfs_test.run(n);
72 71

	
73 72
  l  = dfs_test.dist(n);
74 73
  e  = dfs_test.predArc(n);
75 74
  n  = dfs_test.predNode(n);
76 75
  d  = dfs_test.distMap();
77 76
  p  = dfs_test.predMap();
78 77
  b  = dfs_test.reached(n);
79 78

	
80 79
  Path<Digraph> pp = dfs_test.path(n);
81 80
}
82 81

	
83 82
void checkDfsFunctionCompile()
84 83
{
85 84
  typedef int VType;
86 85
  typedef concepts::Digraph Digraph;
87 86
  typedef Digraph::Arc Arc;
88 87
  typedef Digraph::Node Node;
89 88

	
90 89
  Digraph g;
91
  dfs(g,Node()).run();
92
  dfs(g).source(Node()).run();
90
  bool b;
91
  dfs(g).run(Node());
92
  b=dfs(g).run(Node(),Node());
93
  dfs(g).run();
93 94
  dfs(g)
94
    .predMap(concepts::WriteMap<Node,Arc>())
95
    .distMap(concepts::WriteMap<Node,VType>())
95
    .predMap(concepts::ReadWriteMap<Node,Arc>())
96
    .distMap(concepts::ReadWriteMap<Node,VType>())
96 97
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
97 98
    .processedMap(concepts::WriteMap<Node,bool>())
98 99
    .run(Node());
100
  b=dfs(g)
101
    .predMap(concepts::ReadWriteMap<Node,Arc>())
102
    .distMap(concepts::ReadWriteMap<Node,VType>())
103
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
104
    .processedMap(concepts::WriteMap<Node,bool>())
105
    .path(concepts::Path<Digraph>())
106
    .dist(VType())
107
    .run(Node(),Node());
108
  dfs(g)
109
    .predMap(concepts::ReadWriteMap<Node,Arc>())
110
    .distMap(concepts::ReadWriteMap<Node,VType>())
111
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
112
    .processedMap(concepts::WriteMap<Node,bool>())
113
    .run();
99 114
}
100 115

	
101 116
template <class Digraph>
102 117
void checkDfs() {
103 118
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
104 119

	
105 120
  Digraph G;
106 121
  Node s, t;
107 122

	
108 123
  std::istringstream input(test_lgf);
109 124
  digraphReader(input, G).
110 125
    node("source", s).
111 126
    node("target", t).
112 127
    run();
113 128

	
114 129
  Dfs<Digraph> dfs_test(G);
115 130
  dfs_test.run(s);
116 131

	
117 132
  Path<Digraph> p = dfs_test.path(t);
118 133
  check(p.length() == dfs_test.dist(t),"path() found a wrong path.");
119 134
  check(checkPath(G, p),"path() found a wrong path.");
120 135
  check(pathSource(G, p) == s,"path() found a wrong path.");
121 136
  check(pathTarget(G, p) == t,"path() found a wrong path.");
122 137

	
123 138
  for(NodeIt v(G); v!=INVALID; ++v) {
124 139
    if (dfs_test.reached(v)) {
125 140
      check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree.");
126 141
      if (dfs_test.predArc(v)!=INVALID ) {
127 142
        Arc e=dfs_test.predArc(v);
128 143
        Node u=G.source(e);
129 144
        check(u==dfs_test.predNode(v),"Wrong tree.");
130 145
        check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
131 146
              "Wrong distance. (" << dfs_test.dist(u) << "->"
132
              <<dfs_test.dist(v) << ')');
147
              << dfs_test.dist(v) << ")");
133 148
      }
134 149
    }
135 150
  }
151

	
152
  {
153
    NullMap<Node,Arc> myPredMap;
154
    dfs(G).predMap(myPredMap).run(s);
155
  }
136 156
}
137 157

	
138 158
int main()
139 159
{
140 160
  checkDfs<ListDigraph>();
141 161
  checkDfs<SmartDigraph>();
142 162
  return 0;
143 163
}
Ignore white space 6 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <lemon/concepts/digraph.h>
20 20
#include <lemon/smart_graph.h>
21 21
#include <lemon/list_graph.h>
22 22
#include <lemon/lgf_reader.h>
23

	
24 23
#include <lemon/dijkstra.h>
25 24
#include <lemon/path.h>
26 25

	
27 26
#include "graph_test.h"
28 27
#include "test_tools.h"
29 28

	
30 29
using namespace lemon;
31 30

	
32 31
char test_lgf[] =
33 32
  "@nodes\n"
34 33
  "label\n"
35 34
  "0\n"
36 35
  "1\n"
37 36
  "2\n"
38 37
  "3\n"
39 38
  "4\n"
40 39
  "@arcs\n"
41 40
  "     label length\n"
42 41
  "0 1  0     1\n"
43 42
  "1 2  1     1\n"
44 43
  "2 3  2     1\n"
45 44
  "0 3  4     5\n"
46 45
  "0 3  5     10\n"
47 46
  "0 3  6     7\n"
48 47
  "4 2  7     1\n"
49 48
  "@attributes\n"
50 49
  "source 0\n"
51 50
  "target 3\n";
52 51

	
53 52
void checkDijkstraCompile()
54 53
{
55 54
  typedef int VType;
56 55
  typedef concepts::Digraph Digraph;
57 56
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
58 57
  typedef Dijkstra<Digraph, LengthMap> DType;
59 58

	
60 59
  Digraph G;
61 60
  Digraph::Node n;
62 61
  Digraph::Arc e;
63 62
  VType l;
64 63
  bool b;
65 64
  DType::DistMap d(G);
66 65
  DType::PredMap p(G);
67
  //  DType::PredNodeMap pn(G);
68 66
  LengthMap length;
69 67

	
70 68
  DType dijkstra_test(G,length);
71 69

	
72 70
  dijkstra_test.run(n);
73 71

	
74 72
  l  = dijkstra_test.dist(n);
75 73
  e  = dijkstra_test.predArc(n);
76 74
  n  = dijkstra_test.predNode(n);
77 75
  d  = dijkstra_test.distMap();
78 76
  p  = dijkstra_test.predMap();
79
  //  pn = dijkstra_test.predNodeMap();
80 77
  b  = dijkstra_test.reached(n);
81 78

	
82 79
  Path<Digraph> pp = dijkstra_test.path(n);
83 80
}
84 81

	
85 82
void checkDijkstraFunctionCompile()
86 83
{
87 84
  typedef int VType;
88 85
  typedef concepts::Digraph Digraph;
89 86
  typedef Digraph::Arc Arc;
90 87
  typedef Digraph::Node Node;
91 88
  typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap;
92 89

	
93 90
  Digraph g;
94
  dijkstra(g,LengthMap(),Node()).run();
95
  dijkstra(g,LengthMap()).source(Node()).run();
91
  bool b;
92
  dijkstra(g,LengthMap()).run(Node());
93
  b=dijkstra(g,LengthMap()).run(Node(),Node());
96 94
  dijkstra(g,LengthMap())
97
    .predMap(concepts::WriteMap<Node,Arc>())
98
    .distMap(concepts::WriteMap<Node,VType>())
95
    .predMap(concepts::ReadWriteMap<Node,Arc>())
96
    .distMap(concepts::ReadWriteMap<Node,VType>())
97
    .processedMap(concepts::WriteMap<Node,bool>())
99 98
    .run(Node());
99
  b=dijkstra(g,LengthMap())
100
    .predMap(concepts::ReadWriteMap<Node,Arc>())
101
    .distMap(concepts::ReadWriteMap<Node,VType>())
102
    .processedMap(concepts::WriteMap<Node,bool>())
103
    .path(concepts::Path<Digraph>())
104
    .dist(VType())
105
    .run(Node(),Node());
100 106
}
101 107

	
102 108
template <class Digraph>
103 109
void checkDijkstra() {
104 110
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
105 111
  typedef typename Digraph::template ArcMap<int> LengthMap;
106 112

	
107 113
  Digraph G;
108 114
  Node s, t;
109 115
  LengthMap length(G);
110 116

	
111 117
  std::istringstream input(test_lgf);
112 118
  digraphReader(input, G).
113 119
    arcMap("length", length).
114 120
    node("source", s).
115 121
    node("target", t).
116 122
    run();
117 123

	
118 124
  Dijkstra<Digraph, LengthMap>
119 125
        dijkstra_test(G, length);
120 126
  dijkstra_test.run(s);
121 127

	
122 128
  check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path.");
123 129

	
124 130
  Path<Digraph> p = dijkstra_test.path(t);
125
  check(p.length()==3,"getPath() found a wrong path.");
131
  check(p.length()==3,"path() found a wrong path.");
126 132
  check(checkPath(G, p),"path() found a wrong path.");
127 133
  check(pathSource(G, p) == s,"path() found a wrong path.");
128 134
  check(pathTarget(G, p) == t,"path() found a wrong path.");
129 135

	
130 136
  for(ArcIt e(G); e!=INVALID; ++e) {
131 137
    Node u=G.source(e);
132 138
    Node v=G.target(e);
133 139
    check( !dijkstra_test.reached(u) ||
134 140
           (dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]),
135
           "dist(target)-dist(source)-arc_length= " <<
141
           "Wrong output. dist(target)-dist(source)-arc_length=" <<
136 142
           dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]);
137 143
  }
138 144

	
139 145
  for(NodeIt v(G); v!=INVALID; ++v) {
140 146
    if (dijkstra_test.reached(v)) {
141 147
      check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree.");
142 148
      if (dijkstra_test.predArc(v)!=INVALID ) {
143 149
        Arc e=dijkstra_test.predArc(v);
144 150
        Node u=G.source(e);
145 151
        check(u==dijkstra_test.predNode(v),"Wrong tree.");
146 152
        check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e],
147 153
              "Wrong distance! Difference: " <<
148 154
              std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e]));
149 155
      }
150 156
    }
151 157
  }
152 158

	
153 159
  {
154 160
    NullMap<Node,Arc> myPredMap;
155 161
    dijkstra(G,length).predMap(myPredMap).run(s);
156 162
  }
157 163
}
158 164

	
159 165
int main() {
160 166
  checkDijkstra<ListDigraph>();
161 167
  checkDijkstra<SmartDigraph>();
162 168
  return 0;
163 169
}
0 comments (0 inline)