2
我有一個腳本,它使用鄰接圖和BFS算法來查找兩點之間的路徑。有圖有大約10,000頂點和腳本設置這樣的:Python凍結路徑查找
graph = {...
'9660': ['9661', '9662', '9659'],
'9661': ['9654', '9660'],
'9662': ['9660', '9663'],
'9663': ['9664', '9662'],
'9664': ['9665', '9663'],
...}
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)
print bfs(graph, '50', '8659')
因爲這種算法適用於小鄰接圖,我猜蟒蛇只是需要一個很長的時間來處理這個大小的圖表。我的目標是找到最短的路徑,但是如果我甚至找不到一條路徑,那目前是不可能的。是否有python解決方案來處理使用大型鄰接圖的路徑查找?如果是這樣,我可以舉個例子嗎?
最短路徑從哪裏到哪裏? – irrelephant
首先使用深度,而你至少會找到一條我認爲的路徑...也許使它成爲一個發電機,因爲它也可能... –
我認爲你沒有跟蹤你已經訪問過的節點? – irrelephant