Straight from Guido van Rossum to you:
import collections
import itertools as IT
def find_shortest_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if start not in graph:
return None
shortest = None
for node in graph[start]:
if node not in path:
newpath = find_shortest_path(graph, node, end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
G={'a':['b','c'], 'c':['d']}
for node1, node2 in IT.combinations(list('abcd'), 2):
print('{} -> {}: {}'.format(node1, node2, find_shortest_path(G, node1, node2)))
yields
a -> b: ['a', 'b']
a -> c: ['a', 'c']
a -> d: ['a', 'c', 'd']
b -> c: None
b -> d: None
c -> d: ['c', 'd']
You might also be interested in the networkx, igraph or graph-tool packages.