graph-theory - 使用 Graphs.jl 更改顶点的值

标签 graph-theory julia

我正在尝试使用模块 Graphs.jl 制作一个图表(以模拟 2D Ising Model )。

我发现了如何使用 this 构建简单图问题但是,使用提供的答案,我找不到更改顶点值的方法。也许我应该尝试另一种图表类型,但我真的无法理解文档来找到答案

最佳答案

这个包看起来不错,但是简单的图有整数作为顶点,所以你不能给它们贴标签。因此,您必须使用更通用的类型 ExVertexExEdge see here 。正确使用这个接口(interface)需要构造正确的类型,而使用 REPL 很难做到这一点。这是一个可用于帮助构建图形的模块。

module GraphUtil
   import Graphs
   export empty_graph,add_label!,add_connection!

   function empty_graph()
      va::Array{Graphs.ExVertex,1} = {}
      ea::Array{Graphs.ExEdge{Graphs.ExVertex},1} = {}
      G = Graphs.graph(va,ea)
   end

   function add_label!(G,s::String)
      v = Graphs.ExVertex(Graphs.num_vertices(G) + 1,s)
      Graphs.add_vertex!(G,v)
      v
   end

   function add_connection!(G,from::Int,to::Int)
      va = Graphs.vertices(G)
      e = Graphs.ExEdge(Graphs.num_edges(G) + 1,va[from],va[to])
      Graphs.add_edge!(G,e)
      e
   end
end

一旦您可以访问一般图形,就可以轻松设置顶点标签

julia> using Graphs
julia> using GraphUtil
julia> G = empty_graph()
Directed Graph (0 vertices, 0 edges)

将标记为“a”和“b”的新顶点添加到G;他们将有索引 1 和 2

julia> add_label!(G,"a")
vertex [1] "a"
julia> add_label!(G,"b")
vertex [2] "b"

连接顶点 1 和顶点 2

julia> add_connection!(G,1,2)
edge [1]: vertex [1] "a" -- vertex [2] "b"

获取顶点列表

julia> va = vertices(G)
2-element Array{ExVertex,1}:
 vertex [1] "a"
 vertex [2] "b"

更改顶点 1 的标签和/或属性

julia> va[1].label = "c"
"c"

julia> vertices(G)
2-element Array{ExVertex,1}:
 vertex [1] "c"
 vertex [2] "b"

julia> va[1].attributes["weight"] = 2
2

julia> vertices(G)[1].attributes
Dict{UTF8String,Any} with 1 entry:
  "weight" => 2

关于graph-theory - 使用 Graphs.jl 更改顶点的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24578430/

相关文章:

python - 如何在 Julia 中将多维数组解压为变量

dataframe - 用 Julia Dataframe 中另一列的值替换缺失值

oop - Julia:了解 OOP 的多重分派(dispatch)

algorithm - 如何在具有最大平均子集大小的等距子集上拆分集?

algorithm - 只遍历无向图的所有边一次

python - 如何使用 Python 和 Networkx 在图中找到循环关系?

algorithm - VF2 算法的任何工作示例?

Julia DataFrames.jl - 使用 NA 过滤数据(NAException)

algorithm - 处理图中的负循环

integer - Julia 中 2 个正数的乘积得到负数