gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge Intel C++ compatibility fixes
0 7 0
merge default
6 files changed with 37 insertions and 6 deletions:
↑ Collapse diff ↑
Ignore white space 8192 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(contrib)
128 128
  ADD_SUBDIRECTORY(demo)
129 129
  ADD_SUBDIRECTORY(tools)
130 130
  ADD_SUBDIRECTORY(doc)
131 131
  ADD_SUBDIRECTORY(test)
132 132
ENDIF()
133 133

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

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

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

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

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

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

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

	
181 181
  SET(CPACK_COMPONENT_HEADERS_DEPENDS library)
182 182

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

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

	
192 192
  SET(CPACK_ALL_INSTALL_TYPES Full Developer)
193 193

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

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

	
215 215
  INCLUDE(CPack)
216 216
ENDIF()
Ignore white space 8192 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-2010
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 conform to 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 conform to the \ref concepts::WriteMap "WriteMap" concept.
66 66
    ///By default, it is a NullMap.
67 67
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
68 68
    ///Instantiates a \c ProcessedMap.
69 69

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

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

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

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

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

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

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

	
114 114
  ///%BFS algorithm class.
115 115

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

	
140 140
    ///The type of the digraph the algorithm runs on.
141 141
    typedef typename TR::Digraph Digraph;
142 142

	
143 143
    ///\brief The type of the map that stores the predecessor arcs of the
144 144
    ///shortest paths.
145 145
    typedef typename TR::PredMap PredMap;
146 146
    ///The type of the map that stores the distances of the nodes.
147 147
    typedef typename TR::DistMap DistMap;
148 148
    ///The type of the map that indicates which nodes are reached.
149 149
    typedef typename TR::ReachedMap ReachedMap;
150 150
    ///The type of the map that indicates which nodes are processed.
151 151
    typedef typename TR::ProcessedMap ProcessedMap;
152 152
    ///The type of the paths.
153 153
    typedef PredMapPath<Digraph, PredMap> Path;
154 154

	
155 155
    ///The \ref BfsDefaultTraits "traits class" of the algorithm.
156 156
    typedef TR Traits;
157 157

	
158 158
  private:
159 159

	
160 160
    typedef typename Digraph::Node Node;
161 161
    typedef typename Digraph::NodeIt NodeIt;
162 162
    typedef typename Digraph::Arc Arc;
163 163
    typedef typename Digraph::OutArcIt OutArcIt;
164 164

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

	
184 184
    std::vector<typename Digraph::Node> _queue;
185 185
    int _queue_head,_queue_tail,_queue_next_dist;
186 186
    int _curr_dist;
187 187

	
188 188
    //Creates the maps if necessary.
189 189
    void create_maps()
190 190
    {
191 191
      if(!_pred) {
192 192
        local_pred = true;
193 193
        _pred = Traits::createPredMap(*G);
194 194
      }
195 195
      if(!_dist) {
196 196
        local_dist = true;
197 197
        _dist = Traits::createDistMap(*G);
198 198
      }
199 199
      if(!_reached) {
200 200
        local_reached = true;
201 201
        _reached = Traits::createReachedMap(*G);
202 202
      }
203 203
      if(!_processed) {
204 204
        local_processed = true;
205 205
        _processed = Traits::createProcessedMap(*G);
206 206
      }
207 207
    }
208 208

	
209 209
  protected:
210 210

	
211 211
    Bfs() {}
212 212

	
213 213
  public:
214 214

	
215 215
    typedef Bfs Create;
216 216

	
217 217
    ///\name Named Template Parameters
218 218

	
219 219
    ///@{
220 220

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

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

	
261 261
    template <class T>
262 262
    struct SetReachedMapTraits : public Traits {
263 263
      typedef T ReachedMap;
264 264
      static ReachedMap *createReachedMap(const Digraph &)
265 265
      {
266 266
        LEMON_ASSERT(false, "ReachedMap is not initialized");
267 267
        return 0; // ignore warnings
268 268
      }
269 269
    };
270 270
    ///\brief \ref named-templ-param "Named parameter" for setting
271 271
    ///\c ReachedMap type.
272 272
    ///
273 273
    ///\ref named-templ-param "Named parameter" for setting
274 274
    ///\c ReachedMap type.
275 275
    ///It must conform to
276 276
    ///the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
277 277
    template <class T>
278 278
    struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
279 279
      typedef Bfs< Digraph, SetReachedMapTraits<T> > Create;
280 280
    };
281 281

	
282 282
    template <class T>
283 283
    struct SetProcessedMapTraits : public Traits {
284 284
      typedef T ProcessedMap;
285 285
      static ProcessedMap *createProcessedMap(const Digraph &)
286 286
      {
287 287
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
288 288
        return 0; // ignore warnings
289 289
      }
290 290
    };
291 291
    ///\brief \ref named-templ-param "Named parameter" for setting
292 292
    ///\c ProcessedMap type.
293 293
    ///
294 294
    ///\ref named-templ-param "Named parameter" for setting
295 295
    ///\c ProcessedMap type.
296 296
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
297 297
    template <class T>
298 298
    struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
299 299
      typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create;
300 300
    };
301 301

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

	
321 321
    ///@}
322 322

	
323 323
  public:
324 324

	
325 325
    ///Constructor.
326 326

	
327 327
    ///Constructor.
328 328
    ///\param g The digraph the algorithm runs on.
329 329
    Bfs(const Digraph &g) :
330 330
      G(&g),
331 331
      _pred(NULL), local_pred(false),
332 332
      _dist(NULL), local_dist(false),
333 333
      _reached(NULL), local_reached(false),
334 334
      _processed(NULL), local_processed(false)
335 335
    { }
336 336

	
337 337
    ///Destructor.
338 338
    ~Bfs()
339 339
    {
340 340
      if(local_pred) delete _pred;
341 341
      if(local_dist) delete _dist;
342 342
      if(local_reached) delete _reached;
343 343
      if(local_processed) delete _processed;
344 344
    }
345 345

	
346 346
    ///Sets the map that stores the predecessor arcs.
347 347

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

	
364 364
    ///Sets the map that indicates which nodes are reached.
365 365

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

	
382 382
    ///Sets the map that indicates which nodes are processed.
383 383

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

	
400 400
    ///Sets the map that stores the distances of the nodes.
401 401

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

	
419 419
  public:
420 420

	
421 421
    ///\name Execution Control
422 422
    ///The simplest way to execute the BFS algorithm is to use one of the
423 423
    ///member functions called \ref run(Node) "run()".\n
424 424
    ///If you need better control on the execution, you have to call
425 425
    ///\ref init() first, then you can add several source nodes with
426 426
    ///\ref addSource(). Finally the actual path computation can be
427 427
    ///performed with one of the \ref start() functions.
428 428

	
429 429
    ///@{
430 430

	
431 431
    ///\brief Initializes the internal data structures.
432 432
    ///
433 433
    ///Initializes the internal data structures.
434 434
    void init()
435 435
    {
436 436
      create_maps();
437 437
      _queue.resize(countNodes(*G));
438 438
      _queue_head=_queue_tail=0;
439 439
      _curr_dist=1;
440 440
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
441 441
        _pred->set(u,INVALID);
442 442
        _reached->set(u,false);
443 443
        _processed->set(u,false);
444 444
      }
445 445
    }
446 446

	
447 447
    ///Adds a new source node.
448 448

	
449 449
    ///Adds a new source node to the set of nodes to be processed.
450 450
    ///
451 451
    void addSource(Node s)
452 452
    {
453 453
      if(!(*_reached)[s])
454 454
        {
455 455
          _reached->set(s,true);
456 456
          _pred->set(s,INVALID);
457 457
          _dist->set(s,0);
458 458
          _queue[_queue_head++]=s;
459 459
          _queue_next_dist=_queue_head;
460 460
        }
461 461
    }
462 462

	
463 463
    ///Processes the next node.
464 464

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

	
489 489
    ///Processes the next node.
490 490

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

	
522 522
    ///Processes the next node.
523 523

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

	
558 558
    ///The next node to be processed.
559 559

	
560 560
    ///Returns the next node to be processed or \c INVALID if the queue
561 561
    ///is empty.
562 562
    Node nextNode() const
563 563
    {
564 564
      return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
565 565
    }
566 566

	
567 567
    ///Returns \c false if there are nodes to be processed.
568 568

	
569 569
    ///Returns \c false if there are nodes to be processed
570 570
    ///in the queue.
571 571
    bool emptyQueue() const { return _queue_tail==_queue_head; }
572 572

	
573 573
    ///Returns the number of the nodes to be processed.
574 574

	
575 575
    ///Returns the number of the nodes to be processed
576 576
    ///in the queue.
577 577
    int queueSize() const { return _queue_head-_queue_tail; }
578 578

	
579 579
    ///Executes the algorithm.
580 580

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

	
604 604
    ///Executes the algorithm until the given target node is reached.
605 605

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

	
631 631
    ///Executes the algorithm until a condition is met.
632 632

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

	
666 666
    ///Runs the algorithm from the given source node.
667 667

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

	
687 687
    ///Finds the shortest path between \c s and \c t.
688 688

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

	
709 709
    ///Runs the algorithm to visit all nodes in the digraph.
710 710

	
711 711
    ///This method runs the %BFS algorithm in order to visit all nodes
712 712
    ///in the digraph.
713 713
    ///
714 714
    ///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
715 715
    ///\code
716 716
    ///  b.init();
717 717
    ///  for (NodeIt n(gr); n != INVALID; ++n) {
718 718
    ///    if (!b.reached(n)) {
719 719
    ///      b.addSource(n);
720 720
    ///      b.start();
721 721
    ///    }
722 722
    ///  }
723 723
    ///\endcode
724 724
    void run() {
725 725
      init();
726 726
      for (NodeIt n(*G); n != INVALID; ++n) {
727 727
        if (!reached(n)) {
728 728
          addSource(n);
729 729
          start();
730 730
        }
731 731
      }
732 732
    }
733 733

	
734 734
    ///@}
735 735

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

	
742 742
    ///@{
743 743

	
744 744
    ///The shortest path to the given node.
745 745

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

	
754 754
    ///The distance of the given node from the root(s).
755 755

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

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

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

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

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

	
816 816
    ///Checks if the given node is reached from the root(s).
817 817

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

	
824 824
    ///@}
825 825
  };
826 826

	
827 827
  ///Default traits class of bfs() function.
828 828

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

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

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

	
854 854
    ///The type of the map that indicates which nodes are processed.
855 855

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

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

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

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

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

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

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

	
897 897
    ///This function instantiates a DistMap.
898 898
    ///\param g is the digraph, to which we would like to define
899 899
    ///the DistMap
900 900
    static DistMap *createDistMap(const Digraph &g)
901 901
    {
902 902
      return new DistMap(g);
903 903
    }
904 904

	
905 905
    ///The type of the shortest paths.
906 906

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

	
912 912
  /// Default traits class used by BfsWizard
913 913

	
914 914
  /// Default traits class used by BfsWizard.
915 915
  /// \tparam GR The type of the digraph.
916 916
  template<class GR>
917 917
  class BfsWizardBase : public BfsWizardDefaultTraits<GR>
918 918
  {
919 919

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

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

	
940 940
    public:
941 941
    /// Constructor.
942 942

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

	
948 948
    /// Constructor.
949 949

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

	
957 957
  };
958 958

	
959 959
  /// Auxiliary class for the function-type interface of BFS algorithm.
960 960

	
961 961
  /// This auxiliary class is created to implement the
962 962
  /// \ref bfs() "function-type interface" of \ref Bfs algorithm.
963 963
  /// It does not have own \ref run(Node) "run()" method, it uses the
964 964
  /// functions and features of the plain \ref Bfs.
965 965
  ///
966 966
  /// This class should only be used through the \ref bfs() function,
967 967
  /// which makes it easier to use the algorithm.
968 968
  ///
969 969
  /// \tparam TR The traits class that defines various types used by the
970 970
  /// algorithm.
971 971
  template<class TR>
972 972
  class BfsWizard : public TR
973 973
  {
974 974
    typedef TR Base;
975 975

	
976 976
    typedef typename TR::Digraph Digraph;
977 977

	
978 978
    typedef typename Digraph::Node Node;
979 979
    typedef typename Digraph::NodeIt NodeIt;
980 980
    typedef typename Digraph::Arc Arc;
981 981
    typedef typename Digraph::OutArcIt OutArcIt;
982 982

	
983 983
    typedef typename TR::PredMap PredMap;
984 984
    typedef typename TR::DistMap DistMap;
985 985
    typedef typename TR::ReachedMap ReachedMap;
986 986
    typedef typename TR::ProcessedMap ProcessedMap;
987 987
    typedef typename TR::Path Path;
988 988

	
989 989
  public:
990 990

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

	
994 994
    /// Constructor that requires parameters.
995 995

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

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

	
1005 1005
    ~BfsWizard() {}
1006 1006

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

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

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

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

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

	
1056 1056
    ///This method runs BFS algorithm in order to visit all nodes
1057 1057
    ///in the digraph.
1058 1058
    void run()
1059 1059
    {
1060 1060
      run(INVALID);
1061 1061
    }
1062 1062

	
1063 1063
    template<class T>
1064 1064
    struct SetPredMapBase : public Base {
1065 1065
      typedef T PredMap;
1066 1066
      static PredMap *createPredMap(const Digraph &) { return 0; };
1067 1067
      SetPredMapBase(const TR &b) : TR(b) {}
1068 1068
    };
1069 1069

	
1070 1070
    ///\brief \ref named-templ-param "Named parameter" for setting
1071 1071
    ///the predecessor map.
1072 1072
    ///
1073 1073
    ///\ref named-templ-param "Named parameter" function for setting
1074 1074
    ///the map that stores the predecessor arcs of the nodes.
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

	
1089 1089
    ///\brief \ref named-templ-param "Named parameter" for setting
1090 1090
    ///the reached map.
1091 1091
    ///
1092 1092
    ///\ref named-templ-param "Named parameter" function for setting
1093 1093
    ///the map that indicates which nodes are reached.
1094 1094
    template<class T>
1095 1095
    BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1096 1096
    {
1097 1097
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1098 1098
      return BfsWizard<SetReachedMapBase<T> >(*this);
1099 1099
    }
1100 1100

	
1101 1101
    template<class T>
1102 1102
    struct SetDistMapBase : public Base {
1103 1103
      typedef T DistMap;
1104 1104
      static DistMap *createDistMap(const Digraph &) { return 0; };
1105 1105
      SetDistMapBase(const TR &b) : TR(b) {}
1106 1106
    };
1107 1107

	
1108 1108
    ///\brief \ref named-templ-param "Named parameter" for setting
1109 1109
    ///the distance map.
1110 1110
    ///
1111 1111
    ///\ref named-templ-param "Named parameter" function for setting
1112 1112
    ///the map that stores the distances of the nodes calculated
1113 1113
    ///by the algorithm.
1114 1114
    template<class T>
1115 1115
    BfsWizard<SetDistMapBase<T> > distMap(const T &t)
1116 1116
    {
1117 1117
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1118 1118
      return BfsWizard<SetDistMapBase<T> >(*this);
1119 1119
    }
1120 1120

	
1121 1121
    template<class T>
1122 1122
    struct SetProcessedMapBase : public Base {
1123 1123
      typedef T ProcessedMap;
1124 1124
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1125 1125
      SetProcessedMapBase(const TR &b) : TR(b) {}
1126 1126
    };
1127 1127

	
1128 1128
    ///\brief \ref named-func-param "Named parameter" for setting
1129 1129
    ///the processed map.
1130 1130
    ///
1131 1131
    ///\ref named-templ-param "Named parameter" function for setting
1132 1132
    ///the map that indicates which nodes are processed.
1133 1133
    template<class T>
1134 1134
    BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1135 1135
    {
1136 1136
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1137 1137
      return BfsWizard<SetProcessedMapBase<T> >(*this);
1138 1138
    }
1139 1139

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

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

	
1168 1168
  };
1169 1169

	
1170 1170
  ///Function-type interface for BFS algorithm.
1171 1171

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

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

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

	
1258 1259
  /// \brief Default traits class of BfsVisit class.
1259 1260
  ///
1260 1261
  /// Default traits class of BfsVisit class.
1261 1262
  /// \tparam GR The type of the digraph the algorithm runs on.
1262 1263
  template<class GR>
1263 1264
  struct BfsVisitDefaultTraits {
1264 1265

	
1265 1266
    /// \brief The type of the digraph the algorithm runs on.
1266 1267
    typedef GR Digraph;
1267 1268

	
1268 1269
    /// \brief The type of the map that indicates which nodes are reached.
1269 1270
    ///
1270 1271
    /// The type of the map that indicates which nodes are reached.
1271 1272
    /// It must conform to
1272 1273
    ///the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1273 1274
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1274 1275

	
1275 1276
    /// \brief Instantiates a ReachedMap.
1276 1277
    ///
1277 1278
    /// This function instantiates a ReachedMap.
1278 1279
    /// \param digraph is the digraph, to which
1279 1280
    /// we would like to define the ReachedMap.
1280 1281
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1281 1282
      return new ReachedMap(digraph);
1282 1283
    }
1283 1284

	
1284 1285
  };
1285 1286

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

	
1325 1326
    ///The traits class.
1326 1327
    typedef TR Traits;
1327 1328

	
1328 1329
    ///The type of the digraph the algorithm runs on.
1329 1330
    typedef typename Traits::Digraph Digraph;
1330 1331

	
1331 1332
    ///The visitor type used by the algorithm.
1332 1333
    typedef VS Visitor;
1333 1334

	
1334 1335
    ///The type of the map that indicates which nodes are reached.
1335 1336
    typedef typename Traits::ReachedMap ReachedMap;
1336 1337

	
1337 1338
  private:
1338 1339

	
1339 1340
    typedef typename Digraph::Node Node;
1340 1341
    typedef typename Digraph::NodeIt NodeIt;
1341 1342
    typedef typename Digraph::Arc Arc;
1342 1343
    typedef typename Digraph::OutArcIt OutArcIt;
1343 1344

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

	
1353 1354
    std::vector<typename Digraph::Node> _list;
1354 1355
    int _list_front, _list_back;
1355 1356

	
1356 1357
    //Creates the maps if necessary.
1357 1358
    void create_maps() {
1358 1359
      if(!_reached) {
1359 1360
        local_reached = true;
1360 1361
        _reached = Traits::createReachedMap(*_digraph);
1361 1362
      }
1362 1363
    }
1363 1364

	
1364 1365
  protected:
1365 1366

	
1366 1367
    BfsVisit() {}
1367 1368

	
1368 1369
  public:
1369 1370

	
1370 1371
    typedef BfsVisit Create;
1371 1372

	
1372 1373
    /// \name Named Template Parameters
1373 1374

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

	
1394 1395
  public:
1395 1396

	
1396 1397
    /// \brief Constructor.
1397 1398
    ///
1398 1399
    /// Constructor.
1399 1400
    ///
1400 1401
    /// \param digraph The digraph the algorithm runs on.
1401 1402
    /// \param visitor The visitor object of the algorithm.
1402 1403
    BfsVisit(const Digraph& digraph, Visitor& visitor)
1403 1404
      : _digraph(&digraph), _visitor(&visitor),
1404 1405
        _reached(0), local_reached(false) {}
1405 1406

	
1406 1407
    /// \brief Destructor.
1407 1408
    ~BfsVisit() {
1408 1409
      if(local_reached) delete _reached;
1409 1410
    }
1410 1411

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

	
1428 1429
  public:
1429 1430

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

	
1438 1439
    /// @{
1439 1440

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

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

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

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

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

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

	
1564 1565
    /// \brief Returns \c false if there are nodes
1565 1566
    /// to be processed.
1566 1567
    ///
1567 1568
    /// Returns \c false if there are nodes
1568 1569
    /// to be processed in the queue.
1569 1570
    bool emptyQueue() const { return _list_front == _list_back; }
1570 1571

	
1571 1572
    /// \brief Returns the number of the nodes to be processed.
1572 1573
    ///
1573 1574
    /// Returns the number of the nodes to be processed in the queue.
1574 1575
    int queueSize() const { return _list_back - _list_front; }
1575 1576

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

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

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

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

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

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

	
1729 1730
    ///@}
1730 1731

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

	
1737 1738
    ///@{
1738 1739

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

	
1747 1748
    ///@}
1748 1749

	
1749 1750
  };
1750 1751

	
1751 1752
} //END OF NAMESPACE LEMON
1752 1753

	
1753 1754
#endif
Ignore white space 8192 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-2010
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 concepts 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 has 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 8192 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-2010
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_HEAP_H
20 20
#define LEMON_CONCEPTS_HEAP_H
21 21

	
22 22
///\ingroup concept
23 23
///\file
24 24
///\brief The concept of heaps.
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
    /// This concept class describes the main interface of heaps.
39 39
    /// The various \ref heaps "heap structures" are efficient
40 40
    /// implementations of the abstract data type \e priority \e queue.
41 41
    /// They store items with specified values called \e priorities
42 42
    /// in such a way that finding and removing the item with minimum
43 43
    /// priority are efficient. The basic operations are adding and
44 44
    /// erasing items, changing the priority of an item, etc.
45 45
    ///
46 46
    /// Heaps are crucial in several algorithms, such as Dijkstra and Prim.
47 47
    /// Any class that conforms to this concept can be used easily in such
48 48
    /// algorithms.
49 49
    ///
50 50
    /// \tparam PR Type of the priorities of the items.
51 51
    /// \tparam IM A read-writable item map with \c int values, used
52 52
    /// internally to handle the cross references.
53 53
    /// \tparam CMP A functor class for comparing the priorities.
54 54
    /// The default is \c std::less<PR>.
55 55
#ifdef DOXYGEN
56 56
    template <typename PR, typename IM, typename CMP>
57 57
#else
58 58
    template <typename PR, typename IM, typename CMP = std::less<PR> >
59 59
#endif
60 60
    class Heap {
61 61
    public:
62 62

	
63 63
      /// Type of the item-int map.
64 64
      typedef IM ItemIntMap;
65 65
      /// Type of the priorities.
66 66
      typedef PR Prio;
67 67
      /// Type of the items stored in the heap.
68 68
      typedef typename ItemIntMap::Key Item;
69 69

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

	
84 84
      /// \brief Constructor.
85 85
      ///
86 86
      /// Constructor.
87 87
      /// \param map A map that assigns \c int values to keys of type
88 88
      /// \c Item. It is used internally by the heap implementations to
89 89
      /// handle the cross references. The assigned value must be
90 90
      /// \c PRE_HEAP (<tt>-1</tt>) for each item.
91 91
#ifdef DOXYGEN
92 92
      explicit Heap(ItemIntMap &map) {}
93 93
#else
94 94
      explicit Heap(ItemIntMap&) {}
95 95
#endif
96 96

	
97 97
      /// \brief Constructor.
98 98
      ///
99 99
      /// Constructor.
100 100
      /// \param map A map that assigns \c int values to keys of type
101 101
      /// \c Item. It is used internally by the heap implementations to
102 102
      /// handle the cross references. The assigned value must be
103 103
      /// \c PRE_HEAP (<tt>-1</tt>) for each item.
104 104
      /// \param comp The function object used for comparing the priorities.
105 105
#ifdef DOXYGEN
106 106
      explicit Heap(ItemIntMap &map, const CMP &comp) {}
107 107
#else
108 108
      explicit Heap(ItemIntMap&, const CMP&) {}
109 109
#endif
110 110

	
111 111
      /// \brief The number of items stored in the heap.
112 112
      ///
113 113
      /// This function returns the number of items stored in the heap.
114 114
      int size() const { return 0; }
115 115

	
116 116
      /// \brief Check if the heap is empty.
117 117
      ///
118 118
      /// This function returns \c true if the heap is empty.
119 119
      bool empty() const { return false; }
120 120

	
121 121
      /// \brief Make the heap empty.
122 122
      ///
123 123
      /// This functon makes the heap empty.
124 124
      /// It does not change the cross reference map. If you want to reuse
125 125
      /// a heap that is not surely empty, you should first clear it and
126 126
      /// then you should set the cross reference map to \c PRE_HEAP
127 127
      /// for each item.
128 128
      void clear() {}
129 129

	
130 130
      /// \brief Insert an item into the heap with the given priority.
131 131
      ///
132 132
      /// This function inserts the given item into the heap with the
133 133
      /// given priority.
134 134
      /// \param i The item to insert.
135 135
      /// \param p The priority of the item.
136 136
      /// \pre \e i must not be stored in the heap.
137 137
#ifdef DOXYGEN
138 138
      void push(const Item &i, const Prio &p) {}
139 139
#else
140 140
      void push(const Item&, const Prio&) {}
141 141
#endif
142 142

	
143 143
      /// \brief Return the item having minimum priority.
144 144
      ///
145 145
      /// This function returns the item having minimum priority.
146 146
      /// \pre The heap must be non-empty.
147 147
      Item top() const { return Item(); }
148 148

	
149 149
      /// \brief The minimum priority.
150 150
      ///
151 151
      /// This function returns the minimum priority.
152 152
      /// \pre The heap must be non-empty.
153 153
      Prio prio() const { return Prio(); }
154 154

	
155 155
      /// \brief Remove the item having minimum priority.
156 156
      ///
157 157
      /// This function removes the item having minimum priority.
158 158
      /// \pre The heap must be non-empty.
159 159
      void pop() {}
160 160

	
161 161
      /// \brief Remove the given item from the heap.
162 162
      ///
163 163
      /// This function removes the given item from the heap if it is
164 164
      /// already stored.
165 165
      /// \param i The item to delete.
166 166
      /// \pre \e i must be in the heap.
167 167
#ifdef DOXYGEN
168 168
      void erase(const Item &i) {}
169 169
#else
170 170
      void erase(const Item&) {}
171 171
#endif
172 172

	
173 173
      /// \brief The priority of the given item.
174 174
      ///
175 175
      /// This function returns the priority of the given item.
176 176
      /// \param i The item.
177 177
      /// \pre \e i must be in the heap.
178 178
#ifdef DOXYGEN
179 179
      Prio operator[](const Item &i) const {}
180 180
#else
181 181
      Prio operator[](const Item&) const { return Prio(); }
182 182
#endif
183 183

	
184 184
      /// \brief Set the priority of an item or insert it, if it is
185 185
      /// not stored in the heap.
186 186
      ///
187 187
      /// This method sets the priority of the given item if it is
188 188
      /// already stored in the heap. Otherwise it inserts the given
189 189
      /// item into the heap with the given priority.
190 190
      ///
191 191
      /// \param i The item.
192 192
      /// \param p The priority.
193 193
#ifdef DOXYGEN
194 194
      void set(const Item &i, const Prio &p) {}
195 195
#else
196 196
      void set(const Item&, const Prio&) {}
197 197
#endif
198 198

	
199 199
      /// \brief Decrease the priority of an item to the given value.
200 200
      ///
201 201
      /// This function decreases the priority of an item to the given value.
202 202
      /// \param i The item.
203 203
      /// \param p The priority.
204 204
      /// \pre \e i must be stored in the heap with priority at least \e p.
205 205
#ifdef DOXYGEN
206 206
      void decrease(const Item &i, const Prio &p) {}
207 207
#else
208 208
      void decrease(const Item&, const Prio&) {}
209 209
#endif
210 210

	
211 211
      /// \brief Increase the priority of an item to the given value.
212 212
      ///
213 213
      /// This function increases the priority of an item to the given value.
214 214
      /// \param i The item.
215 215
      /// \param p The priority.
216 216
      /// \pre \e i must be stored in the heap with priority at most \e p.
217 217
#ifdef DOXYGEN
218 218
      void increase(const Item &i, const Prio &p) {}
219 219
#else
220 220
      void increase(const Item&, const Prio&) {}
221 221
#endif
222 222

	
223 223
      /// \brief Return the state of an item.
224 224
      ///
225 225
      /// This method returns \c PRE_HEAP if the given item has never
226 226
      /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
227 227
      /// and \c POST_HEAP otherwise.
228 228
      /// In the latter case it is possible that the item will get back
229 229
      /// to the heap again.
230 230
      /// \param i The item.
231 231
#ifdef DOXYGEN
232 232
      State state(const Item &i) const {}
233 233
#else
234 234
      State state(const Item&) const { return PRE_HEAP; }
235 235
#endif
236 236

	
237 237
      /// \brief Set the state of an item in the heap.
238 238
      ///
239 239
      /// This function sets the state of the given item in the heap.
240 240
      /// It can be used to manually clear the heap when it is important
241 241
      /// to achive better time complexity.
242 242
      /// \param i The item.
243 243
      /// \param st The state. It should not be \c IN_HEAP.
244 244
#ifdef DOXYGEN
245 245
      void state(const Item& i, State st) {}
246 246
#else
247 247
      void state(const Item&, State) {}
248 248
#endif
249 249

	
250 250

	
251 251
      template <typename _Heap>
252 252
      struct Constraints {
253 253
      public:
254 254
        void constraints() {
255 255
          typedef typename _Heap::Item OwnItem;
256 256
          typedef typename _Heap::Prio OwnPrio;
257 257
          typedef typename _Heap::State OwnState;
258 258

	
259 259
          Item item;
260 260
          Prio prio;
261 261
          item=Item();
262 262
          prio=Prio();
263 263
          ignore_unused_variable_warning(item);
264 264
          ignore_unused_variable_warning(prio);
265 265

	
266 266
          OwnItem own_item;
267 267
          OwnPrio own_prio;
268 268
          OwnState own_state;
269 269
          own_item=Item();
270 270
          own_prio=Prio();
271 271
          ignore_unused_variable_warning(own_item);
272 272
          ignore_unused_variable_warning(own_prio);
273 273
          ignore_unused_variable_warning(own_state);
274 274

	
275 275
          _Heap heap1(map);
276 276
          _Heap heap2 = heap1;
277 277
          ignore_unused_variable_warning(heap1);
278 278
          ignore_unused_variable_warning(heap2);
279 279

	
280 280
          int s = heap.size();
281 281
          ignore_unused_variable_warning(s);
282 282
          bool e = heap.empty();
283 283
          ignore_unused_variable_warning(e);
284 284

	
285 285
          prio = heap.prio();
286 286
          item = heap.top();
287 287
          prio = heap[item];
288 288
          own_prio = heap.prio();
289 289
          own_item = heap.top();
290 290
          own_prio = heap[own_item];
291 291

	
292 292
          heap.push(item, prio);
293 293
          heap.push(own_item, own_prio);
294 294
          heap.pop();
295 295

	
296 296
          heap.set(item, prio);
297 297
          heap.decrease(item, prio);
298 298
          heap.increase(item, prio);
299 299
          heap.set(own_item, own_prio);
300 300
          heap.decrease(own_item, own_prio);
301 301
          heap.increase(own_item, own_prio);
302 302

	
303 303
          heap.erase(item);
304 304
          heap.erase(own_item);
305 305
          heap.clear();
306 306

	
307 307
          own_state = heap.state(own_item);
308 308
          heap.state(own_item, own_state);
309 309

	
310 310
          own_state = _Heap::PRE_HEAP;
311 311
          own_state = _Heap::IN_HEAP;
312 312
          own_state = _Heap::POST_HEAP;
313 313
        }
314 314

	
315 315
        _Heap& heap;
316 316
        ItemIntMap& map;
317
        Constraints() {}
317 318
      };
318 319
    };
319 320

	
320 321
    /// @}
321 322
  } // namespace lemon
322 323
}
323 324
#endif
Ignore white space 8192 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 8192 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 paths
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
    /// In a sense, a path can be treated as a list of arcs.
42 42
    /// LEMON path types just store this list. As a consequence, they cannot
43 43
    /// enumerate the nodes on the path directly and a zero length path
44 44
    /// cannot store its source node.
45 45
    ///
46 46
    /// The arcs of a path should be stored in the order of their directions,
47 47
    /// i.e. the target node of each arc should be the same as the source
48 48
    /// node of the next arc. This consistency could be checked using
49 49
    /// \ref checkPath().
50 50
    /// The source and target nodes of a (consistent) path can be obtained
51 51
    /// using \ref pathSource() and \ref pathTarget().
52 52
    ///
53 53
    /// A path can be constructed from another path of any type using the
54 54
    /// copy constructor or the assignment operator.
55 55
    ///
56 56
    /// \tparam GR The digraph type in which the path is.
57 57
    template <typename GR>
58 58
    class Path {
59 59
    public:
60 60

	
61 61
      /// Type of the underlying digraph.
62 62
      typedef GR Digraph;
63 63
      /// Arc type of the underlying digraph.
64 64
      typedef typename Digraph::Arc Arc;
65 65

	
66 66
      class ArcIt;
67 67

	
68 68
      /// \brief Default constructor
69 69
      Path() {}
70 70

	
71 71
      /// \brief Template copy constructor
72 72
      template <typename CPath>
73 73
      Path(const CPath& cpath) {}
74 74

	
75 75
      /// \brief Template assigment operator
76 76
      template <typename CPath>
77 77
      Path& operator=(const CPath& cpath) {
78 78
        ignore_unused_variable_warning(cpath);
79 79
        return *this;
80 80
      }
81 81

	
82 82
      /// Length of the path, i.e. the number of arcs on the path.
83 83
      int length() const { return 0;}
84 84

	
85 85
      /// Returns whether the path is empty.
86 86
      bool empty() const { return true;}
87 87

	
88 88
      /// Resets the path to an empty path.
89 89
      void clear() {}
90 90

	
91 91
      /// \brief LEMON style iterator for enumerating the arcs of a path.
92 92
      ///
93 93
      /// LEMON style iterator class for enumerating the arcs of a path.
94 94
      class ArcIt {
95 95
      public:
96 96
        /// Default constructor
97 97
        ArcIt() {}
98 98
        /// Invalid constructor
99 99
        ArcIt(Invalid) {}
100 100
        /// Sets the iterator to the first arc of the given path
101 101
        ArcIt(const Path &) {}
102 102

	
103 103
        /// Conversion to \c Arc
104 104
        operator Arc() const { return INVALID; }
105 105

	
106 106
        /// Next arc
107 107
        ArcIt& operator++() {return *this;}
108 108

	
109 109
        /// Comparison operator
110 110
        bool operator==(const ArcIt&) const {return true;}
111 111
        /// Comparison operator
112 112
        bool operator!=(const ArcIt&) const {return true;}
113 113
        /// Comparison operator
114 114
        bool operator<(const ArcIt&) const {return false;}
115 115

	
116 116
      };
117 117

	
118 118
      template <typename _Path>
119 119
      struct Constraints {
120 120
        void constraints() {
121 121
          Path<Digraph> pc;
122 122
          _Path p, pp(pc);
123 123
          int l = p.length();
124 124
          int e = p.empty();
125 125
          p.clear();
126 126

	
127 127
          p = pc;
128 128

	
129 129
          typename _Path::ArcIt id, ii(INVALID), i(p);
130 130

	
131 131
          ++i;
132 132
          typename Digraph::Arc ed = i;
133 133

	
134 134
          e = (i == ii);
135 135
          e = (i != ii);
136 136
          e = (i < ii);
137 137

	
138 138
          ignore_unused_variable_warning(l);
139 139
          ignore_unused_variable_warning(pp);
140 140
          ignore_unused_variable_warning(e);
141 141
          ignore_unused_variable_warning(id);
142 142
          ignore_unused_variable_warning(ii);
143 143
          ignore_unused_variable_warning(ed);
144 144
        }
145 145
      };
146 146

	
147 147
    };
148 148

	
149 149
    namespace _path_bits {
150 150

	
151 151
      template <typename _Digraph, typename _Path, typename RevPathTag = void>
152 152
      struct PathDumperConstraints {
153 153
        void constraints() {
154 154
          int l = p.length();
155 155
          int e = p.empty();
156 156

	
157 157
          typename _Path::ArcIt id, i(p);
158 158

	
159 159
          ++i;
160 160
          typename _Digraph::Arc ed = i;
161 161

	
162 162
          e = (i == INVALID);
163 163
          e = (i != INVALID);
164 164

	
165 165
          ignore_unused_variable_warning(l);
166 166
          ignore_unused_variable_warning(e);
167 167
          ignore_unused_variable_warning(id);
168 168
          ignore_unused_variable_warning(ed);
169 169
        }
170 170
        _Path& p;
171
        PathDumperConstraints() {}
171 172
      };
172 173

	
173 174
      template <typename _Digraph, typename _Path>
174 175
      struct PathDumperConstraints<
175 176
        _Digraph, _Path,
176 177
        typename enable_if<typename _Path::RevPathTag, void>::type
177 178
      > {
178 179
        void constraints() {
179 180
          int l = p.length();
180 181
          int e = p.empty();
181 182

	
182 183
          typename _Path::RevArcIt id, i(p);
183 184

	
184 185
          ++i;
185 186
          typename _Digraph::Arc ed = i;
186 187

	
187 188
          e = (i == INVALID);
188 189
          e = (i != INVALID);
189 190

	
190 191
          ignore_unused_variable_warning(l);
191 192
          ignore_unused_variable_warning(e);
192 193
          ignore_unused_variable_warning(id);
193 194
          ignore_unused_variable_warning(ed);
194 195
        }
195 196
        _Path& p;
197
        PathDumperConstraints() {}
196 198
      };
197 199

	
198 200
    }
199 201

	
200 202

	
201 203
    /// \brief A skeleton structure for path dumpers.
202 204
    ///
203 205
    /// A skeleton structure for path dumpers. The path dumpers are
204 206
    /// the generalization of the paths, they can enumerate the arcs
205 207
    /// of the path either in forward or in backward order.
206 208
    /// These classes are typically not used directly, they are rather
207 209
    /// used to be assigned to a real path type.
208 210
    ///
209 211
    /// The main purpose of this concept is that the shortest path
210 212
    /// algorithms can enumerate the arcs easily in reverse order.
211 213
    /// In LEMON, such algorithms give back a (reverse) path dumper that
212 214
    /// can be assigned to a real path. The dumpers can be implemented as
213 215
    /// an adaptor class to the predecessor map.
214 216
    ///
215 217
    /// \tparam GR The digraph type in which the path is.
216 218
    template <typename GR>
217 219
    class PathDumper {
218 220
    public:
219 221

	
220 222
      /// Type of the underlying digraph.
221 223
      typedef GR Digraph;
222 224
      /// Arc type of the underlying digraph.
223 225
      typedef typename Digraph::Arc Arc;
224 226

	
225 227
      /// Length of the path, i.e. the number of arcs on the path.
226 228
      int length() const { return 0;}
227 229

	
228 230
      /// Returns whether the path is empty.
229 231
      bool empty() const { return true;}
230 232

	
231 233
      /// \brief Forward or reverse dumping
232 234
      ///
233 235
      /// If this tag is defined to be \c True, then reverse dumping
234 236
      /// is provided in the path dumper. In this case, \c RevArcIt
235 237
      /// iterator should be implemented instead of \c ArcIt iterator.
236 238
      typedef False RevPathTag;
237 239

	
238 240
      /// \brief LEMON style iterator for enumerating the arcs of a path.
239 241
      ///
240 242
      /// LEMON style iterator class for enumerating the arcs of a path.
241 243
      class ArcIt {
242 244
      public:
243 245
        /// Default constructor
244 246
        ArcIt() {}
245 247
        /// Invalid constructor
246 248
        ArcIt(Invalid) {}
247 249
        /// Sets the iterator to the first arc of the given path
248 250
        ArcIt(const PathDumper&) {}
249 251

	
250 252
        /// Conversion to \c Arc
251 253
        operator Arc() const { return INVALID; }
252 254

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

	
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 true;}
260 262
        /// Comparison operator
261 263
        bool operator<(const ArcIt&) const {return false;}
262 264

	
263 265
      };
264 266

	
265 267
      /// \brief LEMON style iterator for enumerating the arcs of a path
266 268
      /// in reverse direction.
267 269
      ///
268 270
      /// LEMON style iterator class for enumerating the arcs of a path
269 271
      /// in reverse direction.
270 272
      class RevArcIt {
271 273
      public:
272 274
        /// Default constructor
273 275
        RevArcIt() {}
274 276
        /// Invalid constructor
275 277
        RevArcIt(Invalid) {}
276 278
        /// Sets the iterator to the last arc of the given path
277 279
        RevArcIt(const PathDumper &) {}
278 280

	
279 281
        /// Conversion to \c Arc
280 282
        operator Arc() const { return INVALID; }
281 283

	
282 284
        /// Next arc
283 285
        RevArcIt& operator++() {return *this;}
284 286

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

	
292 294
      };
293 295

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

	
302 304
    };
303 305

	
304 306

	
305 307
    ///@}
306 308
  }
307 309

	
308 310
} // namespace lemon
309 311

	
310 312
#endif
Ignore white space 8192 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-2010
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 conform to 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 conform to the \ref concepts::WriteMap "WriteMap" concept.
66 66
    ///By default, it is a NullMap.
67 67
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
68 68
    ///Instantiates a \c ProcessedMap.
69 69

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

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

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

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

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

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

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

	
114 114
  ///%DFS algorithm class.
115 115

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

	
140 140
    ///The type of the digraph the algorithm runs on.
141 141
    typedef typename TR::Digraph Digraph;
142 142

	
143 143
    ///\brief The type of the map that stores the predecessor arcs of the
144 144
    ///DFS paths.
145 145
    typedef typename TR::PredMap PredMap;
146 146
    ///The type of the map that stores the distances of the nodes.
147 147
    typedef typename TR::DistMap DistMap;
148 148
    ///The type of the map that indicates which nodes are reached.
149 149
    typedef typename TR::ReachedMap ReachedMap;
150 150
    ///The type of the map that indicates which nodes are processed.
151 151
    typedef typename TR::ProcessedMap ProcessedMap;
152 152
    ///The type of the paths.
153 153
    typedef PredMapPath<Digraph, PredMap> Path;
154 154

	
155 155
    ///The \ref DfsDefaultTraits "traits class" of the algorithm.
156 156
    typedef TR Traits;
157 157

	
158 158
  private:
159 159

	
160 160
    typedef typename Digraph::Node Node;
161 161
    typedef typename Digraph::NodeIt NodeIt;
162 162
    typedef typename Digraph::Arc Arc;
163 163
    typedef typename Digraph::OutArcIt OutArcIt;
164 164

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

	
184 184
    std::vector<typename Digraph::OutArcIt> _stack;
185 185
    int _stack_head;
186 186

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

	
208 208
  protected:
209 209

	
210 210
    Dfs() {}
211 211

	
212 212
  public:
213 213

	
214 214
    typedef Dfs Create;
215 215

	
216 216
    ///\name Named Template Parameters
217 217

	
218 218
    ///@{
219 219

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

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

	
260 260
    template <class T>
261 261
    struct SetReachedMapTraits : public Traits {
262 262
      typedef T ReachedMap;
263 263
      static ReachedMap *createReachedMap(const Digraph &)
264 264
      {
265 265
        LEMON_ASSERT(false, "ReachedMap is not initialized");
266 266
        return 0; // ignore warnings
267 267
      }
268 268
    };
269 269
    ///\brief \ref named-templ-param "Named parameter" for setting
270 270
    ///\c ReachedMap type.
271 271
    ///
272 272
    ///\ref named-templ-param "Named parameter" for setting
273 273
    ///\c ReachedMap type.
274 274
    ///It must conform to
275 275
    ///the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
276 276
    template <class T>
277 277
    struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
278 278
      typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
279 279
    };
280 280

	
281 281
    template <class T>
282 282
    struct SetProcessedMapTraits : public Traits {
283 283
      typedef T ProcessedMap;
284 284
      static ProcessedMap *createProcessedMap(const Digraph &)
285 285
      {
286 286
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
287 287
        return 0; // ignore warnings
288 288
      }
289 289
    };
290 290
    ///\brief \ref named-templ-param "Named parameter" for setting
291 291
    ///\c ProcessedMap type.
292 292
    ///
293 293
    ///\ref named-templ-param "Named parameter" for setting
294 294
    ///\c ProcessedMap type.
295 295
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
296 296
    template <class T>
297 297
    struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
298 298
      typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
299 299
    };
300 300

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

	
319 319
    ///@}
320 320

	
321 321
  public:
322 322

	
323 323
    ///Constructor.
324 324

	
325 325
    ///Constructor.
326 326
    ///\param g The digraph the algorithm runs on.
327 327
    Dfs(const Digraph &g) :
328 328
      G(&g),
329 329
      _pred(NULL), local_pred(false),
330 330
      _dist(NULL), local_dist(false),
331 331
      _reached(NULL), local_reached(false),
332 332
      _processed(NULL), local_processed(false)
333 333
    { }
334 334

	
335 335
    ///Destructor.
336 336
    ~Dfs()
337 337
    {
338 338
      if(local_pred) delete _pred;
339 339
      if(local_dist) delete _dist;
340 340
      if(local_reached) delete _reached;
341 341
      if(local_processed) delete _processed;
342 342
    }
343 343

	
344 344
    ///Sets the map that stores the predecessor arcs.
345 345

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

	
362 362
    ///Sets the map that indicates which nodes are reached.
363 363

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

	
380 380
    ///Sets the map that indicates which nodes are processed.
381 381

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

	
398 398
    ///Sets the map that stores the distances of the nodes.
399 399

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

	
417 417
  public:
418 418

	
419 419
    ///\name Execution Control
420 420
    ///The simplest way to execute the DFS algorithm is to use one of the
421 421
    ///member functions called \ref run(Node) "run()".\n
422 422
    ///If you need better control on the execution, you have to call
423 423
    ///\ref init() first, then you can add a source node with \ref addSource()
424 424
    ///and perform the actual computation with \ref start().
425 425
    ///This procedure can be repeated if there are nodes that have not
426 426
    ///been reached.
427 427

	
428 428
    ///@{
429 429

	
430 430
    ///\brief Initializes the internal data structures.
431 431
    ///
432 432
    ///Initializes the internal data structures.
433 433
    void init()
434 434
    {
435 435
      create_maps();
436 436
      _stack.resize(countNodes(*G));
437 437
      _stack_head=-1;
438 438
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
439 439
        _pred->set(u,INVALID);
440 440
        _reached->set(u,false);
441 441
        _processed->set(u,false);
442 442
      }
443 443
    }
444 444

	
445 445
    ///Adds a new source node.
446 446

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

	
472 472
    ///Processes the next arc.
473 473

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

	
505 505
    ///Next arc to be processed.
506 506

	
507 507
    ///Next arc to be processed.
508 508
    ///
509 509
    ///\return The next arc to be processed or \c INVALID if the stack
510 510
    ///is empty.
511 511
    OutArcIt nextArc() const
512 512
    {
513 513
      return _stack_head>=0?_stack[_stack_head]:INVALID;
514 514
    }
515 515

	
516 516
    ///Returns \c false if there are nodes to be processed.
517 517

	
518 518
    ///Returns \c false if there are nodes to be processed
519 519
    ///in the queue (stack).
520 520
    bool emptyQueue() const { return _stack_head<0; }
521 521

	
522 522
    ///Returns the number of the nodes to be processed.
523 523

	
524 524
    ///Returns the number of the nodes to be processed
525 525
    ///in the queue (stack).
526 526
    int queueSize() const { return _stack_head+1; }
527 527

	
528 528
    ///Executes the algorithm.
529 529

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

	
553 553
    ///Executes the algorithm until the given target node is reached.
554 554

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

	
572 572
    ///Executes the algorithm until a condition is met.
573 573

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

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

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

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

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

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

	
643 643
    ///This method runs the %DFS algorithm in order to visit all nodes
644 644
    ///in the digraph.
645 645
    ///
646 646
    ///\note <tt>d.run()</tt> is just a shortcut of the following code.
647 647
    ///\code
648 648
    ///  d.init();
649 649
    ///  for (NodeIt n(digraph); n != INVALID; ++n) {
650 650
    ///    if (!d.reached(n)) {
651 651
    ///      d.addSource(n);
652 652
    ///      d.start();
653 653
    ///    }
654 654
    ///  }
655 655
    ///\endcode
656 656
    void run() {
657 657
      init();
658 658
      for (NodeIt it(*G); it != INVALID; ++it) {
659 659
        if (!reached(it)) {
660 660
          addSource(it);
661 661
          start();
662 662
        }
663 663
      }
664 664
    }
665 665

	
666 666
    ///@}
667 667

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

	
674 674
    ///@{
675 675

	
676 676
    ///The DFS path to the given node.
677 677

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

	
686 686
    ///The distance of the given node from the root(s).
687 687

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

	
697 697
    ///Returns the 'previous arc' of the %DFS tree for the given node.
698 698

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

	
711 711
    ///Returns the 'previous node' of the %DFS tree for the given node.
712 712

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

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

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

	
746 746
    ///Checks if the given node. node is reached from the root(s).
747 747

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

	
754 754
    ///@}
755 755
  };
756 756

	
757 757
  ///Default traits class of dfs() function.
758 758

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

	
767 767
    ///\brief The type of the map that stores the predecessor
768 768
    ///arcs of the %DFS paths.
769 769
    ///
770 770
    ///The type of the map that stores the predecessor
771 771
    ///arcs of the %DFS paths.
772 772
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
773 773
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
774 774
    ///Instantiates a PredMap.
775 775

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

	
784 784
    ///The type of the map that indicates which nodes are processed.
785 785

	
786 786
    ///The type of the map that indicates which nodes are processed.
787 787
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
788 788
    ///By default, it is a NullMap.
789 789
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
790 790
    ///Instantiates a ProcessedMap.
791 791

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

	
804 804
    ///The type of the map that indicates which nodes are reached.
805 805

	
806 806
    ///The type of the map that indicates which nodes are reached.
807 807
    ///It must conform to
808 808
    ///the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
809 809
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
810 810
    ///Instantiates a ReachedMap.
811 811

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

	
820 820
    ///The type of the map that stores the distances of the nodes.
821 821

	
822 822
    ///The type of the map that stores the distances of the nodes.
823 823
    ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
824 824
    typedef typename Digraph::template NodeMap<int> DistMap;
825 825
    ///Instantiates a DistMap.
826 826

	
827 827
    ///This function instantiates a DistMap.
828 828
    ///\param g is the digraph, to which we would like to define
829 829
    ///the DistMap
830 830
    static DistMap *createDistMap(const Digraph &g)
831 831
    {
832 832
      return new DistMap(g);
833 833
    }
834 834

	
835 835
    ///The type of the DFS paths.
836 836

	
837 837
    ///The type of the DFS paths.
838 838
    ///It must conform to the \ref concepts::Path "Path" concept.
839 839
    typedef lemon::Path<Digraph> Path;
840 840
  };
841 841

	
842 842
  /// Default traits class used by DfsWizard
843 843

	
844 844
  /// Default traits class used by DfsWizard.
845 845
  /// \tparam GR The type of the digraph.
846 846
  template<class GR>
847 847
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
848 848
  {
849 849

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

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

	
870 870
    public:
871 871
    /// Constructor.
872 872

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

	
878 878
    /// Constructor.
879 879

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

	
887 887
  };
888 888

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

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

	
906 906
    typedef typename TR::Digraph Digraph;
907 907

	
908 908
    typedef typename Digraph::Node Node;
909 909
    typedef typename Digraph::NodeIt NodeIt;
910 910
    typedef typename Digraph::Arc Arc;
911 911
    typedef typename Digraph::OutArcIt OutArcIt;
912 912

	
913 913
    typedef typename TR::PredMap PredMap;
914 914
    typedef typename TR::DistMap DistMap;
915 915
    typedef typename TR::ReachedMap ReachedMap;
916 916
    typedef typename TR::ProcessedMap ProcessedMap;
917 917
    typedef typename TR::Path Path;
918 918

	
919 919
  public:
920 920

	
921 921
    /// Constructor.
922 922
    DfsWizard() : TR() {}
923 923

	
924 924
    /// Constructor that requires parameters.
925 925

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

	
932 932
    ///Copy constructor
933 933
    DfsWizard(const TR &b) : TR(b) {}
934 934

	
935 935
    ~DfsWizard() {}
936 936

	
937 937
    ///Runs DFS algorithm from the given source node.
938 938

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

	
958 958
    ///Finds the DFS path between \c s and \c t.
959 959

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

	
984 984
    ///Runs DFS algorithm to visit all nodes in the digraph.
985 985

	
986 986
    ///This method runs DFS algorithm in order to visit all nodes
987 987
    ///in the digraph.
988 988
    void run()
989 989
    {
990 990
      run(INVALID);
991 991
    }
992 992

	
993 993
    template<class T>
994 994
    struct SetPredMapBase : public Base {
995 995
      typedef T PredMap;
996 996
      static PredMap *createPredMap(const Digraph &) { return 0; };
997 997
      SetPredMapBase(const TR &b) : TR(b) {}
998 998
    };
999 999

	
1000 1000
    ///\brief \ref named-templ-param "Named parameter" for setting
1001 1001
    ///the predecessor map.
1002 1002
    ///
1003 1003
    ///\ref named-templ-param "Named parameter" function for setting
1004 1004
    ///the map that stores the predecessor arcs of the nodes.
1005 1005
    template<class T>
1006 1006
    DfsWizard<SetPredMapBase<T> > predMap(const T &t)
1007 1007
    {
1008 1008
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1009 1009
      return DfsWizard<SetPredMapBase<T> >(*this);
1010 1010
    }
1011 1011

	
1012 1012
    template<class T>
1013 1013
    struct SetReachedMapBase : public Base {
1014 1014
      typedef T ReachedMap;
1015 1015
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1016 1016
      SetReachedMapBase(const TR &b) : TR(b) {}
1017 1017
    };
1018 1018

	
1019 1019
    ///\brief \ref named-templ-param "Named parameter" for setting
1020 1020
    ///the reached map.
1021 1021
    ///
1022 1022
    ///\ref named-templ-param "Named parameter" function for setting
1023 1023
    ///the map that indicates which nodes are reached.
1024 1024
    template<class T>
1025 1025
    DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1026 1026
    {
1027 1027
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1028 1028
      return DfsWizard<SetReachedMapBase<T> >(*this);
1029 1029
    }
1030 1030

	
1031 1031
    template<class T>
1032 1032
    struct SetDistMapBase : public Base {
1033 1033
      typedef T DistMap;
1034 1034
      static DistMap *createDistMap(const Digraph &) { return 0; };
1035 1035
      SetDistMapBase(const TR &b) : TR(b) {}
1036 1036
    };
1037 1037

	
1038 1038
    ///\brief \ref named-templ-param "Named parameter" for setting
1039 1039
    ///the distance map.
1040 1040
    ///
1041 1041
    ///\ref named-templ-param "Named parameter" function for setting
1042 1042
    ///the map that stores the distances of the nodes calculated
1043 1043
    ///by the algorithm.
1044 1044
    template<class T>
1045 1045
    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
1046 1046
    {
1047 1047
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1048 1048
      return DfsWizard<SetDistMapBase<T> >(*this);
1049 1049
    }
1050 1050

	
1051 1051
    template<class T>
1052 1052
    struct SetProcessedMapBase : public Base {
1053 1053
      typedef T ProcessedMap;
1054 1054
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1055 1055
      SetProcessedMapBase(const TR &b) : TR(b) {}
1056 1056
    };
1057 1057

	
1058 1058
    ///\brief \ref named-func-param "Named parameter" for setting
1059 1059
    ///the processed map.
1060 1060
    ///
1061 1061
    ///\ref named-templ-param "Named parameter" function for setting
1062 1062
    ///the map that indicates which nodes are processed.
1063 1063
    template<class T>
1064 1064
    DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1065 1065
    {
1066 1066
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1067 1067
      return DfsWizard<SetProcessedMapBase<T> >(*this);
1068 1068
    }
1069 1069

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

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

	
1098 1098
  };
1099 1099

	
1100 1100
  ///Function-type interface for DFS algorithm.
1101 1101

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

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

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

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

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

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

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

	
1226 1227
  };
1227 1228

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

	
1267 1268
    ///The traits class.
1268 1269
    typedef TR Traits;
1269 1270

	
1270 1271
    ///The type of the digraph the algorithm runs on.
1271 1272
    typedef typename Traits::Digraph Digraph;
1272 1273

	
1273 1274
    ///The visitor type used by the algorithm.
1274 1275
    typedef VS Visitor;
1275 1276

	
1276 1277
    ///The type of the map that indicates which nodes are reached.
1277 1278
    typedef typename Traits::ReachedMap ReachedMap;
1278 1279

	
1279 1280
  private:
1280 1281

	
1281 1282
    typedef typename Digraph::Node Node;
1282 1283
    typedef typename Digraph::NodeIt NodeIt;
1283 1284
    typedef typename Digraph::Arc Arc;
1284 1285
    typedef typename Digraph::OutArcIt OutArcIt;
1285 1286

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

	
1295 1296
    std::vector<typename Digraph::Arc> _stack;
1296 1297
    int _stack_head;
1297 1298

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

	
1306 1307
  protected:
1307 1308

	
1308 1309
    DfsVisit() {}
1309 1310

	
1310 1311
  public:
1311 1312

	
1312 1313
    typedef DfsVisit Create;
1313 1314

	
1314 1315
    /// \name Named Template Parameters
1315 1316

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

	
1336 1337
  public:
1337 1338

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

	
1348 1349
    /// \brief Destructor.
1349 1350
    ~DfsVisit() {
1350 1351
      if(local_reached) delete _reached;
1351 1352
    }
1352 1353

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

	
1370 1371
  public:
1371 1372

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

	
1381 1382
    /// @{
1382 1383

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

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

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

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

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

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

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

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

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

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

	
1565 1566
    /// \brief Finds the %DFS path between \c s and \c t.
1566 1567

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

	
1587 1588
    /// \brief Runs the algorithm to visit all nodes in the digraph.
1588 1589

	
1589 1590
    /// This method runs the %DFS algorithm in order to visit all nodes
1590 1591
    /// in the digraph.
1591 1592
    ///
1592 1593
    /// \note <tt>d.run()</tt> is just a shortcut of the following code.
1593 1594
    ///\code
1594 1595
    ///   d.init();
1595 1596
    ///   for (NodeIt n(digraph); n != INVALID; ++n) {
1596 1597
    ///     if (!d.reached(n)) {
1597 1598
    ///       d.addSource(n);
1598 1599
    ///       d.start();
1599 1600
    ///     }
1600 1601
    ///   }
1601 1602
    ///\endcode
1602 1603
    void run() {
1603 1604
      init();
1604 1605
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
1605 1606
        if (!reached(it)) {
1606 1607
          addSource(it);
1607 1608
          start();
1608 1609
        }
1609 1610
      }
1610 1611
    }
1611 1612

	
1612 1613
    ///@}
1613 1614

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

	
1620 1621
    ///@{
1621 1622

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

	
1630 1631
    ///@}
1631 1632

	
1632 1633
  };
1633 1634

	
1634 1635
} //END OF NAMESPACE LEMON
1635 1636

	
1636 1637
#endif
0 comments (0 inline)