layered_layout#
- graph_tool.draw.layered_layout(g, layer=None, sep=1.0, layer_sep=1.0, n_iter=24)[git][source]#
Calculate a layered (hierarchical) layout of a directed graph.
- Parameters:
- g
Graph Graph to be used. If it is undirected, the edges are oriented from the lower to the higher vertex index.
- layer
VertexPropertyMap(optional, default:None) Integer layer of each vertex. If not supplied, the longest-path layering is used, after the reversal of a set of edges that breaks the directed cycles.
- sep
float(optional, default:1.) Minimum horizontal separation between vertices of the same layer.
- layer_sep
float(optional, default:1.) Vertical separation between layers.
- n_iter
int(optional, default:24) Number of sweeps of the crossing reduction.
- g
- Returns:
- pos
VertexPropertyMap Vector vertex property map with the \((x, y)\) coordinates, with the first layer at the top.
- pos
See also
tree_layoutTidy layered layout of a tree
radial_tree_layoutRadial layout of a tree
graph_tool.structure.topological_sortTopological ordering of the vertices
Notes
The layout follows the framework of layered graph drawing of [sugiyama-methods-1981]:
If the graph has directed cycles, the edges of a feedback arc set found with the greedy heuristic of [eades-fast-1993] are reversed.
Each vertex is assigned to the layer given by the length of the longest path that ends at it (unless
layeris given).Edges that span more than one layer are subdivided by dummy vertices in the intermediate layers.
The number of edge crossings is reduced by reordering each layer by the barycenters of the neighbors in the adjacent layer, in alternating downward and upward sweeps, keeping the best ordering found.
The horizontal coordinates are obtained by moving each vertex towards the mean position of its neighbors in the adjacent layers, subject to the order and the separation
sepwithin each layer.
The dummy vertices are not part of the output, so that long edges are drawn as straight lines. The algorithm runs in time \(O((V + E)\log V + n_{\text{iter}}(V' + E'\log E'))\), where \(V'\) and \(E'\) include the dummy vertices and edges, the logarithmic factors coming from the ordering of step 1 and the counting of the crossings by sorting.
With a given
layer, the function yields a multipartite layout of any graph, with the crossings reduced and the edges within a layer ignored by the crossing reduction: for example, a bipartite layout withlayergiven by the vertex types, or a breadth-first layout withlayergiven by the distances from a root vertex, obtained withshortest_distance().References
[sugiyama-methods-1981]Kozo Sugiyama, Shojiro Tagawa and Mitsuhiko Toda, “Methods for visual understanding of hierarchical system structures”, IEEE Transactions on Systems, Man, and Cybernetics 11, 109-125 (1981), DOI: 10.1109/TSMC.1981.4308636 [sci-hub, @tor]
[eades-fast-1993]Peter Eades, Xuemin Lin and W. F. Smyth, “A fast and effective heuristic for the feedback arc set problem”, Information Processing Letters 47, 319-323 (1993), DOI: 10.1016/0020-0190(93)90079-O [sci-hub, @tor]
Examples
>>> g = gt.price_network(100, seed_graph=gt.complete_graph(2, directed=True)) >>> pos = gt.layered_layout(g) >>> gt.graph_draw(g, pos=pos, output="price-layered.pdf") <...>
Layered layout of a Price network (a directed acyclic graph) with 100 vertices.#