I mean to create a directed graph, whose edges among nodes are created if a certain criterion is met, i.e.
if (crit(i,j)) :
G.add_edge(i,j)
For this, I wrote a loop
nnd = len( ndlist )
for ind in range( nnd ) :
ndi = ndlist[ ind ]
for jnd in range( nnd ) :
if ( jnd != ind ) : # no self interaction
ndj = ndlist[ jnd ]
interaction = inrange( ndi, ndj, threshold )
if ( interaction ) :
G.add_edge( ind, jnd )
ndlist
is a list that contains features of the nodes, one element per node, and this info is used for assessing interaction among node pairs. The nodes are labeled with the same index number as in ndlist
, and this is taken advantage of. This is also the reason for looping over the integers, since edges connect (ind,jnd)
, not (ndi,ndj)
.
This turns out to be very slow. Is there any way of optimizing this?
Of course, execution time will depend on inrange
, but there might be a way of accelerating the code regardless of inrange
(perhaps plugging the contents of ndlist
as node attributes and iterating differently).
Even transforming my "very slow" code into a "just slow" code would help.