Breadth-first search is a graph traversal algorithm
HiteshMohapatra
157 views
26 slides
Dec 11, 2024
Slide 1 of 26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
About This Presentation
Breadth-first search is a graph traversal algorithm that starts traversing the graph from the root node and explores all the neighboring nodes.
Size: 1.57 MB
Language: en
Added: Dec 11, 2024
Slides: 26 pages
Slide Content
BFS in AI
Lab Assignment 1
Dr.HiteshMohapatra
AssociateProfessor
School of Computer Engineering
KIIT Deemed to be University
Assignment-1: Maze Solver using BFS and DFS
Objective:ImplementBFSandDFStosolveamaze.
ProblemStatement:Givenagrid-basedmazewhere0representswallsand1representswalkablepaths,findthe
shortestpathfromastartcelltoanendcell.
Tasks:
•UseBFStofindtheshortestpath.
•UseDFStoexploreallpossiblepathsandreportonevalidpath(notnecessarilytheshortest).
•ComparethenumberofnodesexploredbyBFSandDFS.
AI Search
ArtificialIntelligencecanbereferredtoasanagentthatcanact
rationally,itcanperformsomekindofsearchalgorithmtoachieveits
tasksinsolvingasearchproblemsuchasaMazeproblem.
A search problem in Artificial Intelligence
consists of:
•State Spacerefers to all possible states where the agent can be.
•Start Staterefers to the state where the search begins.
•Goal Staterefers to the state where the search ends.
•Solutionrefers to the sequence of actions that shows how an agent
searches from the start state to the goal state.
•Costrefers to the effort of the agent in doing the searching task.
There are several algorithms can be used to
solve a search problem, such as:
•Breadth First Search (BFS)
•Depth First Search (DFS)
•Greedy Best First Search (GBFS)
•A* Search
Breadth First Search (BFS)
TheBreadthFirstSearch(BFS)algorithmisusedtosearchagraphdata
structureforanodethatmeetsasetofcriteria.Itstartsattherootof
thegraphandvisitsallnodesatthecurrentdepthlevelbeforemoving
ontothenodesatthenextdepthlevel.
Inshort,BFSwillvisitallcurrentdepthlevelnodesbeforeproceeding
tothenodesatthenextlevelofdepth.
Pseudocode of the algorithm:
1.Inputgraph,start node, andgoal node
2.Definesolutionas a list
3.Definefrontieras a queue
4.Definevisitedas a list
5.Appendstart nodetofrontierandvisited
6.While thefrontieris not empty:
7.Dequeue thefrontierand store the value in theselected node
8.If theselected nodeis equal to thegoal node, append theselected nodeto thesolution, break from the loop, and return thesolution.
9.If not, just append theselected nodeto thesolution.
10.Loop through allneighborsof theselected nodefrom thegraph, append eachneighborto thefrontierandvisitedif theneighboris not yet in
thevisitedlist.
Solve Maze Problem with BFS
What is thesolutionandcostof the above Maze problem if we want to move fromAtoN?
Graph representation of Maze
Below is the step-to-step visualization of solving
the search problem using the BFS algorithm:
AppendAto theFrontier.
AppendAto theVisitedandBFS, then appendA’s neighbor (B)
to theFrontier. Finally, popAfrom theFrontier.
AppendBto theVisitedandBFS, then appendB’s neighbor (C)
to theFrontier. Finally, popBfrom theFrontier.
AppendCto theVisitedandBFS, then appendC’s neighbor (D,
G) to theFrontier. Finally, popCfrom theFrontier.
AppendDto theVisitedandBFS, then appendD’s neighbor (E)
to theFrontier. Finally, popDfrom theFrontier.
AppendGto theVisitedandBFS, then appendG’s neighbor (H)
to theFrontier. Finally, popGfrom theFrontier.
AppendEto theVisitedandBFS, then appendE’s neighbor (F)
to theFrontier. Finally, popEfrom theFrontier.
AppendHto theVisitedandBFS, then appendH’s neighbor (K,
I) to theFrontier. Finally, popHfrom theFrontier.
The answer to the above question will be:
•Solution: [A, B, C, D, G, E, H, F, K, I, L, J, M, N]
•Costs: 13 (there are 13 nodes to step to reach theend nodefrom
thestart node)