Put root of BFS tree as constructor parameter
This commit is contained in:
@@ -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;
|
||||
@@ -1,8 +1,8 @@
|
||||
#ifndef BREADTH_FIRST_TREE_H_
|
||||
#define BREADTH_FIRST_TREE_H_
|
||||
|
||||
#include "algorithm/bfs.h"
|
||||
#include "out_tree.h"
|
||||
#include "algorithm/breadth_first_search.h"
|
||||
using namespace graph;
|
||||
|
||||
namespace tree {
|
||||
@@ -12,16 +12,15 @@ class BreadthFirstTree : public OutTree<T> {
|
||||
public:
|
||||
BreadthFirstTree() = default;
|
||||
|
||||
BreadthFirstTree(Digraph<T> G);
|
||||
BreadthFirstTree(Digraph<T> G, T root);
|
||||
|
||||
BreadthFirstTree(std::map<T, std::set<T>> G)
|
||||
: OutTree<T>::OutTree(G) {}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G) {
|
||||
auto bfs = algo::BFS<T>(G).run(1);
|
||||
|
||||
BreadthFirstTree<T>::BreadthFirstTree(Digraph<T> G, T root) {
|
||||
auto bfs = algo::BreadthFirstSearch<T>(G).execute(root);
|
||||
Graph<T>::adjMatrix = bfs;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user