Skip to content
This repository has been archived by the owner on Oct 21, 2021. It is now read-only.

Incidence dictionary produces incorrect graph #256

Closed
GearsAD opened this issue Apr 19, 2019 · 4 comments
Closed

Incidence dictionary produces incorrect graph #256

GearsAD opened this issue Apr 19, 2019 · 4 comments

Comments

@GearsAD
Copy link

GearsAD commented Apr 19, 2019

If I create an undirected incidence dictionary with the following code, it produces incorrect edges.

using Graphs
const FGGdict = Graphs.GenericIncidenceList{Graphs.ExVertex,Graphs.Edge{Graphs.ExVertex},Dict{Int,Graphs.ExVertex},Dict{Int,Array{Graphs.Edge{Graphs.ExVertex},1}}}
fg = Graphs.incdict(Graphs.ExVertex,is_directed=false)
variables = [ExVertex(1, "x1"), ExVertex(2, "x2"), ExVertex(3, "x3"), ExVertex(4, "x4")]
add_vertex!(fg, variables[1])
add_vertex!(fg, variables[2])
add_vertex!(fg, variables[3])
add_vertex!(fg, variables[4])
# Factors
factors =[ExVertex(5, "x1x2f1"), ExVertex(6, "x2x3f1"), ExVertex(7, "x3x4f1")]
add_vertex!(fg, factors[1])
add_vertex!(fg, factors[2])
add_vertex!(fg, factors[3])
edge = Graphs.make_edge(fg, variables[1], factors[1]); Graphs.add_edge!(fg, edge)
edge = Graphs.make_edge(fg, factors[1], variables[2]); Graphs.add_edge!(fg, edge)
edge = Graphs.make_edge(fg, variables[2], factors[2]); Graphs.add_edge!(fg, edge)
edge = Graphs.make_edge(fg, factors[2], variables[3]); Graphs.add_edge!(fg, edge)
edge = Graphs.make_edge(fg, variables[3], factors[3]); Graphs.add_edge!(fg, edge)
edge = Graphs.make_edge(fg, factors[3], variables[4]); Graphs.add_edge!(fg, edge)

# Not great code but I wanted to actually write the data somewhere else, not to a file
m = PipeBuffer()
write(m,Graphs.to_dot(fg))
data = take!(m)
close(m)

open("/tmp/test.dot", "w") do fid
    write(fid,data)
end

This code produces:
image

If you change it to a directed graph:
image

Any ideas?

@dehann
Copy link
Contributor

dehann commented Apr 19, 2019

Hi thanks for the example. That's pretty weird. Maybe there is an internal assumption on the ordering of elements in the dict (which would clearly be incorrect). Should also double check weather this is a drawing issue or a graph lookup issue; likely a discrepancy between in and out neighbor searches.

I can go look at how inclist is built and see if something weird is there...

@dehann
Copy link
Contributor

dehann commented Apr 19, 2019

@GearsAD
Copy link
Author

GearsAD commented Apr 20, 2019

Wait one, actually it's producing the correct graph - I was just reading it wrong. If you look above, those are the vertex numbers and although the shape is different, the actual relationships are the same 1--5--2--6--3-7--4.

I thought that was a simplified version of the issue we were seeing earlier in the week, but actually it seems correct. I need to dig a bit more. I'm going to close this as this specific example is performing correctly.

@GearsAD GearsAD closed this as completed Apr 20, 2019
@GearsAD
Copy link
Author

GearsAD commented Apr 20, 2019

So I wasn't using ExVertex, I had created a new type of vertex, and it seems the reason I was getting strange graphs is because I needed to provide an implementation of vertex_index(v), e.g.:

# This is insanely important - if we don't provide a valid index, the edges don't work correctly.
vertex_index(v::GraphsNode) = v.index

Apologies, the example code above actually produces the correct graphs with ExVertex.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants