Skip to main content

to_prufer

Function to_prufer 

Source
pub fn to_prufer(graph: &Graph) -> IgraphResult<Vec<u32>>
Expand description

Encode a labelled tree into its unique Prüfer sequence.

The graph must be an undirected tree with at least 2 vertices. Returns a Vec<u32> of length n - 2 where n = graph.vcount().

The algorithm iteratively removes the smallest-labelled leaf and records its sole neighbour’s label. Runs in O(n) time using the linear-time variant that avoids repeated linear scans.

§Errors

§Examples

use rust_igraph::{from_prufer, to_prufer};

// Round-trip: encode a known tree, decode it, verify same edges.
let tree = from_prufer(&[2, 3, 2, 3]).unwrap();
let seq = to_prufer(&tree).unwrap();
assert_eq!(seq, vec![2, 3, 2, 3]);