如何在图形对象上使用 max_cliques() 后附加所有子列表编号

how to append all sub-list numbers after using max_cliques() on a graph object

我有一个 igraph 使用以下内容创建的对象:

    g3 <- graph.data.frame(DF.WORK.EDGE, directed=TRUE, vertices=DF.WORK.VERTEX)

然后我使用 max_cliques() 在该图形对象上应用:

    print('max clique for prefer to work with measure is: ')
    max_cliques(g3)

自然地,我得到了一长串不同的集团子列表:

这个列表一直在继续,我需要找到哪个节点出现得最频繁​​(最重叠),所以我知道哪个节点在不同的子集团中。

如果没有临时函数来总结这些值的频率,谁能告诉我如何检索所有子列表并将它们全部连接到同一个列表中?我正在考虑在那之后做一个总结table,看看哪个数字在所有子列表中出现最多。谢谢!


让我们随机分组 10 个:

set.seed(42)
n_cliques <- 10
clique_sizes <- sample(x = 20, size = n_cliques)
x <- lapply(seq_along(clique_sizes), function(i) {
  sample(x = 50, clique_sizes[i]) 
})
x[1:3]
#> [[1]]
#>  [1] 23 36 45 13 22 43 44  6 20 50 37 48 38 49  3 18 14 30 15
#> 
#> [[2]]
#>  [1] 42 37 39 19 32  1 49 45  9 38 25 15 17  2 36 16 33 30
#> 
#> [[3]]
#> [1] 32 48 30 16 47 18

将它们放入数据框中:

d <- do.call(rbind, lapply(seq_along(x), function(i) {
  data.frame(
    clique = i,
    vertex = x[[i]]
  )
}))
head(d)
#>   clique vertex
#> 1      1     23
#> 2      1     36
#> 3      1     45
#> 4      1     13
#> 5      1     22
#> 6      1     43

顶点 30 出现在 7 个派系中:

sort(table(d$vertex), decreasing = TRUE)[1:10]
#> 
#> 30  1 32 36 38 37 50  8 11 14 
#>  7  5  5  5  5  4  4  3  3  3

这是 7 个派系:

subset(d, vertex == 30)$clique
#> [1] 1 2 3 4 6 7 9

您可以 unlisttable cliques

set.seed(8675309) ##Sets the random number generator so that results are exactly reproducible.
g <- graph_from_edgelist(matrix(sample(LETTERS[1:10], 50, replace=T), ncol = 2), directed = FALSE) ##made a random graph
cliques <- max_cliques(g) ##Find maximal cliques

#cliques is a list of vectors containing nodes
#unlist turns the list into one vector, 
#a side effect of unlist is that the node ID is returned not the node
#name (in this case A-J) so we use the names function to grab the node names. 
#Lastly table tabulates the data, in this case counts the instances of each node in the vector. table could be replaced by tapply, or any other number of function.
nodeCliques <- table(names(unlist(cliques)))
#To get the nodes that are in the most cliques we subset the table based on the max number of cliques the nodes are in.
nodeCliques[which(nodeCliques==max(nodeCliques))]
# H I 
# 4 4