Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create simple graph object in Julia using Graphs.jl

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:

enter image description here

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.

like image 689
prcastro Avatar asked Jun 01 '14 06:06

prcastro


1 Answers

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)

graphviz plot

like image 114
vchuravy Avatar answered Sep 29 '22 07:09

vchuravy