Developer Interface
Defining a new AbstractNamedGraph
Subtype AbstractNamedGraph and overload the minimal interface below, the graph on integer vertex codes and the translation between names and codes. Everything else in the Graphs.jl interface has generic fallbacks in terms of these. Graph types that do not store an integer graph get a generic EncodedGraphView fallback for encoded_graph and only need encoded_vertex and decoded_vertex.
Vertex codes are not stable across mutation: adding or removing vertices may reassign the codes of other vertices.
These names are public rather than exported, so reach them with using NamedGraphs: encoded_vertex or by qualifying.
NamedGraphs.encoded_graph — Function
encoded_graph(graph::AbstractNamedGraph) -> AbstractGraph{Int}The graph in coded form: a graph with the same topology as graph whose vertices are the codes 1:nv(graph) of the vertices of graph, i.e. it has the edge encoded_vertex(graph, u) => encoded_vertex(graph, v) if and only if graph has the edge u => v.
May be a stored field or a view of graph; mutate the graph only through graph.
Examples
julia> using Graphs: edges, has_edge, path_graph, vertices
julia> using NamedGraphs: NamedGraph, encoded_graph, encoded_vertex
julia> g = NamedGraph(path_graph(3), ["a", "b", "c"]);
julia> cg = encoded_graph(g)
{3, 2} undirected simple Int64 graph
julia> vertices(cg)
Base.OneTo(3)
julia> collect(edges(cg))
2-element Vector{Graphs.SimpleGraphs.SimpleEdge{Int64}}:
Edge 1 => 2
Edge 2 => 3
julia> encoded_vertex(g, "a"), encoded_vertex(g, "b")
(1, 2)
julia> has_edge(cg, 1, 2)
trueNamedGraphs.encoded_vertex — Function
encoded_vertex(graph::AbstractNamedGraph, vertex) -> IntThe vertex of encoded_graph(graph) corresponding to vertex, i.e. its code in graph. encoded_vertex(graph, ·) and decoded_vertex(graph, ·) are inverse bijections between vertices(graph) and Base.OneTo(nv(graph)).
Codes are not stable across mutation: adding or removing vertices may reassign the codes of other vertices.
Examples
julia> using Graphs: path_graph
julia> using NamedGraphs: NamedGraph, decoded_vertex, encoded_vertex
julia> g = NamedGraph(path_graph(3), ["a", "b", "c"]);
julia> encoded_vertex(g, "b")
2
julia> decoded_vertex(g, 2)
"b"NamedGraphs.decoded_vertex — Function
decoded_vertex(graph::AbstractNamedGraph, code::Integer)The vertex of graph whose code is code, i.e. the vertex corresponding to the vertex code of encoded_graph(graph). Inverse of encoded_vertex.
Examples
julia> using Graphs: nv, path_graph
julia> using NamedGraphs: NamedGraph, decoded_vertex
julia> g = NamedGraph(path_graph(3), ["a", "b", "c"]);
julia> [decoded_vertex(g, c) for c in 1:nv(g)]
3-element Vector{String}:
"a"
"b"
"c"NamedGraphs.encoded_edge — Function
encoded_edge(graph::AbstractNamedGraph, edge) -> AbstractEdge{Int}The edge of encoded_graph(graph) corresponding to edge, i.e. the edge between the codes of the vertices of edge. Inverse of decoded_edge.
Examples
julia> using Graphs: path_graph
julia> using NamedGraphs: NamedEdge, NamedGraph, decoded_edge, encoded_edge
julia> g = NamedGraph(path_graph(3), ["a", "b", "c"]);
julia> ce = encoded_edge(g, NamedEdge("a" => "b"))
Edge 1 => 2
julia> decoded_edge(g, ce)
"a" => "b"NamedGraphs.decoded_edge — Function
decoded_edge(graph::AbstractNamedGraph, encoded_edge)The edge of graph corresponding to the edge encoded_edge of encoded_graph(graph). Inverse of encoded_edge.
Graphs.jl interface extensions
NamedGraphs also defines generic extensions of the Graphs.jl interface. Many of them are written against Graphs.AbstractGraph rather than against named graphs, so they work for any graph type, including Graphs.SimpleGraph. The rest need vertices that carry names and are only defined for AbstractNamedGraph.