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

﹥>﹥吖頭↗ 提交于 2019-11-29 19:05:16

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/36134840/igraph-neighbors-as-subgraph-make-ego-graph-as-single-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!