Skip to main content

reindex_membership

Function reindex_membership 

Source
pub fn reindex_membership(
    membership: &[u32],
) -> IgraphResult<ReindexMembershipResult>
Expand description

Relabel a membership vector so cluster ids run contiguously over 0..k, where k is the number of distinct clusters. The partition is unchanged: two vertices share a cluster in the output iff they shared one in the input. New ids are assigned in first-occurrence order — the first cluster id you encounter when sweeping left to right becomes 0, the next new one becomes 1, and so on.

§Examples

use rust_igraph::reindex_membership;

// Gaps and out-of-order ids are densified by first occurrence.
let r = reindex_membership(&[7, 7, 3, 7, 9, 3]).unwrap();
assert_eq!(r.membership, vec![0, 0, 1, 0, 2, 1]);
assert_eq!(r.new_to_old, vec![7, 3, 9]);
assert_eq!(r.nb_clusters(), 3);

§Errors

Currently infallible — returns IgraphResult for API symmetry with other community helpers and to allow adding fallible checks later without a breaking change.