Skip to main content

reciprocity_with_mode

Function reciprocity_with_mode 

Source
pub fn reciprocity_with_mode(
    graph: &Graph,
    ignore_loops: bool,
    mode: ReciprocityMode,
) -> IgraphResult<Option<f64>>
Expand description

Reciprocity with explicit mode + ignore_loops (ALGO-PR-004b).

Counterpart of igraph_reciprocity(_, _, ignore_loops, mode). See ReciprocityMode for the formula.

ignore_loops:

  • false (matches reciprocity) — self-loops count as mutual in the numerator (a self-loop is its own reverse) and stay in the denominator.
  • true — self-loops drop out of both numerator and denominator.

Returns None for graphs with no edges (matches upstream’s IGRAPH_NAN), including the Default case where m == 0 and the Ratio case where rec + nonrec == 0. Undirected graphs always return Some(1.0).

§Examples

use rust_igraph::{Graph, reciprocity_with_mode, ReciprocityMode};

// Mutual pair + one-way edge: rec = 2 edges, nonrec = 2 (the
// one-way edge contributes once at source AND once at target).
// Default = 2/3, Ratio = 2/(2+2) = 0.5.
let mut g = Graph::new(3, true).unwrap();
g.add_edge(0, 1).unwrap();
g.add_edge(1, 0).unwrap();
g.add_edge(0, 2).unwrap();
assert_eq!(reciprocity_with_mode(&g, false, ReciprocityMode::Default).unwrap(),
           Some(2.0 / 3.0));
assert_eq!(reciprocity_with_mode(&g, false, ReciprocityMode::Ratio).unwrap(),
           Some(0.5));