在igraph中找到n-cliques

时间:2016-10-17 13:48:09

标签: r igraph

我想知道我是否可以在igraph对象中找到所谓的n-cliques。根据Wasserman& Sons,这些被定义为“最大子图,其中任意两个节点之间的最大测地距离不大于 n ”。浮士德。我知道可以通过cliques()找到 n = 1 的派系,并且可以预先定义派系的大小,但有没有找到 n派系的方法大于1?

2 个答案:

答案 0 :(得分:0)

理论中,您可以尝试RBGL::kCliques

library(igraph)
library(RBGL)
set.seed(1)
g <- random.graph.game(100, p.or.m = 300, type = "gnm")
coords <- layout.auto(g)
cl <- kCliques(igraph.to.graphNEL(g))

k <- 2
clSel <- cl[[paste0(k, '-cliques')]][[1]] # select first of all k-cliques (e.g.)

plot(
  g, 
  layout = coords,
  vertex.shape = "none",
  vertex.label.color = ifelse(V(g) %in% clSel, "red", "darkgrey"), 
  edge.color = ifelse(tail_of(g, E(g)) %in% clSel & head_of(g, E(g)) %in% clSel, "orange", "#F0F0F099"),
  vertex.size = .5, 
  edge.curved = 1
)

但是,练习 ...

all(print(distances(induced_subgraph(g, clSel))) <=k ) # should be TRUE
# [1] FALSE

如果我们使用定义,似乎有错误

  

在社交网络分析中,图表中的k-clique是子图   任何两个节点之间的距离不大于k。

或许我误解了一些事情......

答案 1 :(得分:0)

感谢lukeA指出RBGL :: kCliques是R中解决此问题的解决方案。

允许n-cliques通过其他不是派系的节点进行链接。所以 如果A和D通过另一个节点F连接,A-B-C-D,B-E和C-E也可以是2-clique,即使F不在2- clique(因为它距离E 3)。见http://faculty.ucr.edu/~hanneman/nettext/C11_Cliques.html#nclique

但是,不允许n-clans出现这种行为;所有路径必须通过子图的成员来计算。因此,lukeA的测试表明,n-cliques不是所有的n-clans。

你可以通过抛弃路径不完全在子图中的所有子图来构造一个输出n族的函数,例如,

nclan <- function(g,n){
  g <- as.undirected(g)
  E(g)$weight <- 1 #just in case g has weights - does not modify original graph
  ncliques <- kCliques(ugraph(igraph.to.graphNEL(g))) #get cliques
  n.cand <- ncliques[[n]] #n-clique candidates to be an n-clan
  n.clan <- list() #initializes a list to store the n-clans
  n.clan.i <- 1 #initializes a list pointer
  for (n.cand.i in 1:length(n.cand)){ #loop over all of the candidates
    g.n.cand <- induced_subgraph(g,n.cand[[n.cand.i]]) #get the subgraph
    if (diameter(g.n.cand)<=n){ #check diameter of the subgraph
      n.clan[[n.clan.i]] <- n.cand[[n.cand.i]] #add n-clan to the list
      n.clan.i <- n.clan.i+1 #increment list pointer
    }
  }
  return(n.clan) #return the entire list
}

边缘权重的移除是由于RBGL的kCliques实现中的奇怪错误。同样,您可以编写k-plex函数:

kplex <- function(g,k,m){
  g.sym <- as.undirected(g) #to make sure that degree functions properly
  g.sym.degmk <- induced_subgraph(g.sym,igraph::degree(g.sym)>=(m-k)) #makes algorithm faster
  k.cand <- combn(V(g.sym.degmk)$name,m) #all candidate combinations with m members
  k.plex <- list() #initializes a list to store the k-plexes
  k.plex.i <- 1 #initializes a list pointer
  for (k.cand.i in 1:dim(k.cand)[2]){ #loop over all of the columns
    g.k.cand <- induced_subgraph(g.sym.degmk,k.cand[,k.cand.i]) #get the subgraph
    if (min(igraph::degree(g.k.cand))>=(m-k)){ #if minimum degree of sugraph is > m=k, k-plex!
      k.plex[[k.plex.i]] <- k.cand[,k.cand.i] #add k-plex to list
      k.plex.i <- k.plex.i+1 #increment list pointer
    }
  }
  return(k.plex) #return the entire list
}