Put root of BFS tree as constructor parameter

This commit is contained in:
stefiosif
2022-07-12 14:41:18 +03:00
parent 77dee72317
commit e7ad824116
2 changed files with 13 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
#ifndef BFS_H_
#define BFS_H_
#ifndef BREADTH_FIRST_SEARCH_H_
#define BREADTH_FIRST_SEARCH_H_
#include "graph/digraph.h"
@@ -10,24 +10,22 @@ using namespace graph;
namespace algo {
template<typename T>
class BFS {
class BreadthFirstSearch {
public:
BFS() = default;
BreadthFirstSearch() = default;
BFS(Digraph<T> G) : G(G) {}
BreadthFirstSearch(Digraph<T> G) : G(G) {}
//
std::map<T, std::set<T>> run(const T& root);
std::map<T, std::set<T>> execute(const T& root);
// Initialize the lookup table that is used to
// show which vertices have been traversed
// Initialize LU table that show which vertices have been traversed
std::map<T, bool> initExplore();
private:
Graph<T> G;
};
template<typename T>
std::map<T, bool> BFS<T>::initExplore() {
std::map<T, bool> BreadthFirstSearch<T>::initExplore() {
std::map<T, bool> graphExplore;
for (const auto& v : G.vertices) {
graphExplore[v] = false;
@@ -36,7 +34,7 @@ std::map<T, bool> BFS<T>::initExplore() {
}
template<typename T>
std::map<T, std::set<T>> BFS<T>::run(const T& root) {
std::map<T, std::set<T>> BreadthFirstSearch<T>::execute(const T& root) {
std::map<T, std::set<T>> tree;
std::map<T, bool> graphExplore = initExplore();
std::queue<T> Q;