Skip to content

Instantly share code, notes, and snippets.

@alexgolec
Created September 2, 2019 02:46
Show Gist options
  • Save alexgolec/8e24f8eee5ff34e184e3ae0fdd8b9088 to your computer and use it in GitHub Desktop.
Save alexgolec/8e24f8eee5ff34e184e3ae0fdd8b9088 to your computer and use it in GitHub Desktop.
from collections import deque
def __dfs_helper(rate_graph, node, end, rate_from_origin, visited):
if node == end:
return rate_from_origin
visited.add(node)
for unit, rate in rate_graph.get_neighbors(node):
if unit not in visited:
rate = __dfs_helper(rate_graph, unit, end, rate_from_origin * rate, visited)
if rate is not None:
return rate
return None
def dfs(rate_graph, node, end):
return __dfs_helper(rate_graph, node, end, 1.0, set())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment