Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
DFS.hpp
Go to the documentation of this file.
1
6// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved.
7
8#pragma once
9
10#include <functional>
11#include <alib/vector>
12#include <queue>
13#include <alib/set>
14#include <alib/map>
15
17
18namespace graph {
19
20namespace traverse {
21
22class DFS {
23// ---------------------------------------------------------------------------------------------------------------------
24
25 public:
26
36 template<
37 typename TGraph,
38 typename TNode,
39 typename F = std::function<bool(const TNode &, const size_t &)>>
40 static
41 void
42 run(const TGraph &graph,
43 const TNode &start,
44 F f_user = [](const TNode &, const size_t &) -> bool { return false; });
45
46// ---------------------------------------------------------------------------------------------------------------------
47
60 template<
61 typename TGraph,
62 typename TNode,
63 typename F = std::function<void(const TNode &, const size_t &)>>
64 static
66 findPath(const TGraph &graph,
67 const TNode &start,
68 const TNode &goal,
69 F f_user = [](const TNode &, const size_t &) {});
70
71 template<typename TGraph, typename TNode>
72 static
75 const TNode &start,
76 const TNode &goal) {
77 return findPath(graph, start, goal);
78
79 }
80
81// =====================================================================================================================
82
83 private:
84
85// ---------------------------------------------------------------------------------------------------------------------
86
87 template<typename TNode>
88 struct Data {
89 ext::map<TNode, size_t> d; // distance
90 ext::set<TNode> v; // visited
91 ext::map<TNode, TNode> p; // parents
92 };
93
94// ---------------------------------------------------------------------------------------------------------------------
95
96 template<typename TGraph, typename TNode, typename F1, typename F2>
97 static
99 impl(const TGraph &graph,
100 const TNode &start,
101 const TNode &goal,
102 F1 f_user,
103 F2 f_stop);
104
105// ---------------------------------------------------------------------------------------------------------------------
106
107 template<typename TGraph, typename TNode, typename F1, typename F2>
108 static
109 bool
110 expansion(const TGraph &graph,
111 DFS::Data<TNode> &data,
112 const TNode &n,
113 const TNode &p,
114 F1 f_user,
115 F2 f_stop);
116
117// ---------------------------------------------------------------------------------------------------------------------
118
119};
120
121// =====================================================================================================================
122
123template<typename TGraph, typename TNode, typename F>
124void DFS::run(const TGraph &graph, const TNode &start, F f_user) {
125 impl(graph, start, start, f_user, [](const TNode &) -> bool { return false; });
126}
127
128// ---------------------------------------------------------------------------------------------------------------------
129
130template<typename TGraph, typename TNode, typename F>
131ext::vector<TNode> DFS::findPath(const TGraph &graph, const TNode &start, const TNode &goal, F f_user) {
132 return impl(graph, start, goal,
133 [&](const TNode &n, const auto &d) -> bool {
134 f_user(n, d);
135 return false;
136 },
137 [&](const TNode &n) -> bool { return goal == n; });
138}
139
140// ---------------------------------------------------------------------------------------------------------------------
141
142template<typename TGraph, typename TNode, typename F1, typename F2>
143ext::vector<TNode> DFS::impl(const TGraph &graph,
144 const TNode &start,
145 const TNode &goal,
146 F1 f_user,
147 F2 f_stop) {
148 Data<TNode> data;
149
150 // Init search
151 data.d[start] = -1;
152
153 // Start DFS
154 expansion(graph, data, start, start, f_user, f_stop);
155
157}
158
159// ---------------------------------------------------------------------------------------------------------------------
160
161template<typename TGraph, typename TNode, typename F1, typename F2>
162bool
163DFS::expansion(const TGraph &graph,
164 DFS::Data<TNode> &data,
165 const TNode &n,
166 const TNode &p,
167 F1 f_user,
168 F2 f_stop) {
169 if (data.v.find(n) != data.v.end()) return false;
170
171 data.v.insert(n);
172 data.p.insert_or_assign(n, p);
173 data.d[n] = data.d[p] + 1;
174
175 // Run user's function
176 if (f_user(n, data.d[n])) {
177 return true; // Stop algorithm
178 }
179
180 // Stop if reach the goal
181 if (f_stop(n)) {
182 return true; // Stop algorithm
183 }
184
185 for (const auto &i : graph.successors(n)) {
186 bool stop = expansion(graph, data, i, n, f_user, f_stop);
187
188 if (stop) {
189 return true;
190 }
191 }
192
193 return false;
194}
195
196
197// =====================================================================================================================
198
199
200// ---------------------------------------------------------------------------------------------------------------------
201
202} // namespace traverse
203
204} // namespace graph
205
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Definition: set.hpp:44
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
static ext::vector< TNode > reconstructPath(const ext::map< TNode, TNode > &p, const TNode &start, const TNode &goal)
Definition: ReconstructPath.hpp:68
Definition: DFS.hpp:22
static void run(const TGraph &graph, const TNode &start, F f_user=[](const TNode &, const size_t &) -> bool { return false;})
Definition: DFS.hpp:124
static ext::vector< TNode > findPathRegistration(const TGraph &graph, const TNode &start, const TNode &goal)
Definition: DFS.hpp:74
static ext::vector< TNode > findPath(const TGraph &graph, const TNode &start, const TNode &goal, F f_user=[](const TNode &, const size_t &) {})
Definition: DFS.hpp:131
int i
Definition: AllEpsilonClosure.h:118
Definition: ReconstructPath.hpp:14
void start(measurements::stealth_string name, measurements::Type type)
Definition: measurements.cpp:14