【问题标题】:R: igraph eccentricity seems not use weighed edgeR:igraph 偏心率似乎没有使用加权边缘
【发布时间】:2016-12-03 14:55:14
【问题描述】:

我使用 igraph 计算顶点偏心率,图是加权的,随机生成如下

n <- 500
g <- sample_smallworld(1, n, 10, 0.05)
E(g)$weight <- (runif(1)+0.1)*10
is.weighed(g)
dia <- diameter(g)
dia

这是一个小世界网络,有 500 个顶点和随机加权边。使用 diameter 和 'is.weighted' 来检查它是否被加权。但是eccentricity没有使用权重,生成如下结果,

d_list <- eccentricity(g)
summary(d_list)

输出如下,

d_list 摘要(d_list)
分钟。第一曲。中位数平均第三曲。最大
4.000 4.000 4.000 4.004 4.000 5.000

如何解决这个问题?

现在我使用max(distances(g))来解决它,但我认为这不是一种高效优雅的方式。

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    我不认为 eccentricity 给了你选择,但如果我不宣扬 distances() 方法的优雅,从效率的角度来看,这两种算法都将在 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 可以提供更好的时间常数。 (为了说明渐近复杂度,请参见下面的比例图:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多