Skip to content

Instantly share code, notes, and snippets.

@alexgolec
Last active September 4, 2019 13:37
Show Gist options
  • Save alexgolec/12fd2a37f197511c251b7551ec122c9c to your computer and use it in GitHub Desktop.
Save alexgolec/12fd2a37f197511c251b7551ec122c9c to your computer and use it in GitHub Desktop.
from collections import deque
def bfs(rate_graph, start, end):
to_visit = deque()
to_visit.appendleft( (start, 1.0) )
visited = set()
while to_visit:
node, rate_from_origin = to_visit.pop()
if node == end:
return rate_from_origin
visited.add(node)
for unit, rate in rate_graph.get_neighbors(node):
if unit not in visited:
to_visit.appendleft((unit, rate_from_origin * rate))
return None
@arhoads
Copy link

arhoads commented Sep 3, 2019

Shouldn't the pop be a popleft? That or your to_visit.append needs to be an append_left.

@alexgolec
Copy link
Author

Good eye, corrected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment