Skip to main content

density

Function density 

Source
pub fn density(graph: &Graph) -> IgraphResult<Option<f64>>
Expand description

Edge density of graph. Counterpart of igraph_density(_, NULL_weights, _, /*loops=*/false).

Returns None when:

  • vcount() == 0 (matches upstream’s IGRAPH_NAN).
  • vcount() == 1 and loops=false (no possible edges).

Self-loops in the input graph are not removed; they inflate the numerator. Use simplify (when it lands) to drop them first.

§Examples

use rust_igraph::{Graph, density};

// K3 (triangle) on 3 vertices, undirected: 3 edges, max = 3 → 1.0.
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
assert_eq!(density(&g).unwrap(), Some(1.0));