Yes, this is DFS.
To write BFS, you just need to save the "todo" queue. You probably also want to include the function in the generator, because often BFS intentionally ends before it generates all possible paths. Thus, this function can be used to search for path_path or find_all_paths.
def paths(graph, start, end): todo = [[start, [start]]] while 0 < len(todo): (node, path) = todo.pop(0) for next_node in graph[node]: if next_node in path: continue elif next_node == end: yield path + [next_node] else: todo.append([next_node, path + [next_node]])
And an example of how to use it:
graph = {'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['C'], 'E': ['F'], 'F': ['C']} for path in paths(graph, 'A', 'D'): print path
source share