Skip to content

Instantly share code, notes, and snippets.

@aeter
Created April 15, 2010 08:36
Show Gist options
  • Save aeter/366844 to your computer and use it in GitHub Desktop.
Save aeter/366844 to your computer and use it in GitHub Desktop.
import Queue
# prints a breadth-first-search from the starting node
def bfs(node):
visited = set()
visited.add(node)
q = Queue.Queue()
q.put(node)
while not q.empty():
current = q.get()
for nodes in graph[current]:
if nodes not in visited:
print nodes,
q.put(nodes)
visited.add(nodes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment