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)
)
In otherwords have A,B,C, ... instead of 1,2,3.
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