Add a node and connect with every other node using igraph

Viewed 187

I would like to add a single node and connect it with every other node in a given graph with n nodes.

I am sharing an MWE below.

library(igraph)
g <- sample_k_regular(10, 3)
g <- g + vertices(11)

How can I connect node 11 with every other node in an efficient way? Also, when I add node 11 and plot the graph, for some reason, the node numbering is lost. I'd like to preserve the node numbering while adding the node.

enter image description here

enter image description here

1 Answers

You can try

g <- g %>%
  add_vertices(1) %>%
  add_edges(c(rbind(seq(vcount(.) - 1), 11)))

enter image description here

Data

set.seed(1)
g <- sample_k_regular(10, 3)

enter image description here

Related