2

我的最终目标是这种无向的边缘列表:

每个由 3 个组件组成的网络,全连接,一个有 1 个节点,一个有 2 个节点,一个有 3 个节点:

将有 6 个节点,但由于第一个组件仅由 1 个节点组成,因此 node1 没有边。

tibble(from = c(2,4,4,5),
       to = (3,5,6,6)
)

但是组件的数量可能高于 3,比如说 30。组件内的平均边数将在rpois(30,1)+1.

tidygraph中,创建完整图的函数是create_complete,理论上我可以继续生成完整图并绑定它们的边缘列表。但在那种情况下,我会遇到命名边缘的问题,并且我可能会错过未连接的节点。

4

1 回答 1

1

更新

如果要显示集群标签,可以使用membership+components添加此属性,如下所示

set.seed(1)
do.call(
  graph.disjoint.union,
  lapply(
    rpois(30, 1) + 1,
    make_full_graph
  )
) %>%
  set_vertex_attr(name = "names", value = seq(vcount(.))) %>%
  set_vertex_attr(name = "cluster_label", value = membership(components(.))) %>% 
  as_tbl_graph(directed = FALSE)

这使

# A tbl_graph: 62 nodes and 49 edges
#
# An undirected simple graph with 30 components
#
# Node Data: 62 x 2 (active)
  names cluster_label
  <int>         <dbl>
1     1             1
2     2             2
3     3             2
4     4             3
5     5             3
6     6             4
# ... with 56 more rows
#
# Edge Data: 49 x 2
   from    to
  <int> <int>
1     2     3
2     4     5
3     6     7
# ... with 46 more rows

您可以使用disjoint.union+make_full_graph如下所示(假设您有 3 个完全连接的组件,分别具有 1、2 和 3 个节点)

library(tidygraph)
library(igraph)
do.call(
  graph.disjoint.union,
  lapply(
    1:3,
    make_full_graph
  )
) %>%
  set_vertex_attr(name = "names", value = seq(vcount(.))) %>% 
  as_tbl_graph(directed = FALSE)

这给了你

# A tbl_graph: 6 nodes and 4 edges
#
# An undirected simple graph with 3 components
#
# Node Data: 6 x 1 (active)
  names
  <int>
1     1
2     2
3     3
4     4
5     5
6     6
#
# Edge Data: 4 x 2
   from    to
  <int> <int>
1     2     3
2     4     5
3     4     6
# ... with 1 more row

关于 的使用,如果您替换为,rpois(30,1)+1也许这会有所帮助,例如,1:3rpois(30,1)+1

set.seed(1)
do.call(
  graph.disjoint.union,
  lapply(
    rpois(30,1)+1,
    make_full_graph
  )
) %>%
  set_vertex_attr(name = "names", value = seq(vcount(.))) %>% 
  as_tbl_graph(directed = FALSE)

并使用以下信息生成图表

# A tbl_graph: 62 nodes and 49 edges
#
# An undirected simple graph with 30 components
#
# Node Data: 62 x 1 (active)
  names
  <int>
1     1
2     2
3     3
4     4
5     5
6     6
# ... with 56 more rows
#
# Edge Data: 49 x 2
   from    to
  <int> <int>
1     2     3
2     4     5
3     6     7
# ... with 46 more rows
于 2022-02-10T22:52:02.930 回答