3

I want to create a graph with custom vertex names. Is this possible with MetaGraphs.jl ?

using MetaGraphs
using LightGraphs
using GraphPlot

# Create empty graph
gm = MetaGraph()

# Add vertices with properties
add_vertex!(gm, :A, [7.2,8.6])
add_vertex!(gm, :B, [3.2,6.7])
add_vertex!(gm, :C, [6.3,3.9])
add_vertex!(gm, :D, [2.4,6.7])

gplot(gm, nodelabel = vertices(gm))

However is it possible for the vertex to have a name called :A instead of 1. Since in the next step I want to add an edge add_edge!(gm, :A,:B) (This is incorrect, currently the names of the nodes 1,2,3... , so the way to create an edge is add_edge!(gm, 1,2))

enter image description here

In otherwords have A,B,C, ... instead of 1,2,3.

Jonas
  • 1,401
  • 9
  • 25
imantha
  • 2,676
  • 4
  • 23
  • 46

2 Answers2

4

From what I understand, one way to do that in MetaGraphs.jl is to define an "indexing property", for instance :name, which would contain :A, :B, etc.

Then, you can add an edge using the syntax add_edge!(gm, gm[:A, :name], gm[:B, :name]) if my memory serves me. As for plotting, you can simply retrieve the property with get_prop.

4

The best way to do this is to use set_indexing_prop! like so:

g = MetaGraph(path_graph(3))
set_prop!(g, 1, :name, 'a')
set_prop!(g, 2, :name, 'b')
set_prop!(g, 3, :name, 'c')
set_indexing_prop!(g, :name)

Then, you can refer to the names and they will be translated into vertex indices, which are integers:

g['a', :name]  # returns 1
g['b', :name]  # returns 2
g['c', :name]  # returns 3
has_edge(g, g['b', :name], g['c', :name])  # returns true
sbromberger
  • 1,026
  • 6
  • 12