Function graphalgs::spec::prufer_decode

source ·
pub fn prufer_decode(code: &Vec<usize>) -> Vec<(usize, usize)>
Expand description

Restore a tree by its prufer code.

The function returns a set of edges in any order, the order of the vertex numbers in the edge does not matter. If an impossible code is passed to the function, an empty vector is returned.

Example

use graphalgs::spec::prufer_decode;

// tree:
//
//     4
//     |
// 5-0-3-1
//     |
//     2

assert_eq!(
    prufer_decode(&vec![3, 3, 3, 0]),
    vec![(1, 3), (2, 3), (4, 3), (3, 0), (0, 5)]
);

assert_eq!(prufer_decode(&vec![0, 100]), vec![]); // Invalid code