Skip to main content

Hrg

Struct Hrg 

Source
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

Source

pub fn as_ptr(&self) -> *const igraph_hrg_t

Raw pointer to the underlying C struct, for FFI calls.

Source

pub fn size(&self) -> usize

The number of leaves, i.e. of vertices of the modeled graph.

Binds igraph_hrg_size.

Source

pub fn left(&self) -> &[i64]

Left child of each internal node (non-negative: leaf, -j - 1: internal node j).

Source

pub fn right(&self) -> &[i64]

Right child of each internal node (non-negative: leaf, -j - 1: internal node j).

Source

pub fn prob(&self) -> &[f64]

Connection probability of each internal node.

Source

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.

Source

pub fn vertices(&self) -> &[i64]

Number of leaves in the subtree of each internal node.

Source

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.

Source

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.

Source

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));
Source

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.

Trait Implementations§

Source§

impl Clone for Hrg

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Hrg

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Hrg

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Send for Hrg

Source§

impl Sync for Hrg

Auto Trait Implementations§

§

impl Freeze for Hrg

§

impl RefUnwindSafe for Hrg

§

impl Unpin for Hrg

§

impl UnsafeUnpin for Hrg

§

impl UnwindSafe for Hrg

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.