pub fn voronoi(
graph: &Graph,
weights: Option<&[f64]>,
mode: DijkstraMode,
generators: &[VertexId],
tiebreaker: VoronoiTiebreaker,
seed: u64,
) -> IgraphResult<VoronoiPartition>Expand description
Voronoi partitioning of a graph from a set of generator vertices.
Each vertex is assigned to the generator at the smallest distance
(under mode’s direction semantics in directed graphs). Tied
distances are resolved by tiebreaker. Vertices unreachable from
every generator have membership[v] == None and
distances[v] == f64::INFINITY.
weights == None uses BFS (every edge counts as length 1); otherwise
uses Dijkstra. All weights must be non-negative and not NaN; infinite
weights are allowed and treated as non-edges.
seed only affects VoronoiTiebreaker::Random — VoronoiTiebreaker::First
and VoronoiTiebreaker::Last are deterministic regardless of seed.
§Examples
use rust_igraph::{Graph, voronoi, VoronoiTiebreaker, DijkstraMode};
// Path 0-1-2-3-4, generators {0, 4}. The split is "halfway", with
// vertex 2 equidistant (d=2 from both). With `First`, vertex 2 goes
// to generator index 0 (vertex 0); with `Last`, to index 1 (vertex 4).
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 4).unwrap();
let part = voronoi(&g, None, DijkstraMode::Out, &[0, 4], VoronoiTiebreaker::First, 0).unwrap();
assert_eq!(part.membership, vec![Some(0), Some(0), Some(0), Some(1), Some(1)]);
assert_eq!(part.distances, vec![0.0, 1.0, 2.0, 1.0, 0.0]);
let part = voronoi(&g, None, DijkstraMode::Out, &[0, 4], VoronoiTiebreaker::Last, 0).unwrap();
assert_eq!(part.membership, vec![Some(0), Some(0), Some(1), Some(1), Some(1)]);§Errors
IgraphError::InvalidArgumentifweights.len() != graph.ecount(), any weight is NaN or negative, or any generator index is out of range.IgraphError::Internalon graph-diameter overflow (unweighted only — would require > 2^32 - 1 BFS layers).
§Complexity
O(|S| · (|V| + |E|)) for unweighted graphs; O(|S| · (|V| + |E|) · log |V|) for weighted graphs, where |S| is the number of
generators.