Skip to main content

sample_graph_from

Function sample_graph_from 

Source
pub fn sample_graph_from<R, S, ND, ED, F, G, N, E>(
    rng: R,
    elements: Vec<S>,
    num_nodes: usize,
    num_edges: usize,
    node_density: ND,
    edge_density: ED,
    node_weight: F,
    edge_weight: G,
) -> Graph<N, E, Undirected, u32>
where R: Rng, S: Copy + PartialEq, ND: Density<S>, ED: Density<(S, S)>, F: FnMut(&S) -> N, G: FnMut(&(S, S)) -> E,
Expand description

Samples a graph from the given Densitys.

Arguments:

  • rng: random number generator.
  • elements: of the given space from which should be sampled.
  • num_nodes: number of villages to be chosen.
  • num_edges: number of roads connecting the villages to be chosen.
  • node_density: defines the probability from which the elements are chosen.
  • edge_density: defines the probability of the edges.
  • node_weight: sets the node weights of the resulting graph.
  • edge_weight: sets the edge weights of the resulting graph.

Returns:

  • Graph: which is an undirected petgraph whose node weights are defined by node_weight and edge_weight are defined by edge_weight. NOTE: the graph could have fewer than num_nodes nodes or num_edges edges if at one point in the sampling process the probabilities for each node or edges is zero.

Example:

use gengraph::{sample_graph_from, Density};
let rng = rand::rng();
let dist = |x: &u8, y: &u8| if x > y {x - y} else {y - x};
let graph = sample_graph_from(
    rand::rng(),
    (0..10).collect::<Vec<u8>>(),
    5,
    10,
    |_: &u8, _: &[u8]| 1.0, // Vertex probability
    |(x, y): &(u8, u8), _: &[(u8, u8)]| 1.0 / dist(x, y) as f64, // Edge probability
    |_| (), // Vertex weight
    |(x, y): &(u8, u8)| dist(x, y), // Edge weight
);