# Code from Chapter 16 of Machine Learning: An Algorithmic Perspective (2nd Edition)
# by Stephen Marsland (http://stephenmonika.net)
# You are free to use, change, or redistribute the code in any way you wish for
# non-commercial purposes, but please maintain the name of the original author.
# This code comes with no warranty of any kind.
# Stephen Marsland, 2008, 2014
def findPath(graph, start, end, pathSoFar):
pathSoFar = pathSoFar + [start]
if start == end:
return pathSoFar
if start not in graph:
return None
for node in graph[start]:
if node not in pathSoFar:
newpath = findPath(graph, node, end, pathSoFar)
return newpath
return None
graph = {'A': ['B', 'C'],'B': ['C', 'D'],'C': ['D'],'D': ['C'],'E': ['F'],'F': ['C']}
print findPath(graph,'A','D',[])