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

View File

@@ -1,8 +1,8 @@
#ifndef BREADTH_FIRST_TREE_H_ #ifndef BREADTH_FIRST_TREE_H_
#define BREADTH_FIRST_TREE_H_ #define BREADTH_FIRST_TREE_H_
#include "algorithm/bfs.h"
#include "out_tree.h" #include "out_tree.h"
#include "algorithm/breadth_first_search.h"
using namespace graph; using namespace graph;
namespace tree { namespace tree {
@@ -12,16 +12,15 @@ class BreadthFirstTree : public OutTree<T> {
public: public:
BreadthFirstTree() = default; BreadthFirstTree() = default;
BreadthFirstTree(Digraph<T> G); BreadthFirstTree(Digraph<T> G, T root);
BreadthFirstTree(std::map<T, std::set<T>> G) BreadthFirstTree(std::map<T, std::set<T>> G)
: OutTree<T>::OutTree(G) {} : OutTree<T>::OutTree(G) {}
}; };
template<typename T> template<typename T>
BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G) { BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) {
auto bfs = algo::BFS<T>(G).run(1); auto bfs = algo::BreadthFirstSearch<T>(G).execute(root);
Graph<T>::adjMatrix = bfs; Graph<T>::adjMatrix = bfs;
} }