I’m working with a directed network in igraph. Here’s some code to generate such a network:
# example graph
# install.packages(c("igraph"), dependencies = TRUE)
library(igraph)
set.seed(1)
g <- erdos.renyi.game(20, 1/20,directed=TRUE,loops=FALSE)
V(g)$name <- letters[1:20]
par(mar=rep(0,4))
plot(g)
I would like to extract subsets of this network that include an arbitrary vertex and all edges and vertices that direct to this vertex, regardless of the degree or distance of that connection.
Here’s a photoshopped example of what I would like to extract, using vertex “E” in this case. I would like to extract a network that includes all vertices labeled in blue and connected edges.
4
Answers
I thought all three of the answers given here were useful, but the solution from @Zhiya is the one that I ended up using (it appears to be the computationally fastest method).
That said, I did end up using elements from @42-, because my desired output was a subsetted network. Here are the changes to address this:
It seems that the
edge_connectivity
funciton is a good candidate for this task. It hassource
andtarget
parameters that consider the directedness of the edges:It’s possible to recover a graph object with that list:
And you asked for the edge list:
Besides what @42-, I think
distance
can also be used.Briefly, the code within the brackets returns the indices of vertices that has nonzero and finite distances from
e
to others.You can get the neighbourhood graph of a specific node(s) using
make_ego_graph
. Set the order to the number of vertices (or n-1) to allow the full graph to be traversed.