pub struct Sampler { /* private fields */ }
Expand description

A sampler for regular bipartite graph.

This is the main struct of this crate. The builder method is used to build a sampler and the sample_with method is used to sample a random Graph.

A sampler is specified by 4 parameters: - variable’s degree: the same for all variables, - constraint’s degree: the same for all contraints, - number of variables, - number of contraints.

To make sure that the graphs are regular, the number of variables times their degree need to equal the number of contraints times their degree. If this is not satified, the builder will panic during construction.

Example

This can be used to generate a random graph with 5 variables of degree 3 and 3 constraints of degree 5.

use rand::thread_rng;

let sampler = Sampler::builder()
    .number_of_variables(5)
    .variable_degree(3)
    .number_of_constraints(3)
    .constraint_degree(5)
    .build()
    .unwrap();

let graph = sampler.sample_with(&mut thread_rng());

assert_eq!(graph.number_of_variables(), 5);
for variable in graph.variables() {
    assert_eq!(variable.degree(), 3);
}

assert_eq!(graph.number_of_constraints(), 3);
for constraint in graph.constraints() {
    assert_eq!(constraint.degree(), 5);
    }

However, this will return an error since the parameters do not define a regular graph.

let sampler = Sampler::builder()
    .number_of_variables(5)
    .variable_degree(3)
    .number_of_constraints(3)
    .constraint_degree(3)
    .build();

assert!(sampler.is_err());

Reproductibility

If you use the same random number generator, the graphs should be the same.

use rand::SeedableRng;
use rand::rngs::SmallRng; // require the small_rng feature

let sampler = Sampler::builder()
    .number_of_variables(5)
    .variable_degree(3)
    .number_of_constraints(3)
    .constraint_degree(5)
    .build()
    .unwrap();

// Create two rngs with the same seed.
let mut rng = SmallRng::seed_from_u64(123);
let mut other_rng = SmallRng::seed_from_u64(123);

let graph = sampler.sample_with(&mut rng);
let other_graph = sampler.sample_with(&mut other_rng);

assert_eq!(graph, other_graph);

Implementations

Instanciates a builder for samplers.

Samples a random graph with the sampler parameters.

Returns the number of variables in the graphs that will be generated by the sampler.

Returns the number of constraints in the graphs that will be generated by the sampler.

Returns the number of edges in the graphs that will be generated by the sampler.

Returns the variable’s degree in the graphs that will be generated by the sampler.

Returns the contraint’s degree in the graphs that will be generated by the sampler.

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.