Is there a method or a class in igraph to do this procedure fast and efectively?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let’s assume that your graph is in
gand the set of vertices to be used is insampled(which is a vector consisting of zero-based vertex IDs).First, we select the set of edges where at least one endpoint is in
sampled:esis now an “edge sequence” object that consists of the edges of interest. Next, we take the edge list of the graph (which is an m x 2 matrix) and select the rows corresponding to the edges:Here,
as.vector(es)converts the edge sequence into a vector consisting of the edge IDs of the edges in the edge sequence, and use it to select the appropriate subset of the edge list. Note that we had to add 1 to the edge IDs because R vectors are indexed from 1 but igraph edge IDs are from zero.Next, we construct the result from the edge list:
Note that
g1will contain exactly as many vertices asg. You can take the subgraph consisting of the sampled vertices as follows:Note to users of igraph 0.6 and above: igraph 0.6 will switch to 1-based indexing instead of 0-based, so there is no need to subtract 1 from
all.verticesand there is no need to add 1 toas.vector(es). Furthermore, igraph 0.6 will contain a function calledsubgraph.edges, so one could simply use this: