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