On 07/16/2014 03:27 AM, Panos Achlioptas wrote:
> Dear all,
>
> I am using the following code to extract for a node of a graph the induced 
> subgraph containing the node and her neighbors (aka, the ego-netwkork).
>
> Can I make it more /efficiently/? In particular, is there a way to avoid 
> using python to create the neighbor list (or at least avoid the type casting).
>
> If not, could I use some code in C/C++ for doing this an embedded in 
> graph_tool?
>
> Thanks a lot,
> Panos
>
> def egoNetwork(inGraph, node):
>     '''
>     Compute the ego-network subgraph of the -inGraph- where the ego is the 
> -node-.
>     Precondition: inGraph is undirected
>     '''
>     neighbors    = [int(n) for n in node.out_neighbours()]
>     neighborhood = inGraph.new_vertex_property("bool")
>     neighborhood.a[neighbors] = 1
>     neighborhood.a[int(node)] = 1
>     returnGraphView(inGraph, vfilt = neighborhood)

The simplest thing one could do is:

    mask = g.new_vertex_property("bool")
    for u in node.out_neighbours():
        mask[u] = True
    mask[node] = True
    ego = GraphView(inGraph, vfilt=mask)

but if you have many neighbours in comparison to the whole graph, the
following may be faster:

    mask = shortest_distance(g, source=node, max_dist=1)
    mask.a[mask.a > 1] = False
    mask[node] = True

    ego = GraphView(inGraph, vfilt=mask)

Best,
Tiago

-- 
Tiago de Paula Peixoto <[email protected]>

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
graph-tool mailing list
[email protected]
http://lists.skewed.de/mailman/listinfo/graph-tool

Reply via email to