def BFS(gr, s):
 if (not gr.has_node(s)):
  raise Exception(('Node %s not in graph' % s))
 nodes_explored = set([s])
 q = deque([s])
 while (len(q) != 0):
  node = q.popleft()
  for each in gr.neighbors(node):
   if (each not in nodes_explored):
    nodes_explored.add(each)
    q.append(each)
 return nodes_explored

def BFS ( gr , s ) : if ( not gr . has_node ( s ) ) : raise Exception ( ( 'Node %s not in graph' % s ) ) nodes_explored = set ( [ s ] ) q = deque ( [ s ] ) while ( len ( q ) != 0 ) : node = q . popleft ( ) for each in gr . neighbors ( node ) : if ( each not in nodes_explored ) : nodes_explored . add ( each ) q . append ( each ) return nodes_explored
breadth first search returns a list of nodes that are "findable" from s .