Skip to main content

layout_merge_dla

Function layout_merge_dla 

Source
pub fn layout_merge_dla<G: AsRef<Graph>>(
    graphs: impl IntoIterator<Item = G>,
    coords: &[Matrix],
) -> Result<Matrix>
Expand description

Merges the 2D layouts of several graphs (typically the components of a graph) into one, using diffusion-limited aggregation (DLA).

Each layout is covered by a circle and rescaled so that the area of the circle grows with the size of the graph; the largest layout is placed at the origin and the others, from larger to smaller, perform random walks until they stick next to the already placed ones. The result has the rows of all the layouts, in the given order. The graphs are currently only used for bookkeeping (igraph only looks at the coordinates). Uses the calling thread’s default random number generator. The typical input is the list of components from Graph::decompose, each laid out on its own. graphs is any collection of graphs or references to graphs (like the multi-graph functions of operators), e.g. the &Vec<Graph> returned by decompose or a &[&Graph]. Binds igraph_layout_merge_dla.

§Errors

ErrorKind::InvalidValue if the number of graphs and layouts differ, no layout is given, a layout is empty or is not 2D, or a layout does not have one row per vertex of its graph.

§Examples

use igraph::prelude::*;
use igraph::layout::layout_merge_dla;
// A triangle and a separate edge, laid out component by component.
let g = Graph::from_edges(&[(0, 1), (1, 2), (2, 0), (3, 4)], 5, false).unwrap();
let parts = g.decompose(Connectedness::Weak, None, 1).unwrap();
let layouts: Vec<Matrix> = parts.iter().map(|p| p.layout_circle(..).unwrap()).collect();
rng::seed(42).unwrap();
// Any collection of graphs works: `&Vec<Graph>`, `&[&Graph]`, `Vec<Graph>`...
let merged = layout_merge_dla(&parts, &layouts).unwrap();
assert_eq!(merged.shape(), (5, 2));
// Each component is only scaled and translated: the triangle stays equilateral.
let d = |i: usize, j: usize| (merged[(i, 0)] - merged[(j, 0)]).hypot(merged[(i, 1)] - merged[(j, 1)]);
assert!((d(0, 1) - d(1, 2)).abs() < 1e-9 && (d(1, 2) - d(2, 0)).abs() < 1e-9);