pub fn sample_connected_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>Expand description
Samples a graph from the given Densitys and ensures that the graph is connected.
In addition to sample_graph_from, this function ensures that the graph is connected by
constructing a spanning tree on the sampled elements befor sampling the remaining
edges according to edge_density.
For more information about the construction of the spanning tree, see
spanning_tree_on_edge_density and filtered_spanning_tree_on_edge_density.
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 undirectedpetgraphwhose node weights are defined bynode_weightand edge_weight are defined byedge_weight. The graph is connected. NOTE: the graph could have fewer thannum_nodesnodes ornum_edgesedges if at one point in the sampling process the probabilities for each node or edges is zero.
Example:
use gengraph::{sample_connected_graph_from, Density};
let rng = rand::rng();
let dist = |x: &u8, y: &u8| if x > y {x - y} else {y - x};
let graph = sample_connected_graph_from(
rand::rng(),
(0..10).collect::<Vec<u8>>(),
5,
10,
|_: &u8, _: &[u8]| 1.0,
|(x, y): &(u8, u8), _: &[(u8, u8)]| 1.0 / dist(x, y) as f64,
|_| (),
|(x, y): &(u8, u8)| dist(x, y),
);