Skip to main content

square_lattice

Function square_lattice 

Source
pub fn square_lattice(
    dim: &[u32],
    nei: u32,
    directed: bool,
    mutual: bool,
    periodic: Option<&[bool]>,
) -> IgraphResult<Graph>
Expand description

Build a d-dimensional square lattice graph.

Returns a graph on Π dim[k] vertices arranged on a multi-dimensional grid, with edges joining vertices that are one step apart along any single axis. periodic (when supplied) selects which dimensions wrap around (torus mode).

§Parameters

  • dim — per-dimension cardinalities. Empty slice is the zero-dimensional case (singleton).
  • nei — neighbour-distance radius. Only 0 and 1 are supported in this initial port (higher values require igraph_connect_neighborhood, a separate AWU).
  • directed — whether to produce a directed graph.
  • mutual — when directed, emit both directions of every edge. Ignored when directed == false.
  • periodic — optional per-dimension wrap-around flags. Must have the same length as dim when supplied; otherwise treat all dimensions as non-periodic.

§Errors

  • IgraphError::InvalidArgument
    • nei >= 2 (not yet supported),
    • length of periodic does not match dim,
    • vertex count Π dim[k] overflows u32.

§Examples

use rust_igraph::square_lattice;

// 3 × 3 grid: 9 vertices, 12 edges, 2D bounded.
let g = square_lattice(&[3, 3], 1, false, false, None).unwrap();
assert_eq!(g.vcount(), 9);
assert_eq!(g.ecount(), 12);

// 3 × 3 torus: same vertex count, 18 edges (each cell contributes
// one forward edge per dim).
let g = square_lattice(&[3, 3], 1, false, false, Some(&[true, true])).unwrap();
assert_eq!(g.vcount(), 9);
assert_eq!(g.ecount(), 18);