我想从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)页面上看到过该示例,该页面从和边列表生成加权图。但是,如果我可以直接从邻接矩阵生成图形,我会更喜欢。
发布于 2018-09-19 03:02:22
虽然不是Julia图形专家,但我认为你想要的是
julia> A = rand(100,100);
julia> G = SimpleWeightedDiGraph(A)
{100, 10000} directed simple Int64 graph with Float64 weightsGraph(a::AbstractMatrix)是无向(单位加权)图的构造函数:
julia> A = A+transpose(A); # making A symmetric
julia> G = Graph(A)
{100, 5050} undirected simple Int64 graph
julia> weights(G)
100 × 100 default distance matrix (value = 1)发布于 2018-09-19 07:44:27
你遇到的第一个问题是你的随机邻接矩阵是不对称的,这是无向图所必需的。你想创建一个有向图。
其次,如果需要加权图,则需要使用SimpleWeightedGraphs.jl包,这意味着您可以简单地执行以下操作
julia> using LightGraphs, SimpleWeightedGraphs
julia> a = rand(100,100);
julia> g = SimpleWeightedDiGraph(a)
{100, 10000} directed simple Int64 graph with Float64 weights但请注意,这是创建随机加权图的一种非常糟糕的方法,因为rand函数几乎可以保证这将是一个完整的图。更好的方法是使用sprand
julia> using SparseArrays
julia> a = sprand(100, 100, 0.2);
julia> g = SimpleWeightedDiGraph(a)
{100, 2048} directed simple Int64 graph with Float64 weightshttps://stackoverflow.com/questions/52392693
复制相似问题