1

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?

2 Answers2

0

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)

Deisy Gysi
  • 56
  • 2
0

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)

enter image description here

After running

g.out <- delete.vertices(
  g,
  subset(
    V(g),
    with(components(g), membership == which.max(csize))
  )
)

plot(g.out)

you will see

enter image description here

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81