Uninformed BFS

 def BFS_SP(graph,start,goal): 

explored=[] 

queue=[[start]] 

if start==goal: 

print("Same node") 

return 

while queue: 

path=queue.pop(0) 

node=path[-1] 

if node not in explored: 

neighbours=graph[node] 

for neighbour in neighbours: 

new_path=list(path) 

new_path.append(neighbour) 

queue.append(new_path) 

if neighbour==goal: 

print("Shortst path= ", *new_path) 

return 

explored.append(node) 

print("So sorry connecting path doesn't exist :(") 

if __name__ =="__main__": 

graph = {'A': ['B', 'E', 'C'], 

'B': ['A', 'D', 'E'], 

'C': ['A', 'F', 'G'], 

'D': ['B', 'E'], 

'E': ['A', 'B', 'D'], 

'F:': ['C'], 

'G': ['C']} 

BFS_SP(graph,'B','G')

Comments

Popular posts from this blog

GLASS ELECTRODE

HIGH PERFORMANCE LIQUID CHROMATOGRAPHY

CHROMATOGRAPHY