https://github.com/maximilianfeldthusen/bfs
https://github.com/maximilianfeldthusen/bfs
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/maximilianfeldthusen/bfs
- Owner: maximilianfeldthusen
- License: bsd-3-clause
- Created: 2025-03-12T03:51:45.000Z (7 months ago)
- Default Branch: TFD
- Last Pushed: 2025-03-12T03:58:08.000Z (7 months ago)
- Last Synced: 2025-03-12T04:27:20.992Z (7 months ago)
- Language: C++
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Documentation
### bfs
This C++ code implements a Breadth-First Search (BFS) algorithm to traverse a graph. Let's break down the code section by section:
### Includes and Namespace
```cpp
#include
#include
#include
```
- `#include `: This header is included to allow the program to perform input and output operations using `std::cout` and `std::cerr`.
- `#include `: This header is included to use the `std::vector` container which dynamically resizes as needed.
- `#include `: This header is included to use the `std::queue` container, which provides a first-in-first-out (FIFO) data structure needed for the BFS algorithm.### BFS Function
```cpp
void BFS(int start, const std::vector>& graph) {
if (start < 0 || start >= graph.size()) {
std::cerr << "Start node is out of bounds." << std::endl;
return;
}std::vector visited(graph.size(), false);
std::queue queue;queue.push(start);
visited[start] = true;while (!queue.empty()) {
int node = queue.front();
queue.pop();
std::cout << "Visited: " << node << std::endl;for (int neighbor : graph[node]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.push(neighbor);
}
}
}
}
```
- **Function Signature**: `void BFS(int start, const std::vector>& graph)` defines a BFS function that takes a starting node `start` and a graph represented as an adjacency list (`graph`).
- **Bounds Checking**: The function first checks if the `start` node is valid (within the bounds of the graph). If not, it prints an error message and returns.
- **Visited Vector**: `std::vector visited(graph.size(), false)` initializes a vector of boolean values to keep track of which nodes have been visited. All values are initially set to `false`.
- **Queue Initialization**: A queue is created to facilitate the BFS. The starting node is pushed onto the queue, and it is marked as visited (`visited[start] = true`).
- **BFS Loop**: The core of the BFS algorithm is a loop that continues until the queue is empty:
- The front element of the queue (current node) is retrieved and removed.
- The current node is printed as visited.
- For each neighbor of the current node (retrieved from the adjacency list), if the neighbor has not been visited, it is marked as visited and added to the queue.### Main Function
```cpp
int main() {
std::vector> graph = {
{1, 2}, // Connections for node 0
{0, 3, 4}, // Connections for node 1
{0}, // Connections for node 2
{1}, // Connections for node 3
{1} // Connections for node 4
};// Start BFS from node 0
BFS(0, graph);
return 0;
}
```
- **Graph Definition**: A graph is defined using a vector of vectors, where each inner vector represents the neighbors of a node:
- Node 0 is connected to nodes 1 and 2.
- Node 1 is connected to nodes 0, 3, and 4.
- Node 2 is connected to node 0.
- Node 3 is connected to node 1.
- Node 4 is connected to node 1.
- **Calling BFS**: The BFS function is called with the starting node `0` and the defined graph.
- **Return Statement**: The program returns `0`, indicating successful completion.### Summary
This code performs a Breadth-First Search on a simple undirected graph starting from a specified node. It prints out the nodes in the order they are visited, demonstrating the BFS traversal pattern, which explores neighbors level by level.