Detect bi-cliques in r for bipartite graph

935 views Asked by At

I am trying to recreate the Biclique Communities method (Lehmann, Schwartz, & Hansen, 2008) in R which relies on the definition of a Ka,b biclique. The example below shows two adjacent K2,2 bicliques - the first clique is {A,B,1,2} and the second clique is {B,C,2,3}. I would like to be able to identify these cliques using R so that I can apply this to a broader dataset.

Adjacent K2,2 Bicliques

I have included my attempt so far in R and I am stuck with the following two issues:

  1. If I use the standard walktrap.community it recognises the communities but does not allow the set {B,2} to belong in both cliques
  2. If I use an updated clique.community function this doesn't seem to identify the cliques or I don't understand correctly (or both)

Example code:

library(igraph)

clique.community <- function(graph, k) {
  clq <- cliques(graph, min=k, max=k)
  edges <- c()
  for (i in seq_along(clq)) {
    for (j in seq_along(clq)) {
      if ( length(unique(c(clq[[i]], clq[[j]]))) == k+1 ) {
        edges <- c(edges, c(i,j))
      }
    }
  }
  clq.graph <- simplify(graph(edges))
  V(clq.graph)$name <- seq_len(vcount(clq.graph))
  comps <- decompose.graph(clq.graph)

  lapply(comps, function(x) {
    unique(unlist(clq[ V(x)$name ]))
  })
}

users <- c('A', 'A', 'B', 'B', 'B', 'C', 'C')
resources <- c(1, 2, 1, 2, 3, 2, 3)
cluster <- data.frame(users, resources)
matrix <- as.data.frame.matrix(table(cluster))

igraph <- graph.incidence(matrix)

clique.community(igraph, 2)
walktrap.community(igraph)
2

There are 2 answers

0
Mario Angst On BEST ANSWER

Beware that the above solution becomes inefficient very quickly even for small (dense) graphs and values of k,l due to the fact that comb <- combn(vMode1, k) becomes extremely large.

A more efficient solution can be found in the "biclique" package that is in development at https://github.com/YupingLu/biclique.

0
JamesLee On

I managed to find a script for this in the Sisob workbench

computeBicliques <- function(graph, k, l) {

  vMode1 <- c()
  if (!is.null(V(graph)$type)) {

    vMode1 <- which(!V(graph)$type)
    vMode1 <- intersect(vMode1, which(degree(graph) >= l))
  }

  nb <- get.adjlist(graph)

  bicliques <- list()

  if (length(vMode1) >= k) {

    comb <- combn(vMode1, k)
    i <- 1
    sapply(1:ncol(comb), function(c) {

      commonNeighbours <- c()
      isFirst <- TRUE

      sapply(comb[,c], function(n) {

        if (isFirst) {

          isFirst <<- FALSE
          commonNeighbours <<- nb[[n]]
        } else {

          commonNeighbours <<- intersect(commonNeighbours, nb[[n]])
        }
      })

      if (length(commonNeighbours) >= l) {

        bicliques[[i]] <<- list(m1=comb[,c], m2=commonNeighbours)
      }

      i <<- i + 1
    })
  }
  bicliques
}