I am adding edges to a simple weighted directed graph (from SimpleWeightedDiGraph() that is part of LightGraphs package) in Julia. Some of the arcs are "free" (null weight). However, when specifying the weight of 0 it is not added as a new edge and a shortest path problem does not include it in the possible solution. Is there an easy way to add "free" edges/arcs to a graph in Julia?
How to add free edge to graph in LightGraphs (Julia)?
1.1k Views Asked by Berni At
2
There are 2 best solutions below
3
On
This modification of the SimpleWeightedGraphs README example works for me:
using LightGraphs, SimpleWeightedGraphs
# set the size of the graph
g = SimpleWeightedDiGraph(3)
add_edge!(g, 1, 2, 0.5)
add_edge!(g, 2, 3, 0.8)
add_edge!(g, 1, 3, 2.0)
# find the shortest path from vertex 1 to vertex 3 taking weights into account.
enumerate_paths(dijkstra_shortest_paths(g, 1), 3) # gives [1,2,3]
# reweight the edge from 1 to 3 to be "free"
add_edge!(g, 1, 3, 0.0)
enumerate_paths(dijkstra_shortest_paths(g, 1), 3) # gives [1,3]
Notice that the vertices must be in the graph (according to its size) to be able to set their weights, as stated in the docs: ?add_edge!.
The key issue is how zero values are represented in a sparse matrix (which is the underlying data store for
SimpleWeightedGraphs. While it is true that the underlying zero value is preserved once it's explicitly set:this will fail if you have to do anything with the edges:
There's no really good solution to this. My advice is to use a sufficiently small weight as proposed above to approximate a zero value.
(PS: the reason the initial
add_edge!(g, 1, 3, 0.0)doesn't work is because in Julia, setting the value of a new sparsematrix element to zero is a no-op.)