// Applied recursion: graph DFS with visited-state protection against cycles. // Build: c++ -std=c++17 -Wall -Wextra -Wpedantic cpp_graph_dfs.cpp -o cpp_graph_dfs #include #include void dfs(std::size_t vertex, const std::vector>& graph, std::vector& visited) { if (vertex >= graph.size() || visited[vertex]) return; visited[vertex] = true; std::cout << vertex << ' '; for (const std::size_t next : graph[vertex]) if (next < graph.size()) dfs(next, graph, visited); } int main() { const std::vector> graph{ {1, 2}, {0, 3}, {0, 3}, {1, 2, 4}, {3} }; std::vector visited(graph.size(), false); std::cout << "DFS from vertex 0: "; dfs(0U, graph, visited); std::cout << '\n'; }