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:
- g
Graph Graph to be used. If it is directed, it is treated as undirected. It must be connected.
- weight
EdgePropertyMap(optional, default:None) Non-negative edge lengths, used for the graph distances. If not supplied, every edge has unit length.
- pos
VertexPropertyMap(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_length
float(optional, default:1.) Length in the layout of an edge of unit weight.
- tol
float(optional, default:1e-4) Tolerance of the relative change of the largest gradient norm \(\Delta_m\) (see below) used to stop the algorithm.
- max_iter
int(optional, default:None) Maximum number of vertex moves. If
None, \(100V\) is used; if0, there is no limit (the tolerance may never be reached, since the moves of single vertices can oscillate).- dim
int(optional, default:2) Number of dimensions, either 2 or 3.
- g
- Returns:
- pos
VertexPropertyMap Vector vertex property map with the coordinates.
- pos
See also
stress_layoutLayout by stress majorization of the same energy
mds_layoutLayout by classical multidimensional scaling
sfdp_layoutMultilevel 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 thantol(or \(\Delta_m = 0\)). The algorithm stops when the relative change of \(\max_m\Delta_m\) between consecutive selections is smaller thantol, or aftermax_iterselections. Ifposis 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, withdim=3, on a sphere of radius \(\ell\sqrt{V/4\pi}\), as insphere_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. frommds_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") <...>
Kamada-Kawai layout of the karate club network.#