Skip to main content

layout_mds

Function layout_mds 

Source
pub fn layout_mds(
    graph: &Graph,
    dist: Option<&[Vec<f64>]>,
) -> IgraphResult<Vec<[f64; 2]>>
Expand description

Compute the MDS (Multidimensional Scaling) layout.

Places vertices so that Euclidean distances in 2D approximate the graph-theoretic distances. Uses classical (metric) MDS via double centering of the squared distance matrix and eigendecomposition.

For disconnected graphs, each component is laid out independently and the resulting layouts are placed side by side.

§Arguments

  • graph — input graph (edge directions ignored for distances).
  • dist — optional distance matrix (row-major, n×n). If None, shortest-path distances are computed automatically.

Returns [x, y] positions for each vertex.

§Errors

Returns InvalidArgument if the distance matrix dimensions don’t match the vertex count.

§Examples

use rust_igraph::{Graph, layout_mds};

let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 4).unwrap();

let pos = layout_mds(&g, None).unwrap();
assert_eq!(pos.len(), 5);
assert!(pos.iter().all(|p| p[0].is_finite() && p[1].is_finite()));