手动实现怎么样?
library(igraph)
set.seed(1)
resample <- function(x, ...) x[sample.int(length(x), ...)]
n <- 1000
tm <- matrix(sample(0:1, n^2, prob = c(0.95, 0.05), replace = TRUE), n, n)
tm <- (tm == 1 | t(tm) == 1) * 1
diag(tm) <- 0
start <- 23 # Random walk starting vertex
len <- 10 # Walk length
path <- c(start, rep(NA, len))
for(i in 2:(len + 1)) {
idx <- tm[path[i - 1], ] != 0
if(any(idx)) {
path[i] <- resample(which(idx), 1, prob = tm[path[i - 1], idx])
} else {
break # Stopping if we get stuck
}
}
path
# [1] 23 3434 4908 4600 332 4266 1752 1845 4847 4817 1992