layered_layout

Contents

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:
gGraph

Graph to be used. If it is undirected, the edges are oriented from the lower to the higher vertex index.

layerVertexPropertyMap (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.

sepfloat (optional, default: 1.)

Minimum horizontal separation between vertices of the same layer.

layer_sepfloat (optional, default: 1.)

Vertical separation between layers.

n_iterint (optional, default: 24)

Number of sweeps of the crossing reduction.

Returns:
posVertexPropertyMap

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

See also

tree_layout

Tidy layered layout of a tree

radial_tree_layout

Radial layout of a tree

graph_tool.structure.topological_sort

Topological ordering of the vertices

Notes

The layout follows the framework of layered graph drawing of [sugiyama-methods-1981]:

  1. If the graph has directed cycles, the edges of a feedback arc set found with the greedy heuristic of [eades-fast-1993] are reversed.

  2. Each vertex is assigned to the layer given by the length of the longest path that ends at it (unless layer is given).

  3. Edges that span more than one layer are subdivided by dummy vertices in the intermediate layers.

  4. 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.

  5. 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 sep within 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 with layer given by the vertex types, or a breadth-first layout with layer given by the distances from a root vertex, obtained with shortest_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")
<...>
../_images/price-layered.png

Layered layout of a Price network (a directed acyclic graph) with 100 vertices.#