Skip to content

Commit 36d20c1

Browse files
committed
graph: improvement according to code review
1 parent c397288 commit 36d20c1

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

include/graph/impl/edge_impl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#ifndef EDGE_IMPL_HPP
1111
#define EDGE_IMPL_HPP
1212

13+
#include <iostream>
14+
1315
namespace xmotion {
1416

1517
template <typename State, typename Transition, typename StateIndexer>

include/graph/impl/vertex_impl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#ifndef VERTEX_IMPL_HPP
1111
#define VERTEX_IMPL_HPP
1212

13+
#include <iostream>
14+
1315
namespace xmotion {
1416

1517
template <typename State, typename Transition, typename StateIndexer>

include/graph/search/bfs.hpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#define BFS_HPP
1313

1414
#include <limits>
15+
#include <queue>
16+
#include <unordered_set>
1517
#include "graph/search/search_algorithm.hpp"
1618
#include "graph/search/search_strategy.hpp"
1719

@@ -182,11 +184,32 @@ class BFS final {
182184
auto start_it = graph->FindVertex(start);
183185
if (start_it == graph->vertex_end()) return false;
184186

185-
auto strategy = MakeBfsStrategy<State, Transition, StateIndexer>();
186-
auto dummy_goal = graph->vertex_end(); // No specific goal - traverse all
187+
// Manual BFS traversal
188+
using VertexIteratorType = decltype(start_it);
189+
std::queue<VertexIteratorType> q;
190+
std::unordered_set<int64_t> visited;
191+
192+
q.push(start_it);
193+
visited.insert(start_it->vertex_id);
187194

188-
SearchAlgorithm<decltype(strategy), State, Transition, StateIndexer>
189-
::Search(graph, context, start_it, dummy_goal, strategy);
195+
while (!q.empty()) {
196+
auto current = q.front();
197+
q.pop();
198+
199+
// Mark as visited in context
200+
auto& current_info = context.GetSearchInfo(current);
201+
current_info.SetChecked(true);
202+
203+
for (const auto& edge : current->edges_to) {
204+
auto neighbor = edge.dst;
205+
int64_t neighbor_id = neighbor->vertex_id;
206+
207+
if (visited.find(neighbor_id) == visited.end()) {
208+
q.push(neighbor);
209+
visited.insert(neighbor_id);
210+
}
211+
}
212+
}
190213

191214
return true;
192215
}

0 commit comments

Comments
 (0)