Search code examples
julialightgraphs

Create Static Directed Graph in Julia


How can I create a Static Directed Graph from an array of tuples in Julia without having to create a Simple Directed Graph first. An example edge list I have is [(1,2),(2,3),(3,4)]. The documentation of StaticGraphs.jl is limited.


Solution

  • There's a way to do this but it requires you to have the edges and their reverses already sorted into two vectors. Assume you have a directed path graph 1 -> 2 -> 3 -> 4:

    fwd = [(1, 2), (2, 3), (3, 4)]  # these are your forward edges, sorted
    rev = [(2, 1), (3, 2), (4, 3)]  # these are the reverse of the forward edges, sorted
    # also, sort(reverse.(fwd)) will do this easily.
    
    g = StaticDiGraph(4, fwd, rev)  # number of vertices is the first argument
    

    testing:

    julia> h = StaticDiGraph(path_digraph(4))
    {4, 3} directed simple static {UInt8, UInt8} graph
    
    julia> g == h
    true