45 typename G::id_type target)
47 using id_type = G::id_type;
49 if (!graph.node_exists(root)) {
50 throw std::runtime_error(
"Root ID does not exist in Graph, cannot conduct DFS");
51 }
else if (!graph.node_exists(target)) {
52 throw std::runtime_error(
"Target ID does not exist in Graph, cannot conduct DFS");
54 std::set<id_type> seen {};
55 std::vector<id_type> path {};
58 std::function<bool(id_type, std::vector<id_type>&)> dfs_recursive =
59 [&](id_type current, std::vector<id_type>& current_path) ->
bool {
60 if (current == target) {
66 for (id_type neighbor : graph.get_neighbors(current)) {
67 if (!seen.contains(neighbor)) {
68 current_path.push_back(neighbor);
69 if (dfs_recursive(neighbor, current_path)) {
72 current_path.pop_back();
79 if (dfs_recursive(root, path)) {
83 return std::vector<id_type> {};
103 using id_type = G::id_type;
105 if (!graph.node_exists(root)) {
106 throw std::runtime_error(
"Root ID does not exist in Graph, cannot construct Spanning Tree");
110 std::set<id_type> seen {};
112 for (
const auto&
id : graph.get_nodes()) {
113 mst.add_node_with_id(
id, graph.get_node_data(
id));
116 std::function<void(id_type)> dfs_recursive = [&](id_type current) {
117 seen.insert(current);
119 for (id_type neighbor : graph.get_neighbors(current)) {
120 if (!seen.contains(neighbor)) {
122 mst.add_edge(current, neighbor, graph.get_edge_weight(current, neighbor));
124 mst.add_edge(current, neighbor);
126 dfs_recursive(neighbor);
std::vector< typename G::id_type > depth_first_search(const G &graph, typename G::id_type root, typename G::id_type target)
Performs a depth-first search (DFS) to find a path from root to target node in a graph.