I have been recently working with graph, specifically in detecting community structure in graph, and this requires me to program random walks.
I have ran my code several times but all of them show a very slow performance of the random walkers. Meanwhile, the pre-coded algorithm in the igraph
packages runs very smoothly. For example, if I run 1000 walkers on a graph of 10000 nodes, it would take me a few hours just for the task while the cluster_walktrap
function only takes a few seconds.
My random walk algorithm is as follow:
Here is my code in R:
library("igraph")
library("tictoc")
network <- read.csv("mydata", header=T) #Data consisted of two columns Source and Target
network$Source = as.factor(network$Source)#CONVERT TO FACTOR
network$Target = as.factor(network$Target)#CONVERT TO FACTOR
g <- graph_from_edgelist(as.matrix(network), directed=FALSE)
Al <- get.adjlist(g)
n <- length(Al)
d<- matrix(, ncol=n, nrow=1) #Degree vector
for (i in 1:n) {
d[i] <- length(Al[[i]])
}
tic("Simple random walk")
K <- 5000
t <- 5 #length of random walks
x <- vector("list", K) #Generate random walk
Prw <- matrix(0, nrow=n, ncol=n)#Generate Probability Matrix
for (k in 1:n) { #For k-th node
for (i in 1:K) { #For i-th walker
for (j in 2:t) { #For j-th step
x[[i]][1] <- k #Set starting as node k
prev <- x[[i]][j - 1] #Get neighbors
x[[i]][j] <- Al[[prev]][sample(1:d[prev], 1)] #Choose a random neighbor
}
Prw[k, x[[i]][t]] <- Prw[k, x[[i]][t]] + 1/K #Plus 1/K the probability
#to move to where the walker stop
}
}
Is there anyway that I can improve the speed of my algorithm?
Small sample of dataset
network <- structure(list(Source = c(828L, 884L, 856L, 889L, 719L, 861L,
840L, 864L, 719L, 745L, 708L, 888L, 825L, 869L, 729L, 769L, 823L,
861L, 774L, 805L, 713L, 774L, 729L, 697L, 823L), Target = c(697L,
864L, 869L, 856L, 713L, 863L, 803L, 856L, 840L, 805L, 823L, 889L,
889L, 774L, 888L, 869L, 840L, 729L, 769L, 800L, 819L, 856L, 876L,
890L, 810L)), row.names = c(NA, 25L), class = "data.frame")
The link to my full data set
https://drive.google.com/file/d/1iKSTx_2Kjk701dwmueHQ_xsxsFUKngKq/view?usp=sharing