kamada_kawai_layout

kamada_kawai_layout#

graph_tool.draw.kamada_kawai_layout(g, weight=None, pos=None, edge_length=1.0, tol=0.0001, max_iter=None, dim=2)[git][source]#

Calculate the Kamada-Kawai spring layout of a connected graph.

Parameters:
gGraph

Graph to be used. If it is directed, it is treated as undirected. It must be connected.

weightEdgePropertyMap (optional, default: None)

Non-negative edge lengths, used for the graph distances. If not supplied, every edge has unit length.

posVertexPropertyMap (optional, default: None)

Initial vertex positions, which are modified in place. If not supplied, the vertices are initially placed on a circle (or on a sphere, with dim=3).

edge_lengthfloat (optional, default: 1.)

Length in the layout of an edge of unit weight.

tolfloat (optional, default: 1e-4)

Tolerance of the relative change of the largest gradient norm \(\Delta_m\) (see below) used to stop the algorithm.

max_iterint (optional, default: None)

Maximum number of vertex moves. If None, \(100V\) is used; if 0, there is no limit (the tolerance may never be reached, since the moves of single vertices can oscillate).

dimint (optional, default: 2)

Number of dimensions, either 2 or 3.

Returns:
posVertexPropertyMap

Vector vertex property map with the coordinates.

See also

stress_layout

Layout by stress majorization of the same energy

mds_layout

Layout by classical multidimensional scaling

sfdp_layout

Multilevel force-directed layout for large graphs

Notes

The force-directed layout of [kamada-algorithm-1989] places the vertices so that their Euclidean distances approximate the graph distances \(d_{ij}\), by minimizing the energy of springs between all pairs,

\[E = \sum_{i<j}\frac{1}{2d_{ij}^2}\left(|\boldsymbol x_i - \boldsymbol x_j| - \ell d_{ij}\right)^2,\]

where \(\ell\) is edge_length, by moving one vertex at a time with Newton’s method [boost-kamada-kawai]. In every step, the vertex \(m\) with the largest gradient norm \(\Delta_m = |\partial E/\partial\boldsymbol x_m|\) is selected, and it is moved by Newton-Raphson steps, each solving the linear system given by the Hessian of \(E\) with respect to \(\boldsymbol x_m\) (with the other vertices fixed), until the relative decrease of \(\Delta_m\) between consecutive steps of this vertex is smaller than tol (or \(\Delta_m = 0\)). The algorithm stops when the relative change of \(\max_m\Delta_m\) between consecutive selections is smaller than tol, or after max_iter selections. If pos is not given, the \(V\) vertices start on a circle of radius \(\ell V/2\pi\), at equal angles in the order of their indices (or, with dim=3, on a sphere of radius \(\ell\sqrt{V/4\pi}\), as in sphere_layout()). This requires the all-pairs distances, and hence \(O(V^2)\) memory and \(O(V^2)\) time per iteration, so that it is suitable only for small graphs; for large graphs, sfdp_layout() should be used. A good initial layout, e.g. from mds_layout(), reduces the number of iterations.

References

[kamada-algorithm-1989]

Tomihisa Kamada and Satoru Kawai, “An algorithm for drawing general undirected graphs”, Information Processing Letters 31, 7-15 (1989), DOI: 10.1016/0020-0190(89)90102-6 [sci-hub, @tor]

Examples

>>> g = gt.collection.data["karate"]
>>> pos = gt.kamada_kawai_layout(g)
>>> gt.graph_draw(g, pos=pos, output="karate-kamada-kawai.pdf")
<...>
../_images/karate-kamada-kawai.png

Kamada-Kawai layout of the karate club network.#