bfs_layout#

bfs_layout(G, start, *, align='vertical', scale=1, center=None, store_pos_as=None)[source]#

Position nodes according to breadth-first search algorithm.

Parameters:
GNetworkX graph

A position will be assigned to every node in G.

startnode in G

Starting node for bfs

centerarray-like or None

Coordinate pair around which to center the layout.

store_pos_asstr, default None

If non-None, the position of each node will be stored on the graph as an attribute with this string as its name, which can be accessed with G.nodes[...][store_pos_as]. The function still returns the dictionary.

Returns:
posdict

A dictionary of positions keyed by node.

Notes

This algorithm currently only works in two dimensions and does not try to minimize edge crossings.

Examples

>>> G = nx.path_graph(4)
>>> pos = nx.bfs_layout(G, 0)
>>> # supress the returned dict and store on the graph directly
>>> _ = nx.bfs_layout(G, 0, store_pos_as="pos")
>>> nx.get_node_attributes(G, "pos")
{0: array([-1.,  0.]), 1: array([-0.33333333,  0.        ]), 2: array([0.33333333, 0.        ]), 3: array([1., 0.])}