tree_layout

Contents

tree_layout#

graph_tool.draw.tree_layout(g, root, sep=1.0, layer_sep=1.0)[git][source]#

Calculate a tidy layered layout of a tree or a forest.

Parameters:
gGraph

Graph to be used. If it is not a tree, the breadth-first search tree from root is drawn; vertices not reachable from it are placed at the origin.

rootVertex, int, or iterable of these

The root of the tree, or a list of roots, in which case a forest is drawn, with the trees side by side in the order of the roots.

sepfloat (optional, default: 1.)

Minimum horizontal separation between vertices of the same layer.

layer_sepfloat (optional, default: 1.)

Vertical separation between layers.

Returns:
posVertexPropertyMap

Vector vertex property map with the \((x, y)\) coordinates, with the root at the top.

See also

radial_tree_layout

Radial layout of a tree

layered_layout

Layered layout of a directed graph

Notes

The vertices are placed in layers according to their depth \(h_v\) in the breadth-first search tree, with the vertical coordinate \(y_v = h_v\,\delta\), where \(\delta\) is layer_sep, and each parent is centered above the midpoint of its first and last children, with the subtrees packed as closely as allowed by sep at every depth, following the algorithm of [reingold-tidier-1981]: the subtrees are laid out bottom-up, and each new subtree is shifted to the right by the smallest amount for which, at every depth shared with its left siblings, its leftmost vertex is at least sep to the right of their rightmost one. The order of the children is that of the out-edges. It runs in time \(O(Vh)\), where \(h\) is the height of the tree. For a radial version, see radial_tree_layout().

With several roots, the breadth-first search starts from all of them at once, so that every vertex belongs to the tree of the nearest root (the first one in the search, in case of ties), and the trees are packed side by side, as the subtrees of a common parent, with the roots in the top layer.

References

[reingold-tidier-1981]

Edward M. Reingold and John S. Tilford, “Tidier drawings of trees”, IEEE Transactions on Software Engineering SE-7, 223-228 (1981), DOI: 10.1109/TSE.1981.234519 [sci-hub, @tor]

Examples

>>> g = gt.collection.data["karate"]
>>> pos = gt.tree_layout(g, g.vertex(0))
>>> gt.graph_draw(g, pos=pos, output="karate-tree.pdf")
<...>
../_images/karate-tree.png

Breadth-first search tree of the karate club network from vertex 0, drawn with tree_layout().#