Bi-Directional BFS/DFS
Dr Hitesh Mohapatra
Associate Professor
AI-LAB : Assignment 3
Assignment 3: Route Finder Using Bi-
Directional BFS/DFS
Objective: Use Bi-directional BFS/DFS to solve a navigation problem.
Problem Statement: Represent a city map as a graph where intersections are
nodes and roads are edges. Find the shortest path between two locations.
Tasks:
• Implement Bi-directional BFS to minimize the number of nodes explored.
• Compare the performance of Bi-directional BFS with standard BFS and
DFS.
• Visualize the search process (e.g., using a library like networkx in Python).
What is Bi-Directional BFS?
• Instead of searching from just
the source node, Bi-Directional
BFS searches from both the
start and target.
• The search stops when the two
searches meet in the middle.
• Significantly reduces the
number of nodes explored.
Bi-Directional BFS Implementation:
Bi-directional BFS works by simultaneously searching from both the source
and the target nodes. The search meets in the middle, potentially reducing
the number of explored nodes and speeding up the search. This is
particularly effective for large graphs.
Steps for Bi-Directional BFS:
1. Initialize two queues: one starting from the source and the other from
the target.
2. Expand nodes alternately from both ends, marking visited nodes for both
forward and backward searches.
3. If a node is visited by both searches, the algorithm stops, and the path is
reconstructed.
Process
It runs two simultaneous search –
1. Forward search from source/initial vertex toward goal vertex
2. Backward search from goal/target vertex toward source
vertex
Example
Suppose we want to find if
there exists a path from vertex 0
to vertex 14. Here we can
execute two searches, one from
vertex 0 and other from vertex
14. When both forward and
backward search meet at vertex
7, we know that we have found
a path from node 0 to 14 and
search can be terminated now.
We can clearly see that we
have successfully avoided
unnecessary exploration.
When to use bidirectional approach?
We can consider bidirectional approach when-
1. Both initial and goal states are unique and completely
defined.
2. The branching factor is exactly the same in both directions.
Algorithm
while sq != empty and tq != empty
perform next iteration for sq
perform next iteration for tq
save the root of a leaf node in the root array
if we have visited the node of intersection
save it
break
using node of intersection and root node, find the path
Why bidirectional approach?
Comparing BFS and bi-directional BFS.
https://github.com/hm18818/AI-with-Python/blob/main/Bi-Directional%20BFS

Route Finder Using Bi-Directional BFS/DFS

  • 1.
    Bi-Directional BFS/DFS Dr HiteshMohapatra Associate Professor AI-LAB : Assignment 3
  • 2.
    Assignment 3: RouteFinder Using Bi- Directional BFS/DFS Objective: Use Bi-directional BFS/DFS to solve a navigation problem. Problem Statement: Represent a city map as a graph where intersections are nodes and roads are edges. Find the shortest path between two locations. Tasks: • Implement Bi-directional BFS to minimize the number of nodes explored. • Compare the performance of Bi-directional BFS with standard BFS and DFS. • Visualize the search process (e.g., using a library like networkx in Python).
  • 3.
    What is Bi-DirectionalBFS? • Instead of searching from just the source node, Bi-Directional BFS searches from both the start and target. • The search stops when the two searches meet in the middle. • Significantly reduces the number of nodes explored.
  • 4.
    Bi-Directional BFS Implementation: Bi-directionalBFS works by simultaneously searching from both the source and the target nodes. The search meets in the middle, potentially reducing the number of explored nodes and speeding up the search. This is particularly effective for large graphs. Steps for Bi-Directional BFS: 1. Initialize two queues: one starting from the source and the other from the target. 2. Expand nodes alternately from both ends, marking visited nodes for both forward and backward searches. 3. If a node is visited by both searches, the algorithm stops, and the path is reconstructed.
  • 5.
    Process It runs twosimultaneous search – 1. Forward search from source/initial vertex toward goal vertex 2. Backward search from goal/target vertex toward source vertex
  • 6.
    Example Suppose we wantto find if there exists a path from vertex 0 to vertex 14. Here we can execute two searches, one from vertex 0 and other from vertex 14. When both forward and backward search meet at vertex 7, we know that we have found a path from node 0 to 14 and search can be terminated now. We can clearly see that we have successfully avoided unnecessary exploration.
  • 7.
    When to usebidirectional approach? We can consider bidirectional approach when- 1. Both initial and goal states are unique and completely defined. 2. The branching factor is exactly the same in both directions.
  • 8.
    Algorithm while sq !=empty and tq != empty perform next iteration for sq perform next iteration for tq save the root of a leaf node in the root array if we have visited the node of intersection save it break using node of intersection and root node, find the path
  • 9.
  • 10.
    Comparing BFS andbi-directional BFS.
  • 11.