在 Julia 中创建静态有向图

Create Static Directed Graph in 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