Refactor: Replace std::map and std::set with unordered versions

This commit is contained in:
stefiosif
2022-10-12 17:26:11 +03:00
parent c525aeaa43
commit dc7fa93a6a
13 changed files with 44 additions and 41 deletions

View File

@@ -14,24 +14,24 @@ class BreadthFirstSearch {
public:
BreadthFirstSearch() = default;
BreadthFirstSearch(std::map<T, std::set<T>> adjList)
BreadthFirstSearch(std::unordered_map<T, std::unordered_set<T>> adjList)
: adjList(adjList) {}
// Traverse whole graph using the BFS search, and save the tree graph
// which is created when visiting new vertices (Breadth First Tree)
std::map<T, std::set<T>> execute(const T& root);
std::unordered_map<T, std::unordered_set<T>> execute(const T& root);
// Search if target vertex exists in graph
bool query(const T& root, const T& target);
private:
// Represents the graph on which the algorithm will be executed
std::map<T, std::set<T>> adjList;
std::unordered_map<T, std::unordered_set<T>> adjList;
};
template<typename T>
std::map<T, std::set<T>> BreadthFirstSearch<T>::execute(const T& root) {
std::map<T, std::set<T>> tree;
std::map<T, bool> visited;
std::unordered_map<T, std::unordered_set<T>> BreadthFirstSearch<T>::execute(const T& root) {
std::unordered_map<T, std::unordered_set<T>> tree;
std::unordered_map<T, bool> visited;
std::queue<T> Q;
Q.push(root);
visited[root] = true;
@@ -54,7 +54,7 @@ std::map<T, std::set<T>> BreadthFirstSearch<T>::execute(const T& root) {
template<typename T>
bool BreadthFirstSearch<T>::query(const T& root, const T& target) {
std::map<T, bool> visited;
std::unordered_map<T, bool> visited;
std::queue<T> Q;
Q.push(root);
visited[root] = true;