How to speed of my R code when I use igraph?
-
12-10-2022 - |
Question
When I am running the following code it takes like forever to be run. Any idea how to speed up the code? Like changing the layout of matrix or hashing or ... ??
I am aiming to create an edge-node triangle adjancey matrix at the very end and use it for co-clustering.
library(igraph)
set.seed(1)
g <- erdos.renyi.game(100, .6)
#print(g)
plot(g)
ij <- get.edgelist(g)
print(ij)
library(Matrix)
m <- sparseMatrix(
i = rep(seq(nrow(ij)), each=2),
j = as.vector(t(ij)),
x = 1
)
print(m)
# Maximal cliques of size at least 3
cl <- maximal.cliques(g)
print(cl)
cl <- cl[ sapply(cl, length) > 2 ]
print(cl)
# Function to test if an edge is part of a triangle
triangle <- function(e) {
any( sapply( cl, function(u) all( e %in% u ) ) )
}
print(triangle)
# Only keep those edges
kl <- ij[ apply(ij, 1, triangle), ]
print(kl)
# Same code as before
m <- sparseMatrix(
i = rep(seq(nrow(kl)), each=2),
j = as.vector(t(kl)),
x = 1
)
print(m)
This is literally where the code is stuck:
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow