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)
On Sunday, July 6, 2014 12:09:50 PM UTC+4, Charles Santana wrote:
>
> Just to complete the information. I am using Julia Version
> 0.3.0-prerelease+3841 (2014-06-22 11:24 UTC)
>
> Charles
>
>
> On Sun, Jul 6, 2014 at 1:32 AM, Charles Novaes de Santana <
> [email protected] <javascript:>> wrote:
>
>> 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
>>
>
>
>
> --
> Um axé! :)
>
> --
> Charles Novaes de Santana, PhD
> http://www.imedea.uib-csic.es/~charles
>