#ifndef BFS_H_ #define BFS_H_ #include "graph/digraph.h" #include using namespace graph; namespace algo { template class BFS { public: BFS(Graph G) : G(G) {} // Digraph run(const T& root); // Initialize the lookup table that is used to // show which vertices have been traversed std::map initExplore(); private: Graph G; }; template std::map BFS::initExplore() { std::map graphExplore; for (const auto& v : G.vertices) { graphExplore[v] = false; } return graphExplore; } template Digraph BFS::run(const T& root) { 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 Digraph(tree); } } // namespace algo #endif