Created
January 25, 2019 20:44
-
-
Save stevenbeales/0543d4c1b1387089177f8e23fea0730b 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 shortest_path(v,w) | |
raise ArgumentError unless path?(v,w) to_edge = [] | |
bfs(w) { |v1,v2| to_edge[v2] = v1 } result = [] | |
x= v | |
while x != w | |
result << x | |
x = to_edge[x] | |
end | |
result << x | |
end | |
def path?(v1,v2) | |
return false if v1 < 0 or vertices <= v1 | |
return false if v2 < 0 or vertices <= v2 | |
dfs(v1) do |v,w| | |
return true if w == v2 | |
end | |
false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment