Skip to content

Slower speed vs basic edgelist in some tasks #557

Description

@jessexknight

Thanks for all your work building & maintaining this package.

I was exploring custom network generating functions directly in R, and started benchmarking speeds. To my surprise, working directly with edge lists was faster than several igraph functions.

For example, in one use-case (percolation model), I didn't need adjacent vertices as a list (by vertex) but just the total "unlisted" vector. So I wrote adjacent.i function below, which was 10-100 times faster for this task.

I don't raise this as criticism, as I understand there are many features like attributes which add overhead. But in some applications, users may want to explore whether simple edge lists in R may be faster.

# config
library('igraph')
library('microbenchmark')
options(width=200)
# functions
gen.edgelist = function(k.i){ # random graph from degree distribution
  i.e = sample(rep(seq(length(k.i)),times=k.i)) # random vertices, rep by degree
  ii.e = do.call(cbind,split(i.e,1:2)) # egde list (random pairs)
}
adjacent.i = function(ii.e,i){ # get adjacent vertices from edgelist
  i.adj = c(ii.e[ii.e[,1] %in% i,2], ii.e[ii.e[,2] %in% i,1])
}
# graph generation
N    = 999 # number of vertices
k.i  = rgamma(N,1,0.1) # degree distribution
ii.e = gen.edgelist(k.i) # edge list
G    = graph_from_edgelist(ii.e,directed=FALSE) # graph object
# microbenchmark: get adjacent vertices
s = sample(seq(N),round(0.5*N)) # random sample
microbenchmark(times=100,
  {k1 = unlist(adjacent_vertices(G,s))},
  {k2 = adjacent.i(ii.e,s)})
print(all(sort(k1)==sort(k2))) # check same result (maybe different order)
# plot
# plot(G,vertex.label=NA,vertex.size=50/sqrt(N)) # plot, if you want

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions