Creating a graph with objects or structs as nodes

Hi everyone,
I would like to create a graph where the nodes can contain multiple pieces of information. I tried to use graph.jl, but currently I don’t see any way to store more information than the child nodes.
For example, each node represents a robot and I would like the node to have the name of the robot, as well as its position and orientation stored.
Is there a way to build this with graph.jl or another package?

I am new to julia and would be very grateful for help!

based on the https://juliagraphs.org/
I will check the

  • MetaGraphs.jl : An implementation of graphs with graph, vertex, and edge metadata”

example ( from the doc )

# set properties on a vertex in bulk
julia> set_props!(mg, 1, Dict(:name=>"Susan", :id => 123))
true

# set individual properties
julia> set_prop!(mg, 2, :name, "John")
true

# set a property on an edge
julia> set_prop!(mg, Edge(1, 2), :action, "knows")
true

Thank you for the quick answer! This goes definitely in the direction I’m looking for … but am I getting it right that each propertie can only be added as a symbol? probably I missed something basic in julia there, but it would be better if I can set the data or object type myself :thinking:

MetaGraphs.jl stores the vertex information in a Dict{Symbol, Any} so the name of the property is always a Symbol, but the data itself can be anything you want.
You can get back the property you want as follow:

julia> props(mg, 1)[:name]
"Susan"

julia> props(mg, 2)[:name]
"John"


julia> props(mg, 1)[:id]
123

MetaGraphs.jl is now quite old and suffer from poor design (Dict{Symbol, Any} is really bad for type stability). There is an experimental package that you might check if you want better performance. However, as it is experimental, the documentation is relatively sparse and the implementation might have some bugs.

Another option that is really easy to implement is just to manage yourself your metadatas. SimpleGraphs maintain vertices in a UnitRange starting from 1, so you could store your metadata in a Vector, where the data for vertex k is stored at index k. You just need to be careful if you mutate your graph, the index of vertices are modified in order to keep a UnitRange starting from 1. You just need to delete the k-th element of you Vector whenever you delete vertex k.

3 Likes