【问题标题】:second order neighbors of graph nodes in RR中图节点的二阶邻居
【发布时间】:2017-07-19 16:19:32
【问题描述】:

我正在寻找一种有效的方法来查找大图中所有节点的精确度数的邻域。即使它将图形存储为稀疏矩阵,igraph::ego 也会爆炸:

require(Matrix)
require(igraph)
require(ggplot2)

N <- 10^(1:5)
runtimes <- function(N) {
  g <- erdos.renyi.game(N, 1/N)  
  system.time(ego(g, 2, mindist = 2))[3]
}
runtime <- sapply(N, runtimes)
qplot(log10(N), runtime, geom = "line")

有没有更有效的方法?

【问题讨论】:

    标签: r matrix sparse-matrix igraph


    【解决方案1】:

    直接使用邻接矩阵提供了显着的改进。

    # sparse adjacency-matrix calculation of indirect neighbors -------------------
    
    diff_sparse_mat <- function(A, B) {
      # Difference between sparse matrices.
      #   Input: sparse matrices A and B
      #   Output: C = (A & !B), using element-wise diffing, treating B as logical
      stopifnot(identical(dim(A), dim(B)))
      A <- as(A, "generalMatrix")
      AT <- as.data.table(summary(as(A, "TsparseMatrix")))
      setkeyv(AT, c("i", "j"))
      B <- drop0(B)
      B <- as(B, "generalMatrix")
      BT <- as.data.table(summary(as(B, "TsparseMatrix")))
      setkeyv(BT, c("i", "j"))
      C <- AT[!BT]
      if (length(C) == 2) {
        return(sparseMatrix(i = C$i, j = C$j, dims = dim(A)))
      } else {
        return(sparseMatrix(i = C$i, j = C$j, x = C$x, dims = dim(A)))
      }
    }
     
    distance2_peers <- function(adj_mat) {
      # Returns a matrix of indirect neighbors, excluding the diagonal
      #   Input: adjacency matrix A (assumed symmetric)
      #   Output: (A %*% A & !A) with zero diagonal 
      indirect <- forceSymmetric(adj_mat %*% adj_mat) 
      indirect <- diff_sparse_mat(indirect, adj_mat)  # excl. direct neighbors
      indirect <- diff_sparse_mat(indirect, Diagonal(n = dim(indirect)[1]))  # excl. diag.
      return(indirect)  
    }    
    

    以鄂尔多斯人一为例,半分钟后,现在可以分析出 10^7 的网络,而不是 10^5 的网络:

    N <- 10 ^ (1:7)
    runtimes <- function(N) {
      g <- erdos.renyi.game(N, 1 / N, directed = FALSE)
      system.time(distance2_peers(as_adjacency_matrix(g)))[3]
    }
    runtime <- sapply(N, runtimes)
    qplot(log10(N), runtime, geom = "line")
    

    生成的矩阵在 (i, j) 处包含从 i 到 j 的长度为 2 的路径数(不包括包含 i 自身的路径)。

    【讨论】:

      猜你喜欢
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      相关资源
      最近更新 更多