gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Intel C++ compatibility fixes
0 7 0
default
7 files changed with 37 insertions and 6 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1 1
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
2 2

	
3 3
SET(PROJECT_NAME "LEMON")
4 4
PROJECT(${PROJECT_NAME})
5 5

	
6 6
INCLUDE(FindPythonInterp)
7 7
INCLUDE(FindWget)
8 8

	
9 9
IF(EXISTS ${PROJECT_SOURCE_DIR}/cmake/version.cmake)
10 10
  INCLUDE(${PROJECT_SOURCE_DIR}/cmake/version.cmake)
11 11
ELSEIF(DEFINED ENV{LEMON_VERSION})
12 12
  SET(LEMON_VERSION $ENV{LEMON_VERSION} CACHE STRING "LEMON version string.")
13 13
ELSE()
14 14
  EXECUTE_PROCESS(
15 15
    COMMAND ${PYTHON_EXECUTABLE} ./scripts/chg-len.py
16 16
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
17 17
    OUTPUT_VARIABLE HG_REVISION_PATH
18 18
    ERROR_QUIET
19 19
    OUTPUT_STRIP_TRAILING_WHITESPACE
20 20
  )
21 21
  EXECUTE_PROCESS(
22 22
    COMMAND hg id -i
23 23
    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
24 24
    OUTPUT_VARIABLE HG_REVISION
25 25
    ERROR_QUIET
26 26
    OUTPUT_STRIP_TRAILING_WHITESPACE
27 27
  )
28 28
  IF(HG_REVISION STREQUAL "")
29 29
    SET(HG_REVISION_ID "hg-tip")
30 30
  ELSE()
31 31
    IF(HG_REVISION_PATH STREQUAL "")
32 32
      SET(HG_REVISION_ID ${HG_REVISION})
33 33
    ELSE()
34 34
      SET(HG_REVISION_ID ${HG_REVISION_PATH}.${HG_REVISION})
35 35
    ENDIF()
36 36
  ENDIF()
37 37
  SET(LEMON_VERSION ${HG_REVISION_ID} CACHE STRING "LEMON version string.")
38 38
ENDIF()
39 39

	
40 40
SET(PROJECT_VERSION ${LEMON_VERSION})
41 41

	
42 42
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
43 43

	
44 44
FIND_PACKAGE(Doxygen)
45 45
FIND_PACKAGE(Ghostscript)
46 46
FIND_PACKAGE(GLPK 4.33)
47 47
FIND_PACKAGE(CPLEX)
48 48
FIND_PACKAGE(COIN)
49 49

	
50 50
IF(DEFINED ENV{LEMON_CXX_WARNING})
51 51
  SET(CXX_WARNING $ENV{LEMON_CXX_WARNING})
52 52
ELSE()
53 53
  IF(CMAKE_COMPILER_IS_GNUCXX)
54 54
    SET(CXX_WARNING "-Wall -W -Wunused -Wformat=2 -Wctor-dtor-privacy -Wnon-virtual-dtor -Wno-char-subscripts -Wwrite-strings -Wno-char-subscripts -Wreturn-type -Wcast-qual -Wcast-align -Wsign-promo -Woverloaded-virtual -fno-strict-aliasing -Wold-style-cast -Wno-unknown-pragmas")
55 55
    SET(CMAKE_CXX_FLAGS_DEBUG CACHE STRING "-ggdb")
56 56
    SET(CMAKE_C_FLAGS_DEBUG CACHE STRING "-ggdb")
57 57
  ELSEIF(MSVC)
58 58
    # This part is unnecessary 'casue the same is set by the lemon/core.h.
59 59
    # Still keep it as an example.
60 60
    SET(CXX_WARNING "/wd4250 /wd4355 /wd4503 /wd4800 /wd4996")
61 61
    # Suppressed warnings:
62 62
    # C4250: 'class1' : inherits 'class2::member' via dominance
63 63
    # C4355: 'this' : used in base member initializer list
64 64
    # C4503: 'function' : decorated name length exceeded, name was truncated
65 65
    # C4800: 'type' : forcing value to bool 'true' or 'false'
66 66
    #        (performance warning)
67 67
    # C4996: 'function': was declared deprecated
68 68
  ELSE()
69
    SET(CXX_WARNING "-Wall -W")
69
    SET(CXX_WARNING "-Wall")
70 70
  ENDIF()
71 71
ENDIF()
72 72
SET(LEMON_CXX_WARNING_FLAGS ${CXX_WARNING} CACHE STRING "LEMON warning flags.")
73 73

	
74 74
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LEMON_CXX_WARNING_FLAGS}")
75 75

	
76
SET( CMAKE_CXX_FLAGS_MAINTAINER "-Werror -ggdb" CACHE STRING
76
SET( CMAKE_CXX_FLAGS_MAINTAINER "-Werror -ggdb -O0" CACHE STRING
77 77
    "Flags used by the C++ compiler during maintainer builds."
78 78
    FORCE )
79
SET( CMAKE_C_FLAGS_MAINTAINER "-Werror" CACHE STRING
79
SET( CMAKE_C_FLAGS_MAINTAINER "-Werror -O0" CACHE STRING
80 80
    "Flags used by the C compiler during maintainer builds."
81 81
    FORCE )
82 82
SET( CMAKE_EXE_LINKER_FLAGS_MAINTAINER
83 83
    "-Wl,--warn-unresolved-symbols,--warn-once" CACHE STRING
84 84
    "Flags used for linking binaries during maintainer builds."
85 85
    FORCE )
86 86
SET( CMAKE_SHARED_LINKER_FLAGS_MAINTAINER
87 87
    "-Wl,--warn-unresolved-symbols,--warn-once" CACHE STRING
88 88
    "Flags used by the shared libraries linker during maintainer builds."
89 89
    FORCE )
90 90
MARK_AS_ADVANCED(
91 91
    CMAKE_CXX_FLAGS_MAINTAINER
92 92
    CMAKE_C_FLAGS_MAINTAINER
93 93
    CMAKE_EXE_LINKER_FLAGS_MAINTAINER
94 94
    CMAKE_SHARED_LINKER_FLAGS_MAINTAINER )
95 95

	
96 96
IF(CMAKE_CONFIGURATION_TYPES)
97 97
  LIST(APPEND CMAKE_CONFIGURATION_TYPES Maintainer)
98 98
  LIST(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
99 99
  SET(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING
100 100
      "Add the configurations that we need"
101 101
      FORCE)
102 102
 endif()
103 103

	
104 104
IF(NOT CMAKE_BUILD_TYPE)
105 105
  SET(CMAKE_BUILD_TYPE "Release")
106 106
ENDIF()
107 107

	
108 108
SET( CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
109 109
    "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel Maintainer."
110 110
    FORCE )
111 111

	
112 112

	
113 113
INCLUDE(CheckTypeSize)
114 114
CHECK_TYPE_SIZE("long long" LONG_LONG)
115 115
SET(LEMON_HAVE_LONG_LONG ${HAVE_LONG_LONG})
116 116

	
117 117
ENABLE_TESTING()
118 118

	
119 119
IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer")
120 120
  ADD_CUSTOM_TARGET(check ALL COMMAND ${CMAKE_CTEST_COMMAND})
121 121
ELSE()
122 122
  ADD_CUSTOM_TARGET(check COMMAND ${CMAKE_CTEST_COMMAND})
123 123
ENDIF()
124 124

	
125 125
ADD_SUBDIRECTORY(lemon)
126 126
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
127 127
  ADD_SUBDIRECTORY(demo)
128 128
  ADD_SUBDIRECTORY(tools)
129 129
  ADD_SUBDIRECTORY(doc)
130 130
  ADD_SUBDIRECTORY(test)
131 131
ENDIF()
132 132

	
133 133
CONFIGURE_FILE(
134 134
  ${PROJECT_SOURCE_DIR}/cmake/LEMONConfig.cmake.in
135 135
  ${PROJECT_BINARY_DIR}/cmake/LEMONConfig.cmake
136 136
  @ONLY
137 137
)
138 138
IF(UNIX)
139 139
  INSTALL(
140 140
    FILES ${PROJECT_BINARY_DIR}/cmake/LEMONConfig.cmake
141 141
    DESTINATION share/lemon/cmake
142 142
  )
143 143
ELSEIF(WIN32)
144 144
  INSTALL(
145 145
    FILES ${PROJECT_BINARY_DIR}/cmake/LEMONConfig.cmake
146 146
    DESTINATION cmake
147 147
  )
148 148
ENDIF()
149 149

	
150 150
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
151 151
  SET(CPACK_PACKAGE_NAME ${PROJECT_NAME})
152 152
  SET(CPACK_PACKAGE_VENDOR "EGRES")
153 153
  SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY
154 154
    "LEMON - Library for Efficient Modeling and Optimization in Networks")
155 155
  SET(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
156 156

	
157 157
  SET(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
158 158

	
159 159
  SET(CPACK_PACKAGE_INSTALL_DIRECTORY
160 160
    "${PROJECT_NAME} ${PROJECT_VERSION}")
161 161
  SET(CPACK_PACKAGE_INSTALL_REGISTRY_KEY
162 162
    "${PROJECT_NAME} ${PROJECT_VERSION}")
163 163

	
164 164
  SET(CPACK_COMPONENTS_ALL headers library html_documentation bin)
165 165

	
166 166
  SET(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C++ headers")
167 167
  SET(CPACK_COMPONENT_LIBRARY_DISPLAY_NAME "Dynamic-link library")
168 168
  SET(CPACK_COMPONENT_BIN_DISPLAY_NAME "Command line utilities")
169 169
  SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DISPLAY_NAME "HTML documentation")
170 170

	
171 171
  SET(CPACK_COMPONENT_HEADERS_DESCRIPTION
172 172
    "C++ header files")
173 173
  SET(CPACK_COMPONENT_LIBRARY_DESCRIPTION
174 174
    "DLL and import library")
175 175
  SET(CPACK_COMPONENT_BIN_DESCRIPTION
176 176
    "Command line utilities")
177 177
  SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DESCRIPTION
178 178
    "Doxygen generated documentation")
179 179

	
180 180
  SET(CPACK_COMPONENT_HEADERS_DEPENDS library)
181 181

	
182 182
  SET(CPACK_COMPONENT_HEADERS_GROUP "Development")
183 183
  SET(CPACK_COMPONENT_LIBRARY_GROUP "Development")
184 184
  SET(CPACK_COMPONENT_HTML_DOCUMENTATION_GROUP "Documentation")
185 185

	
186 186
  SET(CPACK_COMPONENT_GROUP_DEVELOPMENT_DESCRIPTION
187 187
    "Components needed to develop software using LEMON")
188 188
  SET(CPACK_COMPONENT_GROUP_DOCUMENTATION_DESCRIPTION
189 189
    "Documentation of LEMON")
190 190

	
191 191
  SET(CPACK_ALL_INSTALL_TYPES Full Developer)
192 192

	
193 193
  SET(CPACK_COMPONENT_HEADERS_INSTALL_TYPES Developer Full)
194 194
  SET(CPACK_COMPONENT_LIBRARY_INSTALL_TYPES Developer Full)
195 195
  SET(CPACK_COMPONENT_HTML_DOCUMENTATION_INSTALL_TYPES Full)
196 196

	
197 197
  SET(CPACK_GENERATOR "NSIS")
198 198
  SET(CPACK_NSIS_MUI_ICON "${PROJECT_SOURCE_DIR}/cmake/nsis/lemon.ico")
199 199
  SET(CPACK_NSIS_MUI_UNIICON "${PROJECT_SOURCE_DIR}/cmake/nsis/uninstall.ico")
200 200
  #SET(CPACK_PACKAGE_ICON "${PROJECT_SOURCE_DIR}/cmake/nsis\\\\installer.bmp")
201 201
  SET(CPACK_NSIS_INSTALLED_ICON_NAME "bin\\\\lemon.ico")
202 202
  SET(CPACK_NSIS_DISPLAY_NAME "${CPACK_PACKAGE_INSTALL_DIRECTORY} ${PROJECT_NAME}")
203 203
  SET(CPACK_NSIS_HELP_LINK "http:\\\\\\\\lemon.cs.elte.hu")
204 204
  SET(CPACK_NSIS_URL_INFO_ABOUT "http:\\\\\\\\lemon.cs.elte.hu")
205 205
  SET(CPACK_NSIS_CONTACT "lemon-user@lemon.cs.elte.hu")
206 206
  SET(CPACK_NSIS_CREATE_ICONS_EXTRA "
207 207
    CreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Documentation.lnk\\\" \\\"$INSTDIR\\\\share\\\\doc\\\\index.html\\\"
208 208
    ")
209 209
  SET(CPACK_NSIS_DELETE_ICONS_EXTRA "
210 210
    !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
211 211
    Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\Documentation.lnk\\\"
212 212
    ")
213 213

	
214 214
  INCLUDE(CPack)
215 215
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-2009
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 31
#include <lemon/path.h>
32 32

	
33 33
namespace lemon {
34 34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
114 114
  ///\ingroup search
115 115
  ///This class provides an efficient implementation of the %BFS algorithm.
116 116
  ///
117 117
  ///There is also a \ref bfs() "function-type interface" for the BFS
118 118
  ///algorithm, which is convenient in the simplier cases and it can be
119 119
  ///used easier.
120 120
  ///
121 121
  ///\tparam GR The type of the digraph the algorithm runs on.
122 122
  ///The default type is \ref ListDigraph.
123 123
#ifdef DOXYGEN
124 124
  template <typename GR,
125 125
            typename TR>
126 126
#else
127 127
  template <typename GR=ListDigraph,
128 128
            typename TR=BfsDefaultTraits<GR> >
129 129
#endif
130 130
  class Bfs {
131 131
  public:
132 132

	
133 133
    ///The type of the digraph the algorithm runs on.
134 134
    typedef typename TR::Digraph Digraph;
135 135

	
136 136
    ///\brief The type of the map that stores the predecessor arcs of the
137 137
    ///shortest paths.
138 138
    typedef typename TR::PredMap PredMap;
139 139
    ///The type of the map that stores the distances of the nodes.
140 140
    typedef typename TR::DistMap DistMap;
141 141
    ///The type of the map that indicates which nodes are reached.
142 142
    typedef typename TR::ReachedMap ReachedMap;
143 143
    ///The type of the map that indicates which nodes are processed.
144 144
    typedef typename TR::ProcessedMap ProcessedMap;
145 145
    ///The type of the paths.
146 146
    typedef PredMapPath<Digraph, PredMap> Path;
147 147

	
148 148
    ///The \ref BfsDefaultTraits "traits class" of the algorithm.
149 149
    typedef TR Traits;
150 150

	
151 151
  private:
152 152

	
153 153
    typedef typename Digraph::Node Node;
154 154
    typedef typename Digraph::NodeIt NodeIt;
155 155
    typedef typename Digraph::Arc Arc;
156 156
    typedef typename Digraph::OutArcIt OutArcIt;
157 157

	
158 158
    //Pointer to the underlying digraph.
159 159
    const Digraph *G;
160 160
    //Pointer to the map of predecessor arcs.
161 161
    PredMap *_pred;
162 162
    //Indicates if _pred is locally allocated (true) or not.
163 163
    bool local_pred;
164 164
    //Pointer to the map of distances.
165 165
    DistMap *_dist;
166 166
    //Indicates if _dist is locally allocated (true) or not.
167 167
    bool local_dist;
168 168
    //Pointer to the map of reached status of the nodes.
169 169
    ReachedMap *_reached;
170 170
    //Indicates if _reached is locally allocated (true) or not.
171 171
    bool local_reached;
172 172
    //Pointer to the map of processed status of the nodes.
173 173
    ProcessedMap *_processed;
174 174
    //Indicates if _processed is locally allocated (true) or not.
175 175
    bool local_processed;
176 176

	
177 177
    std::vector<typename Digraph::Node> _queue;
178 178
    int _queue_head,_queue_tail,_queue_next_dist;
179 179
    int _curr_dist;
180 180

	
181 181
    //Creates the maps if necessary.
182 182
    void create_maps()
183 183
    {
184 184
      if(!_pred) {
185 185
        local_pred = true;
186 186
        _pred = Traits::createPredMap(*G);
187 187
      }
188 188
      if(!_dist) {
189 189
        local_dist = true;
190 190
        _dist = Traits::createDistMap(*G);
191 191
      }
192 192
      if(!_reached) {
193 193
        local_reached = true;
194 194
        _reached = Traits::createReachedMap(*G);
195 195
      }
196 196
      if(!_processed) {
197 197
        local_processed = true;
198 198
        _processed = Traits::createProcessedMap(*G);
199 199
      }
200 200
    }
201 201

	
202 202
  protected:
203 203

	
204 204
    Bfs() {}
205 205

	
206 206
  public:
207 207

	
208 208
    typedef Bfs Create;
209 209

	
210 210
    ///\name Named Template Parameters
211 211

	
212 212
    ///@{
213 213

	
214 214
    template <class T>
215 215
    struct SetPredMapTraits : public Traits {
216 216
      typedef T PredMap;
217 217
      static PredMap *createPredMap(const Digraph &)
218 218
      {
219 219
        LEMON_ASSERT(false, "PredMap is not initialized");
220 220
        return 0; // ignore warnings
221 221
      }
222 222
    };
223 223
    ///\brief \ref named-templ-param "Named parameter" for setting
224 224
    ///\c PredMap type.
225 225
    ///
226 226
    ///\ref named-templ-param "Named parameter" for setting
227 227
    ///\c PredMap type.
228 228
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
229 229
    template <class T>
230 230
    struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > {
231 231
      typedef Bfs< Digraph, SetPredMapTraits<T> > Create;
232 232
    };
233 233

	
234 234
    template <class T>
235 235
    struct SetDistMapTraits : public Traits {
236 236
      typedef T DistMap;
237 237
      static DistMap *createDistMap(const Digraph &)
238 238
      {
239 239
        LEMON_ASSERT(false, "DistMap is not initialized");
240 240
        return 0; // ignore warnings
241 241
      }
242 242
    };
243 243
    ///\brief \ref named-templ-param "Named parameter" for setting
244 244
    ///\c DistMap type.
245 245
    ///
246 246
    ///\ref named-templ-param "Named parameter" for setting
247 247
    ///\c DistMap type.
248 248
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
249 249
    template <class T>
250 250
    struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > {
251 251
      typedef Bfs< Digraph, SetDistMapTraits<T> > Create;
252 252
    };
253 253

	
254 254
    template <class T>
255 255
    struct SetReachedMapTraits : public Traits {
256 256
      typedef T ReachedMap;
257 257
      static ReachedMap *createReachedMap(const Digraph &)
258 258
      {
259 259
        LEMON_ASSERT(false, "ReachedMap is not initialized");
260 260
        return 0; // ignore warnings
261 261
      }
262 262
    };
263 263
    ///\brief \ref named-templ-param "Named parameter" for setting
264 264
    ///\c ReachedMap type.
265 265
    ///
266 266
    ///\ref named-templ-param "Named parameter" for setting
267 267
    ///\c ReachedMap type.
268 268
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
269 269
    template <class T>
270 270
    struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
271 271
      typedef Bfs< Digraph, SetReachedMapTraits<T> > Create;
272 272
    };
273 273

	
274 274
    template <class T>
275 275
    struct SetProcessedMapTraits : public Traits {
276 276
      typedef T ProcessedMap;
277 277
      static ProcessedMap *createProcessedMap(const Digraph &)
278 278
      {
279 279
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
280 280
        return 0; // ignore warnings
281 281
      }
282 282
    };
283 283
    ///\brief \ref named-templ-param "Named parameter" for setting
284 284
    ///\c ProcessedMap type.
285 285
    ///
286 286
    ///\ref named-templ-param "Named parameter" for setting
287 287
    ///\c ProcessedMap type.
288 288
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
289 289
    template <class T>
290 290
    struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
291 291
      typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create;
292 292
    };
293 293

	
294 294
    struct SetStandardProcessedMapTraits : public Traits {
295 295
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
296 296
      static ProcessedMap *createProcessedMap(const Digraph &g)
297 297
      {
298 298
        return new ProcessedMap(g);
299 299
        return 0; // ignore warnings
300 300
      }
301 301
    };
302 302
    ///\brief \ref named-templ-param "Named parameter" for setting
303 303
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
304 304
    ///
305 305
    ///\ref named-templ-param "Named parameter" for setting
306 306
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
307 307
    ///If you don't set it explicitly, it will be automatically allocated.
308 308
    struct SetStandardProcessedMap :
309 309
      public Bfs< Digraph, SetStandardProcessedMapTraits > {
310 310
      typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create;
311 311
    };
312 312

	
313 313
    ///@}
314 314

	
315 315
  public:
316 316

	
317 317
    ///Constructor.
318 318

	
319 319
    ///Constructor.
320 320
    ///\param g The digraph the algorithm runs on.
321 321
    Bfs(const Digraph &g) :
322 322
      G(&g),
323 323
      _pred(NULL), local_pred(false),
324 324
      _dist(NULL), local_dist(false),
325 325
      _reached(NULL), local_reached(false),
326 326
      _processed(NULL), local_processed(false)
327 327
    { }
328 328

	
329 329
    ///Destructor.
330 330
    ~Bfs()
331 331
    {
332 332
      if(local_pred) delete _pred;
333 333
      if(local_dist) delete _dist;
334 334
      if(local_reached) delete _reached;
335 335
      if(local_processed) delete _processed;
336 336
    }
337 337

	
338 338
    ///Sets the map that stores the predecessor arcs.
339 339

	
340 340
    ///Sets the map that stores the predecessor arcs.
341 341
    ///If you don't use this function before calling \ref run(Node) "run()"
342 342
    ///or \ref init(), an instance will be allocated automatically.
343 343
    ///The destructor deallocates this automatically allocated map,
344 344
    ///of course.
345 345
    ///\return <tt> (*this) </tt>
346 346
    Bfs &predMap(PredMap &m)
347 347
    {
348 348
      if(local_pred) {
349 349
        delete _pred;
350 350
        local_pred=false;
351 351
      }
352 352
      _pred = &m;
353 353
      return *this;
354 354
    }
355 355

	
356 356
    ///Sets the map that indicates which nodes are reached.
357 357

	
358 358
    ///Sets the map that indicates which nodes are reached.
359 359
    ///If you don't use this function before calling \ref run(Node) "run()"
360 360
    ///or \ref init(), an instance will be allocated automatically.
361 361
    ///The destructor deallocates this automatically allocated map,
362 362
    ///of course.
363 363
    ///\return <tt> (*this) </tt>
364 364
    Bfs &reachedMap(ReachedMap &m)
365 365
    {
366 366
      if(local_reached) {
367 367
        delete _reached;
368 368
        local_reached=false;
369 369
      }
370 370
      _reached = &m;
371 371
      return *this;
372 372
    }
373 373

	
374 374
    ///Sets the map that indicates which nodes are processed.
375 375

	
376 376
    ///Sets the map that indicates which nodes are processed.
377 377
    ///If you don't use this function before calling \ref run(Node) "run()"
378 378
    ///or \ref init(), an instance will be allocated automatically.
379 379
    ///The destructor deallocates this automatically allocated map,
380 380
    ///of course.
381 381
    ///\return <tt> (*this) </tt>
382 382
    Bfs &processedMap(ProcessedMap &m)
383 383
    {
384 384
      if(local_processed) {
385 385
        delete _processed;
386 386
        local_processed=false;
387 387
      }
388 388
      _processed = &m;
389 389
      return *this;
390 390
    }
391 391

	
392 392
    ///Sets the map that stores the distances of the nodes.
393 393

	
394 394
    ///Sets the map that stores the distances of the nodes calculated by
395 395
    ///the algorithm.
396 396
    ///If you don't use this function before calling \ref run(Node) "run()"
397 397
    ///or \ref init(), an instance will be allocated automatically.
398 398
    ///The destructor deallocates this automatically allocated map,
399 399
    ///of course.
400 400
    ///\return <tt> (*this) </tt>
401 401
    Bfs &distMap(DistMap &m)
402 402
    {
403 403
      if(local_dist) {
404 404
        delete _dist;
405 405
        local_dist=false;
406 406
      }
407 407
      _dist = &m;
408 408
      return *this;
409 409
    }
410 410

	
411 411
  public:
412 412

	
413 413
    ///\name Execution Control
414 414
    ///The simplest way to execute the BFS algorithm is to use one of the
415 415
    ///member functions called \ref run(Node) "run()".\n
416 416
    ///If you need more control on the execution, first you have to call
417 417
    ///\ref init(), then you can add several source nodes with
418 418
    ///\ref addSource(). Finally the actual path computation can be
419 419
    ///performed with one of the \ref start() functions.
420 420

	
421 421
    ///@{
422 422

	
423 423
    ///\brief Initializes the internal data structures.
424 424
    ///
425 425
    ///Initializes the internal data structures.
426 426
    void init()
427 427
    {
428 428
      create_maps();
429 429
      _queue.resize(countNodes(*G));
430 430
      _queue_head=_queue_tail=0;
431 431
      _curr_dist=1;
432 432
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
433 433
        _pred->set(u,INVALID);
434 434
        _reached->set(u,false);
435 435
        _processed->set(u,false);
436 436
      }
437 437
    }
438 438

	
439 439
    ///Adds a new source node.
440 440

	
441 441
    ///Adds a new source node to the set of nodes to be processed.
442 442
    ///
443 443
    void addSource(Node s)
444 444
    {
445 445
      if(!(*_reached)[s])
446 446
        {
447 447
          _reached->set(s,true);
448 448
          _pred->set(s,INVALID);
449 449
          _dist->set(s,0);
450 450
          _queue[_queue_head++]=s;
451 451
          _queue_next_dist=_queue_head;
452 452
        }
453 453
    }
454 454

	
455 455
    ///Processes the next node.
456 456

	
457 457
    ///Processes the next node.
458 458
    ///
459 459
    ///\return The processed node.
460 460
    ///
461 461
    ///\pre The queue must not be empty.
462 462
    Node processNextNode()
463 463
    {
464 464
      if(_queue_tail==_queue_next_dist) {
465 465
        _curr_dist++;
466 466
        _queue_next_dist=_queue_head;
467 467
      }
468 468
      Node n=_queue[_queue_tail++];
469 469
      _processed->set(n,true);
470 470
      Node m;
471 471
      for(OutArcIt e(*G,n);e!=INVALID;++e)
472 472
        if(!(*_reached)[m=G->target(e)]) {
473 473
          _queue[_queue_head++]=m;
474 474
          _reached->set(m,true);
475 475
          _pred->set(m,e);
476 476
          _dist->set(m,_curr_dist);
477 477
        }
478 478
      return n;
479 479
    }
480 480

	
481 481
    ///Processes the next node.
482 482

	
483 483
    ///Processes the next node and checks if the given target node
484 484
    ///is reached. If the target node is reachable from the processed
485 485
    ///node, then the \c reach parameter will be set to \c true.
486 486
    ///
487 487
    ///\param target The target node.
488 488
    ///\retval reach Indicates if the target node is reached.
489 489
    ///It should be initially \c false.
490 490
    ///
491 491
    ///\return The processed node.
492 492
    ///
493 493
    ///\pre The queue must not be empty.
494 494
    Node processNextNode(Node target, bool& reach)
495 495
    {
496 496
      if(_queue_tail==_queue_next_dist) {
497 497
        _curr_dist++;
498 498
        _queue_next_dist=_queue_head;
499 499
      }
500 500
      Node n=_queue[_queue_tail++];
501 501
      _processed->set(n,true);
502 502
      Node m;
503 503
      for(OutArcIt e(*G,n);e!=INVALID;++e)
504 504
        if(!(*_reached)[m=G->target(e)]) {
505 505
          _queue[_queue_head++]=m;
506 506
          _reached->set(m,true);
507 507
          _pred->set(m,e);
508 508
          _dist->set(m,_curr_dist);
509 509
          reach = reach || (target == m);
510 510
        }
511 511
      return n;
512 512
    }
513 513

	
514 514
    ///Processes the next node.
515 515

	
516 516
    ///Processes the next node and checks if at least one of reached
517 517
    ///nodes has \c true value in the \c nm node map. If one node
518 518
    ///with \c true value is reachable from the processed node, then the
519 519
    ///\c rnode parameter will be set to the first of such nodes.
520 520
    ///
521 521
    ///\param nm A \c bool (or convertible) node map that indicates the
522 522
    ///possible targets.
523 523
    ///\retval rnode The reached target node.
524 524
    ///It should be initially \c INVALID.
525 525
    ///
526 526
    ///\return The processed node.
527 527
    ///
528 528
    ///\pre The queue must not be empty.
529 529
    template<class NM>
530 530
    Node processNextNode(const NM& nm, Node& rnode)
531 531
    {
532 532
      if(_queue_tail==_queue_next_dist) {
533 533
        _curr_dist++;
534 534
        _queue_next_dist=_queue_head;
535 535
      }
536 536
      Node n=_queue[_queue_tail++];
537 537
      _processed->set(n,true);
538 538
      Node m;
539 539
      for(OutArcIt e(*G,n);e!=INVALID;++e)
540 540
        if(!(*_reached)[m=G->target(e)]) {
541 541
          _queue[_queue_head++]=m;
542 542
          _reached->set(m,true);
543 543
          _pred->set(m,e);
544 544
          _dist->set(m,_curr_dist);
545 545
          if (nm[m] && rnode == INVALID) rnode = m;
546 546
        }
547 547
      return n;
548 548
    }
549 549

	
550 550
    ///The next node to be processed.
551 551

	
552 552
    ///Returns the next node to be processed or \c INVALID if the queue
553 553
    ///is empty.
554 554
    Node nextNode() const
555 555
    {
556 556
      return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
557 557
    }
558 558

	
559 559
    ///Returns \c false if there are nodes to be processed.
560 560

	
561 561
    ///Returns \c false if there are nodes to be processed
562 562
    ///in the queue.
563 563
    bool emptyQueue() const { return _queue_tail==_queue_head; }
564 564

	
565 565
    ///Returns the number of the nodes to be processed.
566 566

	
567 567
    ///Returns the number of the nodes to be processed
568 568
    ///in the queue.
569 569
    int queueSize() const { return _queue_head-_queue_tail; }
570 570

	
571 571
    ///Executes the algorithm.
572 572

	
573 573
    ///Executes the algorithm.
574 574
    ///
575 575
    ///This method runs the %BFS algorithm from the root node(s)
576 576
    ///in order to compute the shortest path to each node.
577 577
    ///
578 578
    ///The algorithm computes
579 579
    ///- the shortest path tree (forest),
580 580
    ///- the distance of each node from the root(s).
581 581
    ///
582 582
    ///\pre init() must be called and at least one root node should be
583 583
    ///added with addSource() before using this function.
584 584
    ///
585 585
    ///\note <tt>b.start()</tt> is just a shortcut of the following code.
586 586
    ///\code
587 587
    ///  while ( !b.emptyQueue() ) {
588 588
    ///    b.processNextNode();
589 589
    ///  }
590 590
    ///\endcode
591 591
    void start()
592 592
    {
593 593
      while ( !emptyQueue() ) processNextNode();
594 594
    }
595 595

	
596 596
    ///Executes the algorithm until the given target node is reached.
597 597

	
598 598
    ///Executes the algorithm until the given target node is reached.
599 599
    ///
600 600
    ///This method runs the %BFS algorithm from the root node(s)
601 601
    ///in order to compute the shortest path to \c t.
602 602
    ///
603 603
    ///The algorithm computes
604 604
    ///- the shortest path to \c t,
605 605
    ///- the distance of \c t from the root(s).
606 606
    ///
607 607
    ///\pre init() must be called and at least one root node should be
608 608
    ///added with addSource() before using this function.
609 609
    ///
610 610
    ///\note <tt>b.start(t)</tt> is just a shortcut of the following code.
611 611
    ///\code
612 612
    ///  bool reach = false;
613 613
    ///  while ( !b.emptyQueue() && !reach ) {
614 614
    ///    b.processNextNode(t, reach);
615 615
    ///  }
616 616
    ///\endcode
617 617
    void start(Node t)
618 618
    {
619 619
      bool reach = false;
620 620
      while ( !emptyQueue() && !reach ) processNextNode(t, reach);
621 621
    }
622 622

	
623 623
    ///Executes the algorithm until a condition is met.
624 624

	
625 625
    ///Executes the algorithm until a condition is met.
626 626
    ///
627 627
    ///This method runs the %BFS algorithm from the root node(s) in
628 628
    ///order to compute the shortest path to a node \c v with
629 629
    /// <tt>nm[v]</tt> true, if such a node can be found.
630 630
    ///
631 631
    ///\param nm A \c bool (or convertible) node map. The algorithm
632 632
    ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
633 633
    ///
634 634
    ///\return The reached node \c v with <tt>nm[v]</tt> true or
635 635
    ///\c INVALID if no such node was found.
636 636
    ///
637 637
    ///\pre init() must be called and at least one root node should be
638 638
    ///added with addSource() before using this function.
639 639
    ///
640 640
    ///\note <tt>b.start(nm)</tt> is just a shortcut of the following code.
641 641
    ///\code
642 642
    ///  Node rnode = INVALID;
643 643
    ///  while ( !b.emptyQueue() && rnode == INVALID ) {
644 644
    ///    b.processNextNode(nm, rnode);
645 645
    ///  }
646 646
    ///  return rnode;
647 647
    ///\endcode
648 648
    template<class NodeBoolMap>
649 649
    Node start(const NodeBoolMap &nm)
650 650
    {
651 651
      Node rnode = INVALID;
652 652
      while ( !emptyQueue() && rnode == INVALID ) {
653 653
        processNextNode(nm, rnode);
654 654
      }
655 655
      return rnode;
656 656
    }
657 657

	
658 658
    ///Runs the algorithm from the given source node.
659 659

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

	
679 679
    ///Finds the shortest path between \c s and \c t.
680 680

	
681 681
    ///This method runs the %BFS algorithm from node \c s
682 682
    ///in order to compute the shortest path to node \c t
683 683
    ///(it stops searching when \c t is processed).
684 684
    ///
685 685
    ///\return \c true if \c t is reachable form \c s.
686 686
    ///
687 687
    ///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a
688 688
    ///shortcut of the following code.
689 689
    ///\code
690 690
    ///  b.init();
691 691
    ///  b.addSource(s);
692 692
    ///  b.start(t);
693 693
    ///\endcode
694 694
    bool run(Node s,Node t) {
695 695
      init();
696 696
      addSource(s);
697 697
      start(t);
698 698
      return reached(t);
699 699
    }
700 700

	
701 701
    ///Runs the algorithm to visit all nodes in the digraph.
702 702

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

	
730 730
    ///@}
731 731

	
732 732
    ///\name Query Functions
733 733
    ///The results of the BFS algorithm can be obtained using these
734 734
    ///functions.\n
735 735
    ///Either \ref run(Node) "run()" or \ref start() should be called
736 736
    ///before using them.
737 737

	
738 738
    ///@{
739 739

	
740 740
    ///The shortest path to a node.
741 741

	
742 742
    ///Returns the shortest path to a node.
743 743
    ///
744 744
    ///\warning \c t should be reached from the root(s).
745 745
    ///
746 746
    ///\pre Either \ref run(Node) "run()" or \ref init()
747 747
    ///must be called before using this function.
748 748
    Path path(Node t) const { return Path(*G, *_pred, t); }
749 749

	
750 750
    ///The distance of a node from the root(s).
751 751

	
752 752
    ///Returns the distance of a node from the root(s).
753 753
    ///
754 754
    ///\warning If node \c v is not reached from the root(s), then
755 755
    ///the return value of this function is undefined.
756 756
    ///
757 757
    ///\pre Either \ref run(Node) "run()" or \ref init()
758 758
    ///must be called before using this function.
759 759
    int dist(Node v) const { return (*_dist)[v]; }
760 760

	
761 761
    ///Returns the 'previous arc' of the shortest path tree for a node.
762 762

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

	
775 775
    ///Returns the 'previous node' of the shortest path tree for a node.
776 776

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

	
790 790
    ///\brief Returns a const reference to the node map that stores the
791 791
    /// distances of the nodes.
792 792
    ///
793 793
    ///Returns a const reference to the node map that stores the distances
794 794
    ///of the nodes calculated by the algorithm.
795 795
    ///
796 796
    ///\pre Either \ref run(Node) "run()" or \ref init()
797 797
    ///must be called before using this function.
798 798
    const DistMap &distMap() const { return *_dist;}
799 799

	
800 800
    ///\brief Returns a const reference to the node map that stores the
801 801
    ///predecessor arcs.
802 802
    ///
803 803
    ///Returns a const reference to the node map that stores the predecessor
804 804
    ///arcs, which form the shortest path tree.
805 805
    ///
806 806
    ///\pre Either \ref run(Node) "run()" or \ref init()
807 807
    ///must be called before using this function.
808 808
    const PredMap &predMap() const { return *_pred;}
809 809

	
810 810
    ///Checks if a node is reached from the root(s).
811 811

	
812 812
    ///Returns \c true if \c v is reached from the root(s).
813 813
    ///
814 814
    ///\pre Either \ref run(Node) "run()" or \ref init()
815 815
    ///must be called before using this function.
816 816
    bool reached(Node v) const { return (*_reached)[v]; }
817 817

	
818 818
    ///@}
819 819
  };
820 820

	
821 821
  ///Default traits class of bfs() function.
822 822

	
823 823
  ///Default traits class of bfs() function.
824 824
  ///\tparam GR Digraph type.
825 825
  template<class GR>
826 826
  struct BfsWizardDefaultTraits
827 827
  {
828 828
    ///The type of the digraph the algorithm runs on.
829 829
    typedef GR Digraph;
830 830

	
831 831
    ///\brief The type of the map that stores the predecessor
832 832
    ///arcs of the shortest paths.
833 833
    ///
834 834
    ///The type of the map that stores the predecessor
835 835
    ///arcs of the shortest paths.
836 836
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
837 837
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
838 838
    ///Instantiates a PredMap.
839 839

	
840 840
    ///This function instantiates a PredMap.
841 841
    ///\param g is the digraph, to which we would like to define the
842 842
    ///PredMap.
843 843
    static PredMap *createPredMap(const Digraph &g)
844 844
    {
845 845
      return new PredMap(g);
846 846
    }
847 847

	
848 848
    ///The type of the map that indicates which nodes are processed.
849 849

	
850 850
    ///The type of the map that indicates which nodes are processed.
851 851
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
852 852
    ///By default it is a NullMap.
853 853
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
854 854
    ///Instantiates a ProcessedMap.
855 855

	
856 856
    ///This function instantiates a ProcessedMap.
857 857
    ///\param g is the digraph, to which
858 858
    ///we would like to define the ProcessedMap.
859 859
#ifdef DOXYGEN
860 860
    static ProcessedMap *createProcessedMap(const Digraph &g)
861 861
#else
862 862
    static ProcessedMap *createProcessedMap(const Digraph &)
863 863
#endif
864 864
    {
865 865
      return new ProcessedMap();
866 866
    }
867 867

	
868 868
    ///The type of the map that indicates which nodes are reached.
869 869

	
870 870
    ///The type of the map that indicates which nodes are reached.
871 871
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
872 872
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
873 873
    ///Instantiates a ReachedMap.
874 874

	
875 875
    ///This function instantiates a ReachedMap.
876 876
    ///\param g is the digraph, to which
877 877
    ///we would like to define the ReachedMap.
878 878
    static ReachedMap *createReachedMap(const Digraph &g)
879 879
    {
880 880
      return new ReachedMap(g);
881 881
    }
882 882

	
883 883
    ///The type of the map that stores the distances of the nodes.
884 884

	
885 885
    ///The type of the map that stores the distances of the nodes.
886 886
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
887 887
    typedef typename Digraph::template NodeMap<int> DistMap;
888 888
    ///Instantiates a DistMap.
889 889

	
890 890
    ///This function instantiates a DistMap.
891 891
    ///\param g is the digraph, to which we would like to define
892 892
    ///the DistMap
893 893
    static DistMap *createDistMap(const Digraph &g)
894 894
    {
895 895
      return new DistMap(g);
896 896
    }
897 897

	
898 898
    ///The type of the shortest paths.
899 899

	
900 900
    ///The type of the shortest paths.
901 901
    ///It must meet the \ref concepts::Path "Path" concept.
902 902
    typedef lemon::Path<Digraph> Path;
903 903
  };
904 904

	
905 905
  /// Default traits class used by BfsWizard
906 906

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

	
917 917
    typedef BfsWizardDefaultTraits<GR> Base;
918 918
  protected:
919 919
    //The type of the nodes in the digraph.
920 920
    typedef typename Base::Digraph::Node Node;
921 921

	
922 922
    //Pointer to the digraph the algorithm runs on.
923 923
    void *_g;
924 924
    //Pointer to the map of reached nodes.
925 925
    void *_reached;
926 926
    //Pointer to the map of processed nodes.
927 927
    void *_processed;
928 928
    //Pointer to the map of predecessors arcs.
929 929
    void *_pred;
930 930
    //Pointer to the map of distances.
931 931
    void *_dist;
932 932
    //Pointer to the shortest path to the target node.
933 933
    void *_path;
934 934
    //Pointer to the distance of the target node.
935 935
    int *_di;
936 936

	
937 937
    public:
938 938
    /// Constructor.
939 939

	
940 940
    /// This constructor does not require parameters, therefore it initiates
941 941
    /// all of the attributes to \c 0.
942 942
    BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
943 943
                      _dist(0), _path(0), _di(0) {}
944 944

	
945 945
    /// Constructor.
946 946

	
947 947
    /// This constructor requires one parameter,
948 948
    /// others are initiated to \c 0.
949 949
    /// \param g The digraph the algorithm runs on.
950 950
    BfsWizardBase(const GR &g) :
951 951
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
952 952
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
953 953

	
954 954
  };
955 955

	
956 956
  /// Auxiliary class for the function-type interface of BFS algorithm.
957 957

	
958 958
  /// This auxiliary class is created to implement the
959 959
  /// \ref bfs() "function-type interface" of \ref Bfs algorithm.
960 960
  /// It does not have own \ref run(Node) "run()" method, it uses the
961 961
  /// functions and features of the plain \ref Bfs.
962 962
  ///
963 963
  /// This class should only be used through the \ref bfs() function,
964 964
  /// which makes it easier to use the algorithm.
965 965
  template<class TR>
966 966
  class BfsWizard : public TR
967 967
  {
968 968
    typedef TR Base;
969 969

	
970 970
    ///The type of the digraph the algorithm runs on.
971 971
    typedef typename TR::Digraph Digraph;
972 972

	
973 973
    typedef typename Digraph::Node Node;
974 974
    typedef typename Digraph::NodeIt NodeIt;
975 975
    typedef typename Digraph::Arc Arc;
976 976
    typedef typename Digraph::OutArcIt OutArcIt;
977 977

	
978 978
    ///\brief The type of the map that stores the predecessor
979 979
    ///arcs of the shortest paths.
980 980
    typedef typename TR::PredMap PredMap;
981 981
    ///\brief The type of the map that stores the distances of the nodes.
982 982
    typedef typename TR::DistMap DistMap;
983 983
    ///\brief The type of the map that indicates which nodes are reached.
984 984
    typedef typename TR::ReachedMap ReachedMap;
985 985
    ///\brief The type of the map that indicates which nodes are processed.
986 986
    typedef typename TR::ProcessedMap ProcessedMap;
987 987
    ///The type of the shortest paths
988 988
    typedef typename TR::Path Path;
989 989

	
990 990
  public:
991 991

	
992 992
    /// Constructor.
993 993
    BfsWizard() : TR() {}
994 994

	
995 995
    /// Constructor that requires parameters.
996 996

	
997 997
    /// Constructor that requires parameters.
998 998
    /// These parameters will be the default values for the traits class.
999 999
    /// \param g The digraph the algorithm runs on.
1000 1000
    BfsWizard(const Digraph &g) :
1001 1001
      TR(g) {}
1002 1002

	
1003 1003
    ///Copy constructor
1004 1004
    BfsWizard(const TR &b) : TR(b) {}
1005 1005

	
1006 1006
    ~BfsWizard() {}
1007 1007

	
1008 1008
    ///Runs BFS algorithm from the given source node.
1009 1009

	
1010 1010
    ///This method runs BFS algorithm from node \c s
1011 1011
    ///in order to compute the shortest path to each node.
1012 1012
    void run(Node s)
1013 1013
    {
1014 1014
      Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
1015 1015
      if (Base::_pred)
1016 1016
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
1017 1017
      if (Base::_dist)
1018 1018
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
1019 1019
      if (Base::_reached)
1020 1020
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
1021 1021
      if (Base::_processed)
1022 1022
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
1023 1023
      if (s!=INVALID)
1024 1024
        alg.run(s);
1025 1025
      else
1026 1026
        alg.run();
1027 1027
    }
1028 1028

	
1029 1029
    ///Finds the shortest path between \c s and \c t.
1030 1030

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

	
1055 1055
    ///Runs BFS algorithm to visit all nodes in the digraph.
1056 1056

	
1057 1057
    ///This method runs BFS algorithm in order to compute
1058 1058
    ///the shortest path to each node.
1059 1059
    void run()
1060 1060
    {
1061 1061
      run(INVALID);
1062 1062
    }
1063 1063

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

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

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

	
1118 1118
    template<class T>
1119 1119
    struct SetProcessedMapBase : public Base {
1120 1120
      typedef T ProcessedMap;
1121 1121
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1122 1122
      SetProcessedMapBase(const TR &b) : TR(b) {}
1123 1123
    };
1124 1124
    ///\brief \ref named-func-param "Named parameter"
1125 1125
    ///for setting ProcessedMap object.
1126 1126
    ///
1127 1127
    /// \ref named-func-param "Named parameter"
1128 1128
    ///for setting ProcessedMap object.
1129 1129
    template<class T>
1130 1130
    BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1131 1131
    {
1132 1132
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1133 1133
      return BfsWizard<SetProcessedMapBase<T> >(*this);
1134 1134
    }
1135 1135

	
1136 1136
    template<class T>
1137 1137
    struct SetPathBase : public Base {
1138 1138
      typedef T Path;
1139 1139
      SetPathBase(const TR &b) : TR(b) {}
1140 1140
    };
1141 1141
    ///\brief \ref named-func-param "Named parameter"
1142 1142
    ///for getting the shortest path to the target node.
1143 1143
    ///
1144 1144
    ///\ref named-func-param "Named parameter"
1145 1145
    ///for getting the shortest path to the target node.
1146 1146
    template<class T>
1147 1147
    BfsWizard<SetPathBase<T> > path(const T &t)
1148 1148
    {
1149 1149
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1150 1150
      return BfsWizard<SetPathBase<T> >(*this);
1151 1151
    }
1152 1152

	
1153 1153
    ///\brief \ref named-func-param "Named parameter"
1154 1154
    ///for getting the distance of the target node.
1155 1155
    ///
1156 1156
    ///\ref named-func-param "Named parameter"
1157 1157
    ///for getting the distance of the target node.
1158 1158
    BfsWizard dist(const int &d)
1159 1159
    {
1160 1160
      Base::_di=const_cast<int*>(&d);
1161 1161
      return *this;
1162 1162
    }
1163 1163

	
1164 1164
  };
1165 1165

	
1166 1166
  ///Function-type interface for BFS algorithm.
1167 1167

	
1168 1168
  /// \ingroup search
1169 1169
  ///Function-type interface for BFS algorithm.
1170 1170
  ///
1171 1171
  ///This function also has several \ref named-func-param "named parameters",
1172 1172
  ///they are declared as the members of class \ref BfsWizard.
1173 1173
  ///The following examples show how to use these parameters.
1174 1174
  ///\code
1175 1175
  ///  // Compute shortest path from node s to each node
1176 1176
  ///  bfs(g).predMap(preds).distMap(dists).run(s);
1177 1177
  ///
1178 1178
  ///  // Compute shortest path from s to t
1179 1179
  ///  bool reached = bfs(g).path(p).dist(d).run(s,t);
1180 1180
  ///\endcode
1181 1181
  ///\warning Don't forget to put the \ref BfsWizard::run(Node) "run()"
1182 1182
  ///to the end of the parameter list.
1183 1183
  ///\sa BfsWizard
1184 1184
  ///\sa Bfs
1185 1185
  template<class GR>
1186 1186
  BfsWizard<BfsWizardBase<GR> >
1187 1187
  bfs(const GR &digraph)
1188 1188
  {
1189 1189
    return BfsWizard<BfsWizardBase<GR> >(digraph);
1190 1190
  }
1191 1191

	
1192 1192
#ifdef DOXYGEN
1193 1193
  /// \brief Visitor class for BFS.
1194 1194
  ///
1195 1195
  /// This class defines the interface of the BfsVisit events, and
1196 1196
  /// it could be the base of a real visitor class.
1197 1197
  template <typename GR>
1198 1198
  struct BfsVisitor {
1199 1199
    typedef GR Digraph;
1200 1200
    typedef typename Digraph::Arc Arc;
1201 1201
    typedef typename Digraph::Node Node;
1202 1202
    /// \brief Called for the source node(s) of the BFS.
1203 1203
    ///
1204 1204
    /// This function is called for the source node(s) of the BFS.
1205 1205
    void start(const Node& node) {}
1206 1206
    /// \brief Called when a node is reached first time.
1207 1207
    ///
1208 1208
    /// This function is called when a node is reached first time.
1209 1209
    void reach(const Node& node) {}
1210 1210
    /// \brief Called when a node is processed.
1211 1211
    ///
1212 1212
    /// This function is called when a node is processed.
1213 1213
    void process(const Node& node) {}
1214 1214
    /// \brief Called when an arc reaches a new node.
1215 1215
    ///
1216 1216
    /// This function is called when the BFS finds an arc whose target node
1217 1217
    /// is not reached yet.
1218 1218
    void discover(const Arc& arc) {}
1219 1219
    /// \brief Called when an arc is examined but its target node is
1220 1220
    /// already discovered.
1221 1221
    ///
1222 1222
    /// This function is called when an arc is examined but its target node is
1223 1223
    /// already discovered.
1224 1224
    void examine(const Arc& arc) {}
1225 1225
  };
1226 1226
#else
1227 1227
  template <typename GR>
1228 1228
  struct BfsVisitor {
1229 1229
    typedef GR Digraph;
1230 1230
    typedef typename Digraph::Arc Arc;
1231 1231
    typedef typename Digraph::Node Node;
1232 1232
    void start(const Node&) {}
1233 1233
    void reach(const Node&) {}
1234 1234
    void process(const Node&) {}
1235 1235
    void discover(const Arc&) {}
1236 1236
    void examine(const Arc&) {}
1237 1237

	
1238 1238
    template <typename _Visitor>
1239 1239
    struct Constraints {
1240 1240
      void constraints() {
1241 1241
        Arc arc;
1242 1242
        Node node;
1243 1243
        visitor.start(node);
1244 1244
        visitor.reach(node);
1245 1245
        visitor.process(node);
1246 1246
        visitor.discover(arc);
1247 1247
        visitor.examine(arc);
1248 1248
      }
1249 1249
      _Visitor& visitor;
1250
      Constraints() {}
1250 1251
    };
1251 1252
  };
1252 1253
#endif
1253 1254

	
1254 1255
  /// \brief Default traits class of BfsVisit class.
1255 1256
  ///
1256 1257
  /// Default traits class of BfsVisit class.
1257 1258
  /// \tparam GR The type of the digraph the algorithm runs on.
1258 1259
  template<class GR>
1259 1260
  struct BfsVisitDefaultTraits {
1260 1261

	
1261 1262
    /// \brief The type of the digraph the algorithm runs on.
1262 1263
    typedef GR Digraph;
1263 1264

	
1264 1265
    /// \brief The type of the map that indicates which nodes are reached.
1265 1266
    ///
1266 1267
    /// The type of the map that indicates which nodes are reached.
1267 1268
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1268 1269
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1269 1270

	
1270 1271
    /// \brief Instantiates a ReachedMap.
1271 1272
    ///
1272 1273
    /// This function instantiates a ReachedMap.
1273 1274
    /// \param digraph is the digraph, to which
1274 1275
    /// we would like to define the ReachedMap.
1275 1276
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1276 1277
      return new ReachedMap(digraph);
1277 1278
    }
1278 1279

	
1279 1280
  };
1280 1281

	
1281 1282
  /// \ingroup search
1282 1283
  ///
1283 1284
  /// \brief BFS algorithm class with visitor interface.
1284 1285
  ///
1285 1286
  /// This class provides an efficient implementation of the BFS algorithm
1286 1287
  /// with visitor interface.
1287 1288
  ///
1288 1289
  /// The BfsVisit class provides an alternative interface to the Bfs
1289 1290
  /// class. It works with callback mechanism, the BfsVisit object calls
1290 1291
  /// the member functions of the \c Visitor class on every BFS event.
1291 1292
  ///
1292 1293
  /// This interface of the BFS algorithm should be used in special cases
1293 1294
  /// when extra actions have to be performed in connection with certain
1294 1295
  /// events of the BFS algorithm. Otherwise consider to use Bfs or bfs()
1295 1296
  /// instead.
1296 1297
  ///
1297 1298
  /// \tparam GR The type of the digraph the algorithm runs on.
1298 1299
  /// The default type is \ref ListDigraph.
1299 1300
  /// The value of GR is not used directly by \ref BfsVisit,
1300 1301
  /// it is only passed to \ref BfsVisitDefaultTraits.
1301 1302
  /// \tparam VS The Visitor type that is used by the algorithm.
1302 1303
  /// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which
1303 1304
  /// does not observe the BFS events. If you want to observe the BFS
1304 1305
  /// events, you should implement your own visitor class.
1305 1306
  /// \tparam TR Traits class to set various data types used by the
1306 1307
  /// algorithm. The default traits class is
1307 1308
  /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<GR>".
1308 1309
  /// See \ref BfsVisitDefaultTraits for the documentation of
1309 1310
  /// a BFS visit traits class.
1310 1311
#ifdef DOXYGEN
1311 1312
  template <typename GR, typename VS, typename TR>
1312 1313
#else
1313 1314
  template <typename GR = ListDigraph,
1314 1315
            typename VS = BfsVisitor<GR>,
1315 1316
            typename TR = BfsVisitDefaultTraits<GR> >
1316 1317
#endif
1317 1318
  class BfsVisit {
1318 1319
  public:
1319 1320

	
1320 1321
    ///The traits class.
1321 1322
    typedef TR Traits;
1322 1323

	
1323 1324
    ///The type of the digraph the algorithm runs on.
1324 1325
    typedef typename Traits::Digraph Digraph;
1325 1326

	
1326 1327
    ///The visitor type used by the algorithm.
1327 1328
    typedef VS Visitor;
1328 1329

	
1329 1330
    ///The type of the map that indicates which nodes are reached.
1330 1331
    typedef typename Traits::ReachedMap ReachedMap;
1331 1332

	
1332 1333
  private:
1333 1334

	
1334 1335
    typedef typename Digraph::Node Node;
1335 1336
    typedef typename Digraph::NodeIt NodeIt;
1336 1337
    typedef typename Digraph::Arc Arc;
1337 1338
    typedef typename Digraph::OutArcIt OutArcIt;
1338 1339

	
1339 1340
    //Pointer to the underlying digraph.
1340 1341
    const Digraph *_digraph;
1341 1342
    //Pointer to the visitor object.
1342 1343
    Visitor *_visitor;
1343 1344
    //Pointer to the map of reached status of the nodes.
1344 1345
    ReachedMap *_reached;
1345 1346
    //Indicates if _reached is locally allocated (true) or not.
1346 1347
    bool local_reached;
1347 1348

	
1348 1349
    std::vector<typename Digraph::Node> _list;
1349 1350
    int _list_front, _list_back;
1350 1351

	
1351 1352
    //Creates the maps if necessary.
1352 1353
    void create_maps() {
1353 1354
      if(!_reached) {
1354 1355
        local_reached = true;
1355 1356
        _reached = Traits::createReachedMap(*_digraph);
1356 1357
      }
1357 1358
    }
1358 1359

	
1359 1360
  protected:
1360 1361

	
1361 1362
    BfsVisit() {}
1362 1363

	
1363 1364
  public:
1364 1365

	
1365 1366
    typedef BfsVisit Create;
1366 1367

	
1367 1368
    /// \name Named Template Parameters
1368 1369

	
1369 1370
    ///@{
1370 1371
    template <class T>
1371 1372
    struct SetReachedMapTraits : public Traits {
1372 1373
      typedef T ReachedMap;
1373 1374
      static ReachedMap *createReachedMap(const Digraph &digraph) {
1374 1375
        LEMON_ASSERT(false, "ReachedMap is not initialized");
1375 1376
        return 0; // ignore warnings
1376 1377
      }
1377 1378
    };
1378 1379
    /// \brief \ref named-templ-param "Named parameter" for setting
1379 1380
    /// ReachedMap type.
1380 1381
    ///
1381 1382
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
1382 1383
    template <class T>
1383 1384
    struct SetReachedMap : public BfsVisit< Digraph, Visitor,
1384 1385
                                            SetReachedMapTraits<T> > {
1385 1386
      typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
1386 1387
    };
1387 1388
    ///@}
1388 1389

	
1389 1390
  public:
1390 1391

	
1391 1392
    /// \brief Constructor.
1392 1393
    ///
1393 1394
    /// Constructor.
1394 1395
    ///
1395 1396
    /// \param digraph The digraph the algorithm runs on.
1396 1397
    /// \param visitor The visitor object of the algorithm.
1397 1398
    BfsVisit(const Digraph& digraph, Visitor& visitor)
1398 1399
      : _digraph(&digraph), _visitor(&visitor),
1399 1400
        _reached(0), local_reached(false) {}
1400 1401

	
1401 1402
    /// \brief Destructor.
1402 1403
    ~BfsVisit() {
1403 1404
      if(local_reached) delete _reached;
1404 1405
    }
1405 1406

	
1406 1407
    /// \brief Sets the map that indicates which nodes are reached.
1407 1408
    ///
1408 1409
    /// Sets the map that indicates which nodes are reached.
1409 1410
    /// If you don't use this function before calling \ref run(Node) "run()"
1410 1411
    /// or \ref init(), an instance will be allocated automatically.
1411 1412
    /// The destructor deallocates this automatically allocated map,
1412 1413
    /// of course.
1413 1414
    /// \return <tt> (*this) </tt>
1414 1415
    BfsVisit &reachedMap(ReachedMap &m) {
1415 1416
      if(local_reached) {
1416 1417
        delete _reached;
1417 1418
        local_reached = false;
1418 1419
      }
1419 1420
      _reached = &m;
1420 1421
      return *this;
1421 1422
    }
1422 1423

	
1423 1424
  public:
1424 1425

	
1425 1426
    /// \name Execution Control
1426 1427
    /// The simplest way to execute the BFS algorithm is to use one of the
1427 1428
    /// member functions called \ref run(Node) "run()".\n
1428 1429
    /// If you need more control on the execution, first you have to call
1429 1430
    /// \ref init(), then you can add several source nodes with
1430 1431
    /// \ref addSource(). Finally the actual path computation can be
1431 1432
    /// performed with one of the \ref start() functions.
1432 1433

	
1433 1434
    /// @{
1434 1435

	
1435 1436
    /// \brief Initializes the internal data structures.
1436 1437
    ///
1437 1438
    /// Initializes the internal data structures.
1438 1439
    void init() {
1439 1440
      create_maps();
1440 1441
      _list.resize(countNodes(*_digraph));
1441 1442
      _list_front = _list_back = -1;
1442 1443
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1443 1444
        _reached->set(u, false);
1444 1445
      }
1445 1446
    }
1446 1447

	
1447 1448
    /// \brief Adds a new source node.
1448 1449
    ///
1449 1450
    /// Adds a new source node to the set of nodes to be processed.
1450 1451
    void addSource(Node s) {
1451 1452
      if(!(*_reached)[s]) {
1452 1453
          _reached->set(s,true);
1453 1454
          _visitor->start(s);
1454 1455
          _visitor->reach(s);
1455 1456
          _list[++_list_back] = s;
1456 1457
        }
1457 1458
    }
1458 1459

	
1459 1460
    /// \brief Processes the next node.
1460 1461
    ///
1461 1462
    /// Processes the next node.
1462 1463
    ///
1463 1464
    /// \return The processed node.
1464 1465
    ///
1465 1466
    /// \pre The queue must not be empty.
1466 1467
    Node processNextNode() {
1467 1468
      Node n = _list[++_list_front];
1468 1469
      _visitor->process(n);
1469 1470
      Arc e;
1470 1471
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1471 1472
        Node m = _digraph->target(e);
1472 1473
        if (!(*_reached)[m]) {
1473 1474
          _visitor->discover(e);
1474 1475
          _visitor->reach(m);
1475 1476
          _reached->set(m, true);
1476 1477
          _list[++_list_back] = m;
1477 1478
        } else {
1478 1479
          _visitor->examine(e);
1479 1480
        }
1480 1481
      }
1481 1482
      return n;
1482 1483
    }
1483 1484

	
1484 1485
    /// \brief Processes the next node.
1485 1486
    ///
1486 1487
    /// Processes the next node and checks if the given target node
1487 1488
    /// is reached. If the target node is reachable from the processed
1488 1489
    /// node, then the \c reach parameter will be set to \c true.
1489 1490
    ///
1490 1491
    /// \param target The target node.
1491 1492
    /// \retval reach Indicates if the target node is reached.
1492 1493
    /// It should be initially \c false.
1493 1494
    ///
1494 1495
    /// \return The processed node.
1495 1496
    ///
1496 1497
    /// \pre The queue must not be empty.
1497 1498
    Node processNextNode(Node target, bool& reach) {
1498 1499
      Node n = _list[++_list_front];
1499 1500
      _visitor->process(n);
1500 1501
      Arc e;
1501 1502
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1502 1503
        Node m = _digraph->target(e);
1503 1504
        if (!(*_reached)[m]) {
1504 1505
          _visitor->discover(e);
1505 1506
          _visitor->reach(m);
1506 1507
          _reached->set(m, true);
1507 1508
          _list[++_list_back] = m;
1508 1509
          reach = reach || (target == m);
1509 1510
        } else {
1510 1511
          _visitor->examine(e);
1511 1512
        }
1512 1513
      }
1513 1514
      return n;
1514 1515
    }
1515 1516

	
1516 1517
    /// \brief Processes the next node.
1517 1518
    ///
1518 1519
    /// Processes the next node and checks if at least one of reached
1519 1520
    /// nodes has \c true value in the \c nm node map. If one node
1520 1521
    /// with \c true value is reachable from the processed node, then the
1521 1522
    /// \c rnode parameter will be set to the first of such nodes.
1522 1523
    ///
1523 1524
    /// \param nm A \c bool (or convertible) node map that indicates the
1524 1525
    /// possible targets.
1525 1526
    /// \retval rnode The reached target node.
1526 1527
    /// It should be initially \c INVALID.
1527 1528
    ///
1528 1529
    /// \return The processed node.
1529 1530
    ///
1530 1531
    /// \pre The queue must not be empty.
1531 1532
    template <typename NM>
1532 1533
    Node processNextNode(const NM& nm, Node& rnode) {
1533 1534
      Node n = _list[++_list_front];
1534 1535
      _visitor->process(n);
1535 1536
      Arc e;
1536 1537
      for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
1537 1538
        Node m = _digraph->target(e);
1538 1539
        if (!(*_reached)[m]) {
1539 1540
          _visitor->discover(e);
1540 1541
          _visitor->reach(m);
1541 1542
          _reached->set(m, true);
1542 1543
          _list[++_list_back] = m;
1543 1544
          if (nm[m] && rnode == INVALID) rnode = m;
1544 1545
        } else {
1545 1546
          _visitor->examine(e);
1546 1547
        }
1547 1548
      }
1548 1549
      return n;
1549 1550
    }
1550 1551

	
1551 1552
    /// \brief The next node to be processed.
1552 1553
    ///
1553 1554
    /// Returns the next node to be processed or \c INVALID if the queue
1554 1555
    /// is empty.
1555 1556
    Node nextNode() const {
1556 1557
      return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
1557 1558
    }
1558 1559

	
1559 1560
    /// \brief Returns \c false if there are nodes
1560 1561
    /// to be processed.
1561 1562
    ///
1562 1563
    /// Returns \c false if there are nodes
1563 1564
    /// to be processed in the queue.
1564 1565
    bool emptyQueue() const { return _list_front == _list_back; }
1565 1566

	
1566 1567
    /// \brief Returns the number of the nodes to be processed.
1567 1568
    ///
1568 1569
    /// Returns the number of the nodes to be processed in the queue.
1569 1570
    int queueSize() const { return _list_back - _list_front; }
1570 1571

	
1571 1572
    /// \brief Executes the algorithm.
1572 1573
    ///
1573 1574
    /// Executes the algorithm.
1574 1575
    ///
1575 1576
    /// This method runs the %BFS algorithm from the root node(s)
1576 1577
    /// in order to compute the shortest path to each node.
1577 1578
    ///
1578 1579
    /// The algorithm computes
1579 1580
    /// - the shortest path tree (forest),
1580 1581
    /// - the distance of each node from the root(s).
1581 1582
    ///
1582 1583
    /// \pre init() must be called and at least one root node should be added
1583 1584
    /// with addSource() before using this function.
1584 1585
    ///
1585 1586
    /// \note <tt>b.start()</tt> is just a shortcut of the following code.
1586 1587
    /// \code
1587 1588
    ///   while ( !b.emptyQueue() ) {
1588 1589
    ///     b.processNextNode();
1589 1590
    ///   }
1590 1591
    /// \endcode
1591 1592
    void start() {
1592 1593
      while ( !emptyQueue() ) processNextNode();
1593 1594
    }
1594 1595

	
1595 1596
    /// \brief Executes the algorithm until the given target node is reached.
1596 1597
    ///
1597 1598
    /// Executes the algorithm until the given target node is reached.
1598 1599
    ///
1599 1600
    /// This method runs the %BFS algorithm from the root node(s)
1600 1601
    /// in order to compute the shortest path to \c t.
1601 1602
    ///
1602 1603
    /// The algorithm computes
1603 1604
    /// - the shortest path to \c t,
1604 1605
    /// - the distance of \c t from the root(s).
1605 1606
    ///
1606 1607
    /// \pre init() must be called and at least one root node should be
1607 1608
    /// added with addSource() before using this function.
1608 1609
    ///
1609 1610
    /// \note <tt>b.start(t)</tt> is just a shortcut of the following code.
1610 1611
    /// \code
1611 1612
    ///   bool reach = false;
1612 1613
    ///   while ( !b.emptyQueue() && !reach ) {
1613 1614
    ///     b.processNextNode(t, reach);
1614 1615
    ///   }
1615 1616
    /// \endcode
1616 1617
    void start(Node t) {
1617 1618
      bool reach = false;
1618 1619
      while ( !emptyQueue() && !reach ) processNextNode(t, reach);
1619 1620
    }
1620 1621

	
1621 1622
    /// \brief Executes the algorithm until a condition is met.
1622 1623
    ///
1623 1624
    /// Executes the algorithm until a condition is met.
1624 1625
    ///
1625 1626
    /// This method runs the %BFS algorithm from the root node(s) in
1626 1627
    /// order to compute the shortest path to a node \c v with
1627 1628
    /// <tt>nm[v]</tt> true, if such a node can be found.
1628 1629
    ///
1629 1630
    /// \param nm must be a bool (or convertible) node map. The
1630 1631
    /// algorithm will stop when it reaches a node \c v with
1631 1632
    /// <tt>nm[v]</tt> true.
1632 1633
    ///
1633 1634
    /// \return The reached node \c v with <tt>nm[v]</tt> true or
1634 1635
    /// \c INVALID if no such node was found.
1635 1636
    ///
1636 1637
    /// \pre init() must be called and at least one root node should be
1637 1638
    /// added with addSource() before using this function.
1638 1639
    ///
1639 1640
    /// \note <tt>b.start(nm)</tt> is just a shortcut of the following code.
1640 1641
    /// \code
1641 1642
    ///   Node rnode = INVALID;
1642 1643
    ///   while ( !b.emptyQueue() && rnode == INVALID ) {
1643 1644
    ///     b.processNextNode(nm, rnode);
1644 1645
    ///   }
1645 1646
    ///   return rnode;
1646 1647
    /// \endcode
1647 1648
    template <typename NM>
1648 1649
    Node start(const NM &nm) {
1649 1650
      Node rnode = INVALID;
1650 1651
      while ( !emptyQueue() && rnode == INVALID ) {
1651 1652
        processNextNode(nm, rnode);
1652 1653
      }
1653 1654
      return rnode;
1654 1655
    }
1655 1656

	
1656 1657
    /// \brief Runs the algorithm from the given source node.
1657 1658
    ///
1658 1659
    /// This method runs the %BFS algorithm from node \c s
1659 1660
    /// in order to compute the shortest path to each node.
1660 1661
    ///
1661 1662
    /// The algorithm computes
1662 1663
    /// - the shortest path tree,
1663 1664
    /// - the distance of each node from the root.
1664 1665
    ///
1665 1666
    /// \note <tt>b.run(s)</tt> is just a shortcut of the following code.
1666 1667
    ///\code
1667 1668
    ///   b.init();
1668 1669
    ///   b.addSource(s);
1669 1670
    ///   b.start();
1670 1671
    ///\endcode
1671 1672
    void run(Node s) {
1672 1673
      init();
1673 1674
      addSource(s);
1674 1675
      start();
1675 1676
    }
1676 1677

	
1677 1678
    /// \brief Finds the shortest path between \c s and \c t.
1678 1679
    ///
1679 1680
    /// This method runs the %BFS algorithm from node \c s
1680 1681
    /// in order to compute the shortest path to node \c t
1681 1682
    /// (it stops searching when \c t is processed).
1682 1683
    ///
1683 1684
    /// \return \c true if \c t is reachable form \c s.
1684 1685
    ///
1685 1686
    /// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a
1686 1687
    /// shortcut of the following code.
1687 1688
    ///\code
1688 1689
    ///   b.init();
1689 1690
    ///   b.addSource(s);
1690 1691
    ///   b.start(t);
1691 1692
    ///\endcode
1692 1693
    bool run(Node s,Node t) {
1693 1694
      init();
1694 1695
      addSource(s);
1695 1696
      start(t);
1696 1697
      return reached(t);
1697 1698
    }
1698 1699

	
1699 1700
    /// \brief Runs the algorithm to visit all nodes in the digraph.
1700 1701
    ///
1701 1702
    /// This method runs the %BFS algorithm in order to
1702 1703
    /// compute the shortest path to each node.
1703 1704
    ///
1704 1705
    /// The algorithm computes
1705 1706
    /// - the shortest path tree (forest),
1706 1707
    /// - the distance of each node from the root(s).
1707 1708
    ///
1708 1709
    /// \note <tt>b.run(s)</tt> is just a shortcut of the following code.
1709 1710
    ///\code
1710 1711
    ///  b.init();
1711 1712
    ///  for (NodeIt n(gr); n != INVALID; ++n) {
1712 1713
    ///    if (!b.reached(n)) {
1713 1714
    ///      b.addSource(n);
1714 1715
    ///      b.start();
1715 1716
    ///    }
1716 1717
    ///  }
1717 1718
    ///\endcode
1718 1719
    void run() {
1719 1720
      init();
1720 1721
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
1721 1722
        if (!reached(it)) {
1722 1723
          addSource(it);
1723 1724
          start();
1724 1725
        }
1725 1726
      }
1726 1727
    }
1727 1728

	
1728 1729
    ///@}
1729 1730

	
1730 1731
    /// \name Query Functions
1731 1732
    /// The results of the BFS algorithm can be obtained using these
1732 1733
    /// functions.\n
1733 1734
    /// Either \ref run(Node) "run()" or \ref start() should be called
1734 1735
    /// before using them.
1735 1736

	
1736 1737
    ///@{
1737 1738

	
1738 1739
    /// \brief Checks if a node is reached from the root(s).
1739 1740
    ///
1740 1741
    /// Returns \c true if \c v is reached from the root(s).
1741 1742
    ///
1742 1743
    /// \pre Either \ref run(Node) "run()" or \ref init()
1743 1744
    /// must be called before using this function.
1744 1745
    bool reached(Node v) const { return (*_reached)[v]; }
1745 1746

	
1746 1747
    ///@}
1747 1748

	
1748 1749
  };
1749 1750

	
1750 1751
} //END OF NAMESPACE LEMON
1751 1752

	
1752 1753
#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-2009
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 graph_concepts
20 20
///\file
21 21
///\brief The concept of graph components.
22 22

	
23 23
#ifndef LEMON_CONCEPTS_GRAPH_COMPONENTS_H
24 24
#define LEMON_CONCEPTS_GRAPH_COMPONENTS_H
25 25

	
26 26
#include <lemon/core.h>
27 27
#include <lemon/concepts/maps.h>
28 28

	
29 29
#include <lemon/bits/alteration_notifier.h>
30 30

	
31 31
namespace lemon {
32 32
  namespace concepts {
33 33

	
34 34
    /// \brief Concept class for \c Node, \c Arc and \c Edge types.
35 35
    ///
36 36
    /// This class describes the concept of \c Node, \c Arc and \c Edge
37 37
    /// subtypes of digraph and graph types.
38 38
    ///
39 39
    /// \note This class is a template class so that we can use it to
40 40
    /// create graph skeleton classes. The reason for this is that \c Node
41 41
    /// and \c Arc (or \c Edge) types should \e not derive from the same 
42 42
    /// base class. For \c Node you should instantiate it with character
43 43
    /// \c 'n', for \c Arc with \c 'a' and for \c Edge with \c 'e'.
44 44
#ifndef DOXYGEN
45 45
    template <char sel = '0'>
46 46
#endif
47 47
    class GraphItem {
48 48
    public:
49 49
      /// \brief Default constructor.
50 50
      ///
51 51
      /// Default constructor.
52 52
      /// \warning The default constructor is not required to set
53 53
      /// the item to some well-defined value. So you should consider it
54 54
      /// as uninitialized.
55 55
      GraphItem() {}
56 56

	
57 57
      /// \brief Copy constructor.
58 58
      ///
59 59
      /// Copy constructor.
60 60
      GraphItem(const GraphItem &) {}
61 61

	
62 62
      /// \brief Constructor for conversion from \c INVALID.
63 63
      ///
64 64
      /// Constructor for conversion from \c INVALID.
65 65
      /// It initializes the item to be invalid.
66 66
      /// \sa Invalid for more details.
67 67
      GraphItem(Invalid) {}
68 68

	
69 69
      /// \brief Assignment operator.
70 70
      ///
71 71
      /// Assignment operator for the item.
72 72
      GraphItem& operator=(const GraphItem&) { return *this; }
73 73

	
74 74
      /// \brief Assignment operator for INVALID.
75 75
      ///
76 76
      /// This operator makes the item invalid.
77 77
      GraphItem& operator=(Invalid) { return *this; }
78 78

	
79 79
      /// \brief Equality operator.
80 80
      ///
81 81
      /// Equality operator.
82 82
      bool operator==(const GraphItem&) const { return false; }
83 83

	
84 84
      /// \brief Inequality operator.
85 85
      ///
86 86
      /// Inequality operator.
87 87
      bool operator!=(const GraphItem&) const { return false; }
88 88

	
89 89
      /// \brief Ordering operator.
90 90
      ///
91 91
      /// This operator defines an ordering of the items.
92 92
      /// It makes possible to use graph item types as key types in 
93 93
      /// associative containers (e.g. \c std::map).
94 94
      ///
95 95
      /// \note This operator only have to define some strict ordering of
96 96
      /// the items; this order has nothing to do with the iteration
97 97
      /// ordering of the items.
98 98
      bool operator<(const GraphItem&) const { return false; }
99 99

	
100 100
      template<typename _GraphItem>
101 101
      struct Constraints {
102 102
        void constraints() {
103 103
          _GraphItem i1;
104 104
          i1=INVALID;
105 105
          _GraphItem i2 = i1;
106 106
          _GraphItem i3 = INVALID;
107 107

	
108 108
          i1 = i2 = i3;
109 109

	
110 110
          bool b;
111 111
          b = (ia == ib) && (ia != ib);
112 112
          b = (ia == INVALID) && (ib != INVALID);
113 113
          b = (ia < ib);
114 114
        }
115 115

	
116 116
        const _GraphItem &ia;
117 117
        const _GraphItem &ib;
118
        Constraints() {}
118 119
      };
119 120
    };
120 121

	
121 122
    /// \brief Base skeleton class for directed graphs.
122 123
    ///
123 124
    /// This class describes the base interface of directed graph types.
124 125
    /// All digraph %concepts have to conform to this class.
125 126
    /// It just provides types for nodes and arcs and functions 
126 127
    /// to get the source and the target nodes of arcs.
127 128
    class BaseDigraphComponent {
128 129
    public:
129 130

	
130 131
      typedef BaseDigraphComponent Digraph;
131 132

	
132 133
      /// \brief Node class of the digraph.
133 134
      ///
134 135
      /// This class represents the nodes of the digraph.
135 136
      typedef GraphItem<'n'> Node;
136 137

	
137 138
      /// \brief Arc class of the digraph.
138 139
      ///
139 140
      /// This class represents the arcs of the digraph.
140 141
      typedef GraphItem<'a'> Arc;
141 142

	
142 143
      /// \brief Return the source node of an arc.
143 144
      ///
144 145
      /// This function returns the source node of an arc.
145 146
      Node source(const Arc&) const { return INVALID; }
146 147

	
147 148
      /// \brief Return the target node of an arc.
148 149
      ///
149 150
      /// This function returns the target node of an arc.
150 151
      Node target(const Arc&) const { return INVALID; }
151 152

	
152 153
      /// \brief Return the opposite node on the given arc.
153 154
      ///
154 155
      /// This function returns the opposite node on the given arc.
155 156
      Node oppositeNode(const Node&, const Arc&) const {
156 157
        return INVALID;
157 158
      }
158 159

	
159 160
      template <typename _Digraph>
160 161
      struct Constraints {
161 162
        typedef typename _Digraph::Node Node;
162 163
        typedef typename _Digraph::Arc Arc;
163 164

	
164 165
        void constraints() {
165 166
          checkConcept<GraphItem<'n'>, Node>();
166 167
          checkConcept<GraphItem<'a'>, Arc>();
167 168
          {
168 169
            Node n;
169 170
            Arc e(INVALID);
170 171
            n = digraph.source(e);
171 172
            n = digraph.target(e);
172 173
            n = digraph.oppositeNode(n, e);
173 174
          }
174 175
        }
175 176

	
176 177
        const _Digraph& digraph;
178
        Constraints() {}
177 179
      };
178 180
    };
179 181

	
180 182
    /// \brief Base skeleton class for undirected graphs.
181 183
    ///
182 184
    /// This class describes the base interface of undirected graph types.
183 185
    /// All graph %concepts have to conform to this class.
184 186
    /// It extends the interface of \ref BaseDigraphComponent with an
185 187
    /// \c Edge type and functions to get the end nodes of edges,
186 188
    /// to convert from arcs to edges and to get both direction of edges.
187 189
    class BaseGraphComponent : public BaseDigraphComponent {
188 190
    public:
189 191

	
190 192
      typedef BaseGraphComponent Graph;
191 193

	
192 194
      typedef BaseDigraphComponent::Node Node;
193 195
      typedef BaseDigraphComponent::Arc Arc;
194 196

	
195 197
      /// \brief Undirected edge class of the graph.
196 198
      ///
197 199
      /// This class represents the undirected edges of the graph.
198 200
      /// Undirected graphs can be used as directed graphs, each edge is
199 201
      /// represented by two opposite directed arcs.
200 202
      class Edge : public GraphItem<'e'> {
201 203
        typedef GraphItem<'e'> Parent;
202 204

	
203 205
      public:
204 206
        /// \brief Default constructor.
205 207
        ///
206 208
        /// Default constructor.
207 209
        /// \warning The default constructor is not required to set
208 210
        /// the item to some well-defined value. So you should consider it
209 211
        /// as uninitialized.
210 212
        Edge() {}
211 213

	
212 214
        /// \brief Copy constructor.
213 215
        ///
214 216
        /// Copy constructor.
215 217
        Edge(const Edge &) : Parent() {}
216 218

	
217 219
        /// \brief Constructor for conversion from \c INVALID.
218 220
        ///
219 221
        /// Constructor for conversion from \c INVALID.
220 222
        /// It initializes the item to be invalid.
221 223
        /// \sa Invalid for more details.
222 224
        Edge(Invalid) {}
223 225

	
224 226
        /// \brief Constructor for conversion from an arc.
225 227
        ///
226 228
        /// Constructor for conversion from an arc.
227 229
        /// Besides the core graph item functionality each arc should
228 230
        /// be convertible to the represented edge.
229 231
        Edge(const Arc&) {}
230 232
     };
231 233

	
232 234
      /// \brief Return one end node of an edge.
233 235
      ///
234 236
      /// This function returns one end node of an edge.
235 237
      Node u(const Edge&) const { return INVALID; }
236 238

	
237 239
      /// \brief Return the other end node of an edge.
238 240
      ///
239 241
      /// This function returns the other end node of an edge.
240 242
      Node v(const Edge&) const { return INVALID; }
241 243

	
242 244
      /// \brief Return a directed arc related to an edge.
243 245
      ///
244 246
      /// This function returns a directed arc from its direction and the
245 247
      /// represented edge.
246 248
      Arc direct(const Edge&, bool) const { return INVALID; }
247 249

	
248 250
      /// \brief Return a directed arc related to an edge.
249 251
      ///
250 252
      /// This function returns a directed arc from its source node and the
251 253
      /// represented edge.
252 254
      Arc direct(const Edge&, const Node&) const { return INVALID; }
253 255

	
254 256
      /// \brief Return the direction of the arc.
255 257
      ///
256 258
      /// Returns the direction of the arc. Each arc represents an
257 259
      /// edge with a direction. It gives back the
258 260
      /// direction.
259 261
      bool direction(const Arc&) const { return true; }
260 262

	
261 263
      /// \brief Return the opposite arc.
262 264
      ///
263 265
      /// This function returns the opposite arc, i.e. the arc representing
264 266
      /// the same edge and has opposite direction.
265 267
      Arc oppositeArc(const Arc&) const { return INVALID; }
266 268

	
267 269
      template <typename _Graph>
268 270
      struct Constraints {
269 271
        typedef typename _Graph::Node Node;
270 272
        typedef typename _Graph::Arc Arc;
271 273
        typedef typename _Graph::Edge Edge;
272 274

	
273 275
        void constraints() {
274 276
          checkConcept<BaseDigraphComponent, _Graph>();
275 277
          checkConcept<GraphItem<'e'>, Edge>();
276 278
          {
277 279
            Node n;
278 280
            Edge ue(INVALID);
279 281
            Arc e;
280 282
            n = graph.u(ue);
281 283
            n = graph.v(ue);
282 284
            e = graph.direct(ue, true);
283 285
            e = graph.direct(ue, false);
284 286
            e = graph.direct(ue, n);
285 287
            e = graph.oppositeArc(e);
286 288
            ue = e;
287 289
            bool d = graph.direction(e);
288 290
            ignore_unused_variable_warning(d);
289 291
          }
290 292
        }
291 293

	
292 294
        const _Graph& graph;
295
      Constraints() {}
293 296
      };
294 297

	
295 298
    };
296 299

	
297 300
    /// \brief Skeleton class for \e idable directed graphs.
298 301
    ///
299 302
    /// This class describes the interface of \e idable directed graphs.
300 303
    /// It extends \ref BaseDigraphComponent with the core ID functions.
301 304
    /// The ids of the items must be unique and immutable.
302 305
    /// This concept is part of the Digraph concept.
303 306
    template <typename BAS = BaseDigraphComponent>
304 307
    class IDableDigraphComponent : public BAS {
305 308
    public:
306 309

	
307 310
      typedef BAS Base;
308 311
      typedef typename Base::Node Node;
309 312
      typedef typename Base::Arc Arc;
310 313

	
311 314
      /// \brief Return a unique integer id for the given node.
312 315
      ///
313 316
      /// This function returns a unique integer id for the given node.
314 317
      int id(const Node&) const { return -1; }
315 318

	
316 319
      /// \brief Return the node by its unique id.
317 320
      ///
318 321
      /// This function returns the node by its unique id.
319 322
      /// If the digraph does not contain a node with the given id,
320 323
      /// then the result of the function is undefined.
321 324
      Node nodeFromId(int) const { return INVALID; }
322 325

	
323 326
      /// \brief Return a unique integer id for the given arc.
324 327
      ///
325 328
      /// This function returns a unique integer id for the given arc.
326 329
      int id(const Arc&) const { return -1; }
327 330

	
328 331
      /// \brief Return the arc by its unique id.
329 332
      ///
330 333
      /// This function returns the arc by its unique id.
331 334
      /// If the digraph does not contain an arc with the given id,
332 335
      /// then the result of the function is undefined.
333 336
      Arc arcFromId(int) const { return INVALID; }
334 337

	
335 338
      /// \brief Return an integer greater or equal to the maximum
336 339
      /// node id.
337 340
      ///
338 341
      /// This function returns an integer greater or equal to the
339 342
      /// maximum node id.
340 343
      int maxNodeId() const { return -1; }
341 344

	
342 345
      /// \brief Return an integer greater or equal to the maximum
343 346
      /// arc id.
344 347
      ///
345 348
      /// This function returns an integer greater or equal to the
346 349
      /// maximum arc id.
347 350
      int maxArcId() const { return -1; }
348 351

	
349 352
      template <typename _Digraph>
350 353
      struct Constraints {
351 354

	
352 355
        void constraints() {
353 356
          checkConcept<Base, _Digraph >();
354 357
          typename _Digraph::Node node;
355 358
          node=INVALID;
356 359
          int nid = digraph.id(node);
357 360
          nid = digraph.id(node);
358 361
          node = digraph.nodeFromId(nid);
359 362
          typename _Digraph::Arc arc;
360 363
          arc=INVALID;
361 364
          int eid = digraph.id(arc);
362 365
          eid = digraph.id(arc);
363 366
          arc = digraph.arcFromId(eid);
364 367

	
365 368
          nid = digraph.maxNodeId();
366 369
          ignore_unused_variable_warning(nid);
367 370
          eid = digraph.maxArcId();
368 371
          ignore_unused_variable_warning(eid);
369 372
        }
370 373

	
371 374
        const _Digraph& digraph;
375
        Constraints() {}
372 376
      };
373 377
    };
374 378

	
375 379
    /// \brief Skeleton class for \e idable undirected graphs.
376 380
    ///
377 381
    /// This class describes the interface of \e idable undirected
378 382
    /// graphs. It extends \ref IDableDigraphComponent with the core ID
379 383
    /// functions of undirected graphs.
380 384
    /// The ids of the items must be unique and immutable.
381 385
    /// This concept is part of the Graph concept.
382 386
    template <typename BAS = BaseGraphComponent>
383 387
    class IDableGraphComponent : public IDableDigraphComponent<BAS> {
384 388
    public:
385 389

	
386 390
      typedef BAS Base;
387 391
      typedef typename Base::Edge Edge;
388 392

	
389 393
      using IDableDigraphComponent<Base>::id;
390 394

	
391 395
      /// \brief Return a unique integer id for the given edge.
392 396
      ///
393 397
      /// This function returns a unique integer id for the given edge.
394 398
      int id(const Edge&) const { return -1; }
395 399

	
396 400
      /// \brief Return the edge by its unique id.
397 401
      ///
398 402
      /// This function returns the edge by its unique id.
399 403
      /// If the graph does not contain an edge with the given id,
400 404
      /// then the result of the function is undefined.
401 405
      Edge edgeFromId(int) const { return INVALID; }
402 406

	
403 407
      /// \brief Return an integer greater or equal to the maximum
404 408
      /// edge id.
405 409
      ///
406 410
      /// This function returns an integer greater or equal to the
407 411
      /// maximum edge id.
408 412
      int maxEdgeId() const { return -1; }
409 413

	
410 414
      template <typename _Graph>
411 415
      struct Constraints {
412 416

	
413 417
        void constraints() {
414 418
          checkConcept<IDableDigraphComponent<Base>, _Graph >();
415 419
          typename _Graph::Edge edge;
416 420
          int ueid = graph.id(edge);
417 421
          ueid = graph.id(edge);
418 422
          edge = graph.edgeFromId(ueid);
419 423
          ueid = graph.maxEdgeId();
420 424
          ignore_unused_variable_warning(ueid);
421 425
        }
422 426

	
423 427
        const _Graph& graph;
428
        Constraints() {}
424 429
      };
425 430
    };
426 431

	
427 432
    /// \brief Concept class for \c NodeIt, \c ArcIt and \c EdgeIt types.
428 433
    ///
429 434
    /// This class describes the concept of \c NodeIt, \c ArcIt and 
430 435
    /// \c EdgeIt subtypes of digraph and graph types.
431 436
    template <typename GR, typename Item>
432 437
    class GraphItemIt : public Item {
433 438
    public:
434 439
      /// \brief Default constructor.
435 440
      ///
436 441
      /// Default constructor.
437 442
      /// \warning The default constructor is not required to set
438 443
      /// the iterator to some well-defined value. So you should consider it
439 444
      /// as uninitialized.
440 445
      GraphItemIt() {}
441 446

	
442 447
      /// \brief Copy constructor.
443 448
      ///
444 449
      /// Copy constructor.
445 450
      GraphItemIt(const GraphItemIt& it) : Item(it) {}
446 451

	
447 452
      /// \brief Constructor that sets the iterator to the first item.
448 453
      ///
449 454
      /// Constructor that sets the iterator to the first item.
450 455
      explicit GraphItemIt(const GR&) {}
451 456

	
452 457
      /// \brief Constructor for conversion from \c INVALID.
453 458
      ///
454 459
      /// Constructor for conversion from \c INVALID.
455 460
      /// It initializes the iterator to be invalid.
456 461
      /// \sa Invalid for more details.
457 462
      GraphItemIt(Invalid) {}
458 463

	
459 464
      /// \brief Assignment operator.
460 465
      ///
461 466
      /// Assignment operator for the iterator.
462 467
      GraphItemIt& operator=(const GraphItemIt&) { return *this; }
463 468

	
464 469
      /// \brief Increment the iterator.
465 470
      ///
466 471
      /// This operator increments the iterator, i.e. assigns it to the
467 472
      /// next item.
468 473
      GraphItemIt& operator++() { return *this; }
469 474
 
470 475
      /// \brief Equality operator
471 476
      ///
472 477
      /// Equality operator.
473 478
      /// Two iterators are equal if and only if they point to the
474 479
      /// same object or both are invalid.
475 480
      bool operator==(const GraphItemIt&) const { return true;}
476 481

	
477 482
      /// \brief Inequality operator
478 483
      ///
479 484
      /// Inequality operator.
480 485
      /// Two iterators are equal if and only if they point to the
481 486
      /// same object or both are invalid.
482 487
      bool operator!=(const GraphItemIt&) const { return true;}
483 488

	
484 489
      template<typename _GraphItemIt>
485 490
      struct Constraints {
486 491
        void constraints() {
487 492
          checkConcept<GraphItem<>, _GraphItemIt>();
488 493
          _GraphItemIt it1(g);
489 494
          _GraphItemIt it2;
490 495
          _GraphItemIt it3 = it1;
491 496
          _GraphItemIt it4 = INVALID;
492 497

	
493 498
          it2 = ++it1;
494 499
          ++it2 = it1;
495 500
          ++(++it1);
496 501

	
497 502
          Item bi = it1;
498 503
          bi = it2;
499 504
        }
500 505
        const GR& g;
506
        Constraints() {}
501 507
      };
502 508
    };
503 509

	
504 510
    /// \brief Concept class for \c InArcIt, \c OutArcIt and 
505 511
    /// \c IncEdgeIt types.
506 512
    ///
507 513
    /// This class describes the concept of \c InArcIt, \c OutArcIt 
508 514
    /// and \c IncEdgeIt subtypes of digraph and graph types.
509 515
    ///
510 516
    /// \note Since these iterator classes do not inherit from the same
511 517
    /// base class, there is an additional template parameter (selector)
512 518
    /// \c sel. For \c InArcIt you should instantiate it with character 
513 519
    /// \c 'i', for \c OutArcIt with \c 'o' and for \c IncEdgeIt with \c 'e'.
514 520
    template <typename GR,
515 521
              typename Item = typename GR::Arc,
516 522
              typename Base = typename GR::Node,
517 523
              char sel = '0'>
518 524
    class GraphIncIt : public Item {
519 525
    public:
520 526
      /// \brief Default constructor.
521 527
      ///
522 528
      /// Default constructor.
523 529
      /// \warning The default constructor is not required to set
524 530
      /// the iterator to some well-defined value. So you should consider it
525 531
      /// as uninitialized.
526 532
      GraphIncIt() {}
527 533

	
528 534
      /// \brief Copy constructor.
529 535
      ///
530 536
      /// Copy constructor.
531 537
      GraphIncIt(const GraphIncIt& it) : Item(it) {}
532 538

	
533 539
      /// \brief Constructor that sets the iterator to the first 
534 540
      /// incoming or outgoing arc.
535 541
      ///
536 542
      /// Constructor that sets the iterator to the first arc 
537 543
      /// incoming to or outgoing from the given node.
538 544
      explicit GraphIncIt(const GR&, const Base&) {}
539 545

	
540 546
      /// \brief Constructor for conversion from \c INVALID.
541 547
      ///
542 548
      /// Constructor for conversion from \c INVALID.
543 549
      /// It initializes the iterator to be invalid.
544 550
      /// \sa Invalid for more details.
545 551
      GraphIncIt(Invalid) {}
546 552

	
547 553
      /// \brief Assignment operator.
548 554
      ///
549 555
      /// Assignment operator for the iterator.
550 556
      GraphIncIt& operator=(const GraphIncIt&) { return *this; }
551 557

	
552 558
      /// \brief Increment the iterator.
553 559
      ///
554 560
      /// This operator increments the iterator, i.e. assigns it to the
555 561
      /// next arc incoming to or outgoing from the given node.
556 562
      GraphIncIt& operator++() { return *this; }
557 563

	
558 564
      /// \brief Equality operator
559 565
      ///
560 566
      /// Equality operator.
561 567
      /// Two iterators are equal if and only if they point to the
562 568
      /// same object or both are invalid.
563 569
      bool operator==(const GraphIncIt&) const { return true;}
564 570

	
565 571
      /// \brief Inequality operator
566 572
      ///
567 573
      /// Inequality operator.
568 574
      /// Two iterators are equal if and only if they point to the
569 575
      /// same object or both are invalid.
570 576
      bool operator!=(const GraphIncIt&) const { return true;}
571 577

	
572 578
      template <typename _GraphIncIt>
573 579
      struct Constraints {
574 580
        void constraints() {
575 581
          checkConcept<GraphItem<sel>, _GraphIncIt>();
576 582
          _GraphIncIt it1(graph, node);
577 583
          _GraphIncIt it2;
578 584
          _GraphIncIt it3 = it1;
579 585
          _GraphIncIt it4 = INVALID;
580 586

	
581 587
          it2 = ++it1;
582 588
          ++it2 = it1;
583 589
          ++(++it1);
584 590
          Item e = it1;
585 591
          e = it2;
586 592
        }
587 593
        const Base& node;
588 594
        const GR& graph;
595
        Constraints() {}
589 596
      };
590 597
    };
591 598

	
592 599
    /// \brief Skeleton class for iterable directed graphs.
593 600
    ///
594 601
    /// This class describes the interface of iterable directed
595 602
    /// graphs. It extends \ref BaseDigraphComponent with the core
596 603
    /// iterable interface.
597 604
    /// This concept is part of the Digraph concept.
598 605
    template <typename BAS = BaseDigraphComponent>
599 606
    class IterableDigraphComponent : public BAS {
600 607

	
601 608
    public:
602 609

	
603 610
      typedef BAS Base;
604 611
      typedef typename Base::Node Node;
605 612
      typedef typename Base::Arc Arc;
606 613

	
607 614
      typedef IterableDigraphComponent Digraph;
608 615

	
609 616
      /// \name Base Iteration
610 617
      ///
611 618
      /// This interface provides functions for iteration on digraph items.
612 619
      ///
613 620
      /// @{
614 621

	
615 622
      /// \brief Return the first node.
616 623
      ///
617 624
      /// This function gives back the first node in the iteration order.
618 625
      void first(Node&) const {}
619 626

	
620 627
      /// \brief Return the next node.
621 628
      ///
622 629
      /// This function gives back the next node in the iteration order.
623 630
      void next(Node&) const {}
624 631

	
625 632
      /// \brief Return the first arc.
626 633
      ///
627 634
      /// This function gives back the first arc in the iteration order.
628 635
      void first(Arc&) const {}
629 636

	
630 637
      /// \brief Return the next arc.
631 638
      ///
632 639
      /// This function gives back the next arc in the iteration order.
633 640
      void next(Arc&) const {}
634 641

	
635 642
      /// \brief Return the first arc incomming to the given node.
636 643
      ///
637 644
      /// This function gives back the first arc incomming to the
638 645
      /// given node.
639 646
      void firstIn(Arc&, const Node&) const {}
640 647

	
641 648
      /// \brief Return the next arc incomming to the given node.
642 649
      ///
643 650
      /// This function gives back the next arc incomming to the
644 651
      /// given node.
645 652
      void nextIn(Arc&) const {}
646 653

	
647 654
      /// \brief Return the first arc outgoing form the given node.
648 655
      ///
649 656
      /// This function gives back the first arc outgoing form the
650 657
      /// given node.
651 658
      void firstOut(Arc&, const Node&) const {}
652 659

	
653 660
      /// \brief Return the next arc outgoing form the given node.
654 661
      ///
655 662
      /// This function gives back the next arc outgoing form the
656 663
      /// given node.
657 664
      void nextOut(Arc&) const {}
658 665

	
659 666
      /// @}
660 667

	
661 668
      /// \name Class Based Iteration
662 669
      ///
663 670
      /// This interface provides iterator classes for digraph items.
664 671
      ///
665 672
      /// @{
666 673

	
667 674
      /// \brief This iterator goes through each node.
668 675
      ///
669 676
      /// This iterator goes through each node.
670 677
      ///
671 678
      typedef GraphItemIt<Digraph, Node> NodeIt;
672 679

	
673 680
      /// \brief This iterator goes through each arc.
674 681
      ///
675 682
      /// This iterator goes through each arc.
676 683
      ///
677 684
      typedef GraphItemIt<Digraph, Arc> ArcIt;
678 685

	
679 686
      /// \brief This iterator goes trough the incoming arcs of a node.
680 687
      ///
681 688
      /// This iterator goes trough the \e incoming arcs of a certain node
682 689
      /// of a digraph.
683 690
      typedef GraphIncIt<Digraph, Arc, Node, 'i'> InArcIt;
684 691

	
685 692
      /// \brief This iterator goes trough the outgoing arcs of a node.
686 693
      ///
687 694
      /// This iterator goes trough the \e outgoing arcs of a certain node
688 695
      /// of a digraph.
689 696
      typedef GraphIncIt<Digraph, Arc, Node, 'o'> OutArcIt;
690 697

	
691 698
      /// \brief The base node of the iterator.
692 699
      ///
693 700
      /// This function gives back the base node of the iterator.
694 701
      /// It is always the target node of the pointed arc.
695 702
      Node baseNode(const InArcIt&) const { return INVALID; }
696 703

	
697 704
      /// \brief The running node of the iterator.
698 705
      ///
699 706
      /// This function gives back the running node of the iterator.
700 707
      /// It is always the source node of the pointed arc.
701 708
      Node runningNode(const InArcIt&) const { return INVALID; }
702 709

	
703 710
      /// \brief The base node of the iterator.
704 711
      ///
705 712
      /// This function gives back the base node of the iterator.
706 713
      /// It is always the source node of the pointed arc.
707 714
      Node baseNode(const OutArcIt&) const { return INVALID; }
708 715

	
709 716
      /// \brief The running node of the iterator.
710 717
      ///
711 718
      /// This function gives back the running node of the iterator.
712 719
      /// It is always the target node of the pointed arc.
713 720
      Node runningNode(const OutArcIt&) const { return INVALID; }
714 721

	
715 722
      /// @}
716 723

	
717 724
      template <typename _Digraph>
718 725
      struct Constraints {
719 726
        void constraints() {
720 727
          checkConcept<Base, _Digraph>();
721 728

	
722 729
          {
723 730
            typename _Digraph::Node node(INVALID);
724 731
            typename _Digraph::Arc arc(INVALID);
725 732
            {
726 733
              digraph.first(node);
727 734
              digraph.next(node);
728 735
            }
729 736
            {
730 737
              digraph.first(arc);
731 738
              digraph.next(arc);
732 739
            }
733 740
            {
734 741
              digraph.firstIn(arc, node);
735 742
              digraph.nextIn(arc);
736 743
            }
737 744
            {
738 745
              digraph.firstOut(arc, node);
739 746
              digraph.nextOut(arc);
740 747
            }
741 748
          }
742 749

	
743 750
          {
744 751
            checkConcept<GraphItemIt<_Digraph, typename _Digraph::Arc>,
745 752
              typename _Digraph::ArcIt >();
746 753
            checkConcept<GraphItemIt<_Digraph, typename _Digraph::Node>,
747 754
              typename _Digraph::NodeIt >();
748 755
            checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc,
749 756
              typename _Digraph::Node, 'i'>, typename _Digraph::InArcIt>();
750 757
            checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc,
751 758
              typename _Digraph::Node, 'o'>, typename _Digraph::OutArcIt>();
752 759

	
753 760
            typename _Digraph::Node n;
754 761
            const typename _Digraph::InArcIt iait(INVALID);
755 762
            const typename _Digraph::OutArcIt oait(INVALID);
756 763
            n = digraph.baseNode(iait);
757 764
            n = digraph.runningNode(iait);
758 765
            n = digraph.baseNode(oait);
759 766
            n = digraph.runningNode(oait);
760 767
            ignore_unused_variable_warning(n);
761 768
          }
762 769
        }
763 770

	
764 771
        const _Digraph& digraph;
772
        Constraints() {}
765 773
      };
766 774
    };
767 775

	
768 776
    /// \brief Skeleton class for iterable undirected graphs.
769 777
    ///
770 778
    /// This class describes the interface of iterable undirected
771 779
    /// graphs. It extends \ref IterableDigraphComponent with the core
772 780
    /// iterable interface of undirected graphs.
773 781
    /// This concept is part of the Graph concept.
774 782
    template <typename BAS = BaseGraphComponent>
775 783
    class IterableGraphComponent : public IterableDigraphComponent<BAS> {
776 784
    public:
777 785

	
778 786
      typedef BAS Base;
779 787
      typedef typename Base::Node Node;
780 788
      typedef typename Base::Arc Arc;
781 789
      typedef typename Base::Edge Edge;
782 790

	
783 791

	
784 792
      typedef IterableGraphComponent Graph;
785 793

	
786 794
      /// \name Base Iteration
787 795
      ///
788 796
      /// This interface provides functions for iteration on edges.
789 797
      ///
790 798
      /// @{
791 799

	
792 800
      using IterableDigraphComponent<Base>::first;
793 801
      using IterableDigraphComponent<Base>::next;
794 802

	
795 803
      /// \brief Return the first edge.
796 804
      ///
797 805
      /// This function gives back the first edge in the iteration order.
798 806
      void first(Edge&) const {}
799 807

	
800 808
      /// \brief Return the next edge.
801 809
      ///
802 810
      /// This function gives back the next edge in the iteration order.
803 811
      void next(Edge&) const {}
804 812

	
805 813
      /// \brief Return the first edge incident to the given node.
806 814
      ///
807 815
      /// This function gives back the first edge incident to the given 
808 816
      /// node. The bool parameter gives back the direction for which the
809 817
      /// source node of the directed arc representing the edge is the 
810 818
      /// given node.
811 819
      void firstInc(Edge&, bool&, const Node&) const {}
812 820

	
813 821
      /// \brief Gives back the next of the edges from the
814 822
      /// given node.
815 823
      ///
816 824
      /// This function gives back the next edge incident to the given 
817 825
      /// node. The bool parameter should be used as \c firstInc() use it.
818 826
      void nextInc(Edge&, bool&) const {}
819 827

	
820 828
      using IterableDigraphComponent<Base>::baseNode;
821 829
      using IterableDigraphComponent<Base>::runningNode;
822 830

	
823 831
      /// @}
824 832

	
825 833
      /// \name Class Based Iteration
826 834
      ///
827 835
      /// This interface provides iterator classes for edges.
828 836
      ///
829 837
      /// @{
830 838

	
831 839
      /// \brief This iterator goes through each edge.
832 840
      ///
833 841
      /// This iterator goes through each edge.
834 842
      typedef GraphItemIt<Graph, Edge> EdgeIt;
835 843

	
836 844
      /// \brief This iterator goes trough the incident edges of a
837 845
      /// node.
838 846
      ///
839 847
      /// This iterator goes trough the incident edges of a certain
840 848
      /// node of a graph.
841 849
      typedef GraphIncIt<Graph, Edge, Node, 'e'> IncEdgeIt;
842 850

	
843 851
      /// \brief The base node of the iterator.
844 852
      ///
845 853
      /// This function gives back the base node of the iterator.
846 854
      Node baseNode(const IncEdgeIt&) const { return INVALID; }
847 855

	
848 856
      /// \brief The running node of the iterator.
849 857
      ///
850 858
      /// This function gives back the running node of the iterator.
851 859
      Node runningNode(const IncEdgeIt&) const { return INVALID; }
852 860

	
853 861
      /// @}
854 862

	
855 863
      template <typename _Graph>
856 864
      struct Constraints {
857 865
        void constraints() {
858 866
          checkConcept<IterableDigraphComponent<Base>, _Graph>();
859 867

	
860 868
          {
861 869
            typename _Graph::Node node(INVALID);
862 870
            typename _Graph::Edge edge(INVALID);
863 871
            bool dir;
864 872
            {
865 873
              graph.first(edge);
866 874
              graph.next(edge);
867 875
            }
868 876
            {
869 877
              graph.firstInc(edge, dir, node);
870 878
              graph.nextInc(edge, dir);
871 879
            }
872 880

	
873 881
          }
874 882

	
875 883
          {
876 884
            checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>,
877 885
              typename _Graph::EdgeIt >();
878 886
            checkConcept<GraphIncIt<_Graph, typename _Graph::Edge,
879 887
              typename _Graph::Node, 'e'>, typename _Graph::IncEdgeIt>();
880 888

	
881 889
            typename _Graph::Node n;
882 890
            const typename _Graph::IncEdgeIt ieit(INVALID);
883 891
            n = graph.baseNode(ieit);
884 892
            n = graph.runningNode(ieit);
885 893
          }
886 894
        }
887 895

	
888 896
        const _Graph& graph;
897
        Constraints() {}
889 898
      };
890 899
    };
891 900

	
892 901
    /// \brief Skeleton class for alterable directed graphs.
893 902
    ///
894 903
    /// This class describes the interface of alterable directed
895 904
    /// graphs. It extends \ref BaseDigraphComponent with the alteration
896 905
    /// notifier interface. It implements
897 906
    /// an observer-notifier pattern for each digraph item. More
898 907
    /// obsevers can be registered into the notifier and whenever an
899 908
    /// alteration occured in the digraph all the observers will be
900 909
    /// notified about it.
901 910
    template <typename BAS = BaseDigraphComponent>
902 911
    class AlterableDigraphComponent : public BAS {
903 912
    public:
904 913

	
905 914
      typedef BAS Base;
906 915
      typedef typename Base::Node Node;
907 916
      typedef typename Base::Arc Arc;
908 917

	
909 918

	
910 919
      /// Node alteration notifier class.
911 920
      typedef AlterationNotifier<AlterableDigraphComponent, Node>
912 921
      NodeNotifier;
913 922
      /// Arc alteration notifier class.
914 923
      typedef AlterationNotifier<AlterableDigraphComponent, Arc>
915 924
      ArcNotifier;
916 925

	
917 926
      /// \brief Return the node alteration notifier.
918 927
      ///
919 928
      /// This function gives back the node alteration notifier.
920 929
      NodeNotifier& notifier(Node) const {
921 930
         return NodeNotifier();
922 931
      }
923 932

	
924 933
      /// \brief Return the arc alteration notifier.
925 934
      ///
926 935
      /// This function gives back the arc alteration notifier.
927 936
      ArcNotifier& notifier(Arc) const {
928 937
        return ArcNotifier();
929 938
      }
930 939

	
931 940
      template <typename _Digraph>
932 941
      struct Constraints {
933 942
        void constraints() {
934 943
          checkConcept<Base, _Digraph>();
935 944
          typename _Digraph::NodeNotifier& nn
936 945
            = digraph.notifier(typename _Digraph::Node());
937 946

	
938 947
          typename _Digraph::ArcNotifier& en
939 948
            = digraph.notifier(typename _Digraph::Arc());
940 949

	
941 950
          ignore_unused_variable_warning(nn);
942 951
          ignore_unused_variable_warning(en);
943 952
        }
944 953

	
945 954
        const _Digraph& digraph;
955
        Constraints() {}
946 956
      };
947 957
    };
948 958

	
949 959
    /// \brief Skeleton class for alterable undirected graphs.
950 960
    ///
951 961
    /// This class describes the interface of alterable undirected
952 962
    /// graphs. It extends \ref AlterableDigraphComponent with the alteration
953 963
    /// notifier interface of undirected graphs. It implements
954 964
    /// an observer-notifier pattern for the edges. More
955 965
    /// obsevers can be registered into the notifier and whenever an
956 966
    /// alteration occured in the graph all the observers will be
957 967
    /// notified about it.
958 968
    template <typename BAS = BaseGraphComponent>
959 969
    class AlterableGraphComponent : public AlterableDigraphComponent<BAS> {
960 970
    public:
961 971

	
962 972
      typedef BAS Base;
963 973
      typedef typename Base::Edge Edge;
964 974

	
965 975

	
966 976
      /// Edge alteration notifier class.
967 977
      typedef AlterationNotifier<AlterableGraphComponent, Edge>
968 978
      EdgeNotifier;
969 979

	
970 980
      /// \brief Return the edge alteration notifier.
971 981
      ///
972 982
      /// This function gives back the edge alteration notifier.
973 983
      EdgeNotifier& notifier(Edge) const {
974 984
        return EdgeNotifier();
975 985
      }
976 986

	
977 987
      template <typename _Graph>
978 988
      struct Constraints {
979 989
        void constraints() {
980 990
          checkConcept<AlterableDigraphComponent<Base>, _Graph>();
981 991
          typename _Graph::EdgeNotifier& uen
982 992
            = graph.notifier(typename _Graph::Edge());
983 993
          ignore_unused_variable_warning(uen);
984 994
        }
985 995

	
986 996
        const _Graph& graph;
997
        Constraints() {}
987 998
      };
988 999
    };
989 1000

	
990 1001
    /// \brief Concept class for standard graph maps.
991 1002
    ///
992 1003
    /// This class describes the concept of standard graph maps, i.e.
993 1004
    /// the \c NodeMap, \c ArcMap and \c EdgeMap subtypes of digraph and 
994 1005
    /// graph types, which can be used for associating data to graph items.
995 1006
    /// The standard graph maps must conform to the ReferenceMap concept.
996 1007
    template <typename GR, typename K, typename V>
997 1008
    class GraphMap : public ReferenceMap<K, V, V&, const V&> {
998 1009
      typedef ReferenceMap<K, V, V&, const V&> Parent;
999 1010

	
1000 1011
    public:
1001 1012

	
1002 1013
      /// The key type of the map.
1003 1014
      typedef K Key;
1004 1015
      /// The value type of the map.
1005 1016
      typedef V Value;
1006 1017
      /// The reference type of the map.
1007 1018
      typedef Value& Reference;
1008 1019
      /// The const reference type of the map.
1009 1020
      typedef const Value& ConstReference;
1010 1021

	
1011 1022
      // The reference map tag.
1012 1023
      typedef True ReferenceMapTag;
1013 1024

	
1014 1025
      /// \brief Construct a new map.
1015 1026
      ///
1016 1027
      /// Construct a new map for the graph.
1017 1028
      explicit GraphMap(const GR&) {}
1018 1029
      /// \brief Construct a new map with default value.
1019 1030
      ///
1020 1031
      /// Construct a new map for the graph and initalize the values.
1021 1032
      GraphMap(const GR&, const Value&) {}
1022 1033

	
1023 1034
    private:
1024 1035
      /// \brief Copy constructor.
1025 1036
      ///
1026 1037
      /// Copy Constructor.
1027 1038
      GraphMap(const GraphMap&) : Parent() {}
1028 1039

	
1029 1040
      /// \brief Assignment operator.
1030 1041
      ///
1031 1042
      /// Assignment operator. It does not mofify the underlying graph,
1032 1043
      /// it just iterates on the current item set and set the  map
1033 1044
      /// with the value returned by the assigned map.
1034 1045
      template <typename CMap>
1035 1046
      GraphMap& operator=(const CMap&) {
1036 1047
        checkConcept<ReadMap<Key, Value>, CMap>();
1037 1048
        return *this;
1038 1049
      }
1039 1050

	
1040 1051
    public:
1041 1052
      template<typename _Map>
1042 1053
      struct Constraints {
1043 1054
        void constraints() {
1044 1055
          checkConcept
1045 1056
            <ReferenceMap<Key, Value, Value&, const Value&>, _Map>();
1046 1057
          _Map m1(g);
1047 1058
          _Map m2(g,t);
1048 1059
          
1049 1060
          // Copy constructor
1050 1061
          // _Map m3(m);
1051 1062

	
1052 1063
          // Assignment operator
1053 1064
          // ReadMap<Key, Value> cmap;
1054 1065
          // m3 = cmap;
1055 1066

	
1056 1067
          ignore_unused_variable_warning(m1);
1057 1068
          ignore_unused_variable_warning(m2);
1058 1069
          // ignore_unused_variable_warning(m3);
1059 1070
        }
1060 1071

	
1061 1072
        const _Map &m;
1062 1073
        const GR &g;
1063 1074
        const typename GraphMap::Value &t;
1075
        Constraints() {}
1064 1076
      };
1065 1077

	
1066 1078
    };
1067 1079

	
1068 1080
    /// \brief Skeleton class for mappable directed graphs.
1069 1081
    ///
1070 1082
    /// This class describes the interface of mappable directed graphs.
1071 1083
    /// It extends \ref BaseDigraphComponent with the standard digraph 
1072 1084
    /// map classes, namely \c NodeMap and \c ArcMap.
1073 1085
    /// This concept is part of the Digraph concept.
1074 1086
    template <typename BAS = BaseDigraphComponent>
1075 1087
    class MappableDigraphComponent : public BAS  {
1076 1088
    public:
1077 1089

	
1078 1090
      typedef BAS Base;
1079 1091
      typedef typename Base::Node Node;
1080 1092
      typedef typename Base::Arc Arc;
1081 1093

	
1082 1094
      typedef MappableDigraphComponent Digraph;
1083 1095

	
1084 1096
      /// \brief Standard graph map for the nodes.
1085 1097
      ///
1086 1098
      /// Standard graph map for the nodes.
1087 1099
      /// It conforms to the ReferenceMap concept.
1088 1100
      template <typename V>
1089 1101
      class NodeMap : public GraphMap<MappableDigraphComponent, Node, V> {
1090 1102
        typedef GraphMap<MappableDigraphComponent, Node, V> Parent;
1091 1103

	
1092 1104
      public:
1093 1105
        /// \brief Construct a new map.
1094 1106
        ///
1095 1107
        /// Construct a new map for the digraph.
1096 1108
        explicit NodeMap(const MappableDigraphComponent& digraph)
1097 1109
          : Parent(digraph) {}
1098 1110

	
1099 1111
        /// \brief Construct a new map with default value.
1100 1112
        ///
1101 1113
        /// Construct a new map for the digraph and initalize the values.
1102 1114
        NodeMap(const MappableDigraphComponent& digraph, const V& value)
1103 1115
          : Parent(digraph, value) {}
1104 1116

	
1105 1117
      private:
1106 1118
        /// \brief Copy constructor.
1107 1119
        ///
1108 1120
        /// Copy Constructor.
1109 1121
        NodeMap(const NodeMap& nm) : Parent(nm) {}
1110 1122

	
1111 1123
        /// \brief Assignment operator.
1112 1124
        ///
1113 1125
        /// Assignment operator.
1114 1126
        template <typename CMap>
1115 1127
        NodeMap& operator=(const CMap&) {
1116 1128
          checkConcept<ReadMap<Node, V>, CMap>();
1117 1129
          return *this;
1118 1130
        }
1119 1131

	
1120 1132
      };
1121 1133

	
1122 1134
      /// \brief Standard graph map for the arcs.
1123 1135
      ///
1124 1136
      /// Standard graph map for the arcs.
1125 1137
      /// It conforms to the ReferenceMap concept.
1126 1138
      template <typename V>
1127 1139
      class ArcMap : public GraphMap<MappableDigraphComponent, Arc, V> {
1128 1140
        typedef GraphMap<MappableDigraphComponent, Arc, V> Parent;
1129 1141

	
1130 1142
      public:
1131 1143
        /// \brief Construct a new map.
1132 1144
        ///
1133 1145
        /// Construct a new map for the digraph.
1134 1146
        explicit ArcMap(const MappableDigraphComponent& digraph)
1135 1147
          : Parent(digraph) {}
1136 1148

	
1137 1149
        /// \brief Construct a new map with default value.
1138 1150
        ///
1139 1151
        /// Construct a new map for the digraph and initalize the values.
1140 1152
        ArcMap(const MappableDigraphComponent& digraph, const V& value)
1141 1153
          : Parent(digraph, value) {}
1142 1154

	
1143 1155
      private:
1144 1156
        /// \brief Copy constructor.
1145 1157
        ///
1146 1158
        /// Copy Constructor.
1147 1159
        ArcMap(const ArcMap& nm) : Parent(nm) {}
1148 1160

	
1149 1161
        /// \brief Assignment operator.
1150 1162
        ///
1151 1163
        /// Assignment operator.
1152 1164
        template <typename CMap>
1153 1165
        ArcMap& operator=(const CMap&) {
1154 1166
          checkConcept<ReadMap<Arc, V>, CMap>();
1155 1167
          return *this;
1156 1168
        }
1157 1169

	
1158 1170
      };
1159 1171

	
1160 1172

	
1161 1173
      template <typename _Digraph>
1162 1174
      struct Constraints {
1163 1175

	
1164 1176
        struct Dummy {
1165 1177
          int value;
1166 1178
          Dummy() : value(0) {}
1167 1179
          Dummy(int _v) : value(_v) {}
1168 1180
        };
1169 1181

	
1170 1182
        void constraints() {
1171 1183
          checkConcept<Base, _Digraph>();
1172 1184
          { // int map test
1173 1185
            typedef typename _Digraph::template NodeMap<int> IntNodeMap;
1174 1186
            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, int>,
1175 1187
              IntNodeMap >();
1176 1188
          } { // bool map test
1177 1189
            typedef typename _Digraph::template NodeMap<bool> BoolNodeMap;
1178 1190
            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, bool>,
1179 1191
              BoolNodeMap >();
1180 1192
          } { // Dummy map test
1181 1193
            typedef typename _Digraph::template NodeMap<Dummy> DummyNodeMap;
1182 1194
            checkConcept<GraphMap<_Digraph, typename _Digraph::Node, Dummy>,
1183 1195
              DummyNodeMap >();
1184 1196
          }
1185 1197

	
1186 1198
          { // int map test
1187 1199
            typedef typename _Digraph::template ArcMap<int> IntArcMap;
1188 1200
            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>,
1189 1201
              IntArcMap >();
1190 1202
          } { // bool map test
1191 1203
            typedef typename _Digraph::template ArcMap<bool> BoolArcMap;
1192 1204
            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>,
1193 1205
              BoolArcMap >();
1194 1206
          } { // Dummy map test
1195 1207
            typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap;
1196 1208
            checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>,
1197 1209
              DummyArcMap >();
1198 1210
          }
1199 1211
        }
1200 1212

	
1201 1213
        const _Digraph& digraph;
1214
        Constraints() {}
1202 1215
      };
1203 1216
    };
1204 1217

	
1205 1218
    /// \brief Skeleton class for mappable undirected graphs.
1206 1219
    ///
1207 1220
    /// This class describes the interface of mappable undirected graphs.
1208 1221
    /// It extends \ref MappableDigraphComponent with the standard graph 
1209 1222
    /// map class for edges (\c EdgeMap).
1210 1223
    /// This concept is part of the Graph concept.
1211 1224
    template <typename BAS = BaseGraphComponent>
1212 1225
    class MappableGraphComponent : public MappableDigraphComponent<BAS>  {
1213 1226
    public:
1214 1227

	
1215 1228
      typedef BAS Base;
1216 1229
      typedef typename Base::Edge Edge;
1217 1230

	
1218 1231
      typedef MappableGraphComponent Graph;
1219 1232

	
1220 1233
      /// \brief Standard graph map for the edges.
1221 1234
      ///
1222 1235
      /// Standard graph map for the edges.
1223 1236
      /// It conforms to the ReferenceMap concept.
1224 1237
      template <typename V>
1225 1238
      class EdgeMap : public GraphMap<MappableGraphComponent, Edge, V> {
1226 1239
        typedef GraphMap<MappableGraphComponent, Edge, V> Parent;
1227 1240

	
1228 1241
      public:
1229 1242
        /// \brief Construct a new map.
1230 1243
        ///
1231 1244
        /// Construct a new map for the graph.
1232 1245
        explicit EdgeMap(const MappableGraphComponent& graph)
1233 1246
          : Parent(graph) {}
1234 1247

	
1235 1248
        /// \brief Construct a new map with default value.
1236 1249
        ///
1237 1250
        /// Construct a new map for the graph and initalize the values.
1238 1251
        EdgeMap(const MappableGraphComponent& graph, const V& value)
1239 1252
          : Parent(graph, value) {}
1240 1253

	
1241 1254
      private:
1242 1255
        /// \brief Copy constructor.
1243 1256
        ///
1244 1257
        /// Copy Constructor.
1245 1258
        EdgeMap(const EdgeMap& nm) : Parent(nm) {}
1246 1259

	
1247 1260
        /// \brief Assignment operator.
1248 1261
        ///
1249 1262
        /// Assignment operator.
1250 1263
        template <typename CMap>
1251 1264
        EdgeMap& operator=(const CMap&) {
1252 1265
          checkConcept<ReadMap<Edge, V>, CMap>();
1253 1266
          return *this;
1254 1267
        }
1255 1268

	
1256 1269
      };
1257 1270

	
1258 1271

	
1259 1272
      template <typename _Graph>
1260 1273
      struct Constraints {
1261 1274

	
1262 1275
        struct Dummy {
1263 1276
          int value;
1264 1277
          Dummy() : value(0) {}
1265 1278
          Dummy(int _v) : value(_v) {}
1266 1279
        };
1267 1280

	
1268 1281
        void constraints() {
1269 1282
          checkConcept<MappableDigraphComponent<Base>, _Graph>();
1270 1283

	
1271 1284
          { // int map test
1272 1285
            typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
1273 1286
            checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
1274 1287
              IntEdgeMap >();
1275 1288
          } { // bool map test
1276 1289
            typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
1277 1290
            checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
1278 1291
              BoolEdgeMap >();
1279 1292
          } { // Dummy map test
1280 1293
            typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap;
1281 1294
            checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>,
1282 1295
              DummyEdgeMap >();
1283 1296
          }
1284 1297
        }
1285 1298

	
1286 1299
        const _Graph& graph;
1300
        Constraints() {}
1287 1301
      };
1288 1302
    };
1289 1303

	
1290 1304
    /// \brief Skeleton class for extendable directed graphs.
1291 1305
    ///
1292 1306
    /// This class describes the interface of extendable directed graphs.
1293 1307
    /// It extends \ref BaseDigraphComponent with functions for adding 
1294 1308
    /// nodes and arcs to the digraph.
1295 1309
    /// This concept requires \ref AlterableDigraphComponent.
1296 1310
    template <typename BAS = BaseDigraphComponent>
1297 1311
    class ExtendableDigraphComponent : public BAS {
1298 1312
    public:
1299 1313
      typedef BAS Base;
1300 1314

	
1301 1315
      typedef typename Base::Node Node;
1302 1316
      typedef typename Base::Arc Arc;
1303 1317

	
1304 1318
      /// \brief Add a new node to the digraph.
1305 1319
      ///
1306 1320
      /// This function adds a new node to the digraph.
1307 1321
      Node addNode() {
1308 1322
        return INVALID;
1309 1323
      }
1310 1324

	
1311 1325
      /// \brief Add a new arc connecting the given two nodes.
1312 1326
      ///
1313 1327
      /// This function adds a new arc connecting the given two nodes
1314 1328
      /// of the digraph.
1315 1329
      Arc addArc(const Node&, const Node&) {
1316 1330
        return INVALID;
1317 1331
      }
1318 1332

	
1319 1333
      template <typename _Digraph>
1320 1334
      struct Constraints {
1321 1335
        void constraints() {
1322 1336
          checkConcept<Base, _Digraph>();
1323 1337
          typename _Digraph::Node node_a, node_b;
1324 1338
          node_a = digraph.addNode();
1325 1339
          node_b = digraph.addNode();
1326 1340
          typename _Digraph::Arc arc;
1327 1341
          arc = digraph.addArc(node_a, node_b);
1328 1342
        }
1329 1343

	
1330 1344
        _Digraph& digraph;
1345
        Constraints() {}
1331 1346
      };
1332 1347
    };
1333 1348

	
1334 1349
    /// \brief Skeleton class for extendable undirected graphs.
1335 1350
    ///
1336 1351
    /// This class describes the interface of extendable undirected graphs.
1337 1352
    /// It extends \ref BaseGraphComponent with functions for adding 
1338 1353
    /// nodes and edges to the graph.
1339 1354
    /// This concept requires \ref AlterableGraphComponent.
1340 1355
    template <typename BAS = BaseGraphComponent>
1341 1356
    class ExtendableGraphComponent : public BAS {
1342 1357
    public:
1343 1358

	
1344 1359
      typedef BAS Base;
1345 1360
      typedef typename Base::Node Node;
1346 1361
      typedef typename Base::Edge Edge;
1347 1362

	
1348 1363
      /// \brief Add a new node to the digraph.
1349 1364
      ///
1350 1365
      /// This function adds a new node to the digraph.
1351 1366
      Node addNode() {
1352 1367
        return INVALID;
1353 1368
      }
1354 1369

	
1355 1370
      /// \brief Add a new edge connecting the given two nodes.
1356 1371
      ///
1357 1372
      /// This function adds a new edge connecting the given two nodes
1358 1373
      /// of the graph.
1359 1374
      Edge addEdge(const Node&, const Node&) {
1360 1375
        return INVALID;
1361 1376
      }
1362 1377

	
1363 1378
      template <typename _Graph>
1364 1379
      struct Constraints {
1365 1380
        void constraints() {
1366 1381
          checkConcept<Base, _Graph>();
1367 1382
          typename _Graph::Node node_a, node_b;
1368 1383
          node_a = graph.addNode();
1369 1384
          node_b = graph.addNode();
1370 1385
          typename _Graph::Edge edge;
1371 1386
          edge = graph.addEdge(node_a, node_b);
1372 1387
        }
1373 1388

	
1374 1389
        _Graph& graph;
1390
        Constraints() {}
1375 1391
      };
1376 1392
    };
1377 1393

	
1378 1394
    /// \brief Skeleton class for erasable directed graphs.
1379 1395
    ///
1380 1396
    /// This class describes the interface of erasable directed graphs.
1381 1397
    /// It extends \ref BaseDigraphComponent with functions for removing 
1382 1398
    /// nodes and arcs from the digraph.
1383 1399
    /// This concept requires \ref AlterableDigraphComponent.
1384 1400
    template <typename BAS = BaseDigraphComponent>
1385 1401
    class ErasableDigraphComponent : public BAS {
1386 1402
    public:
1387 1403

	
1388 1404
      typedef BAS Base;
1389 1405
      typedef typename Base::Node Node;
1390 1406
      typedef typename Base::Arc Arc;
1391 1407

	
1392 1408
      /// \brief Erase a node from the digraph.
1393 1409
      ///
1394 1410
      /// This function erases the given node from the digraph and all arcs 
1395 1411
      /// connected to the node.
1396 1412
      void erase(const Node&) {}
1397 1413

	
1398 1414
      /// \brief Erase an arc from the digraph.
1399 1415
      ///
1400 1416
      /// This function erases the given arc from the digraph.
1401 1417
      void erase(const Arc&) {}
1402 1418

	
1403 1419
      template <typename _Digraph>
1404 1420
      struct Constraints {
1405 1421
        void constraints() {
1406 1422
          checkConcept<Base, _Digraph>();
1407 1423
          const typename _Digraph::Node node(INVALID);
1408 1424
          digraph.erase(node);
1409 1425
          const typename _Digraph::Arc arc(INVALID);
1410 1426
          digraph.erase(arc);
1411 1427
        }
1412 1428

	
1413 1429
        _Digraph& digraph;
1430
        Constraints() {}
1414 1431
      };
1415 1432
    };
1416 1433

	
1417 1434
    /// \brief Skeleton class for erasable undirected graphs.
1418 1435
    ///
1419 1436
    /// This class describes the interface of erasable undirected graphs.
1420 1437
    /// It extends \ref BaseGraphComponent with functions for removing 
1421 1438
    /// nodes and edges from the graph.
1422 1439
    /// This concept requires \ref AlterableGraphComponent.
1423 1440
    template <typename BAS = BaseGraphComponent>
1424 1441
    class ErasableGraphComponent : public BAS {
1425 1442
    public:
1426 1443

	
1427 1444
      typedef BAS Base;
1428 1445
      typedef typename Base::Node Node;
1429 1446
      typedef typename Base::Edge Edge;
1430 1447

	
1431 1448
      /// \brief Erase a node from the graph.
1432 1449
      ///
1433 1450
      /// This function erases the given node from the graph and all edges
1434 1451
      /// connected to the node.
1435 1452
      void erase(const Node&) {}
1436 1453

	
1437 1454
      /// \brief Erase an edge from the digraph.
1438 1455
      ///
1439 1456
      /// This function erases the given edge from the digraph.
1440 1457
      void erase(const Edge&) {}
1441 1458

	
1442 1459
      template <typename _Graph>
1443 1460
      struct Constraints {
1444 1461
        void constraints() {
1445 1462
          checkConcept<Base, _Graph>();
1446 1463
          const typename _Graph::Node node(INVALID);
1447 1464
          graph.erase(node);
1448 1465
          const typename _Graph::Edge edge(INVALID);
1449 1466
          graph.erase(edge);
1450 1467
        }
1451 1468

	
1452 1469
        _Graph& graph;
1470
        Constraints() {}
1453 1471
      };
1454 1472
    };
1455 1473

	
1456 1474
    /// \brief Skeleton class for clearable directed graphs.
1457 1475
    ///
1458 1476
    /// This class describes the interface of clearable directed graphs.
1459 1477
    /// It extends \ref BaseDigraphComponent with a function for clearing
1460 1478
    /// the digraph.
1461 1479
    /// This concept requires \ref AlterableDigraphComponent.
1462 1480
    template <typename BAS = BaseDigraphComponent>
1463 1481
    class ClearableDigraphComponent : public BAS {
1464 1482
    public:
1465 1483

	
1466 1484
      typedef BAS Base;
1467 1485

	
1468 1486
      /// \brief Erase all nodes and arcs from the digraph.
1469 1487
      ///
1470 1488
      /// This function erases all nodes and arcs from the digraph.
1471 1489
      void clear() {}
1472 1490

	
1473 1491
      template <typename _Digraph>
1474 1492
      struct Constraints {
1475 1493
        void constraints() {
1476 1494
          checkConcept<Base, _Digraph>();
1477 1495
          digraph.clear();
1478 1496
        }
1479 1497

	
1480 1498
        _Digraph& digraph;
1499
        Constraints() {}
1481 1500
      };
1482 1501
    };
1483 1502

	
1484 1503
    /// \brief Skeleton class for clearable undirected graphs.
1485 1504
    ///
1486 1505
    /// This class describes the interface of clearable undirected graphs.
1487 1506
    /// It extends \ref BaseGraphComponent with a function for clearing
1488 1507
    /// the graph.
1489 1508
    /// This concept requires \ref AlterableGraphComponent.
1490 1509
    template <typename BAS = BaseGraphComponent>
1491 1510
    class ClearableGraphComponent : public ClearableDigraphComponent<BAS> {
1492 1511
    public:
1493 1512

	
1494 1513
      typedef BAS Base;
1495 1514

	
1496 1515
      /// \brief Erase all nodes and edges from the graph.
1497 1516
      ///
1498 1517
      /// This function erases all nodes and edges from the graph.
1499 1518
      void clear() {}
1500 1519

	
1501 1520
      template <typename _Graph>
1502 1521
      struct Constraints {
1503 1522
        void constraints() {
1504 1523
          checkConcept<Base, _Graph>();
1505 1524
          graph.clear();
1506 1525
        }
1507 1526

	
1508 1527
        _Graph& graph;
1528
        Constraints() {}
1509 1529
      };
1510 1530
    };
1511 1531

	
1512 1532
  }
1513 1533

	
1514 1534
}
1515 1535

	
1516 1536
#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-2009
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 The concept of heaps.
22 22

	
23 23
#ifndef LEMON_CONCEPTS_HEAP_H
24 24
#define LEMON_CONCEPTS_HEAP_H
25 25

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

	
29 29
namespace lemon {
30 30

	
31 31
  namespace concepts {
32 32

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

	
36 36
    /// \brief The heap concept.
37 37
    ///
38 38
    /// Concept class describing the main interface of heaps. A \e heap
39 39
    /// is a data structure for storing items with specified values called
40 40
    /// \e priorities in such a way that finding the item with minimum
41 41
    /// priority is efficient. In a heap one can change the priority of an
42 42
    /// item, add or erase an item, etc.
43 43
    ///
44 44
    /// \tparam PR Type of the priority of the items.
45 45
    /// \tparam IM A read and writable item map with int values, used
46 46
    /// internally to handle the cross references.
47 47
    /// \tparam Comp A functor class for the ordering of the priorities.
48 48
    /// The default is \c std::less<PR>.
49 49
#ifdef DOXYGEN
50 50
    template <typename PR, typename IM, typename Comp = std::less<PR> >
51 51
#else
52 52
    template <typename PR, typename IM>
53 53
#endif
54 54
    class Heap {
55 55
    public:
56 56

	
57 57
      /// Type of the item-int map.
58 58
      typedef IM ItemIntMap;
59 59
      /// Type of the priorities.
60 60
      typedef PR Prio;
61 61
      /// Type of the items stored in the heap.
62 62
      typedef typename ItemIntMap::Key Item;
63 63

	
64 64
      /// \brief Type to represent the states of the items.
65 65
      ///
66 66
      /// Each item has a state associated to it. It can be "in heap",
67 67
      /// "pre heap" or "post heap". The later two are indifferent
68 68
      /// from the point of view of the heap, but may be useful for
69 69
      /// the user.
70 70
      ///
71 71
      /// The item-int map must be initialized in such way that it assigns
72 72
      /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
73 73
      enum State {
74 74
        IN_HEAP = 0,    ///< = 0. The "in heap" state constant.
75 75
        PRE_HEAP = -1,  ///< = -1. The "pre heap" state constant.
76 76
        POST_HEAP = -2  ///< = -2. The "post heap" state constant.
77 77
      };
78 78

	
79 79
      /// \brief The constructor.
80 80
      ///
81 81
      /// The constructor.
82 82
      /// \param map A map that assigns \c int values to keys of type
83 83
      /// \c Item. It is used internally by the heap implementations to
84 84
      /// handle the cross references. The assigned value must be
85 85
      /// \c PRE_HEAP (<tt>-1</tt>) for every item.
86 86
      explicit Heap(ItemIntMap &map) {}
87 87

	
88 88
      /// \brief The number of items stored in the heap.
89 89
      ///
90 90
      /// Returns the number of items stored in the heap.
91 91
      int size() const { return 0; }
92 92

	
93 93
      /// \brief Checks if the heap is empty.
94 94
      ///
95 95
      /// Returns \c true if the heap is empty.
96 96
      bool empty() const { return false; }
97 97

	
98 98
      /// \brief Makes the heap empty.
99 99
      ///
100 100
      /// Makes the heap empty.
101 101
      void clear();
102 102

	
103 103
      /// \brief Inserts an item into the heap with the given priority.
104 104
      ///
105 105
      /// Inserts the given item into the heap with the given priority.
106 106
      /// \param i The item to insert.
107 107
      /// \param p The priority of the item.
108 108
      void push(const Item &i, const Prio &p) {}
109 109

	
110 110
      /// \brief Returns the item having minimum priority.
111 111
      ///
112 112
      /// Returns the item having minimum priority.
113 113
      /// \pre The heap must be non-empty.
114 114
      Item top() const {}
115 115

	
116 116
      /// \brief The minimum priority.
117 117
      ///
118 118
      /// Returns the minimum priority.
119 119
      /// \pre The heap must be non-empty.
120 120
      Prio prio() const {}
121 121

	
122 122
      /// \brief Removes the item having minimum priority.
123 123
      ///
124 124
      /// Removes the item having minimum priority.
125 125
      /// \pre The heap must be non-empty.
126 126
      void pop() {}
127 127

	
128 128
      /// \brief Removes an item from the heap.
129 129
      ///
130 130
      /// Removes the given item from the heap if it is already stored.
131 131
      /// \param i The item to delete.
132 132
      void erase(const Item &i) {}
133 133

	
134 134
      /// \brief The priority of an item.
135 135
      ///
136 136
      /// Returns the priority of the given item.
137 137
      /// \param i The item.
138 138
      /// \pre \c i must be in the heap.
139 139
      Prio operator[](const Item &i) const {}
140 140

	
141 141
      /// \brief Sets the priority of an item or inserts it, if it is
142 142
      /// not stored in the heap.
143 143
      ///
144 144
      /// This method sets the priority of the given item if it is
145 145
      /// already stored in the heap.
146 146
      /// Otherwise it inserts the given item with the given priority.
147 147
      ///
148 148
      /// \param i The item.
149 149
      /// \param p The priority.
150 150
      void set(const Item &i, const Prio &p) {}
151 151

	
152 152
      /// \brief Decreases the priority of an item to the given value.
153 153
      ///
154 154
      /// Decreases the priority of an item to the given value.
155 155
      /// \param i The item.
156 156
      /// \param p The priority.
157 157
      /// \pre \c i must be stored in the heap with priority at least \c p.
158 158
      void decrease(const Item &i, const Prio &p) {}
159 159

	
160 160
      /// \brief Increases the priority of an item to the given value.
161 161
      ///
162 162
      /// Increases the priority of an item to the given value.
163 163
      /// \param i The item.
164 164
      /// \param p The priority.
165 165
      /// \pre \c i must be stored in the heap with priority at most \c p.
166 166
      void increase(const Item &i, const Prio &p) {}
167 167

	
168 168
      /// \brief Returns if an item is in, has already been in, or has
169 169
      /// never been in the heap.
170 170
      ///
171 171
      /// This method returns \c PRE_HEAP if the given item has never
172 172
      /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
173 173
      /// and \c POST_HEAP otherwise.
174 174
      /// In the latter case it is possible that the item will get back
175 175
      /// to the heap again.
176 176
      /// \param i The item.
177 177
      State state(const Item &i) const {}
178 178

	
179 179
      /// \brief Sets the state of an item in the heap.
180 180
      ///
181 181
      /// Sets the state of the given item in the heap. It can be used
182 182
      /// to manually clear the heap when it is important to achive the
183 183
      /// better time complexity.
184 184
      /// \param i The item.
185 185
      /// \param st The state. It should not be \c IN_HEAP.
186 186
      void state(const Item& i, State st) {}
187 187

	
188 188

	
189 189
      template <typename _Heap>
190 190
      struct Constraints {
191 191
      public:
192 192
        void constraints() {
193 193
          typedef typename _Heap::Item OwnItem;
194 194
          typedef typename _Heap::Prio OwnPrio;
195 195
          typedef typename _Heap::State OwnState;
196 196

	
197 197
          Item item;
198 198
          Prio prio;
199 199
          item=Item();
200 200
          prio=Prio();
201 201
          ignore_unused_variable_warning(item);
202 202
          ignore_unused_variable_warning(prio);
203 203

	
204 204
          OwnItem own_item;
205 205
          OwnPrio own_prio;
206 206
          OwnState own_state;
207 207
          own_item=Item();
208 208
          own_prio=Prio();
209 209
          ignore_unused_variable_warning(own_item);
210 210
          ignore_unused_variable_warning(own_prio);
211 211
          ignore_unused_variable_warning(own_state);
212 212

	
213 213
          _Heap heap1(map);
214 214
          _Heap heap2 = heap1;
215 215
          ignore_unused_variable_warning(heap1);
216 216
          ignore_unused_variable_warning(heap2);
217 217

	
218 218
          int s = heap.size();
219 219
          ignore_unused_variable_warning(s);
220 220
          bool e = heap.empty();
221 221
          ignore_unused_variable_warning(e);
222 222

	
223 223
          prio = heap.prio();
224 224
          item = heap.top();
225 225
          prio = heap[item];
226 226
          own_prio = heap.prio();
227 227
          own_item = heap.top();
228 228
          own_prio = heap[own_item];
229 229

	
230 230
          heap.push(item, prio);
231 231
          heap.push(own_item, own_prio);
232 232
          heap.pop();
233 233

	
234 234
          heap.set(item, prio);
235 235
          heap.decrease(item, prio);
236 236
          heap.increase(item, prio);
237 237
          heap.set(own_item, own_prio);
238 238
          heap.decrease(own_item, own_prio);
239 239
          heap.increase(own_item, own_prio);
240 240

	
241 241
          heap.erase(item);
242 242
          heap.erase(own_item);
243 243
          heap.clear();
244 244

	
245 245
          own_state = heap.state(own_item);
246 246
          heap.state(own_item, own_state);
247 247

	
248 248
          own_state = _Heap::PRE_HEAP;
249 249
          own_state = _Heap::IN_HEAP;
250 250
          own_state = _Heap::POST_HEAP;
251 251
        }
252 252

	
253 253
        _Heap& heap;
254 254
        ItemIntMap& map;
255
        Constraints() {}
255 256
      };
256 257
    };
257 258

	
258 259
    /// @}
259 260
  } // namespace lemon
260 261
}
261 262
#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-2009
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_CONCEPTS_MAPS_H
20 20
#define LEMON_CONCEPTS_MAPS_H
21 21

	
22 22
#include <lemon/core.h>
23 23
#include <lemon/concept_check.h>
24 24

	
25 25
///\ingroup map_concepts
26 26
///\file
27 27
///\brief The concept of maps.
28 28

	
29 29
namespace lemon {
30 30

	
31 31
  namespace concepts {
32 32

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

	
36 36
    /// Readable map concept
37 37

	
38 38
    /// Readable map concept.
39 39
    ///
40 40
    template<typename K, typename T>
41 41
    class ReadMap
42 42
    {
43 43
    public:
44 44
      /// The key type of the map.
45 45
      typedef K Key;
46 46
      /// \brief The value type of the map.
47 47
      /// (The type of objects associated with the keys).
48 48
      typedef T Value;
49 49

	
50 50
      /// Returns the value associated with the given key.
51 51
      Value operator[](const Key &) const {
52 52
        return *static_cast<Value *>(0);
53 53
      }
54 54

	
55 55
      template<typename _ReadMap>
56 56
      struct Constraints {
57 57
        void constraints() {
58 58
          Value val = m[key];
59 59
          val = m[key];
60 60
          typename _ReadMap::Value own_val = m[own_key];
61 61
          own_val = m[own_key];
62 62

	
63 63
          ignore_unused_variable_warning(key);
64 64
          ignore_unused_variable_warning(val);
65 65
          ignore_unused_variable_warning(own_key);
66 66
          ignore_unused_variable_warning(own_val);
67 67
        }
68 68
        const Key& key;
69 69
        const typename _ReadMap::Key& own_key;
70 70
        const _ReadMap& m;
71
        Constraints() {}
71 72
      };
72 73

	
73 74
    };
74 75

	
75 76

	
76 77
    /// Writable map concept
77 78

	
78 79
    /// Writable map concept.
79 80
    ///
80 81
    template<typename K, typename T>
81 82
    class WriteMap
82 83
    {
83 84
    public:
84 85
      /// The key type of the map.
85 86
      typedef K Key;
86 87
      /// \brief The value type of the map.
87 88
      /// (The type of objects associated with the keys).
88 89
      typedef T Value;
89 90

	
90 91
      /// Sets the value associated with the given key.
91 92
      void set(const Key &, const Value &) {}
92 93

	
93 94
      /// Default constructor.
94 95
      WriteMap() {}
95 96

	
96 97
      template <typename _WriteMap>
97 98
      struct Constraints {
98 99
        void constraints() {
99 100
          m.set(key, val);
100 101
          m.set(own_key, own_val);
101 102

	
102 103
          ignore_unused_variable_warning(key);
103 104
          ignore_unused_variable_warning(val);
104 105
          ignore_unused_variable_warning(own_key);
105 106
          ignore_unused_variable_warning(own_val);
106 107
        }
107 108
        const Key& key;
108 109
        const Value& val;
109 110
        const typename _WriteMap::Key& own_key;
110 111
        const typename _WriteMap::Value& own_val;
111 112
        _WriteMap& m;
113
        Constraints() {}
112 114
      };
113 115
    };
114 116

	
115 117
    /// Read/writable map concept
116 118

	
117 119
    /// Read/writable map concept.
118 120
    ///
119 121
    template<typename K, typename T>
120 122
    class ReadWriteMap : public ReadMap<K,T>,
121 123
                         public WriteMap<K,T>
122 124
    {
123 125
    public:
124 126
      /// The key type of the map.
125 127
      typedef K Key;
126 128
      /// \brief The value type of the map.
127 129
      /// (The type of objects associated with the keys).
128 130
      typedef T Value;
129 131

	
130 132
      /// Returns the value associated with the given key.
131 133
      Value operator[](const Key &) const {
132
        return *static_cast<Value *>(0);
134
        Value *r = 0;
135
        return *r;
133 136
      }
134 137

	
135 138
      /// Sets the value associated with the given key.
136 139
      void set(const Key &, const Value &) {}
137 140

	
138 141
      template<typename _ReadWriteMap>
139 142
      struct Constraints {
140 143
        void constraints() {
141 144
          checkConcept<ReadMap<K, T>, _ReadWriteMap >();
142 145
          checkConcept<WriteMap<K, T>, _ReadWriteMap >();
143 146
        }
144 147
      };
145 148
    };
146 149

	
147 150

	
148 151
    /// Dereferable map concept
149 152

	
150 153
    /// Dereferable map concept.
151 154
    ///
152 155
    template<typename K, typename T, typename R, typename CR>
153 156
    class ReferenceMap : public ReadWriteMap<K,T>
154 157
    {
155 158
    public:
156 159
      /// Tag for reference maps.
157 160
      typedef True ReferenceMapTag;
158 161
      /// The key type of the map.
159 162
      typedef K Key;
160 163
      /// \brief The value type of the map.
161 164
      /// (The type of objects associated with the keys).
162 165
      typedef T Value;
163 166
      /// The reference type of the map.
164 167
      typedef R Reference;
165 168
      /// The const reference type of the map.
166 169
      typedef CR ConstReference;
167 170

	
168 171
    public:
169 172

	
170 173
      /// Returns a reference to the value associated with the given key.
171 174
      Reference operator[](const Key &) {
172
        return *static_cast<Value *>(0);
175
        Value *r = 0;
176
        return *r;
173 177
      }
174 178

	
175 179
      /// Returns a const reference to the value associated with the given key.
176 180
      ConstReference operator[](const Key &) const {
177
        return *static_cast<Value *>(0);
181
        Value *r = 0;
182
        return *r;
178 183
      }
179 184

	
180 185
      /// Sets the value associated with the given key.
181 186
      void set(const Key &k,const Value &t) { operator[](k)=t; }
182 187

	
183 188
      template<typename _ReferenceMap>
184 189
      struct Constraints {
185 190
        typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type
186 191
        constraints() {
187 192
          checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
188 193
          ref = m[key];
189 194
          m[key] = val;
190 195
          m[key] = ref;
191 196
          m[key] = cref;
192 197
          own_ref = m[own_key];
193 198
          m[own_key] = own_val;
194 199
          m[own_key] = own_ref;
195 200
          m[own_key] = own_cref;
196 201
          m[key] = m[own_key];
197 202
          m[own_key] = m[key];
198 203
        }
199 204
        const Key& key;
200 205
        Value& val;
201 206
        Reference ref;
202 207
        ConstReference cref;
203 208
        const typename _ReferenceMap::Key& own_key;
204 209
        typename _ReferenceMap::Value& own_val;
205 210
        typename _ReferenceMap::Reference own_ref;
206 211
        typename _ReferenceMap::ConstReference own_cref;
207 212
        _ReferenceMap& m;
213
        Constraints() {}
208 214
      };
209 215
    };
210 216

	
211 217
    // @}
212 218

	
213 219
  } //namespace concepts
214 220

	
215 221
} //namespace lemon
216 222

	
217 223
#endif
Ignore white space 49152 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-2009
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_CONCEPTS_PATH_H
25 25
#define LEMON_CONCEPTS_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 GR 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 GR>
49 49
    class Path {
50 50
    public:
51 51

	
52 52
      /// Type of the underlying digraph.
53 53
      typedef GR 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 68
      Path& operator=(const CPath& cpath) {
69 69
        ignore_unused_variable_warning(cpath);
70 70
        return *this;
71 71
      }
72 72

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

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

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

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

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

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

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

	
107 107
      };
108 108

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

	
118 118
          p = pc;
119 119

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

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

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

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

	
138 138
    };
139 139

	
140 140
    namespace _path_bits {
141 141

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

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

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

	
153 153
          e = (i == INVALID);
154 154
          e = (i != INVALID);
155 155

	
156 156
          ignore_unused_variable_warning(l);
157 157
          ignore_unused_variable_warning(e);
158 158
          ignore_unused_variable_warning(id);
159 159
          ignore_unused_variable_warning(ed);
160 160
        }
161 161
        _Path& p;
162
        PathDumperConstraints() {}
162 163
      };
163 164

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

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

	
175 176
          ++i;
176 177
          typename _Digraph::Arc ed = i;
177 178

	
178 179
          e = (i == INVALID);
179 180
          e = (i != INVALID);
180 181

	
181 182
          ignore_unused_variable_warning(l);
182 183
          ignore_unused_variable_warning(e);
183 184
          ignore_unused_variable_warning(id);
184 185
          ignore_unused_variable_warning(ed);
185 186
        }
186 187
        _Path& p;
188
        PathDumperConstraints() {}
187 189
      };
188 190

	
189 191
    }
190 192

	
191 193

	
192 194
    /// \brief A skeleton structure for path dumpers.
193 195
    ///
194 196
    /// A skeleton structure for path dumpers. The path dumpers are
195 197
    /// the generalization of the paths. The path dumpers can
196 198
    /// enumerate the arcs of the path wheter in forward or in
197 199
    /// backward order.  In most time these classes are not used
198 200
    /// directly rather it used to assign a dumped class to a real
199 201
    /// path type.
200 202
    ///
201 203
    /// The main purpose of this concept is that the shortest path
202 204
    /// algorithms can enumerate easily the arcs in reverse order.
203 205
    /// If we would like to give back a real path from these
204 206
    /// algorithms then we should create a temporarly path object. In
205 207
    /// LEMON such algorithms gives back a path dumper what can
206 208
    /// assigned to a real path and the dumpers can be implemented as
207 209
    /// an adaptor class to the predecessor map.
208 210
    ///
209 211
    /// \tparam GR The digraph type in which the path is.
210 212
    ///
211 213
    /// The paths can be constructed from any path type by a
212 214
    /// template constructor or a template assignment operator.
213 215
    template <typename GR>
214 216
    class PathDumper {
215 217
    public:
216 218

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

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

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

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

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

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

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

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

	
261 263
      };
262 264

	
263 265
      /// \brief LEMON style iterator for path arcs
264 266
      ///
265 267
      /// This class is used to iterate on the arcs of the paths in
266 268
      /// reverse direction.
267 269
      class RevArcIt {
268 270
      public:
269 271
        /// Default constructor
270 272
        RevArcIt() {}
271 273
        /// Invalid constructor
272 274
        RevArcIt(Invalid) {}
273 275
        /// Constructor for first arc
274 276
        RevArcIt(const PathDumper &) {}
275 277

	
276 278
        /// Conversion to Arc
277 279
        operator Arc() const { return INVALID; }
278 280

	
279 281
        /// Next arc
280 282
        RevArcIt& operator++() {return *this;}
281 283

	
282 284
        /// Comparison operator
283 285
        bool operator==(const RevArcIt&) const {return true;}
284 286
        /// Comparison operator
285 287
        bool operator!=(const RevArcIt&) const {return true;}
286 288
        /// Comparison operator
287 289
        bool operator<(const RevArcIt&) const {return false;}
288 290

	
289 291
      };
290 292

	
291 293
      template <typename _Path>
292 294
      struct Constraints {
293 295
        void constraints() {
294 296
          function_requires<_path_bits::
295 297
            PathDumperConstraints<Digraph, _Path> >();
296 298
        }
297 299
      };
298 300

	
299 301
    };
300 302

	
301 303

	
302 304
    ///@}
303 305
  }
304 306

	
305 307
} // namespace lemon
306 308

	
307 309
#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-2009
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/maps.h>
31 31
#include <lemon/path.h>
32 32

	
33 33
namespace lemon {
34 34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	
114 114
  ///\ingroup search
115 115
  ///This class provides an efficient implementation of the %DFS algorithm.
116 116
  ///
117 117
  ///There is also a \ref dfs() "function-type interface" for the DFS
118 118
  ///algorithm, which is convenient in the simplier cases and it can be
119 119
  ///used easier.
120 120
  ///
121 121
  ///\tparam GR The type of the digraph the algorithm runs on.
122 122
  ///The default type is \ref ListDigraph.
123 123
#ifdef DOXYGEN
124 124
  template <typename GR,
125 125
            typename TR>
126 126
#else
127 127
  template <typename GR=ListDigraph,
128 128
            typename TR=DfsDefaultTraits<GR> >
129 129
#endif
130 130
  class Dfs {
131 131
  public:
132 132

	
133 133
    ///The type of the digraph the algorithm runs on.
134 134
    typedef typename TR::Digraph Digraph;
135 135

	
136 136
    ///\brief The type of the map that stores the predecessor arcs of the
137 137
    ///DFS paths.
138 138
    typedef typename TR::PredMap PredMap;
139 139
    ///The type of the map that stores the distances of the nodes.
140 140
    typedef typename TR::DistMap DistMap;
141 141
    ///The type of the map that indicates which nodes are reached.
142 142
    typedef typename TR::ReachedMap ReachedMap;
143 143
    ///The type of the map that indicates which nodes are processed.
144 144
    typedef typename TR::ProcessedMap ProcessedMap;
145 145
    ///The type of the paths.
146 146
    typedef PredMapPath<Digraph, PredMap> Path;
147 147

	
148 148
    ///The \ref DfsDefaultTraits "traits class" of the algorithm.
149 149
    typedef TR Traits;
150 150

	
151 151
  private:
152 152

	
153 153
    typedef typename Digraph::Node Node;
154 154
    typedef typename Digraph::NodeIt NodeIt;
155 155
    typedef typename Digraph::Arc Arc;
156 156
    typedef typename Digraph::OutArcIt OutArcIt;
157 157

	
158 158
    //Pointer to the underlying digraph.
159 159
    const Digraph *G;
160 160
    //Pointer to the map of predecessor arcs.
161 161
    PredMap *_pred;
162 162
    //Indicates if _pred is locally allocated (true) or not.
163 163
    bool local_pred;
164 164
    //Pointer to the map of distances.
165 165
    DistMap *_dist;
166 166
    //Indicates if _dist is locally allocated (true) or not.
167 167
    bool local_dist;
168 168
    //Pointer to the map of reached status of the nodes.
169 169
    ReachedMap *_reached;
170 170
    //Indicates if _reached is locally allocated (true) or not.
171 171
    bool local_reached;
172 172
    //Pointer to the map of processed status of the nodes.
173 173
    ProcessedMap *_processed;
174 174
    //Indicates if _processed is locally allocated (true) or not.
175 175
    bool local_processed;
176 176

	
177 177
    std::vector<typename Digraph::OutArcIt> _stack;
178 178
    int _stack_head;
179 179

	
180 180
    //Creates the maps if necessary.
181 181
    void create_maps()
182 182
    {
183 183
      if(!_pred) {
184 184
        local_pred = true;
185 185
        _pred = Traits::createPredMap(*G);
186 186
      }
187 187
      if(!_dist) {
188 188
        local_dist = true;
189 189
        _dist = Traits::createDistMap(*G);
190 190
      }
191 191
      if(!_reached) {
192 192
        local_reached = true;
193 193
        _reached = Traits::createReachedMap(*G);
194 194
      }
195 195
      if(!_processed) {
196 196
        local_processed = true;
197 197
        _processed = Traits::createProcessedMap(*G);
198 198
      }
199 199
    }
200 200

	
201 201
  protected:
202 202

	
203 203
    Dfs() {}
204 204

	
205 205
  public:
206 206

	
207 207
    typedef Dfs Create;
208 208

	
209 209
    ///\name Named Template Parameters
210 210

	
211 211
    ///@{
212 212

	
213 213
    template <class T>
214 214
    struct SetPredMapTraits : public Traits {
215 215
      typedef T PredMap;
216 216
      static PredMap *createPredMap(const Digraph &)
217 217
      {
218 218
        LEMON_ASSERT(false, "PredMap is not initialized");
219 219
        return 0; // ignore warnings
220 220
      }
221 221
    };
222 222
    ///\brief \ref named-templ-param "Named parameter" for setting
223 223
    ///\c PredMap type.
224 224
    ///
225 225
    ///\ref named-templ-param "Named parameter" for setting
226 226
    ///\c PredMap type.
227 227
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
228 228
    template <class T>
229 229
    struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
230 230
      typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
231 231
    };
232 232

	
233 233
    template <class T>
234 234
    struct SetDistMapTraits : public Traits {
235 235
      typedef T DistMap;
236 236
      static DistMap *createDistMap(const Digraph &)
237 237
      {
238 238
        LEMON_ASSERT(false, "DistMap is not initialized");
239 239
        return 0; // ignore warnings
240 240
      }
241 241
    };
242 242
    ///\brief \ref named-templ-param "Named parameter" for setting
243 243
    ///\c DistMap type.
244 244
    ///
245 245
    ///\ref named-templ-param "Named parameter" for setting
246 246
    ///\c DistMap type.
247 247
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
248 248
    template <class T>
249 249
    struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
250 250
      typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
251 251
    };
252 252

	
253 253
    template <class T>
254 254
    struct SetReachedMapTraits : public Traits {
255 255
      typedef T ReachedMap;
256 256
      static ReachedMap *createReachedMap(const Digraph &)
257 257
      {
258 258
        LEMON_ASSERT(false, "ReachedMap is not initialized");
259 259
        return 0; // ignore warnings
260 260
      }
261 261
    };
262 262
    ///\brief \ref named-templ-param "Named parameter" for setting
263 263
    ///\c ReachedMap type.
264 264
    ///
265 265
    ///\ref named-templ-param "Named parameter" for setting
266 266
    ///\c ReachedMap type.
267 267
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
268 268
    template <class T>
269 269
    struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
270 270
      typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
271 271
    };
272 272

	
273 273
    template <class T>
274 274
    struct SetProcessedMapTraits : public Traits {
275 275
      typedef T ProcessedMap;
276 276
      static ProcessedMap *createProcessedMap(const Digraph &)
277 277
      {
278 278
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
279 279
        return 0; // ignore warnings
280 280
      }
281 281
    };
282 282
    ///\brief \ref named-templ-param "Named parameter" for setting
283 283
    ///\c ProcessedMap type.
284 284
    ///
285 285
    ///\ref named-templ-param "Named parameter" for setting
286 286
    ///\c ProcessedMap type.
287 287
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
288 288
    template <class T>
289 289
    struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
290 290
      typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
291 291
    };
292 292

	
293 293
    struct SetStandardProcessedMapTraits : public Traits {
294 294
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
295 295
      static ProcessedMap *createProcessedMap(const Digraph &g)
296 296
      {
297 297
        return new ProcessedMap(g);
298 298
      }
299 299
    };
300 300
    ///\brief \ref named-templ-param "Named parameter" for setting
301 301
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
302 302
    ///
303 303
    ///\ref named-templ-param "Named parameter" for setting
304 304
    ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
305 305
    ///If you don't set it explicitly, it will be automatically allocated.
306 306
    struct SetStandardProcessedMap :
307 307
      public Dfs< Digraph, SetStandardProcessedMapTraits > {
308 308
      typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create;
309 309
    };
310 310

	
311 311
    ///@}
312 312

	
313 313
  public:
314 314

	
315 315
    ///Constructor.
316 316

	
317 317
    ///Constructor.
318 318
    ///\param g The digraph the algorithm runs on.
319 319
    Dfs(const Digraph &g) :
320 320
      G(&g),
321 321
      _pred(NULL), local_pred(false),
322 322
      _dist(NULL), local_dist(false),
323 323
      _reached(NULL), local_reached(false),
324 324
      _processed(NULL), local_processed(false)
325 325
    { }
326 326

	
327 327
    ///Destructor.
328 328
    ~Dfs()
329 329
    {
330 330
      if(local_pred) delete _pred;
331 331
      if(local_dist) delete _dist;
332 332
      if(local_reached) delete _reached;
333 333
      if(local_processed) delete _processed;
334 334
    }
335 335

	
336 336
    ///Sets the map that stores the predecessor arcs.
337 337

	
338 338
    ///Sets the map that stores the predecessor arcs.
339 339
    ///If you don't use this function before calling \ref run(Node) "run()"
340 340
    ///or \ref init(), an instance will be allocated automatically.
341 341
    ///The destructor deallocates this automatically allocated map,
342 342
    ///of course.
343 343
    ///\return <tt> (*this) </tt>
344 344
    Dfs &predMap(PredMap &m)
345 345
    {
346 346
      if(local_pred) {
347 347
        delete _pred;
348 348
        local_pred=false;
349 349
      }
350 350
      _pred = &m;
351 351
      return *this;
352 352
    }
353 353

	
354 354
    ///Sets the map that indicates which nodes are reached.
355 355

	
356 356
    ///Sets the map that indicates which nodes are reached.
357 357
    ///If you don't use this function before calling \ref run(Node) "run()"
358 358
    ///or \ref init(), an instance will be allocated automatically.
359 359
    ///The destructor deallocates this automatically allocated map,
360 360
    ///of course.
361 361
    ///\return <tt> (*this) </tt>
362 362
    Dfs &reachedMap(ReachedMap &m)
363 363
    {
364 364
      if(local_reached) {
365 365
        delete _reached;
366 366
        local_reached=false;
367 367
      }
368 368
      _reached = &m;
369 369
      return *this;
370 370
    }
371 371

	
372 372
    ///Sets the map that indicates which nodes are processed.
373 373

	
374 374
    ///Sets the map that indicates which nodes are processed.
375 375
    ///If you don't use this function before calling \ref run(Node) "run()"
376 376
    ///or \ref init(), an instance will be allocated automatically.
377 377
    ///The destructor deallocates this automatically allocated map,
378 378
    ///of course.
379 379
    ///\return <tt> (*this) </tt>
380 380
    Dfs &processedMap(ProcessedMap &m)
381 381
    {
382 382
      if(local_processed) {
383 383
        delete _processed;
384 384
        local_processed=false;
385 385
      }
386 386
      _processed = &m;
387 387
      return *this;
388 388
    }
389 389

	
390 390
    ///Sets the map that stores the distances of the nodes.
391 391

	
392 392
    ///Sets the map that stores the distances of the nodes calculated by
393 393
    ///the algorithm.
394 394
    ///If you don't use this function before calling \ref run(Node) "run()"
395 395
    ///or \ref init(), an instance will be allocated automatically.
396 396
    ///The destructor deallocates this automatically allocated map,
397 397
    ///of course.
398 398
    ///\return <tt> (*this) </tt>
399 399
    Dfs &distMap(DistMap &m)
400 400
    {
401 401
      if(local_dist) {
402 402
        delete _dist;
403 403
        local_dist=false;
404 404
      }
405 405
      _dist = &m;
406 406
      return *this;
407 407
    }
408 408

	
409 409
  public:
410 410

	
411 411
    ///\name Execution Control
412 412
    ///The simplest way to execute the DFS algorithm is to use one of the
413 413
    ///member functions called \ref run(Node) "run()".\n
414 414
    ///If you need more control on the execution, first you have to call
415 415
    ///\ref init(), then you can add a source node with \ref addSource()
416 416
    ///and perform the actual computation with \ref start().
417 417
    ///This procedure can be repeated if there are nodes that have not
418 418
    ///been reached.
419 419

	
420 420
    ///@{
421 421

	
422 422
    ///\brief Initializes the internal data structures.
423 423
    ///
424 424
    ///Initializes the internal data structures.
425 425
    void init()
426 426
    {
427 427
      create_maps();
428 428
      _stack.resize(countNodes(*G));
429 429
      _stack_head=-1;
430 430
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
431 431
        _pred->set(u,INVALID);
432 432
        _reached->set(u,false);
433 433
        _processed->set(u,false);
434 434
      }
435 435
    }
436 436

	
437 437
    ///Adds a new source node.
438 438

	
439 439
    ///Adds a new source node to the set of nodes to be processed.
440 440
    ///
441 441
    ///\pre The stack must be empty. Otherwise the algorithm gives
442 442
    ///wrong results. (One of the outgoing arcs of all the source nodes
443 443
    ///except for the last one will not be visited and distances will
444 444
    ///also be wrong.)
445 445
    void addSource(Node s)
446 446
    {
447 447
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
448 448
      if(!(*_reached)[s])
449 449
        {
450 450
          _reached->set(s,true);
451 451
          _pred->set(s,INVALID);
452 452
          OutArcIt e(*G,s);
453 453
          if(e!=INVALID) {
454 454
            _stack[++_stack_head]=e;
455 455
            _dist->set(s,_stack_head);
456 456
          }
457 457
          else {
458 458
            _processed->set(s,true);
459 459
            _dist->set(s,0);
460 460
          }
461 461
        }
462 462
    }
463 463

	
464 464
    ///Processes the next arc.
465 465

	
466 466
    ///Processes the next arc.
467 467
    ///
468 468
    ///\return The processed arc.
469 469
    ///
470 470
    ///\pre The stack must not be empty.
471 471
    Arc processNextArc()
472 472
    {
473 473
      Node m;
474 474
      Arc e=_stack[_stack_head];
475 475
      if(!(*_reached)[m=G->target(e)]) {
476 476
        _pred->set(m,e);
477 477
        _reached->set(m,true);
478 478
        ++_stack_head;
479 479
        _stack[_stack_head] = OutArcIt(*G, m);
480 480
        _dist->set(m,_stack_head);
481 481
      }
482 482
      else {
483 483
        m=G->source(e);
484 484
        ++_stack[_stack_head];
485 485
      }
486 486
      while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
487 487
        _processed->set(m,true);
488 488
        --_stack_head;
489 489
        if(_stack_head>=0) {
490 490
          m=G->source(_stack[_stack_head]);
491 491
          ++_stack[_stack_head];
492 492
        }
493 493
      }
494 494
      return e;
495 495
    }
496 496

	
497 497
    ///Next arc to be processed.
498 498

	
499 499
    ///Next arc to be processed.
500 500
    ///
501 501
    ///\return The next arc to be processed or \c INVALID if the stack
502 502
    ///is empty.
503 503
    OutArcIt nextArc() const
504 504
    {
505 505
      return _stack_head>=0?_stack[_stack_head]:INVALID;
506 506
    }
507 507

	
508 508
    ///Returns \c false if there are nodes to be processed.
509 509

	
510 510
    ///Returns \c false if there are nodes to be processed
511 511
    ///in the queue (stack).
512 512
    bool emptyQueue() const { return _stack_head<0; }
513 513

	
514 514
    ///Returns the number of the nodes to be processed.
515 515

	
516 516
    ///Returns the number of the nodes to be processed
517 517
    ///in the queue (stack).
518 518
    int queueSize() const { return _stack_head+1; }
519 519

	
520 520
    ///Executes the algorithm.
521 521

	
522 522
    ///Executes the algorithm.
523 523
    ///
524 524
    ///This method runs the %DFS algorithm from the root node
525 525
    ///in order to compute the DFS path to each node.
526 526
    ///
527 527
    /// The algorithm computes
528 528
    ///- the %DFS tree,
529 529
    ///- the distance of each node from the root in the %DFS tree.
530 530
    ///
531 531
    ///\pre init() must be called and a root node should be
532 532
    ///added with addSource() before using this function.
533 533
    ///
534 534
    ///\note <tt>d.start()</tt> is just a shortcut of the following code.
535 535
    ///\code
536 536
    ///  while ( !d.emptyQueue() ) {
537 537
    ///    d.processNextArc();
538 538
    ///  }
539 539
    ///\endcode
540 540
    void start()
541 541
    {
542 542
      while ( !emptyQueue() ) processNextArc();
543 543
    }
544 544

	
545 545
    ///Executes the algorithm until the given target node is reached.
546 546

	
547 547
    ///Executes the algorithm until the given target node is reached.
548 548
    ///
549 549
    ///This method runs the %DFS algorithm from the root node
550 550
    ///in order to compute the DFS path to \c t.
551 551
    ///
552 552
    ///The algorithm computes
553 553
    ///- the %DFS path to \c t,
554 554
    ///- the distance of \c t from the root in the %DFS tree.
555 555
    ///
556 556
    ///\pre init() must be called and a root node should be
557 557
    ///added with addSource() before using this function.
558 558
    void start(Node t)
559 559
    {
560 560
      while ( !emptyQueue() && !(*_reached)[t] )
561 561
        processNextArc();
562 562
    }
563 563

	
564 564
    ///Executes the algorithm until a condition is met.
565 565

	
566 566
    ///Executes the algorithm until a condition is met.
567 567
    ///
568 568
    ///This method runs the %DFS algorithm from the root node
569 569
    ///until an arc \c a with <tt>am[a]</tt> true is found.
570 570
    ///
571 571
    ///\param am A \c bool (or convertible) arc map. The algorithm
572 572
    ///will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
573 573
    ///
574 574
    ///\return The reached arc \c a with <tt>am[a]</tt> true or
575 575
    ///\c INVALID if no such arc was found.
576 576
    ///
577 577
    ///\pre init() must be called and a root node should be
578 578
    ///added with addSource() before using this function.
579 579
    ///
580 580
    ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
581 581
    ///not a node map.
582 582
    template<class ArcBoolMap>
583 583
    Arc start(const ArcBoolMap &am)
584 584
    {
585 585
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
586 586
        processNextArc();
587 587
      return emptyQueue() ? INVALID : _stack[_stack_head];
588 588
    }
589 589

	
590 590
    ///Runs the algorithm from the given source node.
591 591

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

	
611 611
    ///Finds the %DFS path between \c s and \c t.
612 612

	
613 613
    ///This method runs the %DFS algorithm from node \c s
614 614
    ///in order to compute the DFS path to node \c t
615 615
    ///(it stops searching when \c t is processed)
616 616
    ///
617 617
    ///\return \c true if \c t is reachable form \c s.
618 618
    ///
619 619
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is
620 620
    ///just a shortcut of the following code.
621 621
    ///\code
622 622
    ///  d.init();
623 623
    ///  d.addSource(s);
624 624
    ///  d.start(t);
625 625
    ///\endcode
626 626
    bool run(Node s,Node t) {
627 627
      init();
628 628
      addSource(s);
629 629
      start(t);
630 630
      return reached(t);
631 631
    }
632 632

	
633 633
    ///Runs the algorithm to visit all nodes in the digraph.
634 634

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

	
662 662
    ///@}
663 663

	
664 664
    ///\name Query Functions
665 665
    ///The results of the DFS algorithm can be obtained using these
666 666
    ///functions.\n
667 667
    ///Either \ref run(Node) "run()" or \ref start() should be called
668 668
    ///before using them.
669 669

	
670 670
    ///@{
671 671

	
672 672
    ///The DFS path to a node.
673 673

	
674 674
    ///Returns the DFS path to a node.
675 675
    ///
676 676
    ///\warning \c t should be reached from the root(s).
677 677
    ///
678 678
    ///\pre Either \ref run(Node) "run()" or \ref init()
679 679
    ///must be called before using this function.
680 680
    Path path(Node t) const { return Path(*G, *_pred, t); }
681 681

	
682 682
    ///The distance of a node from the root(s).
683 683

	
684 684
    ///Returns the distance of a node from the root(s).
685 685
    ///
686 686
    ///\warning If node \c v is not reached from the root(s), then
687 687
    ///the return value of this function is undefined.
688 688
    ///
689 689
    ///\pre Either \ref run(Node) "run()" or \ref init()
690 690
    ///must be called before using this function.
691 691
    int dist(Node v) const { return (*_dist)[v]; }
692 692

	
693 693
    ///Returns the 'previous arc' of the %DFS tree for a node.
694 694

	
695 695
    ///This function returns the 'previous arc' of the %DFS tree for the
696 696
    ///node \c v, i.e. it returns the last arc of a %DFS path from a
697 697
    ///root to \c v. It is \c INVALID if \c v is not reached from the
698 698
    ///root(s) or if \c v is a root.
699 699
    ///
700 700
    ///The %DFS tree used here is equal to the %DFS tree used in
701 701
    ///\ref predNode().
702 702
    ///
703 703
    ///\pre Either \ref run(Node) "run()" or \ref init()
704 704
    ///must be called before using this function.
705 705
    Arc predArc(Node v) const { return (*_pred)[v];}
706 706

	
707 707
    ///Returns the 'previous node' of the %DFS tree.
708 708

	
709 709
    ///This function returns the 'previous node' of the %DFS
710 710
    ///tree for the node \c v, i.e. it returns the last but one node
711 711
    ///from a %DFS path from a root to \c v. It is \c INVALID
712 712
    ///if \c v is not reached from the root(s) or if \c v is a root.
713 713
    ///
714 714
    ///The %DFS tree used here is equal to the %DFS tree used in
715 715
    ///\ref predArc().
716 716
    ///
717 717
    ///\pre Either \ref run(Node) "run()" or \ref init()
718 718
    ///must be called before using this function.
719 719
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
720 720
                                  G->source((*_pred)[v]); }
721 721

	
722 722
    ///\brief Returns a const reference to the node map that stores the
723 723
    ///distances of the nodes.
724 724
    ///
725 725
    ///Returns a const reference to the node map that stores the
726 726
    ///distances of the nodes calculated by the algorithm.
727 727
    ///
728 728
    ///\pre Either \ref run(Node) "run()" or \ref init()
729 729
    ///must be called before using this function.
730 730
    const DistMap &distMap() const { return *_dist;}
731 731

	
732 732
    ///\brief Returns a const reference to the node map that stores the
733 733
    ///predecessor arcs.
734 734
    ///
735 735
    ///Returns a const reference to the node map that stores the predecessor
736 736
    ///arcs, which form the DFS tree.
737 737
    ///
738 738
    ///\pre Either \ref run(Node) "run()" or \ref init()
739 739
    ///must be called before using this function.
740 740
    const PredMap &predMap() const { return *_pred;}
741 741

	
742 742
    ///Checks if a node is reached from the root(s).
743 743

	
744 744
    ///Returns \c true if \c v is reached from the root(s).
745 745
    ///
746 746
    ///\pre Either \ref run(Node) "run()" or \ref init()
747 747
    ///must be called before using this function.
748 748
    bool reached(Node v) const { return (*_reached)[v]; }
749 749

	
750 750
    ///@}
751 751
  };
752 752

	
753 753
  ///Default traits class of dfs() function.
754 754

	
755 755
  ///Default traits class of dfs() function.
756 756
  ///\tparam GR Digraph type.
757 757
  template<class GR>
758 758
  struct DfsWizardDefaultTraits
759 759
  {
760 760
    ///The type of the digraph the algorithm runs on.
761 761
    typedef GR Digraph;
762 762

	
763 763
    ///\brief The type of the map that stores the predecessor
764 764
    ///arcs of the %DFS paths.
765 765
    ///
766 766
    ///The type of the map that stores the predecessor
767 767
    ///arcs of the %DFS paths.
768 768
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
769 769
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
770 770
    ///Instantiates a PredMap.
771 771

	
772 772
    ///This function instantiates a PredMap.
773 773
    ///\param g is the digraph, to which we would like to define the
774 774
    ///PredMap.
775 775
    static PredMap *createPredMap(const Digraph &g)
776 776
    {
777 777
      return new PredMap(g);
778 778
    }
779 779

	
780 780
    ///The type of the map that indicates which nodes are processed.
781 781

	
782 782
    ///The type of the map that indicates which nodes are processed.
783 783
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
784 784
    ///By default it is a NullMap.
785 785
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
786 786
    ///Instantiates a ProcessedMap.
787 787

	
788 788
    ///This function instantiates a ProcessedMap.
789 789
    ///\param g is the digraph, to which
790 790
    ///we would like to define the ProcessedMap.
791 791
#ifdef DOXYGEN
792 792
    static ProcessedMap *createProcessedMap(const Digraph &g)
793 793
#else
794 794
    static ProcessedMap *createProcessedMap(const Digraph &)
795 795
#endif
796 796
    {
797 797
      return new ProcessedMap();
798 798
    }
799 799

	
800 800
    ///The type of the map that indicates which nodes are reached.
801 801

	
802 802
    ///The type of the map that indicates which nodes are reached.
803 803
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
804 804
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
805 805
    ///Instantiates a ReachedMap.
806 806

	
807 807
    ///This function instantiates a ReachedMap.
808 808
    ///\param g is the digraph, to which
809 809
    ///we would like to define the ReachedMap.
810 810
    static ReachedMap *createReachedMap(const Digraph &g)
811 811
    {
812 812
      return new ReachedMap(g);
813 813
    }
814 814

	
815 815
    ///The type of the map that stores the distances of the nodes.
816 816

	
817 817
    ///The type of the map that stores the distances of the nodes.
818 818
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
819 819
    typedef typename Digraph::template NodeMap<int> DistMap;
820 820
    ///Instantiates a DistMap.
821 821

	
822 822
    ///This function instantiates a DistMap.
823 823
    ///\param g is the digraph, to which we would like to define
824 824
    ///the DistMap
825 825
    static DistMap *createDistMap(const Digraph &g)
826 826
    {
827 827
      return new DistMap(g);
828 828
    }
829 829

	
830 830
    ///The type of the DFS paths.
831 831

	
832 832
    ///The type of the DFS paths.
833 833
    ///It must meet the \ref concepts::Path "Path" concept.
834 834
    typedef lemon::Path<Digraph> Path;
835 835
  };
836 836

	
837 837
  /// Default traits class used by DfsWizard
838 838

	
839 839
  /// To make it easier to use Dfs algorithm
840 840
  /// we have created a wizard class.
841 841
  /// This \ref DfsWizard class needs default traits,
842 842
  /// as well as the \ref Dfs class.
843 843
  /// The \ref DfsWizardBase is a class to be the default traits of the
844 844
  /// \ref DfsWizard class.
845 845
  template<class GR>
846 846
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
847 847
  {
848 848

	
849 849
    typedef DfsWizardDefaultTraits<GR> Base;
850 850
  protected:
851 851
    //The type of the nodes in the digraph.
852 852
    typedef typename Base::Digraph::Node Node;
853 853

	
854 854
    //Pointer to the digraph the algorithm runs on.
855 855
    void *_g;
856 856
    //Pointer to the map of reached nodes.
857 857
    void *_reached;
858 858
    //Pointer to the map of processed nodes.
859 859
    void *_processed;
860 860
    //Pointer to the map of predecessors arcs.
861 861
    void *_pred;
862 862
    //Pointer to the map of distances.
863 863
    void *_dist;
864 864
    //Pointer to the DFS path to the target node.
865 865
    void *_path;
866 866
    //Pointer to the distance of the target node.
867 867
    int *_di;
868 868

	
869 869
    public:
870 870
    /// Constructor.
871 871

	
872 872
    /// This constructor does not require parameters, therefore it initiates
873 873
    /// all of the attributes to \c 0.
874 874
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
875 875
                      _dist(0), _path(0), _di(0) {}
876 876

	
877 877
    /// Constructor.
878 878

	
879 879
    /// This constructor requires one parameter,
880 880
    /// others are initiated to \c 0.
881 881
    /// \param g The digraph the algorithm runs on.
882 882
    DfsWizardBase(const GR &g) :
883 883
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
884 884
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
885 885

	
886 886
  };
887 887

	
888 888
  /// Auxiliary class for the function-type interface of DFS algorithm.
889 889

	
890 890
  /// This auxiliary class is created to implement the
891 891
  /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
892 892
  /// It does not have own \ref run(Node) "run()" method, it uses the
893 893
  /// functions and features of the plain \ref Dfs.
894 894
  ///
895 895
  /// This class should only be used through the \ref dfs() function,
896 896
  /// which makes it easier to use the algorithm.
897 897
  template<class TR>
898 898
  class DfsWizard : public TR
899 899
  {
900 900
    typedef TR Base;
901 901

	
902 902
    ///The type of the digraph the algorithm runs on.
903 903
    typedef typename TR::Digraph Digraph;
904 904

	
905 905
    typedef typename Digraph::Node Node;
906 906
    typedef typename Digraph::NodeIt NodeIt;
907 907
    typedef typename Digraph::Arc Arc;
908 908
    typedef typename Digraph::OutArcIt OutArcIt;
909 909

	
910 910
    ///\brief The type of the map that stores the predecessor
911 911
    ///arcs of the DFS paths.
912 912
    typedef typename TR::PredMap PredMap;
913 913
    ///\brief The type of the map that stores the distances of the nodes.
914 914
    typedef typename TR::DistMap DistMap;
915 915
    ///\brief The type of the map that indicates which nodes are reached.
916 916
    typedef typename TR::ReachedMap ReachedMap;
917 917
    ///\brief The type of the map that indicates which nodes are processed.
918 918
    typedef typename TR::ProcessedMap ProcessedMap;
919 919
    ///The type of the DFS paths
920 920
    typedef typename TR::Path Path;
921 921

	
922 922
  public:
923 923

	
924 924
    /// Constructor.
925 925
    DfsWizard() : TR() {}
926 926

	
927 927
    /// Constructor that requires parameters.
928 928

	
929 929
    /// Constructor that requires parameters.
930 930
    /// These parameters will be the default values for the traits class.
931 931
    /// \param g The digraph the algorithm runs on.
932 932
    DfsWizard(const Digraph &g) :
933 933
      TR(g) {}
934 934

	
935 935
    ///Copy constructor
936 936
    DfsWizard(const TR &b) : TR(b) {}
937 937

	
938 938
    ~DfsWizard() {}
939 939

	
940 940
    ///Runs DFS algorithm from the given source node.
941 941

	
942 942
    ///This method runs DFS algorithm from node \c s
943 943
    ///in order to compute the DFS path to each node.
944 944
    void run(Node s)
945 945
    {
946 946
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
947 947
      if (Base::_pred)
948 948
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
949 949
      if (Base::_dist)
950 950
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
951 951
      if (Base::_reached)
952 952
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
953 953
      if (Base::_processed)
954 954
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
955 955
      if (s!=INVALID)
956 956
        alg.run(s);
957 957
      else
958 958
        alg.run();
959 959
    }
960 960

	
961 961
    ///Finds the DFS path between \c s and \c t.
962 962

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

	
987 987
    ///Runs DFS algorithm to visit all nodes in the digraph.
988 988

	
989 989
    ///This method runs DFS algorithm in order to compute
990 990
    ///the DFS path to each node.
991 991
    void run()
992 992
    {
993 993
      run(INVALID);
994 994
    }
995 995

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

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

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

	
1050 1050
    template<class T>
1051 1051
    struct SetProcessedMapBase : public Base {
1052 1052
      typedef T ProcessedMap;
1053 1053
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1054 1054
      SetProcessedMapBase(const TR &b) : TR(b) {}
1055 1055
    };
1056 1056
    ///\brief \ref named-func-param "Named parameter"
1057 1057
    ///for setting ProcessedMap object.
1058 1058
    ///
1059 1059
    /// \ref named-func-param "Named parameter"
1060 1060
    ///for setting ProcessedMap object.
1061 1061
    template<class T>
1062 1062
    DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1063 1063
    {
1064 1064
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1065 1065
      return DfsWizard<SetProcessedMapBase<T> >(*this);
1066 1066
    }
1067 1067

	
1068 1068
    template<class T>
1069 1069
    struct SetPathBase : public Base {
1070 1070
      typedef T Path;
1071 1071
      SetPathBase(const TR &b) : TR(b) {}
1072 1072
    };
1073 1073
    ///\brief \ref named-func-param "Named parameter"
1074 1074
    ///for getting the DFS path to the target node.
1075 1075
    ///
1076 1076
    ///\ref named-func-param "Named parameter"
1077 1077
    ///for getting the DFS path to the target node.
1078 1078
    template<class T>
1079 1079
    DfsWizard<SetPathBase<T> > path(const T &t)
1080 1080
    {
1081 1081
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1082 1082
      return DfsWizard<SetPathBase<T> >(*this);
1083 1083
    }
1084 1084

	
1085 1085
    ///\brief \ref named-func-param "Named parameter"
1086 1086
    ///for getting the distance of the target node.
1087 1087
    ///
1088 1088
    ///\ref named-func-param "Named parameter"
1089 1089
    ///for getting the distance of the target node.
1090 1090
    DfsWizard dist(const int &d)
1091 1091
    {
1092 1092
      Base::_di=const_cast<int*>(&d);
1093 1093
      return *this;
1094 1094
    }
1095 1095

	
1096 1096
  };
1097 1097

	
1098 1098
  ///Function-type interface for DFS algorithm.
1099 1099

	
1100 1100
  ///\ingroup search
1101 1101
  ///Function-type interface for DFS algorithm.
1102 1102
  ///
1103 1103
  ///This function also has several \ref named-func-param "named parameters",
1104 1104
  ///they are declared as the members of class \ref DfsWizard.
1105 1105
  ///The following examples show how to use these parameters.
1106 1106
  ///\code
1107 1107
  ///  // Compute the DFS tree
1108 1108
  ///  dfs(g).predMap(preds).distMap(dists).run(s);
1109 1109
  ///
1110 1110
  ///  // Compute the DFS path from s to t
1111 1111
  ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
1112 1112
  ///\endcode
1113 1113
  ///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()"
1114 1114
  ///to the end of the parameter list.
1115 1115
  ///\sa DfsWizard
1116 1116
  ///\sa Dfs
1117 1117
  template<class GR>
1118 1118
  DfsWizard<DfsWizardBase<GR> >
1119 1119
  dfs(const GR &digraph)
1120 1120
  {
1121 1121
    return DfsWizard<DfsWizardBase<GR> >(digraph);
1122 1122
  }
1123 1123

	
1124 1124
#ifdef DOXYGEN
1125 1125
  /// \brief Visitor class for DFS.
1126 1126
  ///
1127 1127
  /// This class defines the interface of the DfsVisit events, and
1128 1128
  /// it could be the base of a real visitor class.
1129 1129
  template <typename GR>
1130 1130
  struct DfsVisitor {
1131 1131
    typedef GR Digraph;
1132 1132
    typedef typename Digraph::Arc Arc;
1133 1133
    typedef typename Digraph::Node Node;
1134 1134
    /// \brief Called for the source node of the DFS.
1135 1135
    ///
1136 1136
    /// This function is called for the source node of the DFS.
1137 1137
    void start(const Node& node) {}
1138 1138
    /// \brief Called when the source node is leaved.
1139 1139
    ///
1140 1140
    /// This function is called when the source node is leaved.
1141 1141
    void stop(const Node& node) {}
1142 1142
    /// \brief Called when a node is reached first time.
1143 1143
    ///
1144 1144
    /// This function is called when a node is reached first time.
1145 1145
    void reach(const Node& node) {}
1146 1146
    /// \brief Called when an arc reaches a new node.
1147 1147
    ///
1148 1148
    /// This function is called when the DFS finds an arc whose target node
1149 1149
    /// is not reached yet.
1150 1150
    void discover(const Arc& arc) {}
1151 1151
    /// \brief Called when an arc is examined but its target node is
1152 1152
    /// already discovered.
1153 1153
    ///
1154 1154
    /// This function is called when an arc is examined but its target node is
1155 1155
    /// already discovered.
1156 1156
    void examine(const Arc& arc) {}
1157 1157
    /// \brief Called when the DFS steps back from a node.
1158 1158
    ///
1159 1159
    /// This function is called when the DFS steps back from a node.
1160 1160
    void leave(const Node& node) {}
1161 1161
    /// \brief Called when the DFS steps back on an arc.
1162 1162
    ///
1163 1163
    /// This function is called when the DFS steps back on an arc.
1164 1164
    void backtrack(const Arc& arc) {}
1165 1165
  };
1166 1166
#else
1167 1167
  template <typename GR>
1168 1168
  struct DfsVisitor {
1169 1169
    typedef GR Digraph;
1170 1170
    typedef typename Digraph::Arc Arc;
1171 1171
    typedef typename Digraph::Node Node;
1172 1172
    void start(const Node&) {}
1173 1173
    void stop(const Node&) {}
1174 1174
    void reach(const Node&) {}
1175 1175
    void discover(const Arc&) {}
1176 1176
    void examine(const Arc&) {}
1177 1177
    void leave(const Node&) {}
1178 1178
    void backtrack(const Arc&) {}
1179 1179

	
1180 1180
    template <typename _Visitor>
1181 1181
    struct Constraints {
1182 1182
      void constraints() {
1183 1183
        Arc arc;
1184 1184
        Node node;
1185 1185
        visitor.start(node);
1186 1186
        visitor.stop(arc);
1187 1187
        visitor.reach(node);
1188 1188
        visitor.discover(arc);
1189 1189
        visitor.examine(arc);
1190 1190
        visitor.leave(node);
1191 1191
        visitor.backtrack(arc);
1192 1192
      }
1193 1193
      _Visitor& visitor;
1194
      Constraints() {}
1194 1195
    };
1195 1196
  };
1196 1197
#endif
1197 1198

	
1198 1199
  /// \brief Default traits class of DfsVisit class.
1199 1200
  ///
1200 1201
  /// Default traits class of DfsVisit class.
1201 1202
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1202 1203
  template<class GR>
1203 1204
  struct DfsVisitDefaultTraits {
1204 1205

	
1205 1206
    /// \brief The type of the digraph the algorithm runs on.
1206 1207
    typedef GR Digraph;
1207 1208

	
1208 1209
    /// \brief The type of the map that indicates which nodes are reached.
1209 1210
    ///
1210 1211
    /// The type of the map that indicates which nodes are reached.
1211 1212
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1212 1213
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1213 1214

	
1214 1215
    /// \brief Instantiates a ReachedMap.
1215 1216
    ///
1216 1217
    /// This function instantiates a ReachedMap.
1217 1218
    /// \param digraph is the digraph, to which
1218 1219
    /// we would like to define the ReachedMap.
1219 1220
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1220 1221
      return new ReachedMap(digraph);
1221 1222
    }
1222 1223

	
1223 1224
  };
1224 1225

	
1225 1226
  /// \ingroup search
1226 1227
  ///
1227 1228
  /// \brief DFS algorithm class with visitor interface.
1228 1229
  ///
1229 1230
  /// This class provides an efficient implementation of the DFS algorithm
1230 1231
  /// with visitor interface.
1231 1232
  ///
1232 1233
  /// The DfsVisit class provides an alternative interface to the Dfs
1233 1234
  /// class. It works with callback mechanism, the DfsVisit object calls
1234 1235
  /// the member functions of the \c Visitor class on every DFS event.
1235 1236
  ///
1236 1237
  /// This interface of the DFS algorithm should be used in special cases
1237 1238
  /// when extra actions have to be performed in connection with certain
1238 1239
  /// events of the DFS algorithm. Otherwise consider to use Dfs or dfs()
1239 1240
  /// instead.
1240 1241
  ///
1241 1242
  /// \tparam GR The type of the digraph the algorithm runs on.
1242 1243
  /// The default type is \ref ListDigraph.
1243 1244
  /// The value of GR is not used directly by \ref DfsVisit,
1244 1245
  /// it is only passed to \ref DfsVisitDefaultTraits.
1245 1246
  /// \tparam VS The Visitor type that is used by the algorithm.
1246 1247
  /// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which
1247 1248
  /// does not observe the DFS events. If you want to observe the DFS
1248 1249
  /// events, you should implement your own visitor class.
1249 1250
  /// \tparam TR Traits class to set various data types used by the
1250 1251
  /// algorithm. The default traits class is
1251 1252
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<GR>".
1252 1253
  /// See \ref DfsVisitDefaultTraits for the documentation of
1253 1254
  /// a DFS visit traits class.
1254 1255
#ifdef DOXYGEN
1255 1256
  template <typename GR, typename VS, typename TR>
1256 1257
#else
1257 1258
  template <typename GR = ListDigraph,
1258 1259
            typename VS = DfsVisitor<GR>,
1259 1260
            typename TR = DfsVisitDefaultTraits<GR> >
1260 1261
#endif
1261 1262
  class DfsVisit {
1262 1263
  public:
1263 1264

	
1264 1265
    ///The traits class.
1265 1266
    typedef TR Traits;
1266 1267

	
1267 1268
    ///The type of the digraph the algorithm runs on.
1268 1269
    typedef typename Traits::Digraph Digraph;
1269 1270

	
1270 1271
    ///The visitor type used by the algorithm.
1271 1272
    typedef VS Visitor;
1272 1273

	
1273 1274
    ///The type of the map that indicates which nodes are reached.
1274 1275
    typedef typename Traits::ReachedMap ReachedMap;
1275 1276

	
1276 1277
  private:
1277 1278

	
1278 1279
    typedef typename Digraph::Node Node;
1279 1280
    typedef typename Digraph::NodeIt NodeIt;
1280 1281
    typedef typename Digraph::Arc Arc;
1281 1282
    typedef typename Digraph::OutArcIt OutArcIt;
1282 1283

	
1283 1284
    //Pointer to the underlying digraph.
1284 1285
    const Digraph *_digraph;
1285 1286
    //Pointer to the visitor object.
1286 1287
    Visitor *_visitor;
1287 1288
    //Pointer to the map of reached status of the nodes.
1288 1289
    ReachedMap *_reached;
1289 1290
    //Indicates if _reached is locally allocated (true) or not.
1290 1291
    bool local_reached;
1291 1292

	
1292 1293
    std::vector<typename Digraph::Arc> _stack;
1293 1294
    int _stack_head;
1294 1295

	
1295 1296
    //Creates the maps if necessary.
1296 1297
    void create_maps() {
1297 1298
      if(!_reached) {
1298 1299
        local_reached = true;
1299 1300
        _reached = Traits::createReachedMap(*_digraph);
1300 1301
      }
1301 1302
    }
1302 1303

	
1303 1304
  protected:
1304 1305

	
1305 1306
    DfsVisit() {}
1306 1307

	
1307 1308
  public:
1308 1309

	
1309 1310
    typedef DfsVisit Create;
1310 1311

	
1311 1312
    /// \name Named Template Parameters
1312 1313

	
1313 1314
    ///@{
1314 1315
    template <class T>
1315 1316
    struct SetReachedMapTraits : public Traits {
1316 1317
      typedef T ReachedMap;
1317 1318
      static ReachedMap *createReachedMap(const Digraph &digraph) {
1318 1319
        LEMON_ASSERT(false, "ReachedMap is not initialized");
1319 1320
        return 0; // ignore warnings
1320 1321
      }
1321 1322
    };
1322 1323
    /// \brief \ref named-templ-param "Named parameter" for setting
1323 1324
    /// ReachedMap type.
1324 1325
    ///
1325 1326
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
1326 1327
    template <class T>
1327 1328
    struct SetReachedMap : public DfsVisit< Digraph, Visitor,
1328 1329
                                            SetReachedMapTraits<T> > {
1329 1330
      typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
1330 1331
    };
1331 1332
    ///@}
1332 1333

	
1333 1334
  public:
1334 1335

	
1335 1336
    /// \brief Constructor.
1336 1337
    ///
1337 1338
    /// Constructor.
1338 1339
    ///
1339 1340
    /// \param digraph The digraph the algorithm runs on.
1340 1341
    /// \param visitor The visitor object of the algorithm.
1341 1342
    DfsVisit(const Digraph& digraph, Visitor& visitor)
1342 1343
      : _digraph(&digraph), _visitor(&visitor),
1343 1344
        _reached(0), local_reached(false) {}
1344 1345

	
1345 1346
    /// \brief Destructor.
1346 1347
    ~DfsVisit() {
1347 1348
      if(local_reached) delete _reached;
1348 1349
    }
1349 1350

	
1350 1351
    /// \brief Sets the map that indicates which nodes are reached.
1351 1352
    ///
1352 1353
    /// Sets the map that indicates which nodes are reached.
1353 1354
    /// If you don't use this function before calling \ref run(Node) "run()"
1354 1355
    /// or \ref init(), an instance will be allocated automatically.
1355 1356
    /// The destructor deallocates this automatically allocated map,
1356 1357
    /// of course.
1357 1358
    /// \return <tt> (*this) </tt>
1358 1359
    DfsVisit &reachedMap(ReachedMap &m) {
1359 1360
      if(local_reached) {
1360 1361
        delete _reached;
1361 1362
        local_reached=false;
1362 1363
      }
1363 1364
      _reached = &m;
1364 1365
      return *this;
1365 1366
    }
1366 1367

	
1367 1368
  public:
1368 1369

	
1369 1370
    /// \name Execution Control
1370 1371
    /// The simplest way to execute the DFS algorithm is to use one of the
1371 1372
    /// member functions called \ref run(Node) "run()".\n
1372 1373
    /// If you need more control on the execution, first you have to call
1373 1374
    /// \ref init(), then you can add a source node with \ref addSource()
1374 1375
    /// and perform the actual computation with \ref start().
1375 1376
    /// This procedure can be repeated if there are nodes that have not
1376 1377
    /// been reached.
1377 1378

	
1378 1379
    /// @{
1379 1380

	
1380 1381
    /// \brief Initializes the internal data structures.
1381 1382
    ///
1382 1383
    /// Initializes the internal data structures.
1383 1384
    void init() {
1384 1385
      create_maps();
1385 1386
      _stack.resize(countNodes(*_digraph));
1386 1387
      _stack_head = -1;
1387 1388
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1388 1389
        _reached->set(u, false);
1389 1390
      }
1390 1391
    }
1391 1392

	
1392 1393
    /// \brief Adds a new source node.
1393 1394
    ///
1394 1395
    /// Adds a new source node to the set of nodes to be processed.
1395 1396
    ///
1396 1397
    /// \pre The stack must be empty. Otherwise the algorithm gives
1397 1398
    /// wrong results. (One of the outgoing arcs of all the source nodes
1398 1399
    /// except for the last one will not be visited and distances will
1399 1400
    /// also be wrong.)
1400 1401
    void addSource(Node s)
1401 1402
    {
1402 1403
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
1403 1404
      if(!(*_reached)[s]) {
1404 1405
          _reached->set(s,true);
1405 1406
          _visitor->start(s);
1406 1407
          _visitor->reach(s);
1407 1408
          Arc e;
1408 1409
          _digraph->firstOut(e, s);
1409 1410
          if (e != INVALID) {
1410 1411
            _stack[++_stack_head] = e;
1411 1412
          } else {
1412 1413
            _visitor->leave(s);
1413 1414
            _visitor->stop(s);
1414 1415
          }
1415 1416
        }
1416 1417
    }
1417 1418

	
1418 1419
    /// \brief Processes the next arc.
1419 1420
    ///
1420 1421
    /// Processes the next arc.
1421 1422
    ///
1422 1423
    /// \return The processed arc.
1423 1424
    ///
1424 1425
    /// \pre The stack must not be empty.
1425 1426
    Arc processNextArc() {
1426 1427
      Arc e = _stack[_stack_head];
1427 1428
      Node m = _digraph->target(e);
1428 1429
      if(!(*_reached)[m]) {
1429 1430
        _visitor->discover(e);
1430 1431
        _visitor->reach(m);
1431 1432
        _reached->set(m, true);
1432 1433
        _digraph->firstOut(_stack[++_stack_head], m);
1433 1434
      } else {
1434 1435
        _visitor->examine(e);
1435 1436
        m = _digraph->source(e);
1436 1437
        _digraph->nextOut(_stack[_stack_head]);
1437 1438
      }
1438 1439
      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
1439 1440
        _visitor->leave(m);
1440 1441
        --_stack_head;
1441 1442
        if (_stack_head >= 0) {
1442 1443
          _visitor->backtrack(_stack[_stack_head]);
1443 1444
          m = _digraph->source(_stack[_stack_head]);
1444 1445
          _digraph->nextOut(_stack[_stack_head]);
1445 1446
        } else {
1446 1447
          _visitor->stop(m);
1447 1448
        }
1448 1449
      }
1449 1450
      return e;
1450 1451
    }
1451 1452

	
1452 1453
    /// \brief Next arc to be processed.
1453 1454
    ///
1454 1455
    /// Next arc to be processed.
1455 1456
    ///
1456 1457
    /// \return The next arc to be processed or INVALID if the stack is
1457 1458
    /// empty.
1458 1459
    Arc nextArc() const {
1459 1460
      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
1460 1461
    }
1461 1462

	
1462 1463
    /// \brief Returns \c false if there are nodes
1463 1464
    /// to be processed.
1464 1465
    ///
1465 1466
    /// Returns \c false if there are nodes
1466 1467
    /// to be processed in the queue (stack).
1467 1468
    bool emptyQueue() const { return _stack_head < 0; }
1468 1469

	
1469 1470
    /// \brief Returns the number of the nodes to be processed.
1470 1471
    ///
1471 1472
    /// Returns the number of the nodes to be processed in the queue (stack).
1472 1473
    int queueSize() const { return _stack_head + 1; }
1473 1474

	
1474 1475
    /// \brief Executes the algorithm.
1475 1476
    ///
1476 1477
    /// Executes the algorithm.
1477 1478
    ///
1478 1479
    /// This method runs the %DFS algorithm from the root node
1479 1480
    /// in order to compute the %DFS path to each node.
1480 1481
    ///
1481 1482
    /// The algorithm computes
1482 1483
    /// - the %DFS tree,
1483 1484
    /// - the distance of each node from the root in the %DFS tree.
1484 1485
    ///
1485 1486
    /// \pre init() must be called and a root node should be
1486 1487
    /// added with addSource() before using this function.
1487 1488
    ///
1488 1489
    /// \note <tt>d.start()</tt> is just a shortcut of the following code.
1489 1490
    /// \code
1490 1491
    ///   while ( !d.emptyQueue() ) {
1491 1492
    ///     d.processNextArc();
1492 1493
    ///   }
1493 1494
    /// \endcode
1494 1495
    void start() {
1495 1496
      while ( !emptyQueue() ) processNextArc();
1496 1497
    }
1497 1498

	
1498 1499
    /// \brief Executes the algorithm until the given target node is reached.
1499 1500
    ///
1500 1501
    /// Executes the algorithm until the given target node is reached.
1501 1502
    ///
1502 1503
    /// This method runs the %DFS algorithm from the root node
1503 1504
    /// in order to compute the DFS path to \c t.
1504 1505
    ///
1505 1506
    /// The algorithm computes
1506 1507
    /// - the %DFS path to \c t,
1507 1508
    /// - the distance of \c t from the root in the %DFS tree.
1508 1509
    ///
1509 1510
    /// \pre init() must be called and a root node should be added
1510 1511
    /// with addSource() before using this function.
1511 1512
    void start(Node t) {
1512 1513
      while ( !emptyQueue() && !(*_reached)[t] )
1513 1514
        processNextArc();
1514 1515
    }
1515 1516

	
1516 1517
    /// \brief Executes the algorithm until a condition is met.
1517 1518
    ///
1518 1519
    /// Executes the algorithm until a condition is met.
1519 1520
    ///
1520 1521
    /// This method runs the %DFS algorithm from the root node
1521 1522
    /// until an arc \c a with <tt>am[a]</tt> true is found.
1522 1523
    ///
1523 1524
    /// \param am A \c bool (or convertible) arc map. The algorithm
1524 1525
    /// will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
1525 1526
    ///
1526 1527
    /// \return The reached arc \c a with <tt>am[a]</tt> true or
1527 1528
    /// \c INVALID if no such arc was found.
1528 1529
    ///
1529 1530
    /// \pre init() must be called and a root node should be added
1530 1531
    /// with addSource() before using this function.
1531 1532
    ///
1532 1533
    /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
1533 1534
    /// not a node map.
1534 1535
    template <typename AM>
1535 1536
    Arc start(const AM &am) {
1536 1537
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
1537 1538
        processNextArc();
1538 1539
      return emptyQueue() ? INVALID : _stack[_stack_head];
1539 1540
    }
1540 1541

	
1541 1542
    /// \brief Runs the algorithm from the given source node.
1542 1543
    ///
1543 1544
    /// This method runs the %DFS algorithm from node \c s.
1544 1545
    /// in order to compute the DFS path to each node.
1545 1546
    ///
1546 1547
    /// The algorithm computes
1547 1548
    /// - the %DFS tree,
1548 1549
    /// - the distance of each node from the root in the %DFS tree.
1549 1550
    ///
1550 1551
    /// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
1551 1552
    ///\code
1552 1553
    ///   d.init();
1553 1554
    ///   d.addSource(s);
1554 1555
    ///   d.start();
1555 1556
    ///\endcode
1556 1557
    void run(Node s) {
1557 1558
      init();
1558 1559
      addSource(s);
1559 1560
      start();
1560 1561
    }
1561 1562

	
1562 1563
    /// \brief Finds the %DFS path between \c s and \c t.
1563 1564

	
1564 1565
    /// This method runs the %DFS algorithm from node \c s
1565 1566
    /// in order to compute the DFS path to node \c t
1566 1567
    /// (it stops searching when \c t is processed).
1567 1568
    ///
1568 1569
    /// \return \c true if \c t is reachable form \c s.
1569 1570
    ///
1570 1571
    /// \note Apart from the return value, <tt>d.run(s,t)</tt> is
1571 1572
    /// just a shortcut of the following code.
1572 1573
    ///\code
1573 1574
    ///   d.init();
1574 1575
    ///   d.addSource(s);
1575 1576
    ///   d.start(t);
1576 1577
    ///\endcode
1577 1578
    bool run(Node s,Node t) {
1578 1579
      init();
1579 1580
      addSource(s);
1580 1581
      start(t);
1581 1582
      return reached(t);
1582 1583
    }
1583 1584

	
1584 1585
    /// \brief Runs the algorithm to visit all nodes in the digraph.
1585 1586

	
1586 1587
    /// This method runs the %DFS algorithm in order to
1587 1588
    /// compute the %DFS path to each node.
1588 1589
    ///
1589 1590
    /// The algorithm computes
1590 1591
    /// - the %DFS tree (forest),
1591 1592
    /// - the distance of each node from the root(s) in the %DFS tree.
1592 1593
    ///
1593 1594
    /// \note <tt>d.run()</tt> is just a shortcut of the following code.
1594 1595
    ///\code
1595 1596
    ///   d.init();
1596 1597
    ///   for (NodeIt n(digraph); n != INVALID; ++n) {
1597 1598
    ///     if (!d.reached(n)) {
1598 1599
    ///       d.addSource(n);
1599 1600
    ///       d.start();
1600 1601
    ///     }
1601 1602
    ///   }
1602 1603
    ///\endcode
1603 1604
    void run() {
1604 1605
      init();
1605 1606
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
1606 1607
        if (!reached(it)) {
1607 1608
          addSource(it);
1608 1609
          start();
1609 1610
        }
1610 1611
      }
1611 1612
    }
1612 1613

	
1613 1614
    ///@}
1614 1615

	
1615 1616
    /// \name Query Functions
1616 1617
    /// The results of the DFS algorithm can be obtained using these
1617 1618
    /// functions.\n
1618 1619
    /// Either \ref run(Node) "run()" or \ref start() should be called
1619 1620
    /// before using them.
1620 1621

	
1621 1622
    ///@{
1622 1623

	
1623 1624
    /// \brief Checks if a node is reached from the root(s).
1624 1625
    ///
1625 1626
    /// Returns \c true if \c v is reached from the root(s).
1626 1627
    ///
1627 1628
    /// \pre Either \ref run(Node) "run()" or \ref init()
1628 1629
    /// must be called before using this function.
1629 1630
    bool reached(Node v) const { return (*_reached)[v]; }
1630 1631

	
1631 1632
    ///@}
1632 1633

	
1633 1634
  };
1634 1635

	
1635 1636
} //END OF NAMESPACE LEMON
1636 1637

	
1637 1638
#endif
0 comments (0 inline)