Sei sulla pagina 1di 13

Breadth First Search

BREADTH FIRST SEARCH (BFS) SEARCHES BREADTH-WISE IN THE


PROBLEM SPACE. BREADTH-FIRST SEARCH IS LIKE TRAVERSING
A TREE WHERE EACH NODE IS A STATE WHICH MAY A BE A
POTENTIAL CANDIDATE FOR SOLUTION. IT EXPANDS NODES
FROM THE ROOT OF THE TREE AND THEN GENERATES ONE LEVEL
OF THE TREE AT A TIME UNTIL A SOLUTION IS FOUND. IT IS
VERY EASILY IMPLEMENTED BY MAINTAINING A QUEUE OF
NODES. INITIALLY THE QUEUE CONTAINS JUST THE ROOT. IN
EACH ITERATION, NODE AT THE HEAD OF THE QUEUE IS
REMOVED AND THEN EXPANDED. THE GENERATED CHILD NODES
ARE THEN ADDED TO THE TAIL OF THE QUEUE.

ALGORITHM:BREADTH-FIRSTSEARCH

CreateavariablecalledNODE-LISTandsetittothe

initialstate.
LoopuntilthegoalstateisfoundorNODE-LISTis
empty.

Removethefirstelement,sayE,fromtheNODE-LIST.IfNODELISTwasemptythenquit.
ForeachwaythateachrulecanmatchthestatedescribedinEdo:

i)Applytheruletogenerateanewstate.

ii)Ifthenewstateisthegoalstate,quitandreturn
thisstate.
iii)OtherwiseaddthisstatetotheendofNODE-LIST

Sinceitnevergeneratesanodeinthetreeuntilallthe

nodesatshallowerlevelshavebeengenerated,breadthfirst searchalwaysfindsashortestpathtoagoal.
Sinceeachnodecanbegeneratedinconstanttime,the
amountoftimeusedbyBreadthfirstsearchis
proportionaltothenumberofnodesgenerated,whichis
afunctionofthebranchingfactorbandthesolutiond.
Sincethenumberofnodesatleveldisb d,thetotal
numberofnodesgeneratedintheworstcaseisb+b 2+
b3++bdi.e.O(bd),theasymptotictimecomplexityof
breadthfirstsearch.

Lookattheabovetreewithnodesstartingfromroot

node,Ratthefirstlevel,AandBatthesecondlevel
andC,D,EandFatthethirdlevel.Ifwewantto
searchfornodeEthenBFSwillsearchlevelbylevel.
FirstitwillcheckifEexistsattheroot.Thenitwill
checknodesatthesecondlevel.FinallyitwillfindE
athethirdlevel.

ADVANTAGESOFBREADTH-FIRSTSEARCH
Breadthfirstsearchwillnevergettrappedexploring

theuselesspathforever.
Ifthereisasolution,BFSwilldefinitelyfinditout.
IfthereismorethanonesolutionthenBFScanfind
theminimalonethatrequireslessnumberofsteps.

DISADVANTAGESOFBREADTH-FIRSTSEARCH

ThemaindrawbackofBreadthfirstsearchisits

memoryrequirement.Sinceeachlevelofthetreemust
besavedinordertogeneratethenextlevel,andthe
amountofmemoryisproportionaltothenumberof
nodesstored,thespacecomplexityofBFSisO(b d).Asa
result,BFSisseverelyspace-boundinpracticesowill
exhaustthememoryavailableontypicalcomputersina
matterofminutes.
Ifthesolutionisfartherawayfromtheroot,breathfirst
searchwillconsumelotoftime.

DepthFirstSearch(DFS)
DepthFirstSearch(DFS)searchesdeeperintothe

problemspace.Breadth-firstsearchalwaysgenerates
successorofthedeepestunexpandednode.Ituses
last-infirst-outstackforkeepingtheunexpanded
nodes.Morecommonly,depth-firstsearchis
implementedrecursively,withtherecursionstack
takingtheplaceofanexplicitnodestack.

ALGORITHM:DEPTHFIRSTSEARCH

1.Iftheinitialstateisagoalstate,quitandreturn

success.
2.Otherwise,loopuntilsuccessorfailureissignaled.
a)Generateastate,sayE,andletitbethesuccessor
oftheinitialstate.Ifthereisnosuccessor,signal
failure.
b)CallDepth-FirstSearchwithEastheinitialstate.
c)Ifsuccessisreturned,signalsuccess.Otherwise
continueinthisloop.

ADVANTAGESOFDEPTH-FIRSTSEARCH

Theadvantageofdepth-firstSearchisthatmemoryrequirementisonly

linearwithrespecttothesearchgraph.Thisisincontrastwith
breadth-firstsearchwhichrequiresmorespace.Thereasonisthatthe
algorithmonlyneedstostoreastackofnodesonthepathfromtheroot
tothecurrentnode.

Thetimecomplexityofadepth-firstSearchtodepthdisO(b^d)sinceit
generatesthesamesetofnodesasbreadth-firstsearch,butsimplyina
differentorder.Thuspracticallydepth-firstsearchistime-limitedrather
thanspace-limited.

Ifdepth-firstsearchfindssolutionwithoutexploringmuchinapaththen
thetimeandspaceittakeswillbeveryless.

DISADVANTAGESOFDEPTH-FIRSTSEARCH

ThedisadvantageofDepth-FirstSearchisthatthereis

apossibilitythatitmaygodowntheleft-mostpath
forever.Evenafinitegraphcangenerateaninfinite
tree.Onesolutiontothisproblemistoimposeacutoff
depthonthesearch.Althoughtheidealcutoffisthe
solutiondepthdandthisvalueisrarelyknownin
advanceofactuallysolvingtheproblem.Ifthechosen
cutoffdepthislessthand,thealgorithmwillfailto
findasolution,whereasifthecutoffdepthisgreater
thand,alargepriceispaidinexecutiontime,andthe
firstsolutionfoundmaynotbeanoptimalone.

Depth-FirstSearchisnotguaranteedtofindthe

solution.

Andthereisnoguaranteetofindaminimalsolution,
ifmorethanonesolutionexists.

Thank You !!!

Potrebbero piacerti anche