我想根据Julia(v0.7)中的邻接矩阵生成加权和定向网络。

到目前为止,我已经尝试过:

using LightGraphs 
using SimpleWeightedGraphs 
 
A = rand(100, 100) 
G = Graph(A) 

但我得到错误:
ERROR: ArgumentError: Adjacency / distance matrices must be symmetric 
Stacktrace: 
 [1] SimpleGraph{Int64}(::Array{Float64,2}) at /home/user/.julia/packages/LightGraphs/PPsyP/src/SimpleGraphs/simplegraph.jl:78 
 [2] SimpleGraph(::Array{Float64,2}) at /home/user/.julia/packages/LightGraphs/PPsyP/src/SimpleGraphs/simplegraph.jl:72 
 [3] top-level scope at none:0 

到目前为止,我只在github( https://github.com/JuliaGraphs/SimpleWeightedGraphs.jl)页面上看到了该示例,该示例从and edgelist生成加权图。但是,如果我可以直接从邻接矩阵生成图,则更希望。

请您参考如下方法:

根据crstnbr的答案,Graph是无权的无向的,因此理想情况下,邻接矩阵与[0, 1]中的值对称。
将任何对称矩阵馈给Graph构造函数,会为每个非零元素创建边:

A = rand(3,3); 
Graph(A+A'); 
println.(edges(G)); 
 Edge 1 => 1 
 Edge 1 => 2 
 Edge 1 => 3 
 Edge 2 => 2 
 Edge 2 => 3 
 Edge 3 => 3 
SimpleWeightedDiGraph具有多个构造函数,这些构造函数可以采用密集或 SparseMatrixCSC邻接矩阵:
SimpleWeightedDiGraph(rand(4,4)) 
 {4, 16} directed simple Int64 graph with Float64 weights 
 
SimpleWeightedDiGraph(rand([0,1], 3, 3)) 
 {3, 5} directed simple Int64 graph with Int64 weights 
 
using SparseArrays 
SimpleWeightedDiGraph( sprand(3, 3, 0.5) ) 
 {3, 5} directed simple Int64 graph with Float64 weights 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!