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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With