我需要从图中删除节点数量最多的组。我尝试使用delete_vertices(name_of_graph,nodes_to_remove),但是如何指示要删除的节点?例如。我有5个组,分别有6、19、27、11和18个节点。所以我需要删除包含27个节点的组,我该怎么做呢?
发布于 2021-04-29 01:34:37
您可以使用delete.vertices()函数,给出图形的名称和要删除的节点的名称。
set.seed(666)
require(igraph)
net = data.frame(
Node.1 = sample(LETTERS[1:15], 15, replace = TRUE),
Node.2 = sample(LETTERS[1:10], 15, replace = TRUE))
g <- igraph::graph_from_data_frame(net, directed = FALSE )
plot(g)
g = delete.vertices(g, V(g)$name == "N")
plot(g)发布于 2021-04-29 05:18:46
您可以尝试下面的代码,使用components标记组,并找到顶点数量最多的组。然后使用subset过滤掉这些顶点,并应用delete.vertices将其移除。
delete.vertices(
g,
subset(
V(g),
with(components(g), membership == which.max(csize))
)
)示例
g1 <- make_ring(5)
g2 <- make_ring(3)
g3 <- make_ring(7)
g4 <- make_ring(4)
g <- disjoint_union(g1, g2, g3, g4)
plot(g)

运行后
g.out <- delete.vertices(
g,
subset(
V(g),
with(components(g), membership == which.max(csize))
)
)
plot(g.out)你会看到

https://stackoverflow.com/questions/67305088
复制相似问题