#Breadth First Search
graph = {
'S' : ['A','B'],
'A' : ['B','C','D'],
'B' : ['C'],
'C' : ['D'],
'D' : []
}
visited = []
queue = []
goal = 'D'
def bfs(visited, graph, node):
visited.append(node)
queue.append(node)
while queue :
s = queue.pop(0)
print(s, end = "\n")
for neighbour in graph[s]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
if goal in visited:
break
bfs(visited,graph,'S')
#above mentioned code is for path of traversal. Can anyone please give the code to find the shortest path. The output of the above code is S A B C D