Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

igraph - Neighbors as subgraph - make_ego_graph() as single graph

I'd like to construct a subgraph of a graph of a directed network with all the vertices sharing a certain vertex attribute (say, V(Grph)$year == "1952") and their first-order (immediate) neighbors, based only on the out-degree.

I've tried ego(), make_ego_graph(), neighbors(), and adjacent_vertices().

For instance, CitGraph <- make_ego_graph(Grph, 1, nodes = which(V(Grph)$year=="1952"), mode = "out") yields a list of graphs (and not a single comprehensive one) and surprisingly takes two hours for 50k vertices in this year and 150k neighbors being pointed to.

One approach I could think of would be to aggregate all these graphs in the list but don't know how. Also, I'd like to preserve the vertex attributes as my ultimate goal is to calculate assortativity_nominal(), based on another vertex attribute (geographical location in this case).

Thanks in advance for any suggestion you may have!

like image 503
user5835099 Avatar asked Mar 21 '16 15:03

user5835099


1 Answers

Indeed make_ego_graph returns a graph for the neighbourhood for each of the vertices in the list nodes.

I suggest you to solve it using the list of edges you need to include in your subgraph, instead of the list of vertices. Assuming your list of vertices is resolved like list_of_vertices <- V(Grph)$year == "1952" or whatever it is your condition, you would do something like,

list_of_edges <- E(your_graph)[from(list_of_vertices) | to(list_of_vertices)]
your_subgraph <- subgraph.edges(your_graph, list_of_edges)

(I'm using a directed graph.)

Hope it helps.

like image 200
lrnzcig Avatar answered Oct 28 '22 01:10

lrnzcig