Skip to content

Add kamada_kawai_layout function#1583

Open
wallies wants to merge 1 commit intoQiskit:mainfrom
wallies:add-kamada-kawai-layout
Open

Add kamada_kawai_layout function#1583
wallies wants to merge 1 commit intoQiskit:mainfrom
wallies:add-kamada-kawai-layout

Conversation

@wallies
Copy link
Copy Markdown

@wallies wallies commented May 5, 2026

Adds rustworkx.kamada_kawai_layout, supporting both PyGraph and PyDiGraph inputs. Implements the original Kamada-Kawai (1989) algorithm: an outer loop selects the node with the largest partial-gradient norm and an inner loop applies a 2D Newton step against the local Hessian until convergence.

Disconnected graphs are laid out by running Kamada-Kawai independently on each connected component and packing the components in a horizontal row, which avoids the visual collapse seen with single-objective Kamada-Kawai on disconnected inputs. Directed graphs have their distance matrix symmetrised before optimising (Kamada-Kawai is fundamentally undirected).

Closes #438

Example outputs

Side-by-side comparisons against networkx.kamada_kawai_layout (NetworkX 3.6.1, which uses L-BFGS-B). Both produce K-K minima; differences are rotation/reflection of the same energy minimum.

Florentine families (15 nodes, connected)

Florentine families comparison

Medici is centrally placed in both. Topologically equivalent under reflection.

3x4 hexagonal lattice (24 nodes, connected)

Hexagonal lattice comparison

Both recover the regular hexagonal grid structure.

Three disconnected components: K4, C5, K3

Disconnected components comparison

NetworkX 3.6.1 solves K-K globally on the full distance matrix; components overlap when solved jointly (left). This implementation solves each component independently and packs them horizontally (right).

Adds rustworkx.kamada_kawai_layout, supporting both PyGraph and
PyDiGraph inputs. Implements the original Kamada-Kawai (1989)
algorithm: an outer loop selects the node with the largest
partial-gradient norm and an inner loop applies a 2D Newton step
against the local Hessian until convergence.

Disconnected graphs are laid out by running Kamada-Kawai
independently on each connected component and packing the
components in a horizontal row, which avoids the visual collapse
seen with single-objective Kamada-Kawai on disconnected inputs.
Directed graphs have their distance matrix symmetrised before
optimising (Kamada-Kawai is fundamentally undirected).

Closes Qiskit#438
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 5, 2026

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add new layout methods

2 participants