使用Graphs.jl在Julia中创建简单的图形对象 [英] Create simple graph object in Julia using Graphs.jl
问题描述
我开始研究图论(我打算将其用于机器学习和/或贝叶斯推理).我想用Julia编码,找到了包图形.但是,如何使用该程序包创建简单的图呢?例如,这个:
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:
如果我想知道如何使用Graphs
创建代表该图形的Julia对象,这将非常有用.它的文档缺少示例,所以我无法开始.
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.
推荐答案
Julia的
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)
这是一个基本情节.
julia> plot(g)
这篇关于使用Graphs.jl在Julia中创建简单的图形对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!