search_graph = { 's': ['a', 'd'], 'a': ['b', 'c', 's'], 'b': ['a'], 'c': ['a'], 'd': ['e', 'h', 's'], 'e': ['f', 'g', 'd'], 'f': ['e'], 'g': ['e'], 'h': ['i', 'j', 'd'], 'i': ['h'], 'j': ['h'], } vertex = search_graph.keys() open_list = ['s',] closed_list = [] parent = {} parent['s'] = None while open_list: print open_list n = open_list.pop(0) #print n, closed_list.append(n) for m in search_graph[n]: if m not in closed_list: open_list.append(m) parent[m] = n print parent route = [] tb = 'g' while tb: route.append(tb) tb = parent[tb] route.reverse() print route