Julia's Graph Analysis Package Graphs.jl
Introduction
Graphs.jl is a package for graph (network) analysis, similar to Python’s NetworkX. It was created by rebooting the LightGraphs.jl package.
The goal of Graphs.jl is to offer a performant platform for network and graph analysis in Julia, following the example of libraries such as NetworkX in Python.
The Graphs.jl project is a reboot of the LightGraphs.jl package (archived in October 2021), which remains available on GitHub at sbromberger/LightGraphs.jl.
Code
The code is broadly similar to NetworkX. However, it seems that there are still some difficulties in using it properly.
Installation
Enter ] add Graphs in the Julia REPL to install.
(@v1.7) pkg> add Graphs
Loading and Version Check
julia> using Graphs
julia> using Pkg
julia> Pkg.status("Graphs")
[86223c79] Graphs v1.7.1
Graph Creation
Similar to NetworkX, Graph() and DiGraph() are used to create null graphs. The default type of vertices is Int64, and they can only be created with sub-types of this. Graph information is represented as $\left\{ \left| V \right|, \left| E \right| \right\}$.
julia> G = Graph()
{0, 0} undirected simple Int64 graph
julia> G = DiGraph()
{0, 0} directed simple Int64 graph
julia> G = Graph{Float64}()
ERROR: TypeError: in SimpleGraph, in T, expected T<:Integer, got Type{Float64}
julia> G = Graph{UInt64}()
{0, 0} undirected simple UInt64 graph
Adding and Removing Vertices
In NetworkX, nodes can be added by naming the nodes, but in Graphs.jl, vertices start from $1$ and are added in order to $2,3,\dots$.
julia> add_vertex!(G)
true
julia> G
{1, 0} undirected simple Int64 graph
julia> add_vertex!(G)
true
julia> G
{2, 0} undirected simple Int64 graph
add_vertices!(G, n) is used to add $n$ vertices.
julia> add_vertices!(G, 3)
3
julia> G
{5, 0} undirected simple Int64 graph
Note that when removing a specific vertex with rem_vertex!(G, v), the numbers are automatically sorted without leaving any empty numbers. For example, if the vertices are $V = \left\{ 1,2,3,4,5 \right\}$ and $2$ is deleted, the name of $3, 4, 5$ changes to $2, 3, 4$.
julia> G = Graph(5)
{5, 0} undirected simple Int64 graph
julia> rem_vertex!(G, 2)
true
julia> G
{4, 0} undirected simple Int64 graph
julia> has_vertex(G, 2)
true
julia> has_vertex(G, 5)
false
Adding and Removing Edges
When the second vertex is removed with rem_vertex!(G, 2), as explained above, the existing third vertex becomes the new second vertex, but the result for edges is different. It seems to be a bug.
julia> G = Graph(5)
{5, 0} undirected simple Int64 graph
julia> add_edge!(G, 1, 2)
true
julia> add_edge!(G, 2, 3)
true
julia> add_edge!(G, 3, 4)
true
julia> G
{5, 3} undirected simple Int64 graph
julia> has_edge(G, 1, 2)
true
julia> has_edge(G, 2, 3)
true
julia> has_edge(G, 3, 4)
true
julia> rem_vertex!(G, 2)
true
julia> G
{4, 1} undirected simple Int64 graph
julia> has_vertex(G, 2)
true
julia> has_edge(G, 1, 2)
false
julia> has_edge(G, 2, 3)
false
julia> has_edge(G, 3, 4)
true
Environment
- OS: Windows10
- Version: Julia 1.7.1, Graphs 1.7.1
