在 Julia 中创建静态有向图



如何从 Julia 中的元组数组创建静态有向图,而无需先创建简单有向图。我有一个示例边缘列表是[(1,2),(2,3),(3,4)].StaticGraphs.jl 的文档是有限的。

有一种方法可以做到这一点,但它需要你把边和它们的反转已经分类到两个向量中。假设您有一个有向路径图 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

测试:

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