def depth_search(graph, current, target, previous = None): if current == target: return (current,) for next in graph.neighbors(current): if next != previous: path = search_nodes(graph, next, target, current) if path: return (current,) + path return None