I have a dictionary representing a directed graph. For example...
myDict = {'foo': ['hello', 'stack'], 'bar' : ['over', 'flow']}
This means that the 'foo' node points to 'hello' and 'stack' while the 'bar' node points to 'over' and 'flow'.
I have also written code for performing a breadth first search to find the shortest path between any two nodes...
from collections import deque
def breadthFirstSearch(graph, start, end):
q = deque()
path = (start, )
q.append(path)
visited = set([start])
while q:
path = q.popleft()
last_node = path[-1]
if last_node == end:
return path
for node in graph[last_node]:
if node not in visited:
visited.add(node)
q.append(path + (node,))
print 'There is no path from ' + start + ' to ' + end + '.'
return None
My question is: Is it possible to modify this breadth first search so as it gives me the largest shortest path and the start and end nodes for that path?