【问题标题】:Number of vertices in Igraph in RR中Igraph中的顶点数
【发布时间】:2015-07-21 01:21:59
【问题描述】:

我对 R 中的 IGraph 相当陌生。

我正在使用 IGraph 进行社区检测,并且已经使用 walktrap 技术构建了我的社区/集群。

接下来,在每个集群中,我想计算每两个特定顶点之间的顶点数。我想这样做的原因是,对于每个顶点 XX,我想列出通过说最大 3 个顶点连接到 XX 的顶点,这意味着距离 XX 不超过 3 个顶点。

谁能帮忙在R中如何做到这一点?

【问题讨论】:

标签: r igraph


【解决方案1】:

制作随机图(用于演示):

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)。

也除以二,因为每个节点对在无向图的矩阵中出现两次。

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

希望这与您所需要的很接近,祝您好运!

【讨论】:

    猜你喜欢
    • 2013-01-21
    • 2017-10-19
    • 1970-01-01
    • 2018-04-14
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多