Skip to content

Commit

Permalink
network_operations: DFS takes callback
Browse files Browse the repository at this point in the history
You can apply a condition to the DFS search using the lambda callback
  • Loading branch information
amritagos committed Jun 20, 2024
1 parent 1a884a9 commit 9b6ae81
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion graph_lib/include/connectivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TarjanConnectivityAlgo {
if (!processed[u]) {
lowest[v] = std::min(lowest[v], num[u]);
} // u not processed
} // u has been visited
} // u has been visited
}

// Now v has been processed
Expand Down
12 changes: 8 additions & 4 deletions graph_lib/include/network_operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ template <typename WeightType = double> class NetworkOperations {
std::optional<std::vector<size_t>> path_from_dfs(size_t s, size_t v) {
std::vector<size_t> path{}; // Path from s to v
reset_variables_counters(); // Reset marked and depth
// There is no extra condition we want to put on the DFS, so make it always
// return true
auto condition = [this]() { return true; };
// DFS starting from the source
dfs(s);
dfs(s, condition);
// Get one path if it exists
return path_to_vertex(s, v);
}
Expand Down Expand Up @@ -62,12 +65,13 @@ template <typename WeightType = double> class NetworkOperations {
// (given by the index in this vector)

// Depth-first search from vertex v
void dfs(size_t v) {
// Condition allows you to add some additional condition for applying the DFS
template <typename Callback> void dfs(size_t v, Callback condition) {
marked[v] = true;
for (size_t w : network.get_neighbours(v)) {
if (!marked[w]) {
if (!marked[w] && condition()) {
edge_to_vertex[w] = v;
dfs(w);
dfs(w, condition);
}
}
}
Expand Down

0 comments on commit 9b6ae81

Please sign in to comment.