找到连通矩阵的最大独立子集

时间:2016-06-01 20:29:20

标签: r matrix subset igraph

我有两个由连接矩阵链接的组,如下所示:

#
#   X1  X2  X3  X4  X5  X6
#   1   0   0   0   0   0  V1
#   1   1   1   0   0   0  V2  
#   0   1   0   0   0   0  V3
#   0   0   1   0   0   0  V4
#   0   0   0   1   0   0  V5
#   0   0   0   1   0   0  V6
#   0   0   0   0   1   0  V7
#   0   0   0   0   1   1  V8
#   0   0   0   0   1   0  V9
#   0   0   0   0   0   1  V10
# 

因此X1链接到V1和V2,而V2链接到X1,X2和X3,依此类推。我需要找到一种方法(算法或命令)来获取矩阵的所有最大的独立子集。所以,在这种情况下:

#   X1  X2  X3 
#   1   0   0  V1
#   1   1   1  V2  
#   0   1   0  V3
#   0   0   1  V4

#   X4
#   1  V5
#   1  V6

#   X5  X6 
#   1   0   V7
#   1   1   V8  
#   1   0   V9
#   0   1   V10

你有任何提示吗?我想已经有一些库或函数可以用于图形分析或线性代数。

1 个答案:

答案 0 :(得分:3)

正如你暗示的那样,我们可以用igraph来做到这一点:

# dummy data
df1 <- read.table(text = "  X1  X2  X3  X4  X5  X6
V1  1   0   0   0   0   0
                  V2    1   1   1   0   0   0
                  V3    0   1   0   0   0   0
                  V4    0   0   1   0   0   0
                  V5    0   0   0   1   0   0
                  V6    0   0   0   1   0   0
                  V7    0   0   0   0   1   0
                  V8    0   0   0   0   1   1
                  V9    0   0   0   0   1   0
                  V10   0   0   0   0   0   1
                  ")

library(dplyr)
library(tidyr)
library(igraph)

# make graph object
gg <- 
  df1 %>% 
  add_rownames(var = "V") %>% 
  gather(X, value, -V) %>% 
  filter(value == 1) %>% 
  graph.data.frame

# split based on clusters of graph
lapply(
  sapply(split(clusters(gg)$membership,
               clusters(gg)$membership), names),
  function(i)
  df1[intersect(rownames(df1), i),
      intersect(colnames(df1), i),
      drop = FALSE])

# $`1`
#    X1 X2 X3
# V1  1  0  0
# V2  1  1  1
# V3  0  1  0
# V4  0  0  1
# 
# $`2`
#    X4
# V5  1
# V6  1
# 
# $`3`
#     X5 X6
# V7   1  0
# V8   1  1
# V9   1  0
# V10  0  1