1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! Determines if two unrooted trees are isomorphic. This algorithm can easily be modified to support
//! checking if two rooted trees are isomorphic.
//!
//! # Resources
//!
//! - [W. Fiset's video](https://www.youtube.com/watch?v=OCKvEMF0Xac&list=PLDV1Zeh2NRsDGO4--qE8yH72HFL1Km93P&index=11)

use crate::algo::graph::tree::center::{Center, TreeCenter};
use crate::algo::graph::tree::rooting::TreeNode;
use crate::algo::graph::UnweightedAdjacencyList;

impl From<Center> for Vec<usize> {
    fn from(center: Center) -> Self {
        match center {
            Center::One(x) => vec![x],
            Center::Two(x, y) => vec![x, y],
        }
    }
}

impl TreeNode {
    pub fn encode(&self) -> Vec<u8> {
        let mut labels: Vec<_> = self.children.iter().map(|node| node.encode()).collect();
        labels.sort();
        let mut res = Vec::new();
        res.push(b'(');
        for label in &labels {
            res.extend_from_slice(label);
        }
        res.push(b')');
        res
    }
}

impl UnweightedAdjacencyList {
    pub fn is_isomorphic_with(&self, other: &UnweightedAdjacencyList) -> bool {
        let this_centers: Vec<usize> = self.center().into();
        let other_centers: Vec<usize> = other.center().into();
        for &c1 in &this_centers {
            let tree1 = TreeNode::from_adjacency_list(&self, c1);
            for &c2 in &other_centers {
                let tree2 = TreeNode::from_adjacency_list(&self, c2);
                if tree1.encode() == tree2.encode() {
                    return true;
                }
            }
        }
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tree_encoding() {
        let adj = UnweightedAdjacencyList::new_undirected(
            10,
            &[
                [0, 2],
                [0, 1],
                [0, 3],
                [2, 6],
                [2, 7],
                [1, 4],
                [1, 5],
                [5, 9],
                [3, 8],
            ],
        );
        let tree = TreeNode::from_adjacency_list(&adj, 0);
        let encoded = tree.encode();
        let encoded = String::from_utf8(encoded).unwrap();
        assert_eq!(&encoded, "(((())())(()())(()))")
    }

    #[test]
    fn test_tree_isomorphism() {
        let tree1 = UnweightedAdjacencyList::new_undirected(5, &[[2, 0], [3, 4], [2, 1], [2, 3]]);
        let tree2 = UnweightedAdjacencyList::new_undirected(5, &[[1, 0], [2, 4], [1, 3], [1, 2]]);
        assert!(tree1.is_isomorphic_with(&tree2));
    }
}