Skip to content

Commit 9b6ae81

Browse files
committed
network_operations: DFS takes callback
You can apply a condition to the DFS search using the lambda callback
1 parent 1a884a9 commit 9b6ae81

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

graph_lib/include/connectivity.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class TarjanConnectivityAlgo {
6969
if (!processed[u]) {
7070
lowest[v] = std::min(lowest[v], num[u]);
7171
} // u not processed
72-
} // u has been visited
72+
} // u has been visited
7373
}
7474

7575
// Now v has been processed

graph_lib/include/network_operations.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ template <typename WeightType = double> class NetworkOperations {
2626
std::optional<std::vector<size_t>> path_from_dfs(size_t s, size_t v) {
2727
std::vector<size_t> path{}; // Path from s to v
2828
reset_variables_counters(); // Reset marked and depth
29+
// There is no extra condition we want to put on the DFS, so make it always
30+
// return true
31+
auto condition = [this]() { return true; };
2932
// DFS starting from the source
30-
dfs(s);
33+
dfs(s, condition);
3134
// Get one path if it exists
3235
return path_to_vertex(s, v);
3336
}
@@ -62,12 +65,13 @@ template <typename WeightType = double> class NetworkOperations {
6265
// (given by the index in this vector)
6366

6467
// Depth-first search from vertex v
65-
void dfs(size_t v) {
68+
// Condition allows you to add some additional condition for applying the DFS
69+
template <typename Callback> void dfs(size_t v, Callback condition) {
6670
marked[v] = true;
6771
for (size_t w : network.get_neighbours(v)) {
68-
if (!marked[w]) {
72+
if (!marked[w] && condition()) {
6973
edge_to_vertex[w] = v;
70-
dfs(w);
74+
dfs(w, condition);
7175
}
7276
}
7377
}

0 commit comments

Comments
 (0)