Route Finder Using Bi-Directional BFS/DFS

460 views 11 slides Jan 02, 2025
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

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.


Slide Content

Bi-Directional BFS/DFS
Dr Hitesh Mohapatra
AssociateProfessor
AI-LAB : Assignment 3

Assignment 3: Route Finder Using Bi-
Directional BFS/DFS
Objective:UseBi-directionalBFS/DFStosolveanavigationproblem.
ProblemStatement:Representacitymapasagraphwhereintersectionsare
nodesandroadsareedges.Findtheshortestpathbetweentwolocations.
Tasks:
•ImplementBi-directionalBFStominimizethenumberofnodesexplored.
•ComparetheperformanceofBi-directionalBFSwithstandardBFSand
DFS.
•Visualizethesearchprocess(e.g.,usingalibrarylikenetworkxinPython).

What is Bi-Directional BFS?
•Insteadofsearchingfromjust
thesourcenode,Bi-Directional
BFSsearchesfromboththe
startandtarget.
•Thesearchstopswhenthetwo
searchesmeetinthemiddle.
•Significantlyreduces the
numberofnodesexplored.

Bi-Directional BFS Implementation:
Bi-directionalBFSworksbysimultaneouslysearchingfromboththesource
andthetargetnodes.Thesearchmeetsinthemiddle,potentiallyreducing
thenumberofexplorednodesandspeedingupthesearch.Thisis
particularlyeffectiveforlargegraphs.
StepsforBi-DirectionalBFS:
1.Initializetwoqueues:onestartingfromthesourceandtheotherfrom
thetarget.
2.Expandnodesalternatelyfrombothends,markingvisitednodesforboth
forwardandbackwardsearches.
3.Ifanodeisvisitedbybothsearches,thealgorithmstops,andthepathis
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