adj={
'S':['A','B','D','E'],
'A':['S'],
'B':['S','C','D'],
'C':['B','J'],
'D':['B','G','S'],
'E':['G','S'],
'F':['G','H'],
'G':['D','E','F','J','H'],
'H':['G','F','I'],
'I':['H','J'],
'J':['I','C','G']
}
def dfs(start,end):
vis=set()
vis.add(start)
stack=[(start,[start])]
while stack:
node,path=stack.pop()
if(node==end):
return path
for child in adj[node]:
if child not in vis:
vis.add(child)
stack.append((child,path+[child]))
start="S"
end="I"
print("BFS solution : ")
print(dfs(start,end))
0 Comments