Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Er No Tmplemetatton o bondth, Fist Soarch ond

Date Uniovm Cost Search

Afm:
To pevsovm Breadth inst sooch ond Uniçorm
Cost Seaich sime for the given graph using

Python
Alqovithm Fvetices and ac weiqhted
8F. Get the nD.
from the useY
91apb as adja cency matne
a sel
sele
queue and
2.
2 Ceate an empt y
to keep tiack oF visited nodes
node to the gueue
3. Add the stasling
a nd mank it as visited
While fhe ueue is not empty
fYom the front
a)Dequeue a node
he Queue the qoal, totwn
b) I the node

node
the
all the unvsit ed
cElse, itevate thvo ugh
the node and ndd Them to
nei qhbours oF as
Mavk ea eh neighboun
the 9ueue
visited
an d he go
the que ue s empty
5. I
rot arn Oone O an
been found,
has not
the gpal was not
in di catiom that
Teached
olP
Sample TlP
&roph otce No. o vovtices Total cest- 9
Solutron poath
Input the adj Matix
o, ,,5.6)
frovevso ot
=(o,,23,4,5,,
2 0 o O

O O 2 O O
2

node 0
Enlo the starting -

Enter the goa node 6

BFS Breadth First Seaych


T-2
Iteration -I:

3
(A) 4
4
2

Trover Sol poth ABe G


AcBD °EFG-H
9oln Path ALcfr?F!G

Total eost 8
Alaovithm
Unioim Cosl Soavch
vovtices and oighted
. Get 1he Dumber o
Ugen
malix fiom
91qph as 0djacency to
Quouo (haop in Pythen)
, Cteate a piovid y
visited and
nodes to be
keep rack visited nodes
track oF the
get to keep
stavting node to the pricnit y
3. Add the
maik it
and
ueue with Cost

visited not empty


the
prioity ueue
While the lowest cast
with the
the node
)fop
que ve return the node
the prionity
f1om the goal,
b)f hode

and its Cost


all he
c) Else, iterate thyouqh add fhem to

nod e and
Unvisited neighbours upd dted
Ces-
their
pionity Queue
wth
the
neigh bour aS visited
Hak each
is
and
emptyand
empty
the
he
ueue
the priovity
5 1 retuxn
Teluin none
none
o on

been found,
has
has not
goal
goal

the gal
hot reached
indicatron
thaf
UCS Unifoim
Cost Seavdh

Ttoation I Jteation-| Tteration Jeration V

A)
3

A A C-B
A-C-E-B /2
Jtoiation - A-c-B-E-F

Cor
GH
A -C-a ED F -

haveical path

Sclution path A cs EFa

Total cost 8
ofP
omple IP:
Solution poth . , 2, 4,5,6J
Veilices: 8
No. or
Tiaversal path- [o,1, 1, 4,3,5,6]
Adj matrx
3 o 00 0 O
DDO 4 o0 o

o0
OO
0O 2 0

ooo oo 5

nodo- 0
ntor clotiq
node 6
Scal

You might also like