ego_graph

Contents

ego_graph#

graph_tool.structure.ego_graph(g, center, radius=1, weights=None, undirected=False)[git][source]#

Return the subgraph induced by the vertices within a given distance of a vertex.

Parameters:
gGraph

Graph to be used.

centerVertex or int

The central vertex.

radiusfloat (optional, default: 1)

The largest distance from the center.

weightsEdgePropertyMap (optional, default: None)

Edge weights (lengths).

undirectedbool (optional, default: False)

If True, the distances in directed graphs ignore the edge directions; otherwise they are measured from the center.

Returns:
uGraphView

The ego network of the center.

See also

label_out_component

Vertices reachable from a given vertex

shortest_distance

Distances from a source vertex

graph_tool.search.bfs_iterator

Breadth-first traversal from a vertex

Notes

The ego graph of the center \(c\) with radius \(r\) is the subgraph induced by the vertices \(\{u : d(c, u) \le r\}\), where \(d(c, u)\) is the length of a shortest path from \(c\) to \(u\) (the number of its edges, or the sum of their weights), following the edge directions for directed graphs unless undirected == True. The distances are computed with shortest_distance(), with the search stopped at radius, so that the time depends only on the size of the neighborhood.

Examples

>>> g = gt.collection.data["karate"]
>>> u = gt.ego_graph(g, 0)
>>> print(u.num_vertices(), u.num_edges())
17 34

The ego graph can be highlighted in a drawing of the network:

>>> inego = g.new_vp("bool")
>>> inego.a[u.get_vertices()] = True
>>> vcolor = g.new_vp("string", vals=np.where(inego.a, "#9bbedc",
...                                           "#cccccc"))
>>> vcolor[0] = "#f28c8e"
>>> s = gt.edge_endpoint_property(g, inego, "source")
>>> t = gt.edge_endpoint_property(g, inego, "target")
>>> ecolor = g.new_ep("string", vals=np.where(s.a & t.a, "#377eb8",
...                                           "#999999"))
>>> gt.graph_draw(g, g.vp.pos, vertex_fill_color=vcolor,
...               edge_color=ecolor, output="ego_graph-karate.pdf")
<...>
../_images/ego_graph-karate.png

Ego graph of radius one of vertex 0 (in red) of Zachary’s karate club network. The other vertices and the edges of the ego graph are shown in blue.#