Skip to content

Instantly share code, notes, and snippets.

@stevenbeales
Created January 25, 2019 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenbeales/0543d4c1b1387089177f8e23fea0730b to your computer and use it in GitHub Desktop.
Save stevenbeales/0543d4c1b1387089177f8e23fea0730b to your computer and use it in GitHub Desktop.
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