|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +// not use this file except in compliance with the License. You may obtain |
| 3 | +// a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +// License for the specific language governing permissions and limitations |
| 11 | +// under the License. |
| 12 | + |
| 13 | +use petgraph::{data::Create, visit::Data}; |
| 14 | + |
| 15 | +use super::InvalidInputError; |
| 16 | + |
| 17 | +/// Generate a Dorogovtsev-Goltsev-Mendes graph |
| 18 | +/// |
| 19 | +/// Generate a graph following the recursive procedure in [1]. |
| 20 | +/// Starting from the two-node, one-edge graph, iterating `n` times generates |
| 21 | +/// a graph with `(3**n + 3) // 2` nodes and `3**n` edges. |
| 22 | +/// |
| 23 | +/// |
| 24 | +/// Arguments: |
| 25 | +/// |
| 26 | +/// * `n` - The number of iterations to perform. n=0 returns the two-node, one-edge graph. |
| 27 | +/// * `default_node_weight` - A callable that will return the weight to use for newly created nodes. |
| 28 | +/// * `default_edge_weight` - A callable that will return the weight object to use for newly created edges. |
| 29 | +/// |
| 30 | +/// # Example |
| 31 | +/// ```rust |
| 32 | +/// use rustworkx_core::petgraph; |
| 33 | +/// use rustworkx_core::generators::dorogovtsev_goltsev_mendes_graph; |
| 34 | +/// use rustworkx_core::petgraph::visit::EdgeRef; |
| 35 | +/// |
| 36 | +/// let g: petgraph::graph::UnGraph<(), ()> = dorogovtsev_goltsev_mendes_graph(2, || (), || ()).unwrap(); |
| 37 | +/// assert_eq!(g.node_count(), 6); |
| 38 | +/// assert_eq!( |
| 39 | +/// vec![(0, 1), (0, 2), (1, 2), (0, 3), (1, 3), (0, 4), (2, 4), (1, 5), (2, 5)], |
| 40 | +/// g.edge_references() |
| 41 | +/// .map(|edge| (edge.source().index(), edge.target().index())) |
| 42 | +/// .collect::<Vec<(usize, usize)>>(), |
| 43 | +/// ); |
| 44 | +/// ``` |
| 45 | +/// |
| 46 | +/// .. [1] S. N. Dorogovtsev, A. V. Goltsev and J. F. F. Mendes |
| 47 | +/// “Pseudofractal scale-free web” |
| 48 | +/// Physical Review E 65, 066122, 2002 |
| 49 | +/// https://arxiv.org/abs/cond-mat/0112143 |
| 50 | +/// |
| 51 | +pub fn dorogovtsev_goltsev_mendes_graph<G, T, F, H, M>( |
| 52 | + n: usize, |
| 53 | + mut default_node_weight: F, |
| 54 | + mut default_edge_weight: H, |
| 55 | +) -> Result<G, InvalidInputError> |
| 56 | +where |
| 57 | + G: Create + Data<NodeWeight = T, EdgeWeight = M>, |
| 58 | + F: FnMut() -> T, |
| 59 | + H: FnMut() -> M, |
| 60 | +{ |
| 61 | + let n_edges = usize::pow(3, n as u32); |
| 62 | + let n_nodes = (n_edges + 3) / 2; |
| 63 | + let mut graph = G::with_capacity(n_nodes, n_edges); |
| 64 | + |
| 65 | + let node_0 = graph.add_node(default_node_weight()); |
| 66 | + let node_1 = graph.add_node(default_node_weight()); |
| 67 | + graph |
| 68 | + .add_edge(node_0, node_1, default_edge_weight()) |
| 69 | + .unwrap(); |
| 70 | + let mut current_endpoints = vec![(node_0, node_1)]; |
| 71 | + |
| 72 | + for _ in 0..n { |
| 73 | + let mut new_endpoints = vec![]; |
| 74 | + for (source, target) in current_endpoints.iter() { |
| 75 | + let new_node = graph.add_node(default_node_weight()); |
| 76 | + graph.add_edge(*source, new_node, default_edge_weight()); |
| 77 | + new_endpoints.push((*source, new_node)); |
| 78 | + graph.add_edge(*target, new_node, default_edge_weight()); |
| 79 | + new_endpoints.push((*target, new_node)); |
| 80 | + } |
| 81 | + current_endpoints.extend(new_endpoints); |
| 82 | + } |
| 83 | + Ok(graph) |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use crate::generators::dorogovtsev_goltsev_mendes_graph; |
| 89 | + use crate::petgraph::graph::Graph; |
| 90 | + use crate::petgraph::visit::EdgeRef; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_dorogovtsev_goltsev_mendes_graph() { |
| 94 | + for n in 0..6 { |
| 95 | + let graph: Graph<(), ()> = match dorogovtsev_goltsev_mendes_graph(n, || (), || ()) { |
| 96 | + Ok(graph) => graph, |
| 97 | + Err(_) => panic!("Error generating graph"), |
| 98 | + }; |
| 99 | + assert_eq!(graph.node_count(), (usize::pow(3, n as u32) + 3) / 2); |
| 100 | + assert_eq!(graph.edge_count(), usize::pow(3, n as u32)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_dorogovtsev_goltsev_mendes_graph_edges() { |
| 106 | + let n = 2; |
| 107 | + let expected_edge_list = vec![ |
| 108 | + (0, 1), |
| 109 | + (0, 2), |
| 110 | + (1, 2), |
| 111 | + (0, 3), |
| 112 | + (1, 3), |
| 113 | + (0, 4), |
| 114 | + (2, 4), |
| 115 | + (1, 5), |
| 116 | + (2, 5), |
| 117 | + ]; |
| 118 | + let graph: Graph<(), ()> = match dorogovtsev_goltsev_mendes_graph(n, || (), || ()) { |
| 119 | + Ok(graph) => graph, |
| 120 | + Err(_) => panic!("Error generating graph"), |
| 121 | + }; |
| 122 | + assert_eq!( |
| 123 | + expected_edge_list, |
| 124 | + graph |
| 125 | + .edge_references() |
| 126 | + .map(|edge| (edge.source().index(), edge.target().index())) |
| 127 | + .collect::<Vec<(usize, usize)>>(), |
| 128 | + ) |
| 129 | + } |
| 130 | +} |
0 commit comments