I'm having trouble parsing through this graph dictionary:
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)]}
How do I separate out the values of each edge while running the dictionary through a DFS Search looking at 2 keys? I'm not very familiar with dict.
So far I have,
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)
I need to return the smallest weighted path, so I need the numbers in the values separated out and summed as the program runs.
defaultdict(dict)is convenient. If you need something faster, there are other data structures which may be slightly less convenient but much faster.