<s> from collections import deque <EOL> def dfs ( graph , start ) : <EOL> stack = [ iter ( [ start ] ) ] <EOL> seen = set ( ) <EOL> while stack : <EOL> node = next ( stack [ - <NUM_LIT:1> ] , None ) <EOL> if node is None : <EOL> stack . pop ( ) <EOL> elif node not in seen : <EOL> seen . add ( node ) <EOL> stack . append ( iter ( graph [ node ] ) ) <EOL> yield node <EOL> def dfs_paths ( graph , start ) : <EOL> stack = [ ( iter ( [ start ] ) , [ ] ) ] <EOL> while stack : <EOL> it , path = stack [ - <NUM_LIT:1> ] <EOL> node = next ( it , None ) <EOL> if node is None : <EOL> stack . pop ( ) <EOL> elif node not in path : <EOL> adj = graph [ node ] <EOL> if adj : <EOL> stack . append ( ( iter ( adj ) , path + [ node ] ) ) <EOL> else : <EOL> yield path + [ node ] <EOL> def bfs ( graph , start ) : <EOL> queue = deque ( [ start ] ) <EOL> seen = { start } <EOL> while queue : <EOL> node = queue . popleft ( ) <EOL> yield node <EOL> queue += ( n for n in graph [ node ] if n not in seen ) <EOL> seen . update ( graph [ node ] ) <EOL> def bfs_paths ( graph , start ) : <EOL> queue = deque ( [ [ start ] ] ) <EOL> while queue : <EOL> path = queue . popleft ( ) <EOL> adj = [ path + [ node ] for node in graph [ path [ - <NUM_LIT:1> ] ] if node not in path ] <EOL> if adj : <EOL> queue += adj <EOL> else : <EOL> yield path </s>
