# Creating a graph with objects or structs as nodes

**URL:** https://discourse.julialang.org/t/creating-a-graph-with-objects-or-structs-as-nodes/76088
**Category:** Graphs
**Tags:** lightgraphs, metagraphs, graphs
**Created:** [February 9, 2022, 2:47pm UTC](https://discourse.julialang.org/t/creating-a-graph-with-objects-or-structs-as-nodes/76088 "2022-02-09T14:47:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![flobe](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@flobe](https://discourse.julialang.org/u/flobe)
#### Post date: [February 9, 2022, 2:47pm UTC](https://discourse.julialang.org/t/creating-a-graph-with-objects-or-structs-as-nodes/76088/1 "2022-02-09T14:47:48Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![ImreSamu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imresamu/32/20677_2.png) [@ImreSamu](https://discourse.julialang.org/u/ImreSamu)
#### Post date: [February 9, 2022, 3:32pm UTC](https://discourse.julialang.org/t/creating-a-graph-with-objects-or-structs-as-nodes/76088/2 "2022-02-09T15:32:17Z")

</div>

based on the [https://juliagraphs.org/](https://juliagraphs.org/)  
I will check the

- “ **MetaGraphs.jl** : An implementation of graphs with graph, vertex, and edge metadata”
  - [code](https://github.com/JuliaGraphs/MetaGraphs.jl) / [docs](https://juliagraphs.org/MetaGraphs.jl/dev/)

example ( from the doc )

```julia
# 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

```

---

<div class="post-metadata">

### Author: ![flobe](https://avatars.discourse-cdn.com/v4/letter/f/977dab/32.png) [@flobe](https://discourse.julialang.org/u/flobe)
#### Post date: [February 10, 2022, 10:21am UTC](https://discourse.julialang.org/t/creating-a-graph-with-objects-or-structs-as-nodes/76088/3 "2022-02-10T10:21:14Z")

</div>

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 🤔

---

<div class="post-metadata">

### Author: ![etienne\_dg](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@etienne\_dg](https://discourse.julialang.org/u/etienne_dg)
#### Post date: [February 10, 2022, 10:54am UTC](https://discourse.julialang.org/t/creating-a-graph-with-objects-or-structs-as-nodes/76088/4 "2022-02-10T10:54:36Z")

</div>

`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
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](https://github.com/JuliaGraphs/MetaGraphsNext.jl) 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.
