dapias
dapias

Reputation: 2632

Distance between nodes LightGraphs julia

I am wondering about how to compute the distance between a given pair of nodes, say nodes "i" and "j" This is a minimal example for say nodes 2 and 12 from a Random Regular Graph with 100 nodes and connectivity 3

julia> using LightGraphs
julia> L = random_regular_graph(100, 3) 
julia> paths= dijkstra_shortest_paths(L, 2)
julia> distances = paths.dists
julia> d = distances[12]

The problem with this approach is that I have to calculate the distances between all the nodes and my node 2 in order to know the distance between my two nodes of interest

Upvotes: 2

Views: 565

Answers (1)

sbromberger
sbromberger

Reputation: 1026

If you just need the shortest path from a specific source to a specific destination, consider using A*:

julia> g = CycleGraph(10)
{10, 10} undirected simple Int64 graph

julia> a_star(g, 1, 8)
3-element Array{LightGraphs.SimpleGraphs.SimpleEdge{Int64},1}:
 Edge 1 => 10
 Edge 10 => 9
 Edge 9 => 8

If you are JUST interested in the (unweighted, unit) distances, use gdistances:

julia> gdistances(g, 1)[8]
3

In any case, do not access the .dists field from the DijkstraResult. Use the dists() method as the accessor. The internals of LightGraphs structs are not intended to be used directly.

Upvotes: 2

Related Questions