我们可以不使用R进行循环就找到图中所有顶点的邻居吗? [英] Can we find the neighbors of all vertices in a graph without a loop using R?

查看:65
本文介绍了我们可以不使用R进行循环就找到图中所有顶点的邻居吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can we find the common neighbors of all pairs of vertices in a graph without a loop using R?

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

For example we use the following command to find the common neighbors of node 1 and 2 in the TD1 graph:

相交(邻居(图= TD1,order = 1,节点= 1),邻居(图= TD1,order = 1,节点= 2))

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

But I need to find the common neighbors of every pairs of vertices and because the graph is large using a loop is very time consuming! Is there function for this procedure?

推荐答案

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

we can use the adjacency graph to get a sparse matrix showing the connections. Then, convert it into data.table for joining by neighbours. Then, concatenate neighbours by pairs of vertices where vertices are not the same and also where pairs of vertices are not repeated

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

这篇关于我们可以不使用R进行循环就找到图中所有顶点的邻居吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆