Skip to main content

sample_negative_edges

Function sample_negative_edges 

Source
pub fn sample_negative_edges(
    graph: &Graph,
    count: usize,
    seed: u64,
) -> IgraphResult<Vec<(VertexId, VertexId)>>
Expand description

Sample non-edges (negative examples) uniformly at random.

Generates count random (u, v) pairs where no edge exists between u and v in the graph. For undirected graphs, only generates pairs with u < v. Self-loops are never generated.

Uses rejection sampling: generates random pairs and keeps those that are non-edges. Efficient when the graph is sparse (density ≪ 1).

§Parameters

  • graph — The input graph.
  • count — Number of negative samples to generate.
  • seed — PRNG seed for deterministic sampling.

§Returns

A vector of (u, v) pairs representing non-edges. May return fewer than count pairs if the graph is too dense (all pairs are edges).

§Examples

use rust_igraph::{Graph, sample_negative_edges};

// Path: 0-1-2-3 (missing edges: 0-2, 0-3, 1-3)
let g = Graph::from_edges(
    &[(0,1),(1,2),(2,3)], false, Some(4)
).unwrap();

let neg = sample_negative_edges(&g, 3, 42).unwrap();
assert_eq!(neg.len(), 3);
for &(u, v) in &neg {
    assert!(!g.has_edge(u, v));
    assert!(u < v); // undirected: canonical order
}