BFS algorithm
The Breadth-First Search (BFS) algorithm is a simple graph traversal algorithm that explores all nodes of a graph by expanding the breadth (or level) of the search tree first before moving to the next level. Here is a step-by-step explanation of how the BFS algorithm works:\n\n1. Initialize a queue data structure to keep track of the nodes to be explored.\n2. Add the starting node to the queue.\n3. Initialize a set to keep track of visited nodes.\n4. While the queue is not empty, do the following:\n   a. Dequeue a node from the front of the queue. This node will be the current node.\n   b. Check if the current node is the goal node. If it is, the algorithm terminates.\n   c. Mark the current node as visited.\n   d. Enqueue all the neighbors (adjacent nodes) of the current node that have not been visited yet.\n5. If the goal node is not reached and there are no more nodes left to explore, there is no path from the start node to the goal node, and the algorithm terminates.\n\nThe BFS algorithm guarantees that the shortest path to each node is found first. Additionally, it explores all the nodes at a given level before moving on to the nodes at the next level. This property makes BFS particularly useful for finding the shortest path between two nodes in an unweighted graph.\n\nOnce the BFS algorithm terminates, one can backtrack from the goal node to the start node to construct the shortest path using parent pointers or any other method of keeping track of the path during the algorithm's execution.