Create simple graph object in Julia using Graphs.jl

回眸只為那壹抹淺笑 提交于 2019-12-23 10:14:06

问题


I am starting to study graph theory (I plan to use it in machine learning and/or bayesian inference). I want to code in Julia, and found the package Graphs. But how can I use this package to create simple graphs? For example, this one:

It would be very useful if I undertood how to create an Julia object that represents this graph using Graphs. Its documentation lacks examples so I can't get started.


回答1:


Julia's Graphs package has simple_graph interface for creating such small graphs. To manually create the above mentioned graph the following code is sufficient.

using Graphs

g = simple_graph(4, is_directed=true) # simple_graph(number_of_vertices, is_directed=true|false)
add_edge!(g, 1, 2)
add_edge!(g, 1, 4)
add_edge!(g, 2, 4)
add_edge!(g, 3, 1)
add_edge!(g, 3, 2)
add_edge!(g, 4, 3)

Short example for using an algorithm from the manual.

test_cyclic_by_dfs(g)

And here's a basic plot.

julia> plot(g)



来源:https://stackoverflow.com/questions/23977361/create-simple-graph-object-in-julia-using-graphs-jl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!