Working with metagraphs

julia> using Graphs

julia> using MetaGraphsNext

Creating a MetaGraph

We provide a default constructor in which you only need to specify types:

julia> colors = MetaGraph(Graph(), VertexData = String, EdgeData = Symbol, graph_data = "graph_of_colors")
Meta graph based on a SimpleGraph{Int64}(0, Vector{Int64}[]) with vertex labels of type Symbol, vertex metadata of type String, edge metadata of type Symbol, graph metadata given by "graph_of_colors", and default weight 1.0

Modifying the graph

Modifications of graph elements and the associated metadata can always be done using setindex! (as in a dictionary) with the relevant labels.


Use setindex! with one key to add a new vertex with the given label and metadata. If a vertex with the given label does not exist, it will be created automatically. Otherwise, the function will simply modify the metadata for the existing vertex.

julia> colors[:red] = "warm";

julia> colors[:yellow] = "warm";

julia> colors[:blue] = "warm";  # wrong metadata

julia> colors[:blue] = "cool";


Use setindex! with two keys to add a new edge between the given labels and containing the given metadata. Beware that this time, nonexistent labels will throw an error.

julia> colors[:red, :yellow] = :orange;

julia> colors[:red, :blue] = :violet;

julia> colors[:yellow, :blue] = :green;

Accessing graph properties

To retrieve graph properties, we still follow a dictionary-like interface based on labels.


To check the presence of a vertex or edge, use haskey:

julia> haskey(colors, :red)

julia> haskey(colors, :black)

julia> haskey(colors, :red, :yellow) && haskey(colors, :yellow, :red)

julia> haskey(colors, :red, :black)


All kinds of metadata can be accessed with getindex:

julia> colors[]

julia> colors[:blue]

julia> colors[:yellow, :blue]

Using vertex codes

In the absence of removal, vertex codes correspond to order of insertion in the underlying graph:

julia> code_for(colors, :red)

julia> code_for(colors, :blue)

You can retrieve the associated labels as follows:

julia> label_for(colors, 1)

julia> label_for(colors, 3)

Adding weights

The most simple way to add edge weights is to speficy a default weight for all of them.

julia> weighted_default = MetaGraph(Graph(), default_weight = 2);

julia> default_weight(weighted_default)

julia> weighttype(weighted_default)

You can use the weight_function keyword to specify a function which will transform edge metadata into a weight. This weight must always be the same type as the default_weight.

julia> weighted = MetaGraph(Graph(), EdgeData = Float64, weight_function = identity);

julia> weighted[:red] = nothing; weighted[:blue] = nothing; weighted[:yellow] = nothing;

julia> weighted[:red, :blue] = 1.0; weighted[:blue, :yellow] = 2.0;

julia> weight_matrix = Graphs.weights(weighted)
MetaWeights of size (3, 3)

julia> size(weight_matrix)
(3, 3)

julia> weight_matrix[1, 3]

julia> get_weight_function(weighted)(0)

You can then use all functions from Graphs.jl that require weighted graphs (see the rest of the tutorial).