发布于 2014-07-12 05:00:45
这个包看起来不错,但是简单的图有整数作为顶点,所以你不能给它们贴上标签。因此,您必须使用更通用的类型ExVertex和ExEdge 请看这里。正确使用此接口需要构造正确的类型,这很难使用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)向G添加标记为"a“和"b”的新顶点;它们将具有索引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" => 2https://stackoverflow.com/questions/24578430
复制相似问题