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:
- g
Graph Graph to be used.
- center
Vertexorint The central vertex.
- radius
float(optional, default:1) The largest distance from the center.
- weights
EdgePropertyMap(optional, default:None) Edge weights (lengths).
- undirected
bool(optional, default:False) If
True, the distances in directed graphs ignore the edge directions; otherwise they are measured from the center.
- g
- Returns:
- u
GraphView The ego network of the center.
- u
See also
label_out_componentVertices reachable from a given vertex
shortest_distanceDistances from a source vertex
graph_tool.search.bfs_iteratorBreadth-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 withshortest_distance(), with the search stopped atradius, 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") <...>
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.#