Use clang-format
This commit is contained in:
@@ -1,35 +1,38 @@
|
||||
#ifndef BREADTH_FIRST_SEARCH_H_
|
||||
#define BREADTH_FIRST_SEARCH_H_
|
||||
|
||||
#include "graph/graph.h"
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
|
||||
using namespace graph;
|
||||
|
||||
namespace algo {
|
||||
|
||||
template<typename T>
|
||||
class BreadthFirstSearch {
|
||||
template <typename T> class BreadthFirstSearch {
|
||||
public:
|
||||
BreadthFirstSearch() = default;
|
||||
|
||||
explicit BreadthFirstSearch(std::unordered_map<T, std::unordered_set<T>> adjList)
|
||||
: adjList(adjList) {}
|
||||
explicit 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::unordered_map<T, std::unordered_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);
|
||||
bool query(const T &root, const T &target);
|
||||
|
||||
private:
|
||||
// Represents the graph on which the algorithm will be executed
|
||||
std::unordered_map<T, std::unordered_set<T>> adjList;
|
||||
};
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
std::unordered_map<T, std::unordered_set<T>> BreadthFirstSearch<T>::execute(const T& root) {
|
||||
template <typename T>
|
||||
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;
|
||||
@@ -40,7 +43,7 @@ std::unordered_map<T, std::unordered_set<T>> BreadthFirstSearch<T>::execute(cons
|
||||
const auto v = Q.front();
|
||||
Q.pop();
|
||||
|
||||
for (const auto& u : adjList[v]) {
|
||||
for (const auto &u : adjList[v]) {
|
||||
if (!visited[u]) {
|
||||
visited[u] = true;
|
||||
tree[v].insert(u);
|
||||
@@ -52,8 +55,8 @@ std::unordered_map<T, std::unordered_set<T>> BreadthFirstSearch<T>::execute(cons
|
||||
return tree;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool BreadthFirstSearch<T>::query(const T& root, const T& target) {
|
||||
template <typename T>
|
||||
bool BreadthFirstSearch<T>::query(const T &root, const T &target) {
|
||||
std::unordered_map<T, bool> visited;
|
||||
std::queue<T> Q;
|
||||
Q.push(root);
|
||||
@@ -63,9 +66,10 @@ bool BreadthFirstSearch<T>::query(const T& root, const T& target) {
|
||||
const auto v = Q.front();
|
||||
Q.pop();
|
||||
|
||||
if (v == target) return true;
|
||||
if (v == target)
|
||||
return true;
|
||||
|
||||
for (const auto& u : adjList[v]) {
|
||||
for (const auto &u : adjList[v]) {
|
||||
if (!visited[u]) {
|
||||
visited[u] = true;
|
||||
Q.push(u);
|
||||
|
||||
Reference in New Issue
Block a user