Hi,

sorry for my naive question. I'd like to import hundreds of large and dense

adjacency matrices available as a numpy arrays into graph_tool.

The following option takes very long. Q1: Is there a preferable alternative?

import graph_tool.all as gt

import numpy as np

adj=np.array([[1,1,0],[1,1,0],[0,0,1]])

n_vertices=adj.shape[0]

g = gt.Graph(directed=False)

vlist = g.add_vertex(n_vertices)

edge_ids = np.where(adj !=0)

for i in range(edge_ids[0].shape[0]):

g.add_edge(g.vertex(edge_ids[0][i]), g.vertex(edge_ids[1][i]))

Since I am dealing with undirected graphs without selfloops a first step

would be to take only one triangle of the symmetric adjacency matrix:

# get ids of upper triangle

upper_tr = np.triu_indices_from(np.zeros((n_vertices,n_vertices)))

adj[a[0],a[1]] = 0

...

Q2: is this a valid approach?

Thanks in advance,

Matthias

attachment.html (976 Bytes)

tiago
(Tiago Peixoto)
#2
Hi,

Hi,

sorry for my naive question. I'd like to import hundreds of large and

dense adjacency matrices available as a numpy arrays into graph_tool.

The following option takes very long. Q1: Is there a preferable alternative?

import graph_tool.all as gt

import numpy as np

adj=np.array([[1,1,0],[1,1,0],[0,0,1]])

n_vertices=adj.shape[0]

g = gt.Graph(directed=False)

vlist = g.add_vertex(n_vertices)

edge_ids = np.where(adj !=0)

for i in range(edge_ids[0].shape[0]):

g.add_edge(g.vertex(edge_ids[0][i]), g.vertex(edge_ids[1][i]))

Since I am dealing with undirected graphs without selfloops a first

step would be to take only one triangle of the symmetric adjacency

matrix:

# get ids of upper triangle

upper_tr = np.triu_indices_from(np.zeros((n_vertices,n_vertices)))

adj[a[0],a[1]] = 0

...

Q2: is this a valid approach?

I would do a simple loop of the form:

n = adj.shape[0]

for i in xrange(n):

for j in xrange(i+1, n):

if adj[i,j]:

g.add_edge(g.vertex(i), g.vertex(j))

This has O(N^2) complexity, of course, but since your matrices are

dense, there is little you can do about that.

An alternative would be to perform the loop in C++ using the

inline() function. This should be much faster.

Cheers,

Tiago