I made this random graph/network in R:
set.seed(123)
library(igraph)
# Create random graph
graph <- erdos.renyi.game(21, 0.3, type=c("gnp", "gnm"), directed = FALSE, loops = FALSE)
Then, I tried to create a random connected subgraph:
# Get the edges of the random subgraph
random_edges <- sample(E(graph), 10)
# Create subgraph from the random edges
subgraph <- subgraph.edges(graph, random_edges)
par(mfrow = c(1,2))
# Plot the subgraph
plot(subgraph, main = "random subgraph")
plot(graph, main = "original graph")
My Question: When I look at the random subgraph, I see that "node 4" is connected to "node 10" - but in the original graph, "node 4" and "node 10" are not connected to each other.
Can someone please show me how to fix this?
Thanks!


I think a workaround is to add a
nameattribute to vertices before conductinginduced.subgraph. Otherwise, the vertex "label" (to be shown in the plot of subgraph) is indexed differently, rather than keeping the samevidin the originalgraph.For example, you can do
then you will obtain a subgraph like below