6 #include "../structures/Graph.hpp"
20 template <
class Graph>
39 BFS(
const BFS &) =
default;
40 BFS(
BFS &&) noexcept = default;
41 BFS &operator=(const
BFS &) = default;
42 BFS &operator=(
BFS &&) = default;
100 template <class
Graph>
103 template <
class Graph>
109 template <
class Graph>
113 idx_t INF = std::numeric_limits<idx_t>::max();
128 while(!queue.empty()) {
133 for (
auto elem : tempList) {
134 visited[elem] =
true;
135 distances[elem] = distances[v] + 1;
137 queue.push_back(elem);
142 distances_ = distances;
143 isInitialized_ =
true;
146 template <
class Graph>
148 GL_ASSERT(isInitialized_,
"BFS::getTraversalOrder | BFS has not been initialized with a graph.")
152 template <
class Graph>
154 GL_ASSERT(isInitialized_,
"BFS::getTraversalOrderMaxDistance | BFS has not been initialized with a graph.")
156 for (
auto x : final_) {
157 if (distances_[x] > distance)
continue;
163 template <
class Graph>
165 GL_ASSERT(isInitialized_,
"BFS::getNodesExactDistance | BFS has not been initialized with a graph.")
167 for (
auto elem : final_) {
168 if (distances_[elem] == distance) result.push_back(elem);
173 template <
class Graph>
175 GL_ASSERT(isInitialized_,
"BFS::getNodeDistance | BFS has not been initialized with a graph.")
176 GL_ASSERT((0 <= dest), (std::string(
"Negative index: ") + std::to_string(dest) + std::string(
" < 0")))
177 GL_ASSERT((dest < distances_.size()), (
"Index " + std::to_string(dest) +
" is larger than the max: " + std::to_string(distances_.size() - 1)))
179 return distances_[dest];
182 template <
class Graph>
184 GL_ASSERT(isInitialized_,
"BFS::getNodesMaxDistance | BFS has not been initialized with a graph.")
186 for (
auto elem : final_) {
187 if (distances_[elem] <= distance) result.push_back(elem);
192 template <
class Graph>
194 GL_ASSERT(isInitialized_,
"BFS::getPath | BFS has not been initialized with a graph.")
195 GL_ASSERT((0 <= dest), (std::string(
"Negative index: ") + std::to_string(dest) + std::string(
" < 0")))
196 GL_ASSERT((dest < distances_.size()), (
"Index " + std::to_string(dest) +
" is larger than the max: " + std::to_string(distances_.size() - 1)))
202 idx_t parent = parents_[dest];
203 result.push_front(dest);
204 result.push_front(parent);
205 while (parent != src_)
207 parent = parents_[parent];
208 result.push_front(parent);
Class that provides Graph traversal using the BFS scheme.
Definition: BFS.hpp:21
typename Graph::visit_list_t visit_list_t
Boolean list that stored previously visited nodes.
Definition: BFS.hpp:24
ordered_list_t getTraversalOrderMaxDistance(const idx_t &distance) const
Computes the BFS node traversal order from the source.
Definition: BFS.hpp:153
idx_list_t parents_
Parents on shortest path tree.
Definition: BFS.hpp:91
idx_list_t getNodesExactDistance(const idx_t &distance) const
Computes all nodes that are at the exact distance given from the source.
Definition: BFS.hpp:164
typename Graph::idx_t idx_t
Type of indices.
Definition: BFS.hpp:22
std::vector< idx_t > idx_list_t
Index List type.
Definition: Graph.hpp:48
gl::index_type idx_t
Index type.
Definition: Graph.hpp:44
Stores and implements a Graph.
Definition: Graph.hpp:39
ordered_list_t getTraversalOrder() const
Computes the BFS node traversal order from the source.
Definition: BFS.hpp:147
ordered_list_t getPath(const idx_t &dest) const
Computes the BFS shortest path from 'src' to 'dest'.
Definition: BFS.hpp:193
#define GL_ASSERT(EXPR, ERROR_MSG)
Custom assert that supports attaching a message.
Definition: gl_assert.hpp:14
std::deque< idx_t > BFS_queue_t
Data structure for BFS searches.
Definition: BFS.hpp:25
idx_t numNodes() const
Returns the number of nodes currently in the graph.
Definition: Graph.hpp:406
void compute(const Graph &graph, const idx_t src)
Computation. This is where the BFS algorithm is performed.
Definition: BFS.hpp:110
typename Graph::idx_list_t idx_list_t
Unordered node ID list.
Definition: BFS.hpp:27
idx_t getNodeDistance(const idx_t &dest) const
Returns the number of edges between 'src' and 'dest'.
Definition: BFS.hpp:174
std::list< idx_t > ordered_list_t
Ordered List type.
Definition: Graph.hpp:49
std::vector< idx_t > distance_list_t
Stores BFS distances of each node.
Definition: BFS.hpp:26
typename Graph::ordered_list_t ordered_list_t
Ordered node ID list.
Definition: BFS.hpp:23
BFS()
Default constructor.
Definition: BFS.hpp:101
idx_t src_
Source node.
Definition: BFS.hpp:90
std::vector< bool > visit_list_t
Visited-List type.
Definition: Graph.hpp:50
void checkRange(const idx_t &idx1) const
Only one index that gets range checked.
Definition: Graph.hpp:1389
idx_list_t getNodesMaxDistance(const idx_t &distance) const
Computes all nodes that are at within a maximum distance given from the source.
Definition: BFS.hpp:183
idx_list_t getUnvisitedNeighbours(const idx_t &node, const std::vector< bool > &visited) const
Returns a list of endpoints + edge weights of outgoing edges from start.
Definition: Graph.hpp:1096
distance_list_t distances_
List containing distances from source.
Definition: BFS.hpp:93
ordered_list_t final_
BFS Traversal order.
Definition: BFS.hpp:92
bool isInitialized_
Boolean storing initialization status.
Definition: BFS.hpp:89