【问题标题】:(local/nodal and global) Efficiency using igraph shortest.paths function(本地/节点和全局)使用 igraph shortest.paths 函数的效率
【发布时间】:2015-01-05 18:37:03
【问题描述】:

我正在尝试使用 igraph 包的 shortest.paths 计算图的局部效率。

根据定义,顶点 v 的局部效率是在 v 的所有直接邻居之间计算的“全局效率”(Latora & Machiori, 2001)。 我想出了下面的代码来提高全局和本地效率。但是,后者在其计算中包括目标顶点。在上面的论文中,他们说必须取出目标顶点。

#Global Efficiency (average inverse shortest paths between all u--v vertices)
eff<-1/(shortest.paths(my.graph))
eff[!is.finite(eff)]<-0
gl.eff<-mean(eff,na.rm=TRUE)    

#Mean local efficiency (global efficiency for each node)
gn<-graph.neighborhood(my.graph,1) #list with subgraphs of directly connected graphs
names(gn)<-colnames(my.corr.matrix)
local.eff<-numeric(length(gn))
for (i in 1:length(gn)){
  gn[[i]]<-gn[[i]] - vertex(V(gn[[i]])[grep(names(gn[i]),V(gn[[i]]))]) #doesn't match
  eff.gn<-1/(shortest.paths(gn[[i]]))
  eff.gn[!is.finite(gleff.gn)]<-0
  eff.gn<-mean(eff.gn,na.rm=TRUE)
  local.eff[i]<-gleff.gn
  mean.local.eff<-mean(local.eff, na.rm=TRUE)
}

我正在尝试将列表名称(列表的每个元素都是一个子图)与该子图中的顶点名称进行匹配。我正在尝试使用“grep()”,但无法正确使用。有人可以帮我解决这个问题吗?

提前致谢,

【问题讨论】:

    标签: r graph-theory igraph shortest-path


    【解决方案1】:

    我已经编写了一个函数来执行此操作,它比您编写的要快很多倍。看看以下是否适合您的需求。对于较小的图形(或者如果您使用的是 Windows),您可能希望将 simplify2array(mclapply(nodes, 替换为 sapply(nodes,,然后当然删除参数 mc.cores=detectCores()。但是,这确实有助于在大型图上提高性能。

    您可以在以下链接查看代码:

    Local efficiency code

    编辑:包括一些基准信息(函数f 是你的,g 是我在上面粘贴的)。这是在 4 核 @2.10 GHz (Intel i3-2310m) 的笔记本电脑上完成的。

    g.rand <- sample_gnp(100, .1)
    V(g.rand)$degree <- degree(g.rand)
    compare <- microbenchmark(f(g.rand), g(g.rand), times=1e2)
    compare
    Unit: milliseconds
          expr      min        lq     mean   median       uq      max neval cld
     f(g.rand) 476.9853 4097.2202 4544.720 4539.911 4895.020 9346.873   100   b
     g(g.rand) 299.2696  329.6629 1319.377 1114.054 2314.304 3003.966   100  a
    

    【讨论】:

    • 嗨@Chris,感谢分享。我会看看你的代码。我已经很久没有编程了,所以能够做到这一点对我来说已经是一件事情了。这种带有更高级解决方案的反馈可以进一步改善它。谢谢!
    • 我注意到您在代码中使用了(未加权)度数。如果我只是用 graph.strength 将其更改为加权程度,您认为它会运行吗?
    • 如果你只是删除weights=NA,那么如果图表被加权,它应该自动选择加权。
    【解决方案2】:

    如果有人需要 python 中的本地效率,这是我的代码:

    Python 版本

    import numpy as np
    from igraph import *
    np.seterr(divide='ignore')
    
    
    def nodal_eff(g):
        """
        This function calculates the nodal efficiency of a weighted graph object.
        Created by: Loukas Serafeim (seralouk), Nov 2017
    
        Args:
         g: A igraph Graph() object.
        Returns:
         The nodal efficiency of each node of the graph
        """
    
        sp = g.shortest_paths_dijkstra(weights = g.es["weight"])
        sp = np.asarray(sp)
        temp = 1.0 / sp
        np.fill_diagonal(temp, 0)
        N = temp.shape[0]
        ne = ( 1.0 / (N - 1)) * np.apply_along_axis(sum, 0, temp)
    
        return ne
    

    【讨论】:

      【解决方案3】:

      我知道我的标题并不能完全说明我的问题的真正含义。所以我自己来回答,因为我刚刚开始工作。

      #Mean local efficiency (global efficiency for each node)
      gn<-graph.neighborhood(my.graph,1) #list with subgraphs of directly connected graphs
      names(gn)<-V(my.graph)$name
      local.eff<-numeric(length(gn))
      for (i in 1:length(gn)){
        gn[[i]]<-gn[[i]] - vertex(V(gn[[i]])[match(names(gn[i]),V(gn[[i]])$name)]) #MATCHES
        eff.gn<-1/(shortest.paths(gn[[i]]))
        eff.gn[!is.finite(eff.gn)]<-0
        eff.gn<-mean(eff.gn,na.rm=TRUE)
        local.eff[i]<-eff.gn
      }
      local.eff[local.eff %in% NaN]<-NA
      mean.local.eff<-mean(local.eff, na.rm=TRUE)
      

      【讨论】:

        猜你喜欢
        • 2018-10-19
        • 2020-05-02
        • 2017-04-22
        • 2012-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多