Академический Документы
Профессиональный Документы
Культура Документы
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.