yog/generator/random
Stochastic graph generators for random graph models.
Random generators use randomness to model real-world networks with properties like scale-free distributions, small-world effects, and community structure.
Available Generators
| Generator | Model | Complexity | Key Property |
|---|---|---|---|
erdos_renyi_gnp | G(n, p) | O(n²) | Each edge with probability p |
erdos_renyi_gnm | G(n, m) | O(m) | Exactly m random edges |
barabasi_albert | Preferential | O(nm) | Scale-free (power-law degrees) |
watts_strogatz | Small-world | O(nk) | High clustering + short paths |
random_tree | Uniform tree | O(n²) | Uniformly random spanning tree |
random_regular | d-regular | O(nd) | All nodes have degree d |
sbm | SBM | O(n²) | Community structure |
dcsbm | DCSBM | O(n²) | Community + Degree control |
hsbm | Hierarchical | O(n²) | Nested community structure |
configuration_model | Fixed Degree | O(M) | Exact degree sequence |
rmat | R-MAT | O(E log V) | Fast Kronecker variant |
kronecker | Kronecker | O(E log V) | Recursive matrix expansion |
geometric | RGG | O(n²) | Distance-based edges |
Quick Start
import yog/generator/random
import yog/model
pub fn main() {
// Random network models
let sparse = random.erdos_renyi_gnp(100, 0.05, seed: None) // Sparse random (p=5%)
let exact = random.erdos_renyi_gnm(50, 100, seed: None) // Exactly 100 edges
let scale_free = random.barabasi_albert(1000, 3, seed: None) // Scale-free network
let small_world = random.watts_strogatz(100, 6, 0.1, seed: None) // Small-world (10% rewire)
let tree = random.random_tree(50, seed: None) // Random spanning tree
let regular = random.random_regular(20, 3, seed: None) // 3-regular graph
}
Network Models Explained
Erdős-Rényi G(n, p)
- Each possible edge included independently with probability p
- Expected edges: p × n(n-1)/2 (undirected) or p × n(n-1) (directed)
- Phase transition at p = 1/n (giant component emerges)
- Use for: Random network modeling, percolation studies
Erdős-Rényi G(n, m)
- Exactly m edges added uniformly at random
- Uniform distribution over all graphs with n nodes and m edges
- Use for: Fixed edge count requirements, specific density testing
Barabási-Albert (Preferential Attachment)
- Starts with m₀ nodes, adds nodes connecting to m existing nodes
- New nodes prefer high-degree nodes (“rich get richer”)
- Power-law degree distribution: P(k) ~ k^(-3)
- Use for: Social networks, citation networks, web graphs
Watts-Strogatz (Small-World)
- Starts with ring lattice (high clustering)
- Rewires edges with probability p (creates shortcuts)
- Balances local clustering with global connectivity
- Use for: Social networks, neural networks, epidemic modeling
Random Tree
- Builds tree by connecting new nodes to random existing nodes
- Produces uniform distribution over all labeled trees
- Use for: Spanning trees, hierarchical structures
Stochastic Block Model (SBM)
- Nodes assigned to communities
- Edge probability depends on community membership
- Use for: Community detection testing, modular networks
References
- Erdős-Rényi Model
- Barabási-Albert Model
- Watts-Strogatz Model
- Scale-Free Networks
- Small-World Network
- Stochastic Block Model
- NetworkX Random Graphs
Values
pub fn barabasi_albert(
n: Int,
m: Int,
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a scale-free network using the Barabási-Albert model.
Creates a random graph with a power-law degree distribution (scale-free). New nodes preferentially attach to existing high-degree nodes (“rich get richer”).
Time Complexity: O(nm)
Example
// Scale-free network with 1000 nodes, each connecting to 3 existing nodes
let graph = random.barabasi_albert(1000, 3, seed: Some(42))
Properties
- Power-law degree distribution: P(k) ~ k^(-3)
- Hub nodes with very high degree
- Small-world properties
Use Cases
- Social network modeling
- Citation network analysis
- Web graph simulation
pub fn barabasi_albert_with_type(
n: Int,
m: Int,
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a Barabási-Albert graph with specified graph type.
pub fn configuration_model(
degrees: List(Int),
seed seed: option.Option(Int),
) -> Result(model.Graph(Nil, Int), Nil)
Generates a random graph with specified degree sequence using the configuration model.
Time Complexity: O(M) where M is total edges. Returns Ok(graph) or Error(Nil) if pairing fails after retries.
pub fn dcsbm(
n: Int,
k: Int,
p_in: Float,
p_out: Float,
thetas: option.Option(List(Float)),
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a Degree-Corrected Stochastic Block Model (DCSBM).
Extends SBM with node-specific degree parameters, allowing more realistic degree distributions while preserving community structure.
Parameters:
n- Number of nodesk- Number of communitiesp_in- Probability within communityp_out- Probability between communitiesthetas- Custom degree parameters for each node (optional)
Time Complexity: O(n²)
pub fn dcsbm_with_type(
n: Int,
k: Int,
p_in: Float,
p_out: Float,
thetas: option.Option(List(Float)),
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a DCSBM graph with specified graph type.
pub fn erdos_renyi_gnm(
n: Int,
m: Int,
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a random graph using the Erdős-Rényi G(n, m) model.
Unlike G(n, p) which includes each edge independently with probability p, G(n, m) guarantees exactly m edges in the resulting graph.
Time Complexity: O(m) expected
Example
// Random graph with 50 nodes and exactly 100 edges
let graph = random.erdos_renyi_gnm(50, 100, seed: Some(42))
Properties
- Exactly m edges (unlike G(n,p) which has expected m edges)
- Uniform distribution over all graphs with n nodes and m edges
Use Cases
- Fixed edge count requirements
- Random graph benchmarking
- Testing with specific densities
pub fn erdos_renyi_gnm_with_type(
n: Int,
m: Int,
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates an Erdős-Rényi G(n, m) graph with specified graph type.
pub fn erdos_renyi_gnp(
n: Int,
p: Float,
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a random graph using the Erdős-Rényi G(n, p) model.
Each possible edge is included independently with probability p. For undirected graphs, each unordered pair is considered once.
Time Complexity: O(n²)
Example
// Sparse random graph with seed for reproducibility
let sparse = random.erdos_renyi_gnp(100, 0.05, seed: Some(42))
// Dense random graph
let dense = random.erdos_renyi_gnp(50, 0.8, seed: None)
Properties
- Expected number of edges: p × n(n-1)/2 (undirected) or p × n(n-1) (directed)
- Phase transition at p = 1/n (giant component emerges)
Use Cases
- Random network modeling
- Percolation studies
- Average-case algorithm analysis
pub fn erdos_renyi_gnp_with_type(
n: Int,
p: Float,
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates an Erdős-Rényi G(n, p) graph with specified graph type.
pub fn geometric(
n: Int,
radius: Float,
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a Random Geometric Graph (RGG).
Nodes are placed uniformly at random in a unit square [0, 1] x [0, 1].
Edges are created between any two nodes if their Euclidean distance
is less than or equal to radius.
Time Complexity: O(n²)
Example
let rgg = random.geometric(100, 0.15, seed: Some(42))
// 100 nodes, connected if distance <= 0.15
pub fn geometric_with_type(
n: Int,
radius: Float,
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a Geometric graph with specified graph type.
pub fn hsbm(
n: Int,
levels: Int,
branching: Int,
p_in: Float,
p_out: Float,
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a hierarchical SBM with nested communities.
Time Complexity: O(n²)
pub fn hsbm_with_type(
n: Int,
levels: Int,
branching: Int,
p_in: Float,
p_out: Float,
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates an HSBM graph with specified graph type.
pub fn kronecker(
k: Int,
initiator: #(Float, Float, Float, Float),
m: Int,
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a Kronecker graph using recursive expansion (via R-MAT).
Parameters:
k- Number of iterations (2^k nodes)initiator- 2x2 probability matrix #(a, b, c, d)m- Desired number of edges
Time Complexity: O(m log n)
pub fn random_regular(
n: Int,
d: Int,
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a random d-regular graph on n nodes.
A d-regular graph has every node with exactly degree d. This implementation uses a configuration model approach with retries to ensure simplicity (no self-loops or parallel edges).
Preconditions:
- n × d must be even (required for any d-regular graph)
- d < n (cannot have degree >= number of nodes in simple graph)
- d >= 0
Properties:
- Uniform distribution over all d-regular graphs (approximate)
- Exactly n nodes, (n × d) / 2 edges
- All nodes have degree exactly d
Time Complexity: O(n × d)
Example
// Generate a 3-regular graph with 10 nodes
let regular = random.random_regular(10, 3, seed: Some(42))
Use Cases
- Testing algorithms that need uniform degree distribution
- Expander graph approximations
- Network models where degree is constrained
pub fn random_regular_with_type(
n: Int,
d: Int,
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a random d-regular graph with specified graph type.
pub fn random_tree(
n: Int,
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a uniformly random tree on n nodes.
Creates a tree by starting with node 0 and repeatedly connecting new nodes to random nodes already in the tree. This produces a uniform distribution over all labeled trees.
Time Complexity: O(n²) expected
Example
let tree = random.random_tree(50, seed: Some(42))
// Random tree with 50 nodes, 49 edges
Properties
- Exactly n-1 edges (tree property)
- Connected
- Acyclic
- Uniform distribution over all labeled trees
Use Cases
- Random spanning tree generation
- Tree algorithm testing
- Network topology generation
- Phylogenetic tree simulation
pub fn random_tree_with_type(
n: Int,
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a random tree with specified graph type.
pub fn randomize_degree_sequence(
graph: model.Graph(Nil, Int),
seed seed: option.Option(Int),
) -> Result(model.Graph(Nil, Int), Nil)
Generates a random graph matching the degree sequence of a given graph.
Time Complexity: O(N + M)
pub fn rmat(
n: Int,
m: Int,
probs: option.Option(#(Float, Float, Float, Float)),
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a random graph using the R-MAT (Recursive MATRIX) model.
R-MAT is a fast generator for graphs with scale-free and small-world properties. It recursively partitions the adjacency matrix into four quadrants with specified probabilities.
Parameters:
n- Number of nodes (must be a power of 2)m- Number of edgesprobs- Quad probabilities #(a, b, c, d) that sum to 1.0
Default Probs: #(0.57, 0.19, 0.19, 0.05) - standard scale-free parameters.
Time Complexity: O(m log n)
Example
let rmat = random.rmat(1024, 5000, None, seed: Some(42))
// 1024 nodes (2^10), 5000 edges
pub fn rmat_with_type(
n: Int,
m: Int,
probs: option.Option(#(Float, Float, Float, Float)),
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates an R-MAT graph with specified graph type.
pub fn sbm(
n: Int,
k: Int,
p_in: Float,
p_out: Float,
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a graph using the Stochastic Block Model (SBM).
Nodes are assigned to communities, and edges are added with probabilities depending on community membership (higher probability within communities).
Parameters
n- Number of nodesk- Number of communitiesp_in- Probability of edge within communityp_out- Probability of edge between communities
Example
let sbm = random.sbm(100, 4, 0.3, 0.05, seed: Some(42))
// 100 nodes, 4 communities, high intra-community connectivity
pub fn sbm_with_type(
n: Int,
k: Int,
p_in: Float,
p_out: Float,
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates an SBM graph with specified graph type.
pub fn watts_strogatz(
n: Int,
k: Int,
p: Float,
seed seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a small-world network using the Watts-Strogatz model.
Generates a graph with both high clustering (like regular lattices) and short path lengths (like random graphs). Starts with a ring lattice and rewires edges with probability p.
Time Complexity: O(nk)
Example
// Small-world network: 100 nodes, 6 neighbors each, 10% rewiring
let graph = random.watts_strogatz(100, 6, 0.1, seed: Some(42))
Properties
- High clustering coefficient
- Short average path length
- p=0: regular lattice, p=1: random graph
Use Cases
- Social network modeling (six degrees of separation)
- Neural network topology
- Epidemic spread modeling
pub fn watts_strogatz_with_type(
n: Int,
k: Int,
p: Float,
graph_type: model.GraphType,
seed: option.Option(Int),
) -> model.Graph(Nil, Int)
Generates a Watts-Strogatz graph with specified graph type.