简体   繁体   English

R中的Igraph中的顶点数

[英]Number of vertices in Igraph in R

I'm fairly new to IGraph in R. 我是R中的IGraph的新手。

I'm doing community detection using IGraph and have already built my communities /clusters using the walktrap technique. 我正在使用IGraph进行社区检测,并且已经使用Walktrap技术建立了社区/集群。

Next, within each cluster, I want to count the number of vertices between each two certain vertices. 接下来,在每个群集中,我要计算两个特定顶点之间的顶点数。 The reason I want to do this is, for each vertex XX, I want to list vertices that are connected to XX via say max 3 vertices, meaning no further than 3 vertices away from XX. 我要执行此操作的原因是,对于每个顶点XX,我都希望列出通过最多3个顶点连接到XX的顶点,这意味着距离XX最多不超过3个顶点。

Can anyone help how this can be done in R please? 任何人都可以帮助在R中完成此操作吗?

making a random graph (for demonstration): 制作随机图(用于演示):

g <- erdos.renyi.game(100, 1/25)
plot(g,vertex.size=3)

get walktrap communities and save as vertex attribute: 获取Walktrap社区并另存为顶点属性:

V(g)$community<-walktrap.community(g, modularity = TRUE, membership = TRUE)$membership
V(g)$community

now make a subgraph containing only edges and vertices of one community, eg community 2: 现在制作一个仅包含一个社区(例如社区2)的边和顶点的子图:

sub<-induced.subgraph(g,v=V(g)$community==2)
plot(sub)

make a matrix containing all shortest paths: 制作一个包含所有最短路径的矩阵:

shortestPs<-shortest.paths(sub)

now count the number of shortest paths smaller or equal to 3. 现在计算最短路径的数量小于或等于3。

I also exclude shortest paths from each node to itself (shortestPaths!=0). 我还排除了从每个节点到其自身的最短路径(shortestPaths!= 0)。

also divide by two because every node pair appears twice in the matrix for undirected graphs. 也除以2,因为每个节点对在无向图的矩阵中出现两次。

Number_of_shortest_paths_smaller_3 <- length(which(shortestPs<=3 & shortestPs!=0))/2
Number_of_shortest_paths_smaller_3

Hope that's close to what you need, good luck! 希望这接近您的需求,祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM