pub fn spectral_embedding<N>(
nodes: &[N],
edges: &[(N, N, f32)],
max_iterations: usize,
tolerance: f64,
) -> Vec<(N, (f64, f64))>Expand description
Deterministic 2D spectral layout over an abstract undirected, unweighted
graph. Returns one (node, (x, y)) pair per distinct node with both
coordinates min–max normalised to [0, 1].
The coordinates are the two smallest non-trivial eigenvectors of the graph
Laplacian L = D − A (the Fiedler vector and the next one) — the classic
spectral-drawing layout: graph-adjacent nodes land near each other and
disconnected components separate cleanly, so a client force-directed layout
seeded from these hints converges faster and to a stable arrangement. The
hint is purely advisory; callers may seed their own layout with it or ignore
it entirely.
Method: shifted, deflated power iteration. We iterate on M = cI − L (whose
DOMINANT eigenvectors are L’s SMALLEST), deflating the trivial constant
mode (L-eigenvalue 0) and then each found coordinate via Gram–Schmidt, so
successive sweeps recover the next Laplacian eigenvector. max_iterations
caps the sweeps per coordinate; tolerance is the L1 convergence threshold
on successive normalised iterates.
Determinism (guaranteed and tested): the node universe is a sorted, deduped
BTreeSet; the start vectors are fixed index-polynomial seeds (no random
initialisation); sweeps run in ascending index order with a fixed cap. The
eigenvector sign — mathematically arbitrary — is pinned by the fixed seed,
so identical input always produces identical coordinates. An isolated node
or an edgeless / single-node graph still yields finite, deterministic hints
(a degenerate coordinate collapses to 0.5).