Hi there,
I’d like to use Graph.jl package to run a shortest path algorithm. I have already read this page and am still not sure how to interpret the return traversal information of shortest path algorithm.
Let’s assume I’m interested in three of these algorithm; bellman_ford, dijkstra, and yen_k.
Here’s a small example. A Weighted digraph with 5 nodes and 5 edges :
using SimpleWeightedGraphs
using Graphs
using SplitApplyCombine
using SparseArrays
sources = [1,1,3,3,4]
destinations = [2, 3,4,5,5]
weight = [3, 0.5,1,2,1]
g = SimpleWeightedDiGraph(sources, destinations, weight)
edges(g)
weights(g)
blfs = Graphs.bellman_ford_shortest_paths(g,1)
dijk = Graphs.dijkstra_shortest_paths(g,1)
yen = Graphs.yen_k_shortest_paths(g, 1, 5, weights(g))
yen2 = Graphs.yen_k_shortest_paths(g, 1, 5, weights(g), 2)
yen2max = Graphs.yen_k_shortest_paths(g, 1, 5, weights(g), 2;maxdist=10) # there's clearly other path 1>3>4>5 with the total distance of 2.5
astar = Graphs.a_star(g, 1, 5, weights(g))
What I receive after running blfs = Graphs.bellman_ford_shortest_paths(g,1)
is:
Graphs.BellmanFordState{Float64, Int64}([0, 1, 1, 3, 3], [0.0, 3.0, 0.5, 1.5, 2.5])
What information do these two lists contain? And why we have 0
in them (when there’s no nodes labeled by 0
) ?
Same question for dijk = Graphs.dijkstra_shortest_paths(g,1)
. The return output is:
Graphs.DijkstraState{Float64, Int64}([0, 1, 1, 3, 3], [0.0, 3.0, 0.5, 1.5, 2.5], [Int64[], Int64[], Int64[], Int64[], Int64[]], [1.0, 1.0, 1.0, 1.0, 2.0], Int64[])
*----------------------------------------------------------------------------------------
According to documentation; the “yen_k” Perform Yen’s algorithm, computing k-shortest distances between source
and target
other vertices. Return contains distances and paths.
When I ran it, however, it does not return “K” different path but K copy of the same path. Why?
For instance:
yen2 = Graphs.yen_k_shortest_paths(g, 1, 5, weights(g), 2)
returns:
Graphs.YenState{Float64, Int64}([2.5, 2.5], [[1, 3, 5], [1, 3, 5]])
Althogh there’s clearly other path 1>3>4>5 with the total distance of 2.5 also. Why it just returns 1>3>5 instead?
Also yen_k_shortest_paths
, what exactly maxdist
control? Is it to have a limit over the shortest distance? if so, why yen2max = Graphs.yen_k_shortest_paths(g, 1, 5, weights(g), 2;maxdist=10)
does not consider 10
at all?
One more question:
How to run A* algorithm? I’ve tried different things (like astar = Graphs.a_star(g, 1, 5, weights(g))
), all of which returned an error.
ERROR: MethodError: no method matching SimpleWeightedEdge{Int64, Float64}(::Int64, ::Int64)
a_star(g, s, t[, distmx][, heuristic][, edgetype_to_return])
Thanks very much,
F