Skip to main content

Module games

Module games 

Source
Expand description

Random graph generators, a.k.a. games (igraph_games.h).

igraph calls its random graph models games. Every generator of this module is an associated function of Graph returning a brand new Result<Graph> (or a small result struct when the model also produces vertex types or coordinates), while the two rewiring functions are methods mutating an existing graph in place.

All the games draw their random numbers from the calling thread’s default random number generator: seed it with rng::seed to obtain reproducible graphs. Two runs with the same seed and the same arguments produce exactly the same graph. Every thread has its own default generator (see rng), so seeding is per-thread and concurrent threads never disturb each other’s streams; an independent generator of a chosen RngType can be installed for the duration of a closure with Rng::scoped:

use igraph::prelude::*;

let sample = || {
    rng::seed(42).unwrap();
    Graph::erdos_renyi_game_gnm(50, 100, false, EdgeTypeSw::Simple, false).unwrap()
};
// Same seed, same graph, even in another thread.
let other_thread = std::thread::spawn(sample).join().unwrap();
assert!(sample().is_same_graph(&other_thread).unwrap());

// A scoped Mersenne Twister leaves the default generator untouched.
let mut mt = Rng::new(RngType::Mt19937, 42).unwrap();
let g = mt.scoped(|| Graph::tree_game(20, false, RandomTreeMethod::Prufer)).unwrap();
assert!(g.is_tree(NeighborMode::All).unwrap());

§Example

use igraph::{games::BarabasiOptions, prelude::*};

rng::seed(42).unwrap();
// An Erdős–Rényi G(n, m) graph: 1000 vertices, 1000 edges, simple.
let g = Graph::erdos_renyi_game_gnm(1000, 1000, false, EdgeTypeSw::Simple, false).unwrap();
let degrees = g.degree(.., NeighborMode::All, Loops::Twice).unwrap();
let mean = degrees.iter().sum::<i64>() as f64 / g.vcount() as f64;
assert_eq!(mean, 2.0); // handshake lemma: 2m / n

// A scale-free graph grown by preferential attachment.
let ba = Graph::barabasi_game(100, &BarabasiOptions::default().with_m(2)).unwrap();
assert_eq!(ba.vcount(), 100);
assert_eq!(ba.ecount(), 1 + 98 * 2); // vertex 1 has only one vertex to attach to

§Provided functionality

ModelRustC function
Erdős–Rényi G(n, m)Graph::erdos_renyi_game_gnmigraph_erdos_renyi_game_gnm
Erdős–Rényi G(n, p)Graph::erdos_renyi_game_gnpigraph_erdos_renyi_game_gnp
Independent edge assignmentGraph::iea_gameigraph_iea_game
Barabási–Albert / PriceGraph::barabasi_gameigraph_barabasi_game
Preferential attachment with agingGraph::barabasi_aging_gameigraph_barabasi_aging_game
Recent degreeGraph::recent_degree_gameigraph_recent_degree_game
Recent degree with agingGraph::recent_degree_aging_gameigraph_recent_degree_aging_game
Growing random graphGraph::growing_random_gameigraph_growing_random_game
Prescribed degree sequenceGraph::degree_sequence_gameigraph_degree_sequence_game
Random regular graphGraph::k_regular_gameigraph_k_regular_game
Static fitnessGraph::static_fitness_gameigraph_static_fitness_game
Static power lawGraph::static_power_law_gameigraph_static_power_law_game
Chung–Lu (expected degrees)Graph::chung_lu_gameigraph_chung_lu_game
Watts–Strogatz small worldGraph::watts_strogatz_gameigraph_watts_strogatz_game
Rewire edgesGraph::rewire_edgesigraph_rewire_edges
Rewire directed edge endpointsGraph::rewire_directed_edgesigraph_rewire_directed_edges
Forest fireGraph::forest_fire_gameigraph_forest_fire_game
Stochastic block modelGraph::sbm_gameigraph_sbm_game
Hierarchical SBMGraph::hsbm_game, Graph::hsbm_list_gameigraph_hsbm_game, igraph_hsbm_list_game
Preference (block model with random types)Graph::preference_gameigraph_preference_game
Asymmetric preferenceGraph::asymmetric_preference_gameigraph_asymmetric_preference_game
Callaway traitsGraph::callaway_traits_gameigraph_callaway_traits_game
EstablishmentGraph::establishment_gameigraph_establishment_game
Geometric random graphGraph::grg_gameigraph_grg_game
Last citationGraph::lastcit_gameigraph_lastcit_game
Cited typeGraph::cited_type_gameigraph_cited_type_game
Citing–cited typeGraph::citing_cited_type_gameigraph_citing_cited_type_game
Interconnected islandsGraph::simple_interconnected_islands_gameigraph_simple_interconnected_islands_game
Correlated graphsGraph::correlated_game, Graph::correlated_pair_gameigraph_correlated_game, igraph_correlated_pair_game
Uniform random treeGraph::tree_gameigraph_tree_game
Random dot product graphGraph::dot_product_gameigraph_dot_product_game

§See also

§Allowed edge types

Several generators take an allowed_edge_types argument (the C type igraph_edge_type_sw_t) controlling whether self-loops and multi-edges may be created. It accepts both the shared EdgeTypeSw enum (simple graphs, loops or multi-edges) and the AllowedEdgeTypes flag set (defined in constants and re-exported here; the graphicality tests of mixing use the very same type), which can also express loops and multi-edges together:

use igraph::{games::AllowedEdgeTypes, prelude::*};

rng::seed(7).unwrap();
let any = AllowedEdgeTypes::LOOPS | AllowedEdgeTypes::MULTI;
assert_eq!(EdgeTypeSw::Loops | EdgeTypeSw::Multi, any);
let g = Graph::erdos_renyi_game_gnm(3, 50, false, any, false).unwrap();
assert_eq!(g.ecount(), 50); // impossible without multi-edges on 3 vertices
assert!(g.has_multiple().unwrap());

Re-exports§

pub use crate::constants::AllowedEdgeTypes;

Structs§

AsymmetricTypedGraph
Result of Graph::asymmetric_preference_game: every vertex has an out-type and an in-type.
BarabasiAgingOptions
Options of Graph::barabasi_aging_game.
BarabasiOptions
Options of Graph::barabasi_game, with the defaults of the classic undirected Barabási–Albert model (m = 1, power = 1, A = 1, BarabasiAlgorithm::Psumtree).
GeometricGraph
Result of Graph::grg_game: a geometric random graph together with the positions of its vertices in the unit square.
RecentDegreeAgingOptions
Options of Graph::recent_degree_aging_game.
RecentDegreeOptions
Options of Graph::recent_degree_game.
TypedGraph
A random graph whose vertices carry a type (category), as produced by the type-based games (Graph::preference_game, Graph::callaway_traits_game, Graph::establishment_game).