我有一个表示有向图的字典。例如...
myDict = {'foo': ['hello', 'stack'], 'bar' : ['over', 'flow']}
这意味着'foo'节点指向'hello'和'stack'而'bar'节点指向'over'和'flow'。
我还编写了用于执行广度优先搜索以找到任何两个节点之间的最短路径的代码......
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
我的问题是:是否可以修改此广度优先搜索,以便为我提供最大的最短路径以及该路径的起始节点和结束节点?