Skip to content

Instantly share code, notes, and snippets.

@hankliu5
Created April 27, 2017 19:54
Show Gist options
  • Save hankliu5/ddc5c4df66296e87995e7dc5dfa95dec to your computer and use it in GitHub Desktop.
Save hankliu5/ddc5c4df66296e87995e7dc5dfa95dec to your computer and use it in GitHub Desktop.
def path_from_s_to_t(G: directed graph, s: starting point, t: ending point)
return dfs(G, s, t, "white")
def dfs(G, u, t, color):
if u is t:
return True
found_path_to_t = False
for all neighbors v in u:
if color == edge(u, v) and v.prev is None:
v.prev = u
if color == "white":
found_path_to_t ||= dfs(G, v, t, "black")
else if color == "black":
found_path_to_t ||= dfs(G, v, t, "white")
if found_path_to_t is True:
return True
else:
# turn curr node to unvisited
u.prev = None
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment