在Julia中创建静态有向图

在Julia中创建静态有向图,julia,lightgraphs,Julia,Lightgraphs,如何从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)] #

如何从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

此外,为了便于将来参考,在Julia Discussion或Julia slack的
#graphs
频道中发布可能会让您得到更及时的答案。