1.
Introduction
2.
Applications of BFS Algorithm
3.
BFS Algorithm
4.
Implementation of BFS Algorithm
4.1.
Python
4.2.
Explanation of the Code
5.
Example of BFS algorithm:
5.1.
Complexity of BFS algorithm
5.2.
Time Complexity
5.3.
Space Complexity
6.
6.1.
What makes BFS different from DFS (Depth-First Search)?
6.2.
Can BFS be used to detect cycles in a graph?
6.3.
Is BFS always the best choice for finding the shortest path?
7.
Conclusion
Last Updated: Apr 30, 2024
Medium

# Breadth-First Search (BFS) in AI

Pallavi singh
0 upvote

## Introduction

Breadth-First Search (BFS) is a fundamental algorithm in artificial intelligence & computer science. It is used to explore a graph or tree data structure by visiting all the neighboring nodes at the current depth before moving on to the nodes at the next depth level. BFS is an important tool for solving various problems, such as finding the shortest path, checking graph connectivity, & traversing all the vertices of a graph.

In this article, we will discuss the applications, algorithm, example, complexity, & implementation of the BFS algorithm in detail.

## Applications of BFS Algorithm

1. Shortest Path in Unweighted Graphs: BFS is ideal for finding the shortest path in unweighted graphs, such as in routing algorithms where each hop has the same cost. This application is crucial for network designs where the shortest route from one node to another needs to be identified quickly and efficiently.

2. Puzzle Games: In gaming, BFS helps in solving puzzles where the goal is to find the minimum number of moves to reach a target state. It's extensively used in games like mazes or sliding puzzles, where each move is uniform and leads to a new state.

3. Social Networks: BFS plays a critical role in social networking platforms. It's used to calculate the degrees of separation between users, often seen in features like "friends of friends". This helps in understanding and visualizing the network's structure by exploring user connections layer by layer.

4. Web Crawling: Search engines use BFS for web crawling to index web pages. Starting from a source page, the crawler visits all linked pages at the current depth before moving to pages linked in the next layer, ensuring comprehensive indexing.

5. Machine Learning: In machine learning, BFS aids in building decision trees, especially for classification problems. The algorithm explores all possible next steps in the decision process, ensuring a thorough analysis of each attribute's impact.

6. Broadcast Networks: In networking, BFS is employed to manage the distribution of messages or data across a network, ensuring all nodes receive the broadcast without redundancies.

## BFS Algorithm

The BFS algorithm is a straightforward yet powerful approach for navigating graphs and trees. Here's how it works in a step-by-step manner:

1. Starting Point: BFS begins at a selected node, often referred to as the 'root'. This is the starting point where the algorithm begins to explore the graph.

2. Queue Initialization: A queue is used to manage the nodes during the traversal process. Initially, this queue contains just the root node.

3. Traversal Operations:The node at the front of the queue is examined and removed from the queue.Each neighbor of this node that has not been visited is added to the queue and marked as visited.

This prevents nodes from being revisited and keeps the algorithm efficient.

This process repeats for each node as it is taken from the front of the queue.

4. Layer-by-Layer Exploration: The key feature of BFS is its layer-by-layer exploration. It first explores all nodes at the present depth before moving to nodes at the next depth level. This ensures that if the graph contains multiple paths from the root to another node, BFS will find the shortest path in terms of the number of edges.

5. Completion: The algorithm continues until the queue is empty, meaning there are no more nodes to explore. At this point, every node reachable from the original root node has been explored.

The BFS algorithm is particularly useful in scenarios where you need to find the shortest path from a single source to all other nodes in an unweighted graph, or when you need to systematically visit all nodes in a graph.

## Implementation of BFS Algorithm

• Python

### Python

``from collections import dequedef bfs(graph, start):    # Create a queue to hold nodes to visit    queue = deque([start])    # Set to keep track of visited nodes to avoid revisiting them    visited = set([start])        print(f"Starting BFS from node: {start}")        # Continue until the queue is empty    while queue:        # Pop the first node from the queue        vertex = queue.popleft()        print(f"Visiting node: {vertex}")                # Process all adjacent nodes        for neighbor in graph[vertex]:            if neighbor not in visited:                # Mark the neighbor as visited and add it to the queue                visited.add(neighbor)                queue.append(neighbor)                print(f"Queueing {neighbor}")# Example graph represented as an adjacency listgraph = {    'A': ['B', 'C'],    'B': ['D', 'E'],    'C': ['F'],    'D': [],    'E': ['F'],    'F': []}# Call the bfs function with the graph and starting nodebfs(graph, 'A')``

Output

``````Starting BFS from node: A
Visiting node: A
Queueing B
Queueing C
Visiting node: B
Queueing D
Queueing E
Visiting node: C
Queueing F
Visiting node: D
Visiting node: E
Visiting node: F``````

### Explanation of the Code

1. Graph Representation: The graph is represented as a dictionary where each key is a node and the value is a list of nodes to which it is connected (its neighbors).

2. Queue: We use a deque (double-ended queue) from Python's collections module for efficient front removal operations.

3. Visited Nodes: A set is used to keep track of the nodes that have already been visited. This ensures each node is processed only once.

4. Traversal: The algorithm dequeues a node, prints it as visited, and then enqueues all its unvisited neighbors, marking them as visited in the process.

## Example of BFS algorithm:

Let's consider a simple example to understand how the BFS algorithm works. Suppose we have an undirected graph with 6 nodes labeled A, B, C, D, E, & F. The graph is represented using an adjacency list, where each node is associated with a list of its neighboring nodes.

We want to perform a BFS traversal starting from node A. Here's how the algorithm proceeds:

1. Start with node A & mark it as visited. Enqueue A into the queue.
• Queue: [A]

• Visited: {A}

1. Dequeue A from the queue & process it. Enqueue its unvisited neighbors B & C.
• Queue: [B, C]

• Visited: {A, B, C}

1. Dequeue B from the queue & process it. Enqueue its unvisited neighbors D & E.
• Queue: [C, D, E]

• Visited: {A, B, C, D, E}

1. Dequeue C from the queue & process it. It has no unvisited neighbors.
• Queue: [D, E]

• Visited: {A, B, C, D, E}

1. Dequeue D from the queue & process it. It has no unvisited neighbors.
• Queue: [E]

• Visited: {A, B, C, D, E}

1. Dequeue E from the queue & process it. Enqueue its unvisited neighbor F.
• Queue: [F]

• Visited: {A, B, C, D, E, F}

1. Dequeue F from the queue & process it. It has no unvisited neighbors.
• Queue: []

• Visited: {A, B, C, D, E, F}

1. The queue is empty, so the BFS traversal is complete.

The BFS traversal order for this example is: A, B, C, D, E, F.

### Complexity of BFS algorithm

The time & space complexity of the BFS algorithm depends on the size of the graph being traversed.

### Time Complexity

• In the worst case, BFS visits all the nodes & edges of the graph.

• The time complexity of BFS is O(V + E), where V is the number of vertices (nodes) & E is the number of edges in the graph.

• This is because BFS needs to visit each node once & process all its neighbors. In an adjacency list representation, processing the neighbors of a node takes O(1) time for each neighbor.

• Therefore, the total time complexity is the sum of the number of nodes & the number of edges in the graph.

### Space Complexity

The space complexity of BFS is O(V), where V is the number of vertices (nodes) in the graph.

• This is because BFS uses a queue to store the nodes to be visited & a set or array to keep track of the visited nodes.

• In the worst case, when all the nodes are reachable from the source node, the queue & the visited set can contain all the nodes of the graph.

• Additionally, if an adjacency list is used to represent the graph, it requires O(V + E) space to store the graph itself.

### What makes BFS different from DFS (Depth-First Search)?

BFS explores all nodes at the present depth level before moving on to nodes at the next depth level, ensuring it finds the shortest path in unweighted graphs. DFS, on the other hand, dives deep into one node of the graph before exploring its neighbors, which can lead to finding paths that are not shortest.

### Can BFS be used to detect cycles in a graph?

Yes, BFS can be used to detect cycles in graphs. During the traversal, if BFS encounters a node that has already been visited & is not the parent of the current node, a cycle is detected. This is particularly useful in applications like network routing and scheduling where cycle detection is crucial.

### Is BFS always the best choice for finding the shortest path?

BFS is optimal for finding the shortest path in unweighted graphs because it explores uniformly at all levels. However, in weighted graphs, algorithms like Dijkstra's or the A* algorithm are more suitable as they take into account the weights of the paths and can find the shortest path more efficiently in such scenarios.

## Conclusion

In this article, we have learned about the Breadth-First Search (BFS) algorithm, a fundamental tool in the field of computer science and artificial intelligence. We began by understanding the diverse applications of BFS, including its role in finding the shortest paths in unweighted graphs, aiding in puzzle solutions, analyzing social networks, web crawling, aiding machine learning models, and facilitating efficient data broadcast in networks.

We then talked about the algorithm's structure, highlighting its systematic approach to traversing graphs layer by layer to ensure  comprehensive and efficient exploration. Our practical code example demonstrated how BFS is implemented to navigate through a network of nodes, underscoring the algorithmâ€™s real-world applicability.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.