使用 Graphs.jl 在 Julia 中创建简单的图形对象 [英] Create simple graph object in Julia using Graphs.jl

查看:31
本文介绍了使用 Graphs.jl 在 Julia 中创建简单的图形对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始研究图论(我计划将它用于机器学习和/或贝叶斯推理).我想在 Julia 中编写代码,并找到了包 有 simple_graph 接口用于创建这样的小图.要手动创建上述图表,以下代码就足够了.

使用图表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)

使用手册中算法的简短示例.

test_cyclic_by_dfs(g)

这是一个基本情节.

朱莉娅>情节(g)

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.

解决方案

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)

这篇关于使用 Graphs.jl 在 Julia 中创建简单的图形对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆