graph :: Digraph :: a_shortest_path
u
to v
.If no path exists between u
and v
, it returns null
.
var g = new HashDigraph[Int]
g.add_arc(1, 2)
g.add_arc(2, 3)
g.add_arc(3, 4)
assert g.a_shortest_path(1, 4).length == 4
g.add_arc(1, 3)
assert g.a_shortest_path(1, 4).length == 3
assert g.a_shortest_path(4, 1) == null
# Returns a shortest path from vertex `u` to `v`.
#
# If no path exists between `u` and `v`, it returns `null`.
#
# ~~~
# var g = new HashDigraph[Int]
# g.add_arc(1, 2)
# g.add_arc(2, 3)
# g.add_arc(3, 4)
# assert g.a_shortest_path(1, 4).length == 4
# g.add_arc(1, 3)
# assert g.a_shortest_path(1, 4).length == 3
# assert g.a_shortest_path(4, 1) == null
# ~~~
fun a_shortest_path(u, v: V): nullable Sequence[V]
do
var queue = new List[V].from([u]).as_fifo
var pred = new HashMap[V, nullable V]
var visited = new HashSet[V]
var w: nullable V = null
pred[u] = null
while not queue.is_empty do
w = queue.take
if not visited.has(w) then
visited.add(w)
if w == v then break
for wp in successors(w) do
if not pred.keys.has(wp) then
queue.add(wp)
pred[wp] = w
end
end
end
end
if w != v then
return null
else
var path = new List[V]
path.add(v)
w = v
while pred[w] != null do
path.unshift(pred[w].as(not null))
w = pred[w]
end
return path
end
end
lib/graph/digraph.nit:461,2--507,4
# Returns a shortest path from vertex `u` to `v`.
#
# If `u` is the same as `v` return `[u]`
#
# ~~~
# var g = new ReflexiveHashDigraph[Int]
# g.add_arc(1, 2)
# g.add_arc(2, 3)
# g.add_arc(3, 4)
# assert g.a_shortest_path(1, 4).length == 4
# assert g.a_shortest_path(1, 1).length == 1
# ~~~
redef fun a_shortest_path(u, v)
do
if u == v then
var path = new List[V]
path.add(u)
return path
end
return super
end
lib/graph/digraph.nit:1076,2--1096,4