我不認爲偏心給你的選擇,但如果我不會發出自己關於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的給出了一個更好的時間常數。 (爲了說明漸近的複雜性,請參見下面的縮放圖:)