I need to remove from the graph the group with the greatest number of nodes. I try to use the delete_vertices(name_of_graph, nodes_to_remove), but how can I indicate the nodes to remove? Ex. I have 5 groups with 6, 19, 27, 11, and 18 nodes. So I need to remove the group with 27 nodes, how can I do it?
you can use the delete.vertices()
function, you give the name of the graph and the name of the nodes you want to delete.
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)
You can try the code below using components
to label groups and find the one with largest number of vertices. Then you use subset
to filter out those vertices and apply delete.vertices
to remove them.
delete.vertices(
g,
subset(
V(g),
with(components(g), membership == which.max(csize))
)
)
Example
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)
After running
g.out <- delete.vertices(
g,
subset(
V(g),
with(components(g), membership == which.max(csize))
)
)
plot(g.out)
you will see
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.