Skip to main content

preference_game

Function preference_game 

Source
pub fn preference_game(
    nodes: u32,
    types: u32,
    type_dist: Option<&[f64]>,
    fixed_sizes: bool,
    pref_matrix: &[Vec<f64>],
    directed: bool,
    loops: bool,
    seed: u64,
) -> IgraphResult<(Graph, Vec<u32>)>
Expand description

Generate a random graph from a type-aware preference (block) model.

Each vertex is assigned to one of types types (uniformly, by type_dist, or pinned by fixed_sizes); each ordered/unordered vertex pair (u, v) is then connected with probability pref_matrix[type(u)][type(v)].

  • nodes — number of vertices in the graph.
  • types — number of vertex types (≥ 1).
  • type_dist — categorical weights over types; None ⇒ uniform.
  • fixed_sizes — when true, types are assigned by deterministic block-by-position rather than sampled. With type_dist = Some(td) the block sizes are td[i] (must be integer and sum to nodes). With type_dist = None the function tries to make groups of equal size.
  • pref_matrixtypes × types connection probabilities in [0, 1]. Must be symmetric when directed = false.
  • directed — generate a directed graph.
  • loops — allow self-loop edges.
  • seed — initialises an internal SplitMix64 PRNG.

§Returns

(graph, node_types) where node_types[i] is the type of vertex i in 0..types.

§Errors

Returns IgraphError::InvalidArgument when:

  • types < 1,
  • type_dist length disagrees with types, contains NaN or a negative value,
  • pref_matrix is non-square or has the wrong dimensions,
  • any pref entry is NaN or outside [0, 1],
  • pref_matrix is not symmetric when !directed,
  • fixed_sizes and type_dist are both supplied but the entries are not integer or do not sum to nodes,
  • nodes exceeds the 2^53 f64-exact ceiling.

§Examples

use rust_igraph::preference_game;

// Three types, equal mixture; dense within types, sparse between.
let pref = vec![
    vec![0.20, 0.02, 0.02],
    vec![0.02, 0.20, 0.02],
    vec![0.02, 0.02, 0.20],
];
let (g, types) = preference_game(60, 3, None, false, &pref, false, false, 42).unwrap();
assert_eq!(g.vcount(), 60);
assert_eq!(types.len(), 60);
assert!(types.iter().all(|&t| t < 3));