pub fn label_spread(
graph: &Graph,
labels: &[Option<u32>],
alpha: f64,
max_iter: usize,
tol: f64,
) -> IgraphResult<LabelSpreadResult>Expand description
Predict labels for unlabeled vertices using label spreading.
Iteratively propagates label information from labeled vertices to
their neighbors via the graph structure. At each step:
Y_{t+1} = α · S · Y_t + (1-α) · Y_0
where S = D^{-1/2} A D^{-1/2} is the symmetric normalized adjacency,
Y_0 is the initial label matrix, and α controls the balance between
propagation and clamping to initial labels.
§Parameters
graph— Undirected graph.labels— Label for each vertex:Some(class_id)for labeled vertices,Nonefor unlabeled vertices to predict.alpha— Propagation strength (0 < alpha < 1). Higher = more propagation. Typical: 0.2–0.8.max_iter— Maximum iterations.tol— Convergence tolerance on max label probability change.
§Returns
A LabelSpreadResult with predicted labels and confidence scores.
§Examples
use rust_igraph::{Graph, label_spread};
// Path 0-1-2-3: label vertex 0 as class 0, vertex 3 as class 1
let g = Graph::from_edges(&[(0,1),(1,2),(2,3)], false, Some(4)).unwrap();
let labels = vec![Some(0), None, None, Some(1)];
let result = label_spread(&g, &labels, 0.5, 50, 1e-6).unwrap();
// Vertex 1 should be closer to class 0, vertex 2 closer to class 1
assert_eq!(result.labels[0], 0);
assert_eq!(result.labels[3], 1);
assert_eq!(result.labels[1], 0);
assert_eq!(result.labels[2], 1);