graaf/gen/growing_network.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
//! Generate growing network.
//!
//! A growing network is a digraph that starts with a single vertex and adds a
//! new vertex with an arc to an existing vertex at each step.
//!
//! # Examples
//!
//! Generate a growing network of order `6`.
//!
//! 
//!
//! ```
//! use graaf::{
//! AdjacencyList,
//! Arcs,
//! GrowingNetwork,
//! };
//!
//! assert!(AdjacencyList::growing_network(6, 0).arcs().eq([
//! (1, 0),
//! (2, 0),
//! (3, 1),
//! (4, 0),
//! (5, 2)
//! ]));
//! ```
/// Generate growing network.
///
/// A growing network is a digraph that starts with a single vertex and adds a
/// new vertex with an arc to an existing vertex at each step.
///
/// # Implementing [`GrowingNetwork`] for a custom type
///
/// Provide an implementation of
/// [`growing_network`](GrowingNetwork::growing_network) that generates a
/// growing network of a given `order` from a given `seed`.
///
/// ```
/// use {
/// graaf::{
/// gen::prng::Xoshiro256StarStar,
/// GrowingNetwork,
/// },
/// std::collections::BTreeSet,
/// };
///
/// struct AdjacencyList {
/// arcs: Vec<BTreeSet<usize>>,
/// }
///
/// impl GrowingNetwork for AdjacencyList {
/// fn growing_network(order: usize, seed: u64) -> Self {
/// let mut arcs = Vec::with_capacity(order);
/// let mut rng = Xoshiro256StarStar::new(seed);
///
/// arcs.push(BTreeSet::new());
///
/// for (u, v) in (1..order).zip(rng) {
/// arcs.push(BTreeSet::from([usize::try_from(v)
/// .expect("conversion failed")
/// % u]));
/// }
///
/// Self { arcs }
/// }
/// }
///
/// let digraph = AdjacencyList::growing_network(6, 0);
///
/// assert!(digraph.arcs.iter().eq(&[
/// BTreeSet::new(),
/// BTreeSet::from([0]),
/// BTreeSet::from([0]),
/// BTreeSet::from([1]),
/// BTreeSet::from([0]),
/// BTreeSet::from([2]),
/// ]));
/// ```
///
/// Implementations can be built with the [`AddArc`](crate::AddArc) and
/// [`Empty`](crate::Empty) traits.
///
/// ```
/// use {
/// graaf::{
/// gen::prng::Xoshiro256StarStar,
/// AddArc,
/// Empty,
/// GrowingNetwork,
/// },
/// std::collections::BTreeSet,
/// };
///
/// struct AdjacencyList {
/// arcs: Vec<BTreeSet<usize>>,
/// }
///
/// impl AddArc for AdjacencyList {
/// fn add_arc(&mut self, u: usize, v: usize) {
/// self.arcs[u].insert(v);
/// }
/// }
///
/// impl Empty for AdjacencyList {
/// fn empty(order: usize) -> Self {
/// Self {
/// arcs: vec![BTreeSet::new(); order],
/// }
/// }
/// }
///
/// impl GrowingNetwork for AdjacencyList {
/// fn growing_network(order: usize, seed: u64) -> Self {
/// let mut digraph = Self::empty(order);
/// let rng = Xoshiro256StarStar::new(seed);
///
/// for (u, v) in (1..order).zip(rng) {
/// digraph.add_arc(
/// u,
/// usize::try_from(v).expect("conversion failed") % u,
/// );
/// }
///
/// digraph
/// }
/// }
///
/// let digraph = AdjacencyList::growing_network(6, 0);
///
/// assert!(digraph.arcs.iter().eq(&[
/// BTreeSet::new(),
/// BTreeSet::from([0]),
/// BTreeSet::from([0]),
/// BTreeSet::from([1]),
/// BTreeSet::from([0]),
/// BTreeSet::from([2]),
/// ]));
/// ```
pub trait GrowingNetwork {
/// Generate a growing network.
///
/// # Arguments
///
/// * `order` - The number of vertices in the digraph.
/// * `seed` - The seed for the random number generator.
///
/// # Panics
///
/// * Panics if `order` is zero.
///
/// # Examples
///
/// Generate a growing network of order `6`.
///
/// 
///
/// ```
/// use graaf::{
/// AdjacencyList,
/// Arcs,
/// GrowingNetwork,
/// };
///
/// assert!(AdjacencyList::growing_network(6, 0).arcs().eq([
/// (1, 0),
/// (2, 0),
/// (3, 1),
/// (4, 0),
/// (5, 2)
/// ]));
/// ```
#[must_use]
fn growing_network(order: usize, seed: u64) -> Self;
}