[graph-tool] Extract a neighborhood of a randomly selected node

gogurt gogurt at gmail.com
Fri May 6 23:02:00 CEST 2016

Hi all,

I have a pretty large graph G from which want to do the following:

1) Randomly select a node from G
2) Get the vertices that lie within radius 2 of the node
3) Extract the subgraph induced by those vertices

My code for doing this is the following:
randnode = random.randint(0,G.num_vertices())

pred_map = gt.shortest_distance(G, source=G.vertex(randnode), max_dist=2,
pred_tree = gt.predecessor_tree(G, pred_map[1])


verts = pred_tree.vertices()
vmap = G.new_vertex_property('bool')
for i in verts:
    vmap[G.vertex_index[i]] = True

Then using vmap, I plan to do a GraphView on G and get the subgraph. But
something is clearly going wrong here. If I uncomment the line under the #
DEBUG comment, then I clearly see that pred_tree always has the same number
of vertices as the original graph.

Am I missing something about how predecessor_tree() works? I expected it to
only return a subgraph of the original graph, which should have a lot fewer

View this message in context: http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/Extract-a-neighborhood-of-a-randomly-selected-node-tp4026565.html
Sent from the Main discussion list for the graph-tool project mailing list archive at Nabble.com.

More information about the graph-tool mailing list