49 typename G::id_type target)
51 using id_type = G::id_type;
53 if (!graph.node_exists(root)) {
54 throw std::runtime_error(
"Root ID does not exist in Graph, cannot conduct BFS");
55 }
else if (!graph.node_exists(target)) {
56 throw std::runtime_error(
"Target ID does not exist in Graph, cannot conduct BFS");
60 std::queue<std::pair<id_type, std::vector<id_type>>> queue {{{root, {root}}}};
62 std::set<id_type> seen {root};
63 std::vector<id_type> path {};
65 while (!queue.empty()) {
66 auto [current_node, path_so_far] = queue.front();
69 if (current_node == target)
72 for (id_type neighbor : graph.get_neighbors(current_node)) {
73 if (!seen.contains(neighbor)) {
74 auto new_path = path_so_far;
75 new_path.push_back(neighbor);
76 queue.push({neighbor, new_path});
78 seen.insert(neighbor);
81 return std::vector<id_type> {};
100 using id_type = G::id_type;
102 if (!graph.node_exists(root)) {
103 throw std::runtime_error(
"Root ID does not exist in Graph, cannot construct Spanning Tree");
107 std::set<id_type> seen {root};
108 std::queue<id_type> queue({root});
110 for (
const auto&
id : graph.get_nodes()) {
111 mst.add_node_with_id(
id, graph.get_node_data(
id));
114 while (!queue.empty()) {
115 id_type current = queue.front();
118 for (id_type neighbor : graph.get_neighbors(current)) {
119 if (!seen.contains(neighbor)) {
121 mst.add_edge(current, neighbor, graph.get_edge_weight(current, neighbor));
123 mst.add_edge(current, neighbor);
125 seen.insert(neighbor);
126 queue.push(neighbor);
std::vector< typename G::id_type > breadth_first_search(const G &graph, typename G::id_type root, typename G::id_type target)
Performs a breadth-first search (BFS) to find a path from root to target node in a graph.