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:
- g
Graph Graph to be used. If it is not a tree, the breadth-first search tree from
rootis drawn; vertices not reachable from it are placed at the origin.- root
Vertex,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.
- sep
float(optional, default:1.) Minimum horizontal separation between vertices of the same layer.
- layer_sep
float(optional, default:1.) Vertical separation between layers.
- g
- Returns:
- pos
VertexPropertyMap Vector vertex property map with the \((x, y)\) coordinates, with the root at the top.
- pos
See also
radial_tree_layoutRadial layout of a tree
layered_layoutLayered 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 bysepat 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 leastsepto 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, seeradial_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") <...>
Breadth-first search tree of the karate club network from vertex 0, drawn with
tree_layout().#