label_out_component

label_out_component#

graph_tool.structure.label_out_component(g, root, label=None)[git][source]#

Label the out-component (or simply the component for undirected graphs) of a root vertex.

Parameters:
gGraph

Graph to be used.

rootVertex

The root vertex.

labelVertexPropertyMap (optional, default: None)

If provided, this must be an initialized Boolean vertex property map where the out-component will be labeled.

Returns:
labelVertexPropertyMap

Boolean vertex property map which labels the out-component.

See also

label_components

Connected components

count_reachable

Number of vertices reachable from every vertex

ego_graph

Subgraph within a given distance of a vertex

shortest_distance

Distances from a source vertex

Notes

The algorithm runs in \(O(V + E)\) time.

Examples

>>> g = gt.random_graph(100, lambda: poisson(2.2), directed=False)
>>> l = gt.label_out_component(g, g.vertex(2))
>>> print(l.a)
[1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 0 0 1 0 1 0
 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1
 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0]

The in-component can be obtained by reversing the graph.

>>> l = gt.label_out_component(gt.GraphView(g, reversed=True, directed=True),
...                            g.vertex(1))
>>> print(l.a)
[0 1 1 0 0 1 0 0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 1 0
 1 1 0 0 0 1 1 1 1 0 1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 1 0 0 0
 0 1 0 1 0 1 0 0 1 0 1 1 1 1 0 1 0 0 0 1 0 0 1 0 0 0]

In a directed graph, the out-component contains the vertices reachable from the given one, as shown for a Price network, whose edges point from newer to older vertices:

>>> g = gt.price_network(60, m=2)
>>> l = gt.label_out_component(g, g.vertex(59))
>>> pos = gt.sfdp_layout(g)
>>> vcolor = g.new_vp("string", vals=np.where(l.a, "#f28c8e", "#cccccc"))
>>> vcolor[59] = "#9bbedc"
>>> gt.graph_draw(g, pos, vertex_fill_color=vcolor, edge_color="#999999",
...               output="label_out_component-price.pdf")
<...>
../_images/label_out_component-price.png

Out-component (in red) of the vertex shown in blue in a Price network of 60 vertices.#