igraph graph.lattice 所有组合(在 R 中)

igraph graph.lattice for all combinations (in R)

问候:这张图看起来应该是使用 R 的 igraph 包非常简单的东西。我试图弄清楚我需要传递哪些参数来获得子模式和超级模式之间的边缘。在此示例中,表示了一个包含 5 个项目的项目集。

我确定这可以通过 igraph 中的 graph.lattice 函数解决,但我还没有想出要传递的参数。请注意,所提供的图表来自 Data Mining: Concepts and Techniques (Han, Kamber, & Pei, 2011)。

在此先感谢您的指导。

我认为 graph.lattice 无法创建这种图表。但是你可以定义一个新函数:

graph.comb <- function(word) {
  # creates graph objects from character combination in word
  # example graph.comb("abc")

  do_layer <- function(words) {
    do.call( rbind, lapply(words, function(word){
      l_vec <- sapply(seq_len(nchar(word)), function(l) substring(word, l, l))
      w_comb <- apply(combn(l_vec, nchar(word)-1), 2, paste, collapse = "")
      w_df <- expand.grid(from = w_comb, to = word, stringsAsFactors = FALSE)
    }))
  }

  df_edges <- data.frame(from = word, to = NA, stringsAsFactors = FALSE)
  df2 <- df_edges
  while( min(nchar(df_edges$from)) > 0) {
    df2 <- do_layer(df2$from)
    df_edges <- rbind(df_edges, df2)
  }
  df_edges <- df_edges[complete.cases(df_edges), ]
  df_edges <- df_edges[!duplicated(df_edges), ]
  return(graph.data.frame(df_edges ))
}

与任意字符串一起使用:

g1 <- graph.comb("abcd")
plot(g1, layout = layout.sugiyama(g1)$layout )

结果: