I have this network graph in R:
set.seed(123)
library(igraph)
# Define a vector of names
names <- c("John", "Alex", "Jason", "Matt", "Tim", "Luke", "Shawn", "Henry", "Steven", "Scott", "Adam", "Jeff", "Connor", "Peter", "Andrew", "Dave", "Daniel", "Benjamin", "Joseph", "Martin")
# Create an empty graph with 20 nodes
g <- make_empty_graph(20)
# Add random edges between nodes to represent friendships
set.seed(123) # for reproducibility
num_edges <- 40
edge_list <- sample(names, size = num_edges * 2, replace = TRUE)
edge_list <- matrix(edge_list, ncol = 2, byrow = TRUE)
g <- graph_from_edgelist(edge_list, directed = FALSE)
# Set the node names to be the names vector
V(g)$name <- names
# Plot the graph
plot(g, vertex.label.cex = 0.7, vertex.label.color = "black", vertex.label.dist = 2)
My Question: Suppose I start with John - I want to make a random subgraph such that:
- The "maximum degree" is = n
- The "number of nodes" in the subgraph is = m
- All nodes in the subgraph can be traced back to John
Using this previous question R: All Nodes of Degree "N" that can be reached from Some Node, I attempted to solve this problem:
all_distances = as.numeric(max(distances(g, "John")))
max_degree = as.numeric(sample(0:all_distances, 1))
frame_with_max_degree = data.frame(unlist(ego(g, max_degree, "John")))
number_of_nodes = as.numeric(sample(0:nrow(frame_with_max_degree), 1))
My Question: But from here, I am not sure how to randomly select individual nodes number_of_nodes
such that all these nodes are necessarily connected to "John".
For instance - suppose n = 3 and m = 2: I would not want to create a subgraph with "John" , "Jason" and "Steven" - because even though "Jason and Steven" might be within a randomly generated radius, "Jason and Steven" are still not directly connected to "John".
Can someone please show me how to do this?
Thanks!