I have been trying to use BFS to find the shortest path between 2 nodes. I have tried looking at tutorials and online readings but none of them were clear enough for me. I can only traverse through the graph currently, but I am not really sure on what checks do I need to have to find the minimum path between 2 nodes in a graph. The graph is unweighted and bidirectional. This is my code to traverse through all the nodes in the graph using BFS.
def bredthfirstsearch(start, end, graph):
queue = [start]
visited = [False]*(roads+1) #marks if a node has been visited
visited[start] = True
while len(queue) != 0:
# source node popped and later used to find neighboring nodes
node = queue.pop(0)
for item in graph[node]:
# if the node has not been found previously
if visited[item[0]] == False:
queue.append(item[0]) # it is added to the queue for future checking on its neighbors
visited[item[0]] = True #node then set to true
Now I may need an additional array to store the shortest path but I am not sure how to do that. Thank you so much.