para_graph/graph/
task_dag.rs

1use petgraph::prelude::*;
2use rand::distributions::uniform::SampleRange;
3use rand::distributions::{Distribution, Standard};
4use rand::Rng;
5use std::collections::HashMap;
6
7use crate::graph::random_dag_np::random_dag_np;
8
9pub fn random_tasks_graph<N, E, R, A>(
10    rng: &mut R,
11    num_components: usize,
12    n_range: A,
13    p: f64,
14) -> DiGraph<N, E>
15where
16    N: Clone,
17    E: Clone,
18    Standard: Distribution<N> + Distribution<E>,
19    R: Rng + ?Sized,
20    A: SampleRange<usize> + Clone,
21{
22    let mut tasks_graph = DiGraph::new();
23    for _ in 0..num_components {
24        let n = rng.gen_range(n_range.clone());
25        let component: DiGraph<N, E> = random_dag_np(rng, n, p);
26
27        let mut matching: HashMap<NodeIndex, NodeIndex> = HashMap::with_capacity(n);
28
29        let old_indices: Vec<_> = component.node_indices().collect();
30        for node_idx in old_indices.iter() {
31            let node = component[*node_idx].clone();
32            let new_node_idx = tasks_graph.add_node(node);
33            matching.insert(*node_idx, new_node_idx);
34        }
35
36        for edge in component.raw_edges() {
37            let source = matching[&edge.source()];
38            let target = matching[&edge.target()];
39            tasks_graph.add_edge(source, target, edge.weight.clone());
40        }
41    }
42    tasks_graph
43}