Create a Graph from a matrix by using Graphs.jl

1,130 views
Skip to first unread message

Charles Novaes de Santana

unread,
Jul 5, 2014, 7:32:52 PM7/5/14
to julia...@googlegroups.com
Dear all,

I am starting to use Graphs.jl and some simple questions arrived. It is not clear for me how to create a Graph from my data if my data is in a matrix format, for example.

The documentation is plenty of information about how the classes, types, and algorithms are defined, and they also provide some good examples to use these functions once you have a graph object. But I couldn't find a clear example about how to create a graph from my data. Let's suppose my data is the matrix 'mat' (representing the distances between each pair of nodes in my graph), as defined below:

julia> mat=rand(4,4);

julia> for i in 1:4
        mat[i,i]=0;
        end

julia> mat
4x4 Array{Float64,2}:
 0.0       0.310287  0.497059  0.0472071
 0.624904  0.0       0.256988  0.675347
 0.305605  0.504063  0.0       0.915409
 0.85426   0.145528  0.055314  0.0     

Please forgive me if this information has been asked before and I didn't find the correct reference. I would much appreciate any help.

Best wishes,

Charles

--
Um axé! :)

--
Charles Novaes de Santana, PhD
http://www.imedea.uib-csic.es/~charles

Charles Novaes de Santana

unread,
Jul 6, 2014, 4:09:50 AM7/6/14
to julia...@googlegroups.com
Just to complete the information. I am using Julia Version 0.3.0-prerelease+3841 (2014-06-22 11:24 UTC)

Charles

Benjamin Lind

unread,
Jul 6, 2014, 9:21:47 AM7/6/14
to julia...@googlegroups.com
There are a few ways to do it. I prefer to make arrays of the vertices and then the edges and enter them into the graph function. For example:

mat=rand(4,4)

nnodes = size(mat, 1)
nedges = nnodes * (nnodes - 1)
vlist = Array(KeyVertex{Int64}, nnodes)
for i = 1:nnodes
    vlist[i] = KeyVertex(i, i)
end
ecounter = 1
elist = Array(ExEdge{typeof(vlist[1])}, nedges)
for i = 1:nnodes
    for j = 1:nnodes
        if i != j
            elist[ecounter] = ExEdge(ecounter, vlist[i], vlist[j])
            elist[ecounter].attributes["distance"] = mat[i, j]
            ecounter += 1
        end
    end
end

g = graph(vlist, elist, is_directed = true)

Charles Novaes de Santana

unread,
Jul 6, 2014, 4:07:23 PM7/6/14
to julia...@googlegroups.com
Thank you, Benjamin!

I was wondering if Graphs had something like function "graph.adjacency" in Igraph library (http://www.inside-r.org/packages/cran/igraph/docs/graph.adjacency). But your suggestion is very good for what I am doing. Thank you, indeed!!

Best regards,

Charles

Kevin Squire

unread,
Jul 6, 2014, 4:44:48 PM7/6/14
to julia...@googlegroups.com
Graphs.jl does implement some Matrix representations for graphs: http://graphsjl-docs.readthedocs.org/en/latest/matrix.html.  However, I haven't used them, and it's a little unclear from the docs how to do so.

There is an issue (https://github.com/JuliaLang/Graphs.jl/issues/111) to add examples to the documentation, but it hasn't been followed up on yet.

Cheers,
   Kevin
Reply all
Reply to author
Forward
0 new messages