JuliaGraphs / MetaGraphsNext.jl

A package for graphs with vertex labels and metadata in Julia
http://juliagraphs.org/MetaGraphsNext.jl/
Other
78 stars 18 forks source link

Document how to create a graph that has metadata but does not use labeled vertices #37

Closed Krastanov closed 1 year ago

Krastanov commented 2 years ago

With the old MetaGraphs it was easy to make a graph where indexing was still done with integers, not with symbolic labels. How do I do this with MetaGraphsNext.

E.g., how do I do g = MetaGraph(Graph()); add_vertex!(g)? (and maybe then followed by set_prop!(g,1,:key,:value))

bramtayl commented 2 years ago

How about

using Graphs
using MetaGraphsNext
meta_graph = MetaGraph(Graph(), Label = Int, VertexData = Pair{Symbol, Symbol})
meta_graph[1] = :key => :value

This will work, you will just get a warning, because it's easy to confuse integer vertex labels with integer vertex codes.

Krastanov commented 2 years ago

This seems like a neat workaround! I am still curious though, does this not involve an extra lookup step that needs to map an integer vertex label to an integer vertex code? Is there something I can do in order to avoid this computational expense if all I need is vertex/edge metadata, without the need for custom labels?

bramtayl commented 2 years ago

Yes, there is an extra lookup step. Do you plan on deleting vertices? If so, I think it's probably worth it. Graphs.jl reassigns vertex codes when a vertex is deleted.

Krastanov commented 2 years ago

Thanks! This was very helpful and it answers all my questions. I would leave this issue opened and rename it to focus on adding this to the documentation. Please close it if it is a duplicate.

bramtayl commented 2 years ago

Ok, feel free to make a PR to update the docs!

gdalle commented 1 year ago

Hi @Krastanov, would you like to contribute by documenting this workaround?

gdalle commented 1 year ago

@bramtayl if we decide to forbid integer labels for good, what happens to this method?

bramtayl commented 1 year ago

Yeah this wouldn't work anymore...part of why I'm conflicted

gdalle commented 1 year ago

One alternative could be to provide a custom integer type with the package, which would not subtype Integer, so that people can use it as label. Something like

struct IntegerLabel{T<:Integer}
    n::T
end
const IL = IntegerLabel

but it seems a bit overkill

gdalle commented 1 year ago

53 removes the warning for integer labels, as long as it's well-documented I think we're okay