Skip to main content

random_regular

Function random_regular 

Source
pub fn random_regular<R: Rng>(
    n: usize,
    d: usize,
    rng: &mut R,
) -> Option<Graph<usize, f64>>
Expand description

Generate a uniformly random d-regular graph on n nodes.

A d-regular graph is one where every node has exactly degree d. This is feasible only when n·d is even.

The algorithm uses the configuration model with self-loop / parallel-edge rejection: stubs are generated (n·d stubs total), randomly paired, and the pairing is rejected and retried if it produces a self-loop or multi-edge. The expected number of retries is O(1) for fixed d.

§Arguments

  • n – number of nodes
  • d – required degree of every node
  • rng – random-number generator

§Returns

Some(Graph) when a valid d-regular graph was found within the attempt budget, None when the degree sequence is infeasible or sampling consistently fails (e.g., n < d+1).