【问题标题】:Can we find the neighbors of all vertices in a graph without a loop using R?我们可以使用 R 在没有循环的情况下找到图中所有顶点的邻居吗?
【发布时间】:2016-04-22 08:14:05
【问题描述】:

我们能否使用 R 在没有循环的情况下找到图中所有顶点对的公共邻居?

例如我们使用以下命令在TD1图中查找节点1和2的公共邻居:

intersect(neighborhood(graph=TD1,order=1,nodes=1),neighborhood(graph=TD1,order=1,nodes=2))

但是我需要找到每对顶点的公共邻居,并且因为图很大,所以使用循环非常耗时!有这个程序的功能吗?

【问题讨论】:

    标签: r graph igraph complex-networks


    【解决方案1】:

    我们可以使用邻接图来获得一个显示连接的稀疏矩阵。然后,将其转换为 data.table 以供邻居加入。然后,通过顶点对连接相邻顶点,其中顶点不相同并且顶点对不重复

    library(data.table)
    library(igraph)
    numVer <- 5
    g <- erdos.renyi.game(numVer, 1)
    plot(g)
    
    adjSM <- as(get.adjacency(g), "dgTMatrix")
    adjDT <- data.table(V1=adjSM@i+1, V2=adjSM@j+1)
    res <- adjDT[adjDT, nomatch=0, on="V2", allow.cartesian=TRUE
                 ][V1 < i.V1, .(Neighbours=paste(V2, collapse=",")),
                   by=c("V1","i.V1")][order(V1)]
    res
    

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      相关资源
      最近更新 更多