Graphs.jl interface
using Graphs
using MetaGraphsNextMetaGraphs inherit many methods from Graphs.jl. In general, inherited methods refer to vertices by codes, not labels, for compatibility with the AbstractGraph interface.
Note that vertex codes get reassigned after rem_vertex! operations to remain contiguous, so we recommend systematically converting to and from labels.
Undirected graphs
We can make MetaGraphs based on (undirected) Graphs.
cities = MetaGraph(
    Graph();
    label_type=Symbol,
    vertex_data_type=String,
    edge_data_type=Int,
    graph_data=nothing,
    weight_function=identity,
);Let us add some cities and the distance between them:
cities[:Paris] = "France";
cities[:London] = "UK";
cities[:Berlin] = "Germany";
cities[:Paris, :London] = 344;
cities[:Paris, :Berlin] = 878;The general properties of the graph are as expected:
is_directed(cities)falseeltype(cities)Int64edgetype(cities)Graphs.SimpleGraphs.SimpleEdge{Int64}We can check the set of vertices:
nv(cities)3collect(vertices(cities))3-element Vector{Int64}:
 1
 2
 3has_vertex(cities, 2)truehas_vertex(cities, 4)falseNote that we can't add the same city (i.e. vertex label) twice:
add_vertex!(cities, :London, "Italy")falsenv(cities)3cities[:London]"UK"We then check the set of edges:
ne(cities)2collect(edges(cities))2-element Vector{Graphs.SimpleGraphs.SimpleEdge{Int64}}:
 Edge 1 => 2
 Edge 1 => 3has_edge(cities, 1, 2)truehas_edge(cities, 2, 3)falseFrom this initial graph, we can create some others:
copy(cities)Meta graph based on a SimpleGraph{Int64} with vertex labels of type Symbol, vertex metadata of type String, edge metadata of type Int64, graph metadata given by nothing, and default weight 1.0zero(cities)Meta graph based on a SimpleGraph{Int64} with vertex labels of type Symbol, vertex metadata of type String, edge metadata of type Int64, graph metadata given by nothing, and default weight 1.0Since cities is a weighted graph, we can leverage the whole Graphs.jl machinery of graph analysis and traversal:
diameter(cities)1222.0ds = dijkstra_shortest_paths(cities, 2)Graphs.DijkstraState{Float64, Int64}([2, 0, 1], [344.0, 0.0, 1222.0], [Int64[], Int64[], Int64[]], [1.0, 1.0, 1.0], Int64[])Finally, let us remove some edges and vertices
rem_edge!(cities, 1, 3);
rem_vertex!(cities, 3);
has_vertex(cities, 1) && !has_vertex(cities, 3)trueDirected graphs
We can make MetaGraphs based on DiGraphs as well.
rock_paper_scissors = MetaGraph(DiGraph(); label_type=Symbol, edge_data_type=String);
for label in [:rock, :paper, :scissors]
    rock_paper_scissors[label] = nothing
end
rock_paper_scissors[:rock, :scissors] = "rock beats scissors"
rock_paper_scissors[:scissors, :paper] = "scissors beat paper"
rock_paper_scissors[:paper, :rock] = "paper beats rock";We see that the underlying graph has changed:
is_directed(rock_paper_scissors)trueDirected graphs can be reversed:
haskey(rock_paper_scissors, :scissors, :rock)falsehaskey(reverse(rock_paper_scissors), :scissors, :rock)trueFinally, let us take a subgraph:
rock_paper, _ = induced_subgraph(rock_paper_scissors, [1, 2])(Meta graph based on a SimpleDiGraph{Int64} with vertex labels of type Symbol, vertex metadata of type Nothing, edge metadata of type String, graph metadata given by nothing, and default weight 1.0, [1, 2])issubset(rock_paper, rock_paper_scissors)truehaskey(rock_paper, :paper, :rock)truehaskey(rock_paper, :rock, :scissors)falseThis page was generated using Literate.jl.