[英]Number of vertices in Igraph in R
我是R中的IGraph的新手。
我正在使用IGraph进行社区检测,并且已经使用Walktrap技术建立了社区/集群。
接下来,在每个群集中,我要计算两个特定顶点之间的顶点数。 我要执行此操作的原因是,对于每个顶点XX,我都希望列出通过最多3个顶点连接到XX的顶点,这意味着距离XX最多不超过3个顶点。
任何人都可以帮助在R中完成此操作吗?
制作随机图(用于演示):
g <- erdos.renyi.game(100, 1/25)
plot(g,vertex.size=3)
获取Walktrap社区并另存为顶点属性:
V(g)$community<-walktrap.community(g, modularity = TRUE, membership = TRUE)$membership
V(g)$community
现在制作一个仅包含一个社区(例如社区2)的边和顶点的子图:
sub<-induced.subgraph(g,v=V(g)$community==2)
plot(sub)
制作一个包含所有最短路径的矩阵:
shortestPs<-shortest.paths(sub)
现在计算最短路径的数量小于或等于3。
我还排除了从每个节点到其自身的最短路径(shortestPaths!= 0)。
也除以2,因为每个节点对在无向图的矩阵中出现两次。
Number_of_shortest_paths_smaller_3 <- length(which(shortestPs<=3 & shortestPs!=0))/2
Number_of_shortest_paths_smaller_3
希望这接近您的需求,祝您好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.