26#ifndef DEPTH_FIRST_SEARCH_INCLUDED
27#define DEPTH_FIRST_SEARCH_INCLUDED
59template <
typename TVertex,
typename TVisit_start,
typename TVisit_end,
60 typename TGet_neighbors,
typename TVertex_less = std::less<TVertex>>
62 TVisit_end visitor_end, TGet_neighbors get_neighbors,
63 std::set<TVertex> &visited_set = std::set<TVertex>{}) {
66 constexpr int START_VISITING = 0;
68 constexpr int CURRENT_VERTEX = 1;
71 std::stack<std::tuple<bool, TVertex>>
stack;
74 if (!visited_set.insert(start_vertex).second) {
77 stack.emplace(
true, start_vertex);
79 while (!
stack.empty()) {
80 std::tuple<bool, TVertex> &elem =
stack.top();
81 if (std::get<START_VISITING>(elem)) {
82 visitor_start(std::get<CURRENT_VERTEX>(elem));
84 std::get<START_VISITING>(elem) =
false;
85 for (TVertex neighbor : get_neighbors(std::get<CURRENT_VERTEX>(elem))) {
87 if (visited_set.insert(neighbor).second) {
88 stack.emplace(
true, neighbor);
92 visitor_end(std::get<CURRENT_VERTEX>(elem));
void depth_first_search(TVertex start_vertex, TVisit_start visitor_start, TVisit_end visitor_end, TGet_neighbors get_neighbors, std::set< TVertex > &visited_set=std::set< TVertex >{})
Performs a Depth First Search algorithm on a graph defined by a set of vertices being an subset of un...
Definition: depth_first_search.h:61
task_env * stack
Definition: task.cc:876