Created
April 27, 2017 19:54
-
-
Save hankliu5/ddc5c4df66296e87995e7dc5dfa95dec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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