Property of edge lost when going from MetaDiGraph with inversed edge direction to MetaGraph
Hi, I just discovered some unexpected behaviour when converting a MetaDiGraph to MetaGraph. In my MetaDiGraph I have one edge that is directed from a high number to a lower number, i.e. 3->2 instead of 2->3. When I convert the MetaDiGraph to a MetaGraph the property is lost on the edge directed in the "wrong" direction, but on another edge directed in the opposite direction the property is kept.
Would it maybe make more sense if the property was kept for both edges?
Below is a code example demonstrating the behaviour.
using LightGraphs
using MetaGraphs
G = MetaDiGraph(3)
add_edge!(G, 1, 2)
add_edge!(G, 3, 2)
set_prop!(G, 1, 2, :name, "a")
set_prop!(G, 3, 2, :name, "b")
Confirm that the property was correctly set.
get_prop(G, 3, 2, :name)
"b"
Convert the directed graph to an undirected graph.
G2 = MetaGraph(G)
Check that the property on the first edge is still there.
get_prop(G2, 1, 2, :name)
"a"
Try to get the property of the second edge.
get_prop(G2, 3, 2, :name)
KeyError: key :name not found
Stacktrace:
[1] getindex at .\dict.jl:477 [inlined]
[2] get_prop at C:\Users\user\.julia\packages\MetaGraphs\SUjFO\src\MetaGraphs.jl:256 [inlined]
[3] get_prop(::MetaGraph{Int64,Float64}, ::Int64, ::Int64, ::Symbol) at C:\Users\user\.julia\packages\MetaGraphs\SUjFO\src\MetaGraphs.jl:258
[4] top-level scope at In[10]:1
Try to get the property with inversed vertex order
get_prop(G2, 2, 3, :name)
KeyError: key :name not found
Stacktrace:
[1] getindex at .\dict.jl:477 [inlined]
[2] get_prop at C:\Users\user\.julia\packages\MetaGraphs\SUjFO\src\MetaGraphs.jl:256 [inlined]
[3] get_prop(::MetaGraph{Int64,Float64}, ::Int64, ::Int64, ::Symbol) at C:\Users\user\.julia\packages\MetaGraphs\SUjFO\src\MetaGraphs.jl:258
[4] top-level scope at In[11]:1
Check if the edge is in the graph
has_edge(G2, 2, 3)
true
I think the problem is, that if you have
set_prop!(G, 1, 2, :name, "a")
set_prop!(G, 2, 1, :name, "b")
what should then the expected value for get_prop(MetaGraph(G), 1, 2, :name) be?
What is missing here though is some documentation for the MetaGraph(::MetaDiGraph) constructor that would explain what one should expect.
Hi, thanks for the reply :)
In that case I would expect two parallel edges with the properties "a" and "b". However, it seems parallel edges are not supported by LightGraphs, which means one of them has to be removed.
Anyway, I don't think this is the same problem as mine. In your problem you have two parallel edges, and since one of them has to removed it is ambiguous which of the edges to keep. It does seem that the solution is to keep the edge going from the lower to higher number. In my problem there is no ambiguity. There are no parallel edges, but one of them looses its property.
In any case this is not a very hard problem to work around when you know it's there. I just thought it best to share it, so other people don't get as confused as I was for a little while ;p
Cheers