Breadth-first search is a graph traversal algorithm

HiteshMohapatra 157 views 26 slides Dec 11, 2024
Slide 1
Slide 1 of 26
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
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
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.


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.

AppendFto theVisitedandBFS. Finally, popFfrom
theFrontier.

AppendKto theVisitedandBFS, then appendK’s neighbor (L)
to theFrontier. Finally, popKfrom theFrontier.

AppendIto theVisitedandBFS, then appendI’s neighbor (J) to
theFrontier. Finally, popIfrom theFrontier.

AppendLto theVisitedandBFS, then appendL’s neighbor (M)
to theFrontier. Finally, popLfrom theFrontier.

AppendJto theVisitedandBFS. Finally, popJfrom
theFrontier.

AppendMto theVisitedandBFS, then appendM’s neighbor
(N) to theFrontier. Finally, popMfrom theFrontier.

AppendNto theVisitedandBFS. Finally, popNfrom
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)

Code (Python)
Link: https://github.com/hm18818/AI-with-Python.git