I'm using graph-tool and would like to find the second-order neighbors of a node (neighbors of neighbors who are not the node itself or the original neighbors). I thought it might be faster to use graph-tool's built-in topology functions, so I tried
# Here g is the digraph, and node is the vertex whose neighbors we want
d = gt.topology.shortest_distance(g,g.vertex(node),max_dist=2)
f = gt.GraphView(g,vfilt=d.a == radius)
second_neighbors = f.vertices()
However, this appears to be considerably slower than direct iteration, even on large graphs. How can I do this very quickly for large networks?
Using iter_out_neighbors and collecting nodes in Python sets should indeed be faster than using intermediary
GraphViewobjects. Here's an example:Here's a visual check of the result with the network visualization package gravis inside a Jupyter notebook. The start node 22 is colored green, the second-order neighbors are colored red, and I'm hovering with the mouse over the first-order neighbor 132 to highlight it's direct surroundings. Disclaimer: I'm the author of gravis. It can be used to easily create interactive visualizations of graphs coming from graph-tool, NetworkX, igraph and a few other network packages in Python.