我無法通過這個圖表字典解析:Python的DFS最短路徑與加權搜索,無向圖
routes = {'a': [('b', 5.0), ('c', 8.0)], 'c': [('a', 8.0), ('d', 2.0)],
'b' [('a', 5.0), ('d', 6.0)], 'e': [('d', 12.0), ('g', 3.0)],
'd': [('b', 6.0),('c', 2.0), ('e', 12.0), ('f', 2.0)], 'g': [('e', 3.0),
('f', 7.0)],'f': [('d',2.0), ('g', 7.0)]}
如何分離出每條邊的價值通過DFS搜索運行字典一邊尋找在2鍵?我對dict不是很熟悉。
到目前爲止,我有,
def dfs(graph, start, end, path):
path = path + [start]
if start == end:
paths.append(path)
for node in graph.childrenOf(start):
if node not in path:
dfs(graph, node, end, path)
我需要回到最小加權路徑,所以我需要在分離出來,在程序運行時總結出的值的數字。
你想要什麼字典?鍵,值或部分值(哪些部分)? – nbro 2015-02-06 21:12:57
鍵是冒號左邊的東西,右邊的東西是,你猜對了,我需要返回最小的加權路徑的值 – nbro 2015-02-06 21:17:18
,所以我需要在程序運行時將數值分離出來並相加。 – 2015-02-06 21:18:57