pub struct Hrg { /* private fields */ }Expand description
A hierarchical random graph (HRG) model (igraph_hrg_t), after Clauset,
Moore and Newman.
An HRG with n leaves (the vertices of the modeled graph) is a binary
dendrogram with n − 1 internal nodes, each labeled with a probability
p: two vertices are connected with the probability of their lowest
common ancestor. Internal node i has a left and a right child: a
non-negative child id is a leaf (vertex), a negative one -j - 1 is the
internal node j.
An Hrg is always a valid, complete dendrogram: it is obtained by fitting
(Graph::hrg_fit), from the MCMC of Graph::hrg_consensus and
Graph::hrg_predict, or from an explicit tree with Hrg::create. Its
storage is freed on drop.
use igraph::prelude::*;
use igraph::community::Hrg;
// A root (vertex 0) splitting into leaf 3 and an internal node (1) whose
// children are leaf 4 and internal node 2 with leaves 5 and 6.
let tree = Graph::from_edges(&[(0, 3), (0, 1), (1, 4), (1, 2), (2, 5), (2, 6)], 7, true).unwrap();
let hrg = Hrg::create(&tree, &[1.0, 0.0, 0.0]).unwrap();
assert_eq!(hrg.size(), 4);
// Leaf 3 (first leaf, id 0) connects to everyone, the others never connect.
let sample = hrg.sample().unwrap();
assert_eq!(sample.edge_list(), vec![(0, 1), (0, 2), (0, 3)]);Implementations§
Source§impl Hrg
impl Hrg
Sourcepub fn as_ptr(&self) -> *const igraph_hrg_t
pub fn as_ptr(&self) -> *const igraph_hrg_t
Raw pointer to the underlying C struct, for FFI calls.
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
The number of leaves, i.e. of vertices of the modeled graph.
Binds igraph_hrg_size.
Sourcepub fn left(&self) -> &[i64]
pub fn left(&self) -> &[i64]
Left child of each internal node (non-negative: leaf, -j - 1: internal node j).
Sourcepub fn right(&self) -> &[i64]
pub fn right(&self) -> &[i64]
Right child of each internal node (non-negative: leaf, -j - 1: internal node j).
Sourcepub fn edges(&self) -> &[i64]
pub fn edges(&self) -> &[i64]
Edge count stored for each internal node. For models fitted by MCMC
(Graph::hrg_fit, …) this is the number of graph edges whose
endpoints have this node as lowest common ancestor (they sum to the
number of edges of the graph); Hrg::create stores the number of
dendrogram edges below the node instead.
Sourcepub fn create(tree: &Graph, prob: &[f64]) -> Result<Self>
pub fn create(tree: &Graph, prob: &[f64]) -> Result<Self>
Creates an HRG from its dendrogram given as a directed binary tree and the probabilities of its internal nodes.
tree must be a simple directed tree with edges pointing away from
the root, in which every internal node has exactly two children; it
has n leaves and n − 1 internal nodes (at least 3 vertices in total).
prob has one entry per internal node (vcount / 2 values), and
prob[v] is the probability of the internal tree vertex v: igraph
indexes it by tree vertex id, so the internal vertices must be
numbered first (0..n-1), as in the example of Hrg (this is
checked). The leaves of the tree become the vertices 0..n of the
model, in increasing id order.
Binds igraph_hrg_create.
§Errors
ErrorKind::InvalidValue if tree
is not a valid dendrogram or prob has a wrong length.
Sourcepub fn sample(&self) -> Result<Graph>
pub fn sample(&self) -> Result<Graph>
Draws a random graph from the model: every pair of vertices is connected independently with the probability of its lowest common ancestor in the dendrogram. The result is undirected and simple.
Binds igraph_hrg_sample.
Sourcepub fn sample_many(&self, num_samples: usize) -> Result<Vec<Graph>>
pub fn sample_many(&self, num_samples: usize) -> Result<Vec<Graph>>
Draws num_samples independent random graphs from the model.
Binds igraph_hrg_sample_many (see the
HRG chapter of the
C documentation).
§Examples
use igraph::prelude::*;
use igraph::community::Hrg;
// Two leaves joined by a root with probability 1/2.
let tree = Graph::from_edges(&[(0, 1), (0, 2)], 3, true).unwrap();
let hrg = Hrg::create(&tree, &[0.5]).unwrap();
rng::seed(1).unwrap();
let samples = hrg.sample_many(1000).unwrap();
let with_edge = samples.iter().filter(|s| s.ecount() == 1).count();
assert!((400..600).contains(&with_edge));Sourcepub fn dendrogram(&self) -> Result<(Graph, Vec<f64>)>
pub fn dendrogram(&self) -> Result<(Graph, Vec<f64>)>
The dendrogram as a directed tree with the probability of each tree
vertex, see Graph::from_hrg_dendrogram.