2

It seems there's no built-in function in graph-tool library to generate a subgraph that contains all the neighbours of a certain node up to the n-th degree. The problem can be also framed as building an egonet around a node using n-degree neighbours. Let's consider the following toy example:

from graph_tool.all import *

edge_list = [
    [('node', 0), ('node', 1), 'xyz'],
    [('node', 1), ('node', 2)],
    [('node', 2), ('node', 3)],
    [('node', 3), ('node', 4), 'abc'],
    [('node', 0), ('node', 4)],
    [('node', 4), ('node', 5)],
    [('node', 5), ('node', 6)],
    [('node', 6), ('node', 7)],
    [('node', 0), ('node', 8)],
    [('node', 7), ('node', 8)],
    [('node', 7), ('node', 9)],
    [('node', 9), ('node', 10)]
]

g = Graph(directed=False)

I add few properties (vertices and edges) in order to check whether they propagate to the subgraph:

edge_attributes = g.new_edge_property("string")
g.edge_properties['edge_attributes'] = edge_attributes

nodes_id = g.add_edge_list(edge_list, hashed=True, eprops = [edge_attributes] )
g.vertex_properties['nodes_id'] = nodes_id

bool_flg = g.new_vertex_property('int')
bool_flg.set_value(0)
bool_flg[4] = 1
g.vertex_properties['bool_flg'] = bool_flg

Since I'm starting with an external name of the node, e.g. ('node', 0) (the graph-tool library operates on consecutive non-negative integers) I define a node-id retrieval function:

def find_vertex_id(G, node, id_mapping='nodes_id'):
    return int(find_vertex(G, G.vertex_properties[id_mapping], node)[0])

The first (and the most time-consuming) step (based on the networkx solution) is the one that generates IDs of nodes that are connected to the root (satisfying the egonet requirement):

def get_neighbours_n_degree(G, source, cutoff=None):
    seen = set()
    level = 0

    source_id = find_vertex_id(G, source) # translate into int id

    nextlevel={source_id}

    while nextlevel:
        thislevel = nextlevel
        nextlevel = set()
        for v in thislevel:
            if v not in seen:
                seen.update([v]) #set must be updated with an iterable
                nextlevel.update(g.get_all_neighbors(v)) #add neighbours
        if (cutoff is not None and cutoff <= level):
            break
        level = level + 1

    return seen # include the root

Subgraph generation follows:

def generate_subgraph(G, source, cutoff=None):
    subgraph_nodes = get_neighbours_n_degree(G=G, source=source, cutoff=cutoff)

    vfilt = G.new_vertex_property('bool')
    for i in subgraph_nodes:
        vfilt[i] = True

    sub = GraphView(G, vfilt)
    sub = Graph(sub, prune=True) #create independent copy; restart the node index

    return sub

I also define a drawing function:

def graph_draw_enhanced(graph):
    graph_draw(graph, vertex_text=graph.vertex_index,
              vertex_fill_color = graph.vertex_properties['bool_flg'])

My custom function works fine for the example provided but starts to slow down when provided with a network of 8M nodes - computing the 4th-degree egonet takes about 2,5 minutes. Is there a more optimal way to create an egonet in graph-tool?

The solution should return a subgraph that contains the same vertex and edge info as the original graph.

subgraph = generate_subgraph(g, ('node', 0), cutoff=2)

# check for edges
for edge in subgraph.edges():
    print(edge, g.edge_properties['edge_attributes'][edge])

# check for nodes
for node in subgraph.vertices():
    print(node, g.vertex_properties['bool_flg'][node])
balkon16
  • 1,338
  • 4
  • 20
  • 40

1 Answers1

4

This is substantially easier to do using graph-tool's filtering functionality. Here's a simple function that generates an ego net up to order n:

def ego_net(g, ego, n): 
    d = shortest_distance(g, ego, max_dist=n) 
    u = GraphView(g, vfilt=d.a < g.num_vertices()) 
    return u

This is returns a GraphView into the original graph, and hence preserves all internal property maps and vertex indexes. If a stand-alone graph is desired, this can be done by creating a new pruned graph:

u = ego_net(g, ego, n)
u = Graph(u, prune=True)

The approach above should be much faster that was proposed in the question.

Tiago Peixoto
  • 5,149
  • 2
  • 28
  • 28