【发布时间】:2016-07-18 08:24:11
【问题描述】:
我正在使用以下 python 代码来查找每两个节点之间的所有路径。小图没问题。
def bfs(graph, start, end):
# maintain a queue of paths
queue = []
# push the first path into the queue
queue.append([start])
while queue:
# get the first path from the queue
path = queue.pop(0)
# get the last node from the path
node = path[-1]
# path found
if node == end:
return path
# enumerate all adjacent nodes, construct a new path and push it into the queue
for adjacent in graph.get(node, []):
new_path = list(path)
new_path.append(adjacent)
queue.append(new_path)
for node3 in graph:
for node4 in graph:
few = bfs(graph, node3, node4)
if not few == None:
print ("{0} -> {1}".format(node3,node4))
print (few)
print ("\n")
但是,对于具有大约 4K 节点和 20K 边的大图,我想找到每两个节点之间的所有路径。程序中断,不返回任何输出。
能否请您帮助我如何设置输入图以及如何设置输出以添加到单独的文件中?
【问题讨论】:
标签: python graph shortest-path bigdata