Skip to main content

chung_lu_game

Function chung_lu_game 

Source
pub fn chung_lu_game(
    out_weights: &[f64],
    in_weights: Option<&[f64]>,
    loops: bool,
    variant: ChungLuVariant,
    seed: u64,
) -> IgraphResult<Graph>
Expand description

Sample a random graph from the Chung–Lu expected-degree model.

  • out_weights — non-negative, finite per-vertex weights. Length is the resulting graph’s vertex count n. In the sparse-graph limit each out_weights[i] is approximately the expected (out-)degree of vertex i.
  • in_weights — when Some, generates a directed graph; must have the same length as out_weights and the same total mass (within f64 exact equality, per upstream igraph). When None, generates an undirected graph with in_weights = out_weights.
  • loops — when true, self-loop edges are allowed (sampled independently with p_ii).
  • variant — which connection-probability formula to use; see ChungLuVariant.
  • seed — initialises an internal SplitMix64 PRNG so output is reproducible given the inputs.

§Errors

Returns IgraphError::InvalidArgument if:

  • any weight is negative, NaN, or non-finite (±∞);
  • in_weights has a different length than out_weights;
  • in_weights and out_weights have a different sum (directed case);
  • out_weights.len() exceeds 2^53 (the largest integer exactly representable as f64, matching upstream igraph’s IGRAPH_MAX_EXACT_REAL guard).

§Complexity

O(|V| log |V| + |E|) — the log |V| factor comes from the descending in-weight sort; the sampler itself is linear in the realised edge count.

§Examples

use rust_igraph::{chung_lu_game, ChungLuVariant};

// Power-law-style weights: a handful of "hubs" plus many low-degree
// vertices. The expected degree of vertex i ≈ out_weights[i].
let weights: Vec<f64> = (0..50).map(|i| 1.0 + (i as f64) * 0.2).collect();
let g = chung_lu_game(&weights, None, false, ChungLuVariant::Maxent, 42).unwrap();
assert_eq!(g.vcount(), 50);
assert!(!g.is_directed());