#ifndef BFS_H_ #define BFS_H_ #include "graph/digraph.h" #include using namespace graph; namespace algo { template class BFS { public: BFS(Digraph G, T root) : G(G), root(root) {} // std::map> run(); // std::map initExplore(); private: Digraph G; T root; }; template std::map BFS::initExplore() { std::map graphExplore; for (const auto& v : G.vertices) { graphExplore[v] = false; } return graphExplore; } template std::map> BFS::run() { std::map> tree; std::map graphExplore = initExplore(); std::queue Q; Q.push(root); graphExplore[root] = true; while (!Q.empty()) { const auto v = Q.front(); Q.pop(); for (const auto& u : G.adjMatrix[v]) { if (!graphExplore[u]) { graphExplore[u] = true; tree[v].insert(u); Q.push(u); } } } return tree; } } // namespace algo #endif