stress_layout#
- graph_tool.draw.stress_layout(g, weight=None, pos=None, dim=2, pivots=None, max_iter=1000, epsilon=0.0001)[git][source]#
Calculate a layout of the graph by stress majorization.
- Parameters:
- g
Graph Graph to be used (the edge directions are ignored).
- weight
EdgePropertyMap(optional, default:None) Positive edge lengths. If not given, every edge has unit length.
- pos
VertexPropertyMap(optional, default:None) Initial positions. If not given, they are obtained with pivot MDS, and the connected components are placed side by side.
- dim
int(optional, default:2) Number of dimensions.
- pivots
int(optional, default:None) Number of pivots of the sparse stress model. If
0, the full stress is used; ifNone, the full stress is used for components with at most 1000 vertices, and 100 pivots otherwise.- max_iter
int(optional, default:1000) Maximum number of iterations.
- epsilon
float(optional, default:1e-4) The iterations stop when no vertex moves by more than this fraction of the size of the layout.
- g
- Returns:
- pos
VertexPropertyMap The vertex positions.
- pos
See also
kamada_kawai_layoutKamada-Kawai spring layout
mds_layoutLayout by classical multidimensional scaling
sfdp_layoutMultilevel force-directed layout for large graphs
Notes
The layout places the vertices so that their Euclidean distances approximate the graph distances \(d_{ij}\), by minimizing the stress (see stress majorization)
\[\sigma = \sum_{i<j} w_{ij}\left(|\boldsymbol x_i - \boldsymbol x_j| - d_{ij}\right)^2, \qquad w_{ij} = d_{ij}^{-2},\]with the localized majorization of Gansner, Koren, and North [gansner-graph-2004], in which every vertex moves to the weighted average of the positions that would put it at the right distance from each other vertex,
\[\boldsymbol x_i \leftarrow \frac{\sum_{j\ne i} w_{ij}\left[\boldsymbol x_j + d_{ij}\frac{\boldsymbol x_i - \boldsymbol x_j}{|\boldsymbol x_i - \boldsymbol x_j|}\right]}{\sum_{j\ne i} w_{ij}},\]where the fraction is replaced by zero for coinciding positions. All vertices are updated at once, in parallel, from the positions of the previous iteration, and the iterations stop when the largest move of a vertex is at most
epsilontimes the largest side of the bounding box of the layout, or aftermax_iteriterations. This is the energy ofkamada_kawai_layout(), but the majorization converges more reliably. The full stress needs the all-pairs distances, i.e. \(O(V^2)\) memory and time per iteration.The sparse stress model of Ortmann, Klimenta, and Brandes [ortmann-sparse-2017] keeps in the sum of vertex \(i\) only the terms of its incident edges, with \(d_{ij}\) the edge length, and of \(k\) pivots \(p\), with the weights \(w_{ip} = s_{ip}/d_{ip}^2\), where \(s_{ip}\) is the number of vertices of the region of \(p\) (the vertices closer to \(p\) than to the other pivots) at distance at most \(d_{ip}/2\) from \(p\) (or one, if there are none), so that a pivot represents the vertices around it. The first pivot is a random vertex, and every next one is the vertex farthest from the pivots chosen so far. This takes \(O(k(V + E))\) time for the distances and \(O(kV + E)\) per iteration. The initial layout is given by pivot MDS [brandes-eigensolver-2007] (with 50 pivots), which uses NumPy’s random number generator, and the connected components are laid out separately.
Parallel implementation.
If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.
References
[gansner-graph-2004]Emden R. Gansner, Yehuda Koren and Stephen North, “Graph drawing by stress majorization”, Graph Drawing (GD 2004), Lecture Notes in Computer Science 3383, 239-250 (2005), DOI: 10.1007/978-3-540-31843-9_25 [sci-hub, @tor]
[ortmann-sparse-2017]Mark Ortmann, Mirza Klimenta and Ulrik Brandes, “A sparse stress model”, Journal of Graph Algorithms and Applications 21, 791-821 (2017), DOI: 10.7155/jgaa.00440 [sci-hub, @tor], arXiv: 1608.08909
[brandes-eigensolver-2007]Ulrik Brandes and Christian Pich, “Eigensolver methods for progressive multidimensional scaling of large data”, Graph Drawing (GD 2006), Lecture Notes in Computer Science 4372, 42-53 (2007), DOI: 10.1007/978-3-540-70904-6_6 [sci-hub, @tor]
Examples
>>> g = gt.collection.data["power"] >>> pos = gt.stress_layout(g) >>> gt.graph_draw(g, pos=pos, output="power-stress.png") <...>
Stress layout of the power grid of the western United States.#