25 Jan 2013 sness   » (Journeyer)

python - Breadth-first search trace path - Stack Overflow

python - Breadth-first search trace path - Stack Overflow: "# graph is in adjacent list representation
graph = {
'1': ['2', '3', '4'],
'2': ['5', '6'],
'5': ['9', '10'],
'4': ['7', '8'],
'7': ['11', '12']

def bfs(graph, start, end):
# maintain a queue of paths
queue = []
# push the first path into the queue
while queue:
# get the first path from the queue
path = queue.pop(0)
# get the last node from the path
node = path[-1]
# path found
if node == end:
return path
# enumerate all adjacent nodes, construct a new path and push it into the queue
for adjacent in graph.get(node, []):
new_path = list(path)

print bfs(graph, '1', '11')"

'via Blog this'

Syndicated 2013-01-24 23:20:00 from sness

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!