[英]Large graph as input to find all paths
我正在使用以下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邊緣的大圖。 該程序入侵並且不返回任何輸出。
您可以幫助我如何設置輸入圖表,以及如何設置輸出以添加到單獨的文件中?
你的答案是它可能無法完成 , 除了你的圖形是一個特殊的圖形,這種圖形中兩個節點之間的路徑數量可能是巨大的。考慮以下情況:對於200個頂點和20K邊緣的完整圖形在任意兩個頂點之間是198!/ 2個不同的路徑。 如果你的圖形包含一個循環,那么它就有無限的路徑。
您的圖形可能在兩個頂點之間具有這樣的路徑數,即使超級計算機也無法計算十年內的數字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.