我不认为偏心给你的选择,但如果我不会发出自己关于distance()方法的优点,从效率的角度来看,这两种算法都将在O(|V|^2*log(|V|))
(假设|E| = O(|V|)
)中执行以计算每个节点的偏心率,如果你运行一些测试,你会得到:
f1 <- function(n) {
g <- sample_smallworld(1, n, 10, 0.05)
E(g)$weight <- (runif(n*10)+0.1)*10
system.time(eccentricity(g))
}
f2 <- function(n) {
g <- sample_smallworld(1, n, 10, 0.05)
E(g)$weight <- (runif(n*10)+0.1)*10
system.time(distances(g))
}
f3 <- function(n) {
g <- sample_smallworld(1, n, 10, 0.05)
tmp <- (runif(n*10)+0.1)*10
system.time(eccentricity(g))
}
f4 <- function(n) {
g <- sample_smallworld(1, n, 10, 0.05)
tmp <- (runif(n*10)+0.1)*10
system.time(distances(g))
}
t1 <- sapply((10:60)*50, function(x){f1(x)[3]})
t2 <- sapply((10:60)*50, function(x){f2(x)[3]})
t3 <- sapply((10:60)*50, function(x){f3(x)[3]})
t4 <- sapply((10:60)*50, function(x){f4(x)[3]})
d <- data.frame(x = (10:60)*50, t1, t2, t3, t4)
ggplot(d, aes(x = x))+
geom_line(aes(y = t1, col = "'Weighted' eccentricity"))+
geom_line(aes(y = t2, col = "Weighted distances"))+
geom_line(aes(y = t3, col = "Unweighted eccentricity"))+
geom_line(aes(y = t4, col = "Unweighted distances")) +
scale_x_continuous(name = "Number of Nodes") +
scale_y_continuous(name = "Time (s)")
正如你可以看到它们都具有相同的时间渐近复杂性,但在未加权的情况下,使用BFS的给出了一个更好的时间常数。 (为了说明渐近的复杂性,请参见下面的缩放图:)