para_graph/graph/
random_graph_np.rs

1use petgraph::prelude::*;
2use rand::distributions::{Bernoulli, Standard};
3use rand::prelude::*;
4
5pub fn random_graph_np<N, E, R>(rng: &mut R, n: usize, p: f64) -> UnGraph<N, E>
6where
7    Standard: Distribution<N> + Distribution<E>,
8    R: Rng + ?Sized,
9{
10    let mut graph = UnGraph::with_capacity(n, 0);
11    let mut nodes = Vec::with_capacity(n);
12    for _ in 0..n {
13        let node_weight: N = rng.gen();
14        nodes.push(graph.add_node(node_weight));
15    }
16
17    let distribution = Bernoulli::new(p).unwrap();
18    for i in 0..n {
19        for j in i + 1..n {
20            if distribution.sample(rng) {
21                let edge_weight_1: E = rng.gen();
22                let edge_weight_2: E = rng.gen();
23                graph.add_edge(nodes[i], nodes[j], edge_weight_1);
24                graph.add_edge(nodes[j], nodes[i], edge_weight_2);
25            }
26        }
27    }
28
29    graph
30}