如何使用igraph识别完全连接的节点集群? [英] How to identify fully connected node clusters with igraph?

查看:135
本文介绍了如何使用igraph识别完全连接的节点集群?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的igraph(连接了所有节点)来计算网络的群集.该绘图似乎可以正常运行,但是随后我无法从群集中返回正确的分组.

I'm trying to calculate the clusters of a network using igraph in R, where all nodes are connected. The plot seems to work OK, but then I'm not able to return the correct groupings from my clusters.

在此示例中,该图显示了4个主要群集,但在最大群集中,并非所有节点都已连接:

In this example, the plot shows 4 main clusters, but in the largest cluster, not all nodes are connected:

我希望能够从该graph对象返回以下群集列表:

I would like to be able to return the following list of clusters from this graph object:

[[1]]
[1] 8 9

[[2]]
[1]  7 10

[[3]]
[1]  4  6 11

[[4]]
[1] 2 3 5

[[5]]
[1]  1  3  5 12

示例代码:

library(igraph)

topology <- structure(list(N1 = c(1, 3, 5, 12, 2, 3, 5, 1, 2, 3, 5, 12, 4, 
6, 11, 1, 2, 3, 5, 12, 4, 6, 11, 7, 10, 8, 9, 8, 9, 7, 10, 4, 
6, 11, 1, 3, 5, 12), N2 = c(1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 
3, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 
11, 11, 11, 12, 12, 12, 12)), .Names = c("N1", "N2"), row.names = c(NA, 
-38L), class = "data.frame")

g2 <- graph.data.frame(topology, directed=FALSE)
g3 <- simplify(g2)
plot(g3)

cliques函数使我成为其中的一部分:

The cliques function gets me part of the way there:

tmp <- cliques(g3)
tmp

但是,此列表还提供了并非所有节点都连接的分组.例如,此集团包括节点1,2,3,5,但1仅连接到3,而2仅连接到3和5,而5仅连接到2:

but, this list also gives groupings where not all nodes connect. For example, this clique includes the nodes 1,2,3,5 but 1 only connects to 3, and 2 only connects to 3 and 5, and 5 only connects to 2 :

topology[tmp[[31]],]
#  N1 N2
#6  3  2
#7  5  2
#8  1  3

在此先感谢您的帮助.

推荐答案

您可以在igraph包中使用maximal.cliques.见下文.

You could use maximal.cliques in the igraph package. See below.

# Load package
library(igraph)

# Load data
topology <- structure(list(N1 = c(1, 3, 5, 12, 2, 3, 5, 1, 2, 3, 5, 12, 4, 
6, 11, 1, 2, 3, 5, 12, 4, 6, 11, 7, 10, 8, 9, 8, 9, 7, 10, 4, 
6, 11, 1, 3, 5, 12), N2 = c(1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 
3, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 
11, 11, 11, 12, 12, 12, 12)), .Names = c("N1", "N2"), row.names = c(NA, 
-38L), class = "data.frame")

# Get rid of loops and ensure right naming of vertices
g3 <- simplify(graph.data.frame(topology[order(topology[[1]]),],directed = FALSE))

# Plot graph
plot(g3)

# Calcuate the maximal cliques
maximal.cliques(g3)

# > maximal.cliques(g3)
# [[1]]
# [1] 9 8
# 
# [[2]]
# [1] 10  7
# 
# [[3]]
# [1] 2 3 5
# 
# [[4]]
# [1]  6  4 11
# 
# [[5]]
# [1] 12  1  5  3

这篇关于如何使用igraph识别完全连接的节点集群?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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