Merge pull request #1 from GAbdulWahab/ghada-1

added Breadth-first search in python #734
pull/739/head
GAbdulWahab 2022-04-10 00:54:25 +02:00 committed by GitHub
commit 0f3092659d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,32 @@
# this graph to check the algorithm
graph={
'S':['B','D','A'],
'A':['C'],
'B':['D'],
'C':['G','D'],
'S':['G'],
}
#function of BFS
def BFS(graph,start,goal):
Visited=[]
queue=[[start]]
while queue:
path=queue.pop(0)
node=path[-1]
if node in Visited:
continue
Visited.append(node)
if node==goal:
return path
else:
adjecent_nodes=graph.get(node,[])
for node2 in adjecent_nodes:
new_path=path.copy()
new_path.append(node2)
queue.append(new_path)
Solution=BFS(graph,'S','G')
print('Solution is ',Solution)